SlideShare ist ein Scribd-Unternehmen logo
1 von 18
Downloaden Sie, um offline zu lesen
PDB 的使用與原理
果凍
簡介
● 中興大學學士
● 任職於曼克斯
● 接觸 python 時間有七年
● 喜歡學習新的程式語言
● C、C++、java、golang。
● Linkin: http://www.linkedin.
com/pub/kao-kuo-
tung/67/9a2/6b3
● About me: http://about.
me/ya790206
PDB 常用指令介紹
1. break
2. clear
3. step
4. next
5. return
6. continue
7. jump
8. list
9. p
*可惜 pdb 無法做到監看變數改變的功能
What is frame?
● 儲存 function 執行時的相關資訊,如區域變
數、函數位址等資訊。
● 作用像 C 的 call stack( run-time stack )。
PyFrameObject
f_back next outer frame object (this frame’s caller)
f_code code object being executed in this frame
f_exc_traceback traceback if raised in this frame, or None
f_exc_type exception type if raised in this frame, or None
f_exc_value exception value if raised in this frame, or None
f_globals global namespace seen by this frame
f_lineno current line number in Python source code
f_locals local namespace seen by this frame
f_trace tracing function for this frame, or None
REF: http://docs.python.org/2/library/inspect.html
fun1 call fun2, fun2 call fun3, fun3 call fun1
fun1's frame
fun2's frame
fun3's frame
fun1's frame
f_code
f_code
fun1's
code
object
fun2's
code
object
fun3's
code
object
f_back
f_back
f_back
f_code
f_code
frame 未必擺在連續的記憶體上,因此需要 f_back 的存在。詳情請見
PyFrame_New。
More Details
f_trace, if not None, is a function called at the start of each source code line
(this is used by the debugger)
f_lineno is the current line number of the frame — writing to this from within a
trace function jumps to the given line (only for the bottom-most frame).
sys._getframe([depth])¶
Return a frame object from the call stack. If optional integer depth is given,
return the frame object that many calls below the top of the stack. If that is
deeper than the call stack, ValueError is raised. The default for depthis zero,
returning the frame at the top of the call stack.
sys.settrace(tracefunc)
Set the system’s trace function, which allows you to implement a Python source
code debugger in Python. The function is thread-specific; for a debugger to
support multiple threads, it must be registered using settrace() for each thread
being debugged.
why?
it must be registered using settrace() for each thread being debugged.
Because: each thread have their own starck. In the python, the stack means
the frame.
process
thread
process
thread thread thread
看看 pdb
● break => pdb:do_break -> bdb: set_break
(just create break point)
● step => pdb:do_step -> bdb: set_step -> self.
_set_stopinfo
● return => pdb:do_return -> bdb:set_return ->
self._set_stopinfo
trace function => bdb:trace_dispatch -> bdb:
dispatch_line -> stop_here -> pdb:user_line
(frame) -> pdb:interaction
問題
A: 當離開互動模式時,最上層的 frame 是有 bug
code 的 frame。就像一般程式碼的執行,他會一
直執行最上層的 frame 的 code。
Q: 當有 bug 的 code 呼叫 set_trace 時, pdb 要
如何執行有 bug 的 code ?
問題
Q: 我只有呼叫一次 pdb 的 set_trace,那為何程
式可以重複進入 pdb 的方法?
A: 因為每一層 frame 的 f_trace 都設定成 self.
trace_dispatch,所以每次在執行有bug 的 code
時,都會執行 self.trace_dispatch。所以藉由這個
方法,可以重複進入 pdb 的方法。
問題
A: 因為 pdb 把相關資料存在物件裡(如 self.xxx
= yyy) 而非 frame 裡(如 xxx=yyy)
Q: 在執行有 bug 的 code 時,都是從 pdb 的方
法 return 回來的。因此 pdb 方法的 frame 會被
刪除。那為何我設定的中斷點資料不會消失?
問題
Q: 既然 pdb 的資料是存在物件裡。可是我的程
式碼( 有 bug 的 code) 並沒有變數指向 pdb 的物
件,為何他不會被回收( garbage collection )?
A: 的確你的程式碼( 有 bug 的 code ) 沒有變數
指向 pdb。但是 pdb 在每個 frame 偷偷動了手
腳,讓每個 frame 的 f_trace 指向 pdb 物件的
trace_dispatch。這就是他長生的秘密。
recoverable_pdb
● github: https://github.com/ya790206/recoverable_pdb
● Like time machine, you can recover the runtime to the
point you save.
● addition command:
○ save point_name: save the current frame to the point.
○ restore point_name: restore frame from the frame you
saved.
○ diff point_name: compare the current frame and the
another you saved.
● limit:
○ same as jump command.
○ it can't recover file or database.
為何需要自定義一個 try_copy?
1. 因為不是每個物件都可以被 copy。如 code
object。
2. 因為 dict 除了給程式設計師用外,他也會被用
來表示內部結構。因此需要對他做特別處理。
ref: http://docs.python.org/2/library/copy.html
Thanks all
thanks for all listener, taipei.py, and the
manx.
Taipe pyi: http://www.meetup.
com/Taipei-py/
The manx:
http://www.themanxgroup.tw/
The manx production:
http://lucky-lane.com/

Weitere ähnliche Inhalte

Ähnlich wie recover_pdb 原理與介紹

Gdb principle
Gdb principleGdb principle
Gdb principlelibfetion
 
大话Php之性能
大话Php之性能大话Php之性能
大话Php之性能liqiang xu
 
手把手教你玩转Gdb
手把手教你玩转Gdb手把手教你玩转Gdb
手把手教你玩转GdbZesheng Wu
 
Java Crash分析(2012-05-10)
Java Crash分析(2012-05-10)Java Crash分析(2012-05-10)
Java Crash分析(2012-05-10)Kris Mok
 
前端爆肝之旅+React上山前的小專案心得分享
前端爆肝之旅+React上山前的小專案心得分享前端爆肝之旅+React上山前的小專案心得分享
前端爆肝之旅+React上山前的小專案心得分享Yao Nien Chung
 
Intro to C++ Basic
Intro to C++ BasicIntro to C++ Basic
Intro to C++ BasicShih Chi Lin
 
signed distance function
signed distance functionsigned distance function
signed distance functionEnigmatisms
 
轻量级Flash服务器开发框架(刘恒)
轻量级Flash服务器开发框架(刘恒)轻量级Flash服务器开发框架(刘恒)
轻量级Flash服务器开发框架(刘恒)FLASH开发者交流会
 
Introduction to Basic Haskell Components (In Chinese)
Introduction to Basic Haskell Components (In Chinese)Introduction to Basic Haskell Components (In Chinese)
Introduction to Basic Haskell Components (In Chinese)ChengHui Weng
 
为啥别读HotSpot VM的源码(2012-03-03)
为啥别读HotSpot VM的源码(2012-03-03)为啥别读HotSpot VM的源码(2012-03-03)
为啥别读HotSpot VM的源码(2012-03-03)Kris Mok
 
用Cython封装c++代码为python模块的一点经验
用Cython封装c++代码为python模块的一点经验用Cython封装c++代码为python模块的一点经验
用Cython封装c++代码为python模块的一点经验Leo Zhou
 
C++工程实践
C++工程实践C++工程实践
C++工程实践Shuo Chen
 
用Raspberry PI學Linux驅動程式
用Raspberry PI學Linux驅動程式用Raspberry PI學Linux驅動程式
用Raspberry PI學Linux驅動程式Stanley Ho
 
Groovy Introduction for Java Programmer
Groovy Introduction for Java ProgrammerGroovy Introduction for Java Programmer
Groovy Introduction for Java ProgrammerLi Ding
 
Golang server design pattern
Golang server design patternGolang server design pattern
Golang server design pattern理 傅
 
Python 中 += 與 join比較
Python 中 += 與 join比較Python 中 += 與 join比較
Python 中 += 與 join比較kao kuo-tung
 
Google protocol buffers简析
Google protocol buffers简析Google protocol buffers简析
Google protocol buffers简析wavefly
 

Ähnlich wie recover_pdb 原理與介紹 (20)

Gdb principle
Gdb principleGdb principle
Gdb principle
 
大话Php之性能
大话Php之性能大话Php之性能
大话Php之性能
 
手把手教你玩转Gdb
手把手教你玩转Gdb手把手教你玩转Gdb
手把手教你玩转Gdb
 
Java Crash分析(2012-05-10)
Java Crash分析(2012-05-10)Java Crash分析(2012-05-10)
Java Crash分析(2012-05-10)
 
前端爆肝之旅+React上山前的小專案心得分享
前端爆肝之旅+React上山前的小專案心得分享前端爆肝之旅+React上山前的小專案心得分享
前端爆肝之旅+React上山前的小專案心得分享
 
Intro to C++ Basic
Intro to C++ BasicIntro to C++ Basic
Intro to C++ Basic
 
Nio trick and trap
Nio trick and trapNio trick and trap
Nio trick and trap
 
signed distance function
signed distance functionsigned distance function
signed distance function
 
轻量级Flash服务器开发框架(刘恒)
轻量级Flash服务器开发框架(刘恒)轻量级Flash服务器开发框架(刘恒)
轻量级Flash服务器开发框架(刘恒)
 
Introduction to Basic Haskell Components (In Chinese)
Introduction to Basic Haskell Components (In Chinese)Introduction to Basic Haskell Components (In Chinese)
Introduction to Basic Haskell Components (In Chinese)
 
Tcfsh bootcamp day2
 Tcfsh bootcamp day2 Tcfsh bootcamp day2
Tcfsh bootcamp day2
 
为啥别读HotSpot VM的源码(2012-03-03)
为啥别读HotSpot VM的源码(2012-03-03)为啥别读HotSpot VM的源码(2012-03-03)
为啥别读HotSpot VM的源码(2012-03-03)
 
Reply
ReplyReply
Reply
 
用Cython封装c++代码为python模块的一点经验
用Cython封装c++代码为python模块的一点经验用Cython封装c++代码为python模块的一点经验
用Cython封装c++代码为python模块的一点经验
 
C++工程实践
C++工程实践C++工程实践
C++工程实践
 
用Raspberry PI學Linux驅動程式
用Raspberry PI學Linux驅動程式用Raspberry PI學Linux驅動程式
用Raspberry PI學Linux驅動程式
 
Groovy Introduction for Java Programmer
Groovy Introduction for Java ProgrammerGroovy Introduction for Java Programmer
Groovy Introduction for Java Programmer
 
Golang server design pattern
Golang server design patternGolang server design pattern
Golang server design pattern
 
Python 中 += 與 join比較
Python 中 += 與 join比較Python 中 += 與 join比較
Python 中 += 與 join比較
 
Google protocol buffers简析
Google protocol buffers简析Google protocol buffers简析
Google protocol buffers简析
 

Mehr von kao kuo-tung

用 Open source 改造鍵盤
用 Open source 改造鍵盤用 Open source 改造鍵盤
用 Open source 改造鍵盤kao kuo-tung
 
Immutable infrastructure 介紹與實做:以 kolla 為例
Immutable infrastructure 介紹與實做:以 kolla 為例Immutable infrastructure 介紹與實做:以 kolla 為例
Immutable infrastructure 介紹與實做:以 kolla 為例kao kuo-tung
 
Openstack swift, how does it work?
Openstack swift, how does it work?Openstack swift, how does it work?
Openstack swift, how does it work?kao kuo-tung
 
Why is a[1] fast than a.get(1)
Why is a[1]  fast than a.get(1)Why is a[1]  fast than a.get(1)
Why is a[1] fast than a.get(1)kao kuo-tung
 
減少重複的測試程式碼的一些方法
減少重複的測試程式碼的一些方法減少重複的測試程式碼的一些方法
減少重複的測試程式碼的一些方法kao kuo-tung
 
Openstack taskflow 簡介
Openstack taskflow 簡介Openstack taskflow 簡介
Openstack taskflow 簡介kao kuo-tung
 
Async: ways to store state
Async:  ways to store stateAsync:  ways to store state
Async: ways to store statekao kuo-tung
 
Docker 原理與實作
Docker 原理與實作Docker 原理與實作
Docker 原理與實作kao kuo-tung
 
那些年,我們一起看的例外
那些年,我們一起看的例外那些年,我們一起看的例外
那些年,我們一起看的例外kao kuo-tung
 
Garbage collection 介紹
Garbage collection 介紹Garbage collection 介紹
Garbage collection 介紹kao kuo-tung
 
Python 如何執行
Python 如何執行Python 如何執行
Python 如何執行kao kuo-tung
 
C python 原始碼解析 投影片
C python 原始碼解析 投影片C python 原始碼解析 投影片
C python 原始碼解析 投影片kao kuo-tung
 

Mehr von kao kuo-tung (15)

用 Open source 改造鍵盤
用 Open source 改造鍵盤用 Open source 改造鍵盤
用 Open source 改造鍵盤
 
Immutable infrastructure 介紹與實做:以 kolla 為例
Immutable infrastructure 介紹與實做:以 kolla 為例Immutable infrastructure 介紹與實做:以 kolla 為例
Immutable infrastructure 介紹與實做:以 kolla 為例
 
Python to scala
Python to scalaPython to scala
Python to scala
 
Intorduce to Ceph
Intorduce to CephIntorduce to Ceph
Intorduce to Ceph
 
Openstack swift, how does it work?
Openstack swift, how does it work?Openstack swift, how does it work?
Openstack swift, how does it work?
 
Why is a[1] fast than a.get(1)
Why is a[1]  fast than a.get(1)Why is a[1]  fast than a.get(1)
Why is a[1] fast than a.get(1)
 
減少重複的測試程式碼的一些方法
減少重複的測試程式碼的一些方法減少重複的測試程式碼的一些方法
減少重複的測試程式碼的一些方法
 
Openstack taskflow 簡介
Openstack taskflow 簡介Openstack taskflow 簡介
Openstack taskflow 簡介
 
Async: ways to store state
Async:  ways to store stateAsync:  ways to store state
Async: ways to store state
 
Openstack 簡介
Openstack 簡介Openstack 簡介
Openstack 簡介
 
Docker 原理與實作
Docker 原理與實作Docker 原理與實作
Docker 原理與實作
 
那些年,我們一起看的例外
那些年,我們一起看的例外那些年,我們一起看的例外
那些年,我們一起看的例外
 
Garbage collection 介紹
Garbage collection 介紹Garbage collection 介紹
Garbage collection 介紹
 
Python 如何執行
Python 如何執行Python 如何執行
Python 如何執行
 
C python 原始碼解析 投影片
C python 原始碼解析 投影片C python 原始碼解析 投影片
C python 原始碼解析 投影片
 

recover_pdb 原理與介紹

  • 2. 簡介 ● 中興大學學士 ● 任職於曼克斯 ● 接觸 python 時間有七年 ● 喜歡學習新的程式語言 ● C、C++、java、golang。 ● Linkin: http://www.linkedin. com/pub/kao-kuo- tung/67/9a2/6b3 ● About me: http://about. me/ya790206
  • 3. PDB 常用指令介紹 1. break 2. clear 3. step 4. next 5. return 6. continue 7. jump 8. list 9. p *可惜 pdb 無法做到監看變數改變的功能
  • 4. What is frame? ● 儲存 function 執行時的相關資訊,如區域變 數、函數位址等資訊。 ● 作用像 C 的 call stack( run-time stack )。
  • 5. PyFrameObject f_back next outer frame object (this frame’s caller) f_code code object being executed in this frame f_exc_traceback traceback if raised in this frame, or None f_exc_type exception type if raised in this frame, or None f_exc_value exception value if raised in this frame, or None f_globals global namespace seen by this frame f_lineno current line number in Python source code f_locals local namespace seen by this frame f_trace tracing function for this frame, or None REF: http://docs.python.org/2/library/inspect.html
  • 6. fun1 call fun2, fun2 call fun3, fun3 call fun1 fun1's frame fun2's frame fun3's frame fun1's frame f_code f_code fun1's code object fun2's code object fun3's code object f_back f_back f_back f_code f_code frame 未必擺在連續的記憶體上,因此需要 f_back 的存在。詳情請見 PyFrame_New。
  • 7. More Details f_trace, if not None, is a function called at the start of each source code line (this is used by the debugger) f_lineno is the current line number of the frame — writing to this from within a trace function jumps to the given line (only for the bottom-most frame).
  • 8. sys._getframe([depth])¶ Return a frame object from the call stack. If optional integer depth is given, return the frame object that many calls below the top of the stack. If that is deeper than the call stack, ValueError is raised. The default for depthis zero, returning the frame at the top of the call stack. sys.settrace(tracefunc) Set the system’s trace function, which allows you to implement a Python source code debugger in Python. The function is thread-specific; for a debugger to support multiple threads, it must be registered using settrace() for each thread being debugged.
  • 9. why? it must be registered using settrace() for each thread being debugged. Because: each thread have their own starck. In the python, the stack means the frame. process thread process thread thread thread
  • 10. 看看 pdb ● break => pdb:do_break -> bdb: set_break (just create break point) ● step => pdb:do_step -> bdb: set_step -> self. _set_stopinfo ● return => pdb:do_return -> bdb:set_return -> self._set_stopinfo
  • 11. trace function => bdb:trace_dispatch -> bdb: dispatch_line -> stop_here -> pdb:user_line (frame) -> pdb:interaction
  • 12. 問題 A: 當離開互動模式時,最上層的 frame 是有 bug code 的 frame。就像一般程式碼的執行,他會一 直執行最上層的 frame 的 code。 Q: 當有 bug 的 code 呼叫 set_trace 時, pdb 要 如何執行有 bug 的 code ?
  • 13. 問題 Q: 我只有呼叫一次 pdb 的 set_trace,那為何程 式可以重複進入 pdb 的方法? A: 因為每一層 frame 的 f_trace 都設定成 self. trace_dispatch,所以每次在執行有bug 的 code 時,都會執行 self.trace_dispatch。所以藉由這個 方法,可以重複進入 pdb 的方法。
  • 14. 問題 A: 因為 pdb 把相關資料存在物件裡(如 self.xxx = yyy) 而非 frame 裡(如 xxx=yyy) Q: 在執行有 bug 的 code 時,都是從 pdb 的方 法 return 回來的。因此 pdb 方法的 frame 會被 刪除。那為何我設定的中斷點資料不會消失?
  • 15. 問題 Q: 既然 pdb 的資料是存在物件裡。可是我的程 式碼( 有 bug 的 code) 並沒有變數指向 pdb 的物 件,為何他不會被回收( garbage collection )? A: 的確你的程式碼( 有 bug 的 code ) 沒有變數 指向 pdb。但是 pdb 在每個 frame 偷偷動了手 腳,讓每個 frame 的 f_trace 指向 pdb 物件的 trace_dispatch。這就是他長生的秘密。
  • 16. recoverable_pdb ● github: https://github.com/ya790206/recoverable_pdb ● Like time machine, you can recover the runtime to the point you save. ● addition command: ○ save point_name: save the current frame to the point. ○ restore point_name: restore frame from the frame you saved. ○ diff point_name: compare the current frame and the another you saved. ● limit: ○ same as jump command. ○ it can't recover file or database.
  • 17. 為何需要自定義一個 try_copy? 1. 因為不是每個物件都可以被 copy。如 code object。 2. 因為 dict 除了給程式設計師用外,他也會被用 來表示內部結構。因此需要對他做特別處理。 ref: http://docs.python.org/2/library/copy.html
  • 18. Thanks all thanks for all listener, taipei.py, and the manx. Taipe pyi: http://www.meetup. com/Taipei-py/ The manx: http://www.themanxgroup.tw/ The manx production: http://lucky-lane.com/