About fail_over_mac option in network bonding active-backup mode


Environment and Prerequisite

  • CentOS 7.7.1908
  • Bash shell(/bin/bash)
  • Network bonding


fail_over_mac option

About fail_over_mac option


fail_over_mac option types

  • none or 0: (Default) This setting disables fail_over_mac and sets all of the bond and slave interfaces to the same MAC address. Usually first slave’s MAC address is allocated to all bond and slave interfaces.
  • active or 1: The bond interface’s MAC address is always the MAC address of the active slave interface. If the active slave interface is changed during a failover, the bond interface’s MAC address is then changed to match the new active slave’s MAC address.
  • follow or 2: The bond interface’s MAC address is fixed(Usually first slave’s MAC address is allocated) and it gives its MAC address to newly active slave interface. If the slave is changed during a failover, the active slave will get the MAC address of the bond interface and the formerly active slave interface receives the newly active slave’s MAC address.


fail_over_mac option examples

Example running environment

  • Example bonding setting is in (Ref) Bonding Setting
  • Host OS: Mac OS
  • Guest OS: CentOS 7.7.1908
  • Hypervisor: VirtualBox


none or 0 option example

  • Default option
  • fail_over_mac=0
  • In VirtualBox environment, it is not work well.
[root@centos-client ~]# cat /etc/sysconfig/network-scripts/ifcfg-bond0 | grep fail_over_mac
BONDING_OPTS="mode=1 miimon=100 fail_over_mac=0"
  • bond0’s and slaves’(enp0s8 and enp0s9) MAC addresses are same.
[root@centos-client ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:2b:04:6e brd ff:ff:ff:ff:ff:ff
3: enp0s8: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:e8:26:58 brd ff:ff:ff:ff:ff:ff
4: enp0s9: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:e8:26:58 brd ff:ff:ff:ff:ff:ff
8: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:e8:26:58 brd ff:ff:ff:ff:ff:ff


active or 1 option example

  • fail_over_mac=1
[root@centos-client ~]# cat /etc/sysconfig/network-scripts/ifcfg-bond0 | grep fail_over_mac
BONDING_OPTS="mode=1 miimon=100 fail_over_mac=1"
  • bond0’s MAC address and first slave’s(enp0s8) MAC address are same.
[root@centos-client ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:2b:04:6e brd ff:ff:ff:ff:ff:ff
3: enp0s8: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:e8:26:58 brd ff:ff:ff:ff:ff:ff
4: enp0s9: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:27:a9:2d brd ff:ff:ff:ff:ff:ff
9: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:e8:26:58 brd ff:ff:ff:ff:ff:ff
  • Link down enp0s8 interface
ip link set dev enp0s8 down
  • bond0’s MAC address is changed to enp0s9’s MAC address.
[root@centos-client ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:2b:04:6e brd ff:ff:ff:ff:ff:ff
3: enp0s8: <BROADCAST,MULTICAST,SLAVE> mtu 1500 qdisc pfifo_fast master bond0 state DOWN mode DEFAULT group default qlen 1000
    link/ether 08:00:27:e8:26:58 brd ff:ff:ff:ff:ff:ff
4: enp0s9: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:27:a9:2d brd ff:ff:ff:ff:ff:ff
9: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:27:a9:2d brd ff:ff:ff:ff:ff:ff


follow or 2 option example

  • fail_over_mac=2
  • In VirtualBox environment, it is not work well.
[root@centos-client ~]# cat /etc/sysconfig/network-scripts/ifcfg-bond0 | grep fail_over_mac
BONDING_OPTS="mode=1 miimon=100 fail_over_mac=2"
  • bond0’s MAC address and first slave’s(enp0s8) MAC address are same.
[root@centos-client ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:2b:04:6e brd ff:ff:ff:ff:ff:ff
3: enp0s8: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:e8:26:58 brd ff:ff:ff:ff:ff:ff
4: enp0s9: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:27:a9:2d brd ff:ff:ff:ff:ff:ff
10: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:e8:26:58 brd ff:ff:ff:ff:ff:ff
  • Link down enp0s8 interface
ip link set dev enp0s8 down
  • bond0’s MAC address is same as before.
  • bond0’s MAC address is allocated to newly active slave(enp0s9)
  • enp0s9’s previous MAC address is moved to enp0s8’s MAC address which is failed.
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:2b:04:6e brd ff:ff:ff:ff:ff:ff
3: enp0s8: <BROADCAST,MULTICAST,SLAVE> mtu 1500 qdisc pfifo_fast master bond0 state DOWN mode DEFAULT group default qlen 1000
    link/ether 08:00:27:27:a9:2d brd ff:ff:ff:ff:ff:ff
4: enp0s9: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:e8:26:58 brd ff:ff:ff:ff:ff:ff
10: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:e8:26:58 brd ff:ff:ff:ff:ff:ff


(Ref) Bonding Setting

  • Basic bonding settings are like below.

Interfaces

  • Bonding Interface: bond0
  • Slave Interfaces: enp0s8, enp0s9

bond0

  • /etc/sysconfig/network-scripts/ifcfg-bond0
  • IPADDR and PREFIX could be different.
  • fail_over_mac can set be from 0 to 2.
TYPE=Bond
BOOTPROTO=static
DEFROUTE=no
NAME=bond0
DEVICE=bond0
ONBOOT=yes
PREFIX=24
IPADDR=192.168.99.101
BONDING_OPTS="mode=1 miimon=100 fail_over_mac=1"

enp0s8, enp0s9

  • /etc/sysconfig/network-scripts/ifcfg-enp0s8
NAME=enp0s8
DEVICE=enp0s8
TYPE=Ethernet
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
  • /etc/sysconfig/network-scripts/ifcfg-enp0s9
NAME=enp0s9
DEVICE=enp0s9
TYPE=Ethernet
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes


Reference

vi 또는 vim에서 문자열들을 원하는 문자열로 바꿔보자.


환경

  • Linux 기반 시스템
  • Bash shell(/bin/bash)
  • vi 또는 vim


vi(또는 vim) 문자열 치환

기본형

형식

:%s/[원래문자열]/[바꿀문자열]/[옵션]
  • 위와 같은 형식으로 다양한 옵션과 함께 사용 가능
  • 옵션은 하나만 사용하는게 아니라 혼합해서 같이 사용 가능
  • 위 명령어에서 %를 제외하면 현재 커서가 위치한 줄에서만 치환을 합니다.

옵션

  • g: 해당 문자열에 해당하는 문자열들을 모두 치환하는 옵션
ThisThisThis -> testtesttest
:%s/This/test/g
  • c: 치환하기 전에 매번 치환할지 확인하는 옵션
ThisThisThis -> testThisThis
~
replace with test (y/n/a/q/l/^E/^Y)?y
:%s/This/test/c
  • i: 대소문자를 구분하지 않는 옵션
ThistHisthIs -> testtesttest
:%s/This/test/ig


예제

  • 테스트에 사용할 예제 작성
$ vi test.txt
This is test string. This is it!
This is second string.

예제 0 - 현재 줄에서 첫 ‘This’를 ‘tHIS’로 치환

  • 다음 아래처럼 %를 제외하고 하면 됩니다.
tHIS is test string. This is it!
This is second string.
~
~
:s/This/tHIS

예제 1 - 각 줄의 첫 ‘This’를 ‘tHIS’로 치환

  • 아무 옵션이 없다면 하나의 줄에서 해당 문자열을 찾으면 치환하고 다음 문자열로 넘어갑니다. 즉, 뒤에 동일하게 맞는 문자열이 있더라도 넘어가게 됩니다.
tHIS is test string. This is it!
tHIS is second string.
~
~
:%s/This/tHIS

예제 2 - 문서 전체의 ‘This’를 ‘tHIS’로 치환

  • g 옵션을 이용해 전체에서 해당하는 문자열을 모두 치환할 수 있다.
tHIS is test string. tHIS is it!
tHIS is second string.
~
~
:%s/This/tHIS/g

예제 3 - 매번 치환할 때 확인해서 바꾸기

  • c 옵션을 사용해 매번 확인 가능
  • g 옵션을 함께 이용해 문서 전체에서 바꾸도록 설정
  • 해당 예제에서는 y->n->y로 입력해서 순서대로 치환
this is test string. This is it!
this is second string.
~
~
replace with test (y/n/a/q/l/^E/^Y)?y
:%s/This/tHIS/cg

예제 4 - 대소문자 구분없이 문서 전체에서 ‘This’를 ‘tHIS’로 치환

  • i 옵션을 이용해 대소문자를 구분하지 않으면서 g 옵션을 이용해 전체에서 해당하는 문자열을 모두 치환
tHIS is test string. tHIS is it!
tHIS is second string.
~
~
:%s/this/tHIS/ig


참고자료

Change strings in vi or vim.


Environment and Prerequisite

  • Linux base system
  • Bash shell(/bin/bash)
  • vi or vim


vi(or vim) string replacement

Basic Form

Format

:%s/[original string]/[string to replace]/[options]
  • Use above form with various options.
  • Options can be used in multiple.
  • If omit % in above command, only current cursor line string will be replaced.

Options

  • g: Replace all matched strings(not only first occurrence in each line).
ThisThisThis -> testtesttest
:%s/This/test/g
  • c: Check before replacement.
ThisThisThis -> testThisThis
~
replace with test (y/n/a/q/l/^E/^Y)?y
:%s/This/test/c
  • i: Don’t care upper and lower case.
ThistHisthIs -> testtesttest
:%s/This/test/ig


Examples

  • Make test text file.
$ vi test.txt
This is test string. This is it!
This is second string.

Example 0 - Replace first ‘This’ to ‘tHIS’ in current line

  • Just remove % like below.
tHIS is test string. This is it!
This is second string.
~
~
:s/This/tHIS

Example 1 - Replace first ‘This’ to ‘tHIS’ in each line

  • If there is no option, replace string in one line and move to next line. So, it moves to next line even though there are more matched strings after replacement.
tHIS is test string. This is it!
tHIS is second string.
~
~
:%s/This/tHIS

Example 2 - Replace all ‘This’ to ‘tHIS’

  • Replace all matched strings using g option.
tHIS is test string. tHIS is it!
tHIS is second string.
~
~
:%s/This/tHIS/g

Example 3 - Check every time when replacing

  • Check every time by using c option.
  • Use g option to replace all matched strings.
  • Current example give input in y->n->y order.
this is test string. This is it!
this is second string.
~
~
replace with test (y/n/a/q/l/^E/^Y)?y
:%s/This/tHIS/cg

Example 4 - Replace ‘This’ to ‘tHIS’ while ignoring upper and lower case

  • Use i option to ignore upper and lower case.
  • Use g option to replace all matched strings.
tHIS is test string. tHIS is it!
tHIS is second string.
~
~
:%s/this/tHIS/ig


Reference

pdb를 이용해 python 코드를 디버깅하는 방법을 알아보자.


환경

  • Linux 기반 시스템
  • Bash shell(/bin/bash)
  • Python


pdb

pdb란?

  • pdb: 파이썬 디버깅 도구로 파인썬 인터프리터를 줄마다 보면서 실행할 수 있도록 도와주는 도구입니다.


실행 방법

pdb 실행방법 - 1

  • 처음부터 코드를 실행할 때 사용
python3 -m pdb example.py
  • 실행 예제
$ python -m pdb test.py
> /Users/taewoo/test.py(1)<module>()
-> def test_sum(a,b):
(Pdb)

pdb 실행방법 - 2

  • 코드의 특정 부분부터 사용
# In python code
...
import pdb; pdb.set_trace()
...
  • 실행 예제
$ python test.py
> /Users/taewoo/test.py(6)<module>()
-> b=30
(Pdb)


사용법

사용 명령어 목록

  • l: 주변 소스코드들을 출력하며 현재 라인이 화살표로 나옵니다.
  • n: 다음 문장으로 이동합니다. 즉, 위에 l에서 위치한 부분을 실행하고 다음 명령어줄로 가는겁니다.
  • s: 흔히 사용하는 ‘Step Into’로 함수 내부로 들어갑니다. --Call--이라고 나오며 다 진행된 후에는 --Return--이 나옵니다.
  • c: 다음줄부터 중단점을 만날때까지 코드를 실행하며 중단점이 없다면 끝까지 실행합니다.
  • r: 현재 함수의 return이 나올때까지 실행합니다.
  • w: 함수의 call stack을 보여줍니다.
  • cl: 모든 중단점들을 삭제합니다.
  • b: 특정파일이나 패키지의 줄번호나 함수명으로 중단점을 만들 수 있습니다.
  • a: 현재 함수의 매개변수들을 출력.

사용 명령어 예제

  • l: 주변 소스코드들을 출력하며 현재 라인이 화살표로 나옵니다.
(Pdb) l
  1  	def test_sum(a,b):
  2  	    return a+b
  3
  4  	a=10
  5  ->	b=30
  6  	c=test_sum(a,b)
  7
  8  	for i in range(1,10):
  9  	    a = a + i
 10
 11  	import requests
  • n: 다음 문장으로 이동합니다. 즉, 위에 l에서 위치한 부분을 실행하고 다음 명령어줄로 가는겁니다.
(Pdb)  n
> /Users/taewoo/test.py(6)<module>()
-> c=test_sum(a,b)
  • s: 흔히 사용하는 ‘Step Into’로 함수 내부로 들어갑니다. --Call--이라고 나오며 다 진행된 후에는 --Return--이 나옵니다.
(Pdb)  s
--Call--
> /Users/taewoo/test.py(1)test_sum()
-> def test_sum(a,b):
...
--Return--
> /Users/taewoo/test.py(5)test_sum()->40
-> return a+b
  • c: 다음줄부터 중단점을 만날때까지 코드를 실행하며 중단점이 없다면 끝까지 실행합니다.
(Pdb) c
The program finished and will be restarted
  • r: 현재 함수의 return이 나올때까지 실행합니다.
(Pdb) s
--Call--
> /Users/taewoo/test.py(1)test_sum()
-> def test_sum(a,b):
(Pdb) r
--Return--
> /Users/taewoo/test.py(5)test_sum()->40
-> return a+b
  • w: 함수의 call stack을 보여줍니다.
(Pdb) w
  /Users/taewoo/.pyenv/versions/3.6.2/lib/python3.6/bdb.py(431)run()
-> exec(cmd, globals, locals)
  <string>(1)<module>()
  /Users/taewoo/test.py(9)<module>()
-> c=test_sum(a,b)
> /Users/taewoo/test.py(5)test_sum()->40
-> return a+b
  • cl: 모든 중단점들을 삭제합니다.
(Pdb) cl
Clear all breaks? y
  • b: 특정파일이나 패키지의 줄번호나 함수명으로 중단점을 만들 수 있습니다.
(Pdb) b 8
Breakpoint 1 at /Users/taewoo/test.py:8
(Pdb) b test_sum
Breakpoint 2 at /Users/taewoo/test.py:1
# Stop at line number 8
(Pdb) c
> /Users/taewoo/test.py(8)<module>()
-> b=30
# Stop at function
(Pdb) c
> /Users/taewoo/test.py(2)test_sum()
-> tmp=1
(Pdb) b
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /Users/taewoo/test.py:8
	breakpoint already hit 2 times
2   breakpoint   keep yes   at /Users/taewoo/test.py:1
	breakpoint already hit 1 time
  • a: 현재 함수의 매개변수들을 출력.
(Pdb)  s
--Call--
> /Users/taewoo/test.py(1)test_sum()
-> def test_sum(a,b):
(Pdb) a
a = 10
b = 30

기타

  • 변수 값을 확인하고 싶을때 해당 변수를 명령어처럼 입력하던가 print함수를 이용할 수 있습니다.
(Pdb) c
> /Users/taewoo/test.py(2)test_sum()
-> tmp=1
(Pdb) n
> /Users/taewoo/test.py(3)test_sum()
-> tmp=2
(Pdb) tmp
1
(Pdb) print(tmp)
1
(Pdb) type(data)
<class 'dict'>
(Pdb) data
{'data': [{'type': 'articles', 'id': '1', 'attributes': {'title': 'JSON API paints my bikeshed!', 'body': 'The shortest article. Ever.', 'created': '2015-05-22T14:56:29.000Z', 'updated': '2015-05-22T14:56:28.000Z'}, 'relationships': {'author': {'data': {'id': '42', 'type': 'people'}}}}], 'included': [{'type': 'people', 'id': '42', 'attributes': {'name': 'John', 'age': 80, 'gender': 'male'}}]}
  • 코드 상에 중단점 추가하기
  • 코드 중간에 import pdb; pdb.set_trace()를 추가하면 해당 부분에서 멈춥니다.
def test_sum(a,b):
    tmp=1
    tmp=2
    tmp=3
    return a+b

a=10
b=30
c=test_sum(a,b)

for i in range(1,10):
    a = a + i

import requests
import pdb; pdb.set_trace()
url = 'https://gist.githubusercontent.com/TWpower/771f9dfc8d9e1ddc0ecbdaea5b2e379e/raw/2c7785b4835138255bdadb71bd83702e53ac2677/test-example.json'

data = requests.get(url).json()
$ python -m pdb test.py
> /Users/taewoo/test.py(1)<module>()
-> def test_sum(a,b):
(Pdb) c
> /Users/taewoo/test.py(16)<module>()
-> url = 'https://gist.githubusercontent.com/TWpower/771f9dfc8d9e1ddc0ecbdaea5b2e379e/raw/2c7785b4835138255bdadb71bd83702e53ac2677/test-example.json'
(Pdb)


참고자료

Debug python code using pdb.


Environment and Prerequisite

  • Linux base system
  • Bash shell(/bin/bash)
  • Python


pdb

What is pdb?

  • pdb: Interactive source code debugger for Python programs.


How to run

pdb run method - 1

  • Run pdb in start time.
python3 -m pdb example.py
  • Run example
$ python -m pdb test.py
> /Users/taewoo/test.py(1)<module>()
-> def test_sum(a,b):
(Pdb)

pdb run method - 2

  • Add to code
# In python code
...
import pdb; pdb.set_trace()
...
  • Run example
$ python test.py
> /Users/taewoo/test.py(6)<module>()
-> b=30
(Pdb)


Usage

Usage command list

  • l: Print codes of around current point. Current point will be marked by arrow.
  • n: Move to next statement. In other words, run statement which is shown in above l command’s arrow and move to next statement.
  • s: Commonly used ‘Step Into’ command. --Call-- will be shown after stepped in and --Return-- will be shown after return.
  • c: Run codes until meet next break point.
  • r: Run command until meet return.
  • w: Show current call stack.
  • cl: Clear all break points.
  • b: Make break point to file’s or package’s specific function or line.
  • a: Print function’s all arguments.

Usage command example

  • l: Print codes of around current point. Current point will be marked by arrow.
(Pdb) l
  1  	def test_sum(a,b):
  2  	    return a+b
  3
  4  	a=10
  5  ->	b=30
  6  	c=test_sum(a,b)
  7
  8  	for i in range(1,10):
  9  	    a = a + i
 10
 11  	import requests
  • n: Move to next statement. In other words, run statement which is shown in above l command’s arrow and move to next statement.
(Pdb)  n
> /Users/taewoo/test.py(6)<module>()
-> c=test_sum(a,b)
  • s: Commonly used ‘Step Into’ command. --Call-- will be shown after stepped in and --Return-- will be shown after return.
(Pdb)  s
--Call--
> /Users/taewoo/test.py(1)test_sum()
-> def test_sum(a,b):
...
--Return--
> /Users/taewoo/test.py(5)test_sum()->40
-> return a+b
  • c: Run codes until meet next break point.
(Pdb) c
The program finished and will be restarted
  • r: Run command until meet return.
(Pdb) s
--Call--
> /Users/taewoo/test.py(1)test_sum()
-> def test_sum(a,b):
(Pdb) r
--Return--
> /Users/taewoo/test.py(5)test_sum()->40
-> return a+b
  • w: Show current call stack.
(Pdb) w
  /Users/taewoo/.pyenv/versions/3.6.2/lib/python3.6/bdb.py(431)run()
-> exec(cmd, globals, locals)
  <string>(1)<module>()
  /Users/taewoo/test.py(9)<module>()
-> c=test_sum(a,b)
> /Users/taewoo/test.py(5)test_sum()->40
-> return a+b
  • cl: Clear all break points.
(Pdb) cl
Clear all breaks? y
  • b: Make break point to file’s or package’s specific function or line.
(Pdb) b 8
Breakpoint 1 at /Users/taewoo/test.py:8
(Pdb) b test_sum
Breakpoint 2 at /Users/taewoo/test.py:1
# Stop at line number 8
(Pdb) c
> /Users/taewoo/test.py(8)<module>()
-> b=30
# Stop at function
(Pdb) c
> /Users/taewoo/test.py(2)test_sum()
-> tmp=1
(Pdb) b
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /Users/taewoo/test.py:8
	breakpoint already hit 2 times
2   breakpoint   keep yes   at /Users/taewoo/test.py:1
	breakpoint already hit 1 time
  • a: Print function’s all arguments.
(Pdb)  s
--Call--
> /Users/taewoo/test.py(1)test_sum()
-> def test_sum(a,b):
(Pdb) a
a = 10
b = 30

Others

  • We can check value of variable using print function or just type it to pdb.
(Pdb) c
> /Users/taewoo/test.py(2)test_sum()
-> tmp=1
(Pdb) n
> /Users/taewoo/test.py(3)test_sum()
-> tmp=2
(Pdb) tmp
1
(Pdb) print(tmp)
1
(Pdb) type(data)
<class 'dict'>
(Pdb) data
{'data': [{'type': 'articles', 'id': '1', 'attributes': {'title': 'JSON API paints my bikeshed!', 'body': 'The shortest article. Ever.', 'created': '2015-05-22T14:56:29.000Z', 'updated': '2015-05-22T14:56:28.000Z'}, 'relationships': {'author': {'data': {'id': '42', 'type': 'people'}}}}], 'included': [{'type': 'people', 'id': '42', 'attributes': {'name': 'John', 'age': 80, 'gender': 'male'}}]}
  • Add break point in code.
  • Pdb will be stopped on import pdb; pdb.set_trace()
def test_sum(a,b):
    tmp=1
    tmp=2
    tmp=3
    return a+b

a=10
b=30
c=test_sum(a,b)

for i in range(1,10):
    a = a + i

import requests
import pdb; pdb.set_trace()
url = 'https://gist.githubusercontent.com/TWpower/771f9dfc8d9e1ddc0ecbdaea5b2e379e/raw/2c7785b4835138255bdadb71bd83702e53ac2677/test-example.json'

data = requests.get(url).json()
$ python -m pdb test.py
> /Users/taewoo/test.py(1)<module>()
-> def test_sum(a,b):
(Pdb) c
> /Users/taewoo/test.py(16)<module>()
-> url = 'https://gist.githubusercontent.com/TWpower/771f9dfc8d9e1ddc0ecbdaea5b2e379e/raw/2c7785b4835138255bdadb71bd83702e53ac2677/test-example.json'
(Pdb)


Reference