기반 지식

pdb 도구를 이용한 python 디버깅

real-again 2024. 10. 18. 09:31

 

1. pdb란?

 프로그래밍에서, PDB는 "Python Debugger"의 약자로, 파이썬 프로그래밍 언어의 내장 디버깅 도구이다.

  PDB를 사용하면 코드의 실행을 한 줄씩 추적하며, 변수의 값, 함수의 흐름 등을 실시간으로 검사하고 수정할 수 있다.

  디버깅 과정에서 중단점(breakpoint)을 설정하고, 코드를 단계별로 실행하거나 특정 조건에서 멈추도록 할 수 있어, 코드 오류를 찾고 수정하는 데 매우 유용하다.

  명령어를 통해 디버깅 작업을 진행하며, pdb.set_trace()를 코드 중간에 넣으면 그 지점에서 디버거가 실행되어 코드 실행을 멈추고 확인할 수 있다.

 

2. 실습

import pdb

 

중단점(break point) 지정

 

Debugger 확인

 

3. Command

Command Description Example
l Show a few lines of code around the current line. l
n Move to the next line (do not enter functions). n
s Step into the function if called. s
c Continue execution until a breakpoint is reached. c
b Set a breakpoint at a specific line or function. b 10
condition Add a condition to an existing breakpoint. condition 1 x == 5
tbreak Set a temporary breakpoint that clears after hitting once. tbreak 10
cl Clear a specific breakpoint or all breakpoints. cl 1
p Print the value of a variable. p x