티스토리 뷰

How to use pdb Python Debugger



간단하게 Python Debugging툴인 pdb에 대해 정리하고자 한다.

pdb 사용법을 소개하고 django project에 적용하는 예제를 통해 

사용법에 대해 소개하고자 한다.


pdb command list

h(help) : 도움말

n(ext) : 다음 문 실행

ENTER : 마지막 명령 실행

q(uit) : 디버거 종료

p(rint) : 변수값 인쇄

c(ontinue) : 프로그램 계속 실행

l(ist) : 소스 위치 출력

s(tep) : 서브루틴 진입

r(un) : 서브루틴 끝까지 실행


출처 : https://docs.python.org/2/library/pdb.html



아래와 같이 테스트 코드를 작성한다.

test.py

 
  a = 'abc'
  print a

  def fn(tmp) :
    a = 'cde'
    print tmp

  fn('test debug')


주요 command 사용 예제

주요 커맨드 사용 예제를 통해 사용법을 알아보도록 한다.


help command (도움말)

$ pdb test.py

(Pdb) help


Documented commands (type help <topic>):

========================================

EOF    bt         cont      enable  jump  pp       run      unt

a      c          continue  exit    l     q        s        until

alias  cl         d         h       list  quit     step     up

args   clear      debug     help    n     r        tbreak   w

b      commands   disable   ignore  next  restart  u        whatis

break  condition  down      j       p     return   unalias  where


Miscellaneous help topics:

==========================

exec  pdb


Undocumented commands:

======================

retval  rv



list command (코드 목록 출력)

(Pdb) list

  1  -> a = 'abc'

  2     print a

  3

  4     def fn(tmp) :

  5       a = 'cde'

  6       print tmp

  7

  8     fn('test debug')

[EOF]

(Pdb)


next command (다음 루틴 수행)

(Pdb) n

> /home/trabi/tmp/debugger.py(2)<module>()

-> print a

(Pdb)


print command (변수 print 수행)

(Pdb) p a

'abc'

(Pdb)


step command (서브루틴 수행)

(Pdb) s

--Call--

> /home/trabi/tmp/debugger.py(4)fn()

-> def fn(tmp) :

(Pdb) s

> /home/trabi/tmp/debugger.py(5)fn()

-> a = 'cde'

(Pdb) s

> /home/trabi/tmp/debugger.py(6)fn()

-> print tmp

(Pdb) s

test debug



Django pdb 적용

지금까지 기본적인 주요 command에 대해 알아보았다. 이제 django-pdb를 이용하여

실제 django 프로젝트에 적용하여 확인 해 보도록 해본다.

$ pip install django-pdb


settings.py

  # Order is important and depends on your Django version.
  # With Django 1.7+ put it towards the beginning, otherwise towards the end.
  INSTALLED_APPS = (
      ...
      'django_pdb',
      ...
  )

  # Make sure to add PdbMiddleware after all other middleware.
  # PdbMiddleware only activates when settings.DEBUG is True.
  MIDDLEWARE_CLASSES = (
      ...
      'django_pdb.middleware.PdbMiddleware',
  )

  DEBUG = True #False일 경우 pdb 적용 안됨.


디버깅 방법엔 두가지 방법이 있다.

1. 구동 시 pdb 옵션 설정

$ python manage.py runserver 0.0.0.0:5000 --pdb


2. HTTP 요청시 get parameter 추가

http://localhost:8000/index?pdb


아래와 같이 서버 구동 콘솔에서 디버깅 command를 통해 모든 stack trace를

통해 디버깅이 가능하다.

$ python manage.py runserver 0.0.0.0:5000

December 23, 2015 - 07:57:00

Django version 1.8, using settings 'rocksea.settings'

Starting development server at http://0.0.0.0:5000/

Quit the server with CONTROL-C.

()

GET /index?pdb

function "index" in rocksea/views.py:4

args: ()

kwargs: {}

()

> /home/trabi/.virtualenvs/trabi/local/lib/python2.7/site-packages/django/middleware/csrf.py(105)process_view()

-> if getattr(request, 'csrf_processing_done', False):

(Pdb)


출처 : https://github.com/tomchristie/django-pdb

'Developer' 카테고리의 다른 글

[vim] How to use Shell Command on VIM  (2) 2016.01.28
[JAVA] Collectoion 활용하기  (0) 2016.01.05
[python] encoding 체계 정리  (3) 2015.12.16
[python] ImportError: No module named Crypto.Cipher  (0) 2015.12.06
[Django#4] Digging up Django  (0) 2015.10.10
댓글