debugger.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. # debugger.py
  2. # A debugger for Pythonwin. Built from pdb.
  3. # Mark Hammond (MHammond@skippinet.com.au) - Dec 94.
  4. # usage:
  5. # >>> import pywin.debugger
  6. # >>> pywin.debugger.GetDebugger().run("command")
  7. import pdb
  8. import bdb
  9. import sys
  10. import string
  11. import os
  12. import types
  13. import win32ui
  14. import win32api
  15. import win32con
  16. import pywin.docking.DockingBar
  17. from pywin.mfc import dialog, object, afxres, window
  18. from pywin.framework import app, interact, editor, scriptutils
  19. from pywin.framework.editor.color.coloreditor import MARKER_CURRENT, MARKER_BREAKPOINT
  20. from pywin.tools import browser, hierlist
  21. import commctrl
  22. import traceback
  23. #import win32traceutil
  24. if win32ui.UNICODE:
  25. LVN_ENDLABELEDIT = commctrl.LVN_ENDLABELEDITW
  26. else:
  27. LVN_ENDLABELEDIT = commctrl.LVN_ENDLABELEDITA
  28. from .dbgcon import *
  29. error = "pywin.debugger.error"
  30. def SetInteractiveContext(globs, locs):
  31. if interact.edit is not None and interact.edit.currentView is not None:
  32. interact.edit.currentView.SetContext(globs, locs)
  33. def _LineStateToMarker(ls):
  34. if ls==LINESTATE_CURRENT:
  35. return MARKER_CURRENT
  36. # elif ls == LINESTATE_CALLSTACK:
  37. # return MARKER_CALLSTACK
  38. return MARKER_BREAKPOINT
  39. class HierListItem(browser.HLIPythonObject):
  40. pass
  41. class HierFrameItem(HierListItem):
  42. def __init__(self, frame, debugger):
  43. HierListItem.__init__(self, frame, repr(frame))
  44. self.debugger = debugger
  45. def GetText(self):
  46. name = self.myobject.f_code.co_name
  47. if not name or name == '?' :
  48. # See if locals has a '__name__' (ie, a module)
  49. if '__name__' in self.myobject.f_locals:
  50. name = str(self.myobject.f_locals['__name__']) + " module"
  51. else:
  52. name = '<Debugger Context>'
  53. return "%s (%s:%d)" % (name, os.path.split(self.myobject.f_code.co_filename)[1], self.myobject.f_lineno)
  54. def GetBitmapColumn(self):
  55. if self.debugger.curframe is self.myobject:
  56. return 7
  57. else:
  58. return 8
  59. def GetSubList(self):
  60. ret = []
  61. ret.append(HierFrameDict(self.myobject.f_locals, "Locals", 2))
  62. ret.append(HierFrameDict(self.myobject.f_globals, "Globals", 1))
  63. return ret
  64. def IsExpandable(self):
  65. return 1
  66. def TakeDefaultAction(self):
  67. # Set the default frame to be this frame.
  68. self.debugger.set_cur_frame(self.myobject)
  69. return 1
  70. class HierFrameDict(browser.HLIDict):
  71. def __init__(self, dict, name, bitmapColumn):
  72. self.bitmapColumn=bitmapColumn
  73. browser.HLIDict.__init__(self, dict, name)
  74. def GetBitmapColumn(self):
  75. return self.bitmapColumn
  76. class NoStackAvailableItem(HierListItem):
  77. def __init__(self, why):
  78. HierListItem.__init__(self, None, why)
  79. def IsExpandable(self):
  80. return 0
  81. def GetText(self):
  82. return self.name
  83. def GetBitmapColumn(self):
  84. return 8
  85. class HierStackRoot(HierListItem):
  86. def __init__( self, debugger ):
  87. HierListItem.__init__(self, debugger, None)
  88. self.last_stack = []
  89. ## def __del__(self):
  90. ## print "HierStackRoot dieing"
  91. def GetSubList(self):
  92. debugger = self.myobject
  93. # print self.debugger.stack, self.debugger.curframe
  94. ret = []
  95. if debugger.debuggerState==DBGSTATE_BREAK:
  96. stackUse=debugger.stack[:]
  97. stackUse.reverse()
  98. self.last_stack = []
  99. for frame, lineno in stackUse:
  100. self.last_stack.append( (frame, lineno) )
  101. if frame is debugger.userbotframe: # Dont bother showing frames below our bottom frame.
  102. break
  103. for frame, lineno in self.last_stack:
  104. ret.append( HierFrameItem( frame, debugger ) )
  105. ## elif debugger.debuggerState==DBGSTATE_NOT_DEBUGGING:
  106. ## ret.append(NoStackAvailableItem('<nothing is being debugged>'))
  107. ## else:
  108. ## ret.append(NoStackAvailableItem('<stack not available while running>'))
  109. return ret
  110. def GetText(self):
  111. return 'root item'
  112. def IsExpandable(self):
  113. return 1
  114. class HierListDebugger(hierlist.HierListWithItems):
  115. """ Hier List of stack frames, breakpoints, whatever """
  116. def __init__(self):
  117. hierlist.HierListWithItems.__init__(self, None, win32ui.IDB_DEBUGGER_HIER, None, win32api.RGB(255,0,0))
  118. def Setup(self, debugger):
  119. root = HierStackRoot(debugger)
  120. self.AcceptRoot(root)
  121. # def Refresh(self):
  122. # self.Setup()
  123. class DebuggerWindow(window.Wnd):
  124. def __init__(self, ob):
  125. window.Wnd.__init__(self, ob)
  126. self.debugger = None
  127. def Init(self, debugger):
  128. self.debugger = debugger
  129. def GetDefRect(self):
  130. defRect = app.LoadWindowSize("Debugger Windows\\" + self.title)
  131. if defRect[2]-defRect[0]==0:
  132. defRect = 0, 0, 150, 150
  133. return defRect
  134. def OnDestroy(self, msg):
  135. newSize = self.GetWindowPlacement()[4]
  136. pywin.framework.app.SaveWindowSize("Debugger Windows\\" + self.title, newSize)
  137. return window.Wnd.OnDestroy(self, msg)
  138. def OnKeyDown(self, msg):
  139. key = msg[2]
  140. if key in [13, 27, 32]: return 1
  141. if key in [46,8]: # delete/BS key
  142. self.DeleteSelected()
  143. return 0
  144. view = scriptutils.GetActiveView()
  145. try:
  146. firer = view.bindings.fire_key_event
  147. except AttributeError:
  148. firer = None
  149. if firer is not None:
  150. return firer(msg)
  151. else:
  152. return 1
  153. def DeleteSelected(self):
  154. win32api.MessageBeep()
  155. def EditSelected(self):
  156. win32api.MessageBeep()
  157. class DebuggerStackWindow(DebuggerWindow):
  158. title = "Stack"
  159. def __init__(self):
  160. DebuggerWindow.__init__(self, win32ui.CreateTreeCtrl())
  161. self.list = HierListDebugger()
  162. self.listOK = 0
  163. def SaveState(self):
  164. self.list.DeleteAllItems()
  165. self.listOK = 0
  166. win32ui.WriteProfileVal("Debugger Windows\\" + self.title, "Visible", self.IsWindowVisible())
  167. def CreateWindow(self, parent):
  168. style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_BORDER | commctrl.TVS_HASLINES | commctrl.TVS_LINESATROOT | commctrl.TVS_HASBUTTONS
  169. self._obj_.CreateWindow(style, self.GetDefRect(), parent, win32ui.IDC_LIST1)
  170. self.HookMessage(self.OnKeyDown, win32con.WM_KEYDOWN)
  171. self.HookMessage(self.OnKeyDown, win32con.WM_SYSKEYDOWN)
  172. self.list.HierInit (parent, self)
  173. self.listOK = 0 # delayed setup
  174. #self.list.Setup()
  175. def RespondDebuggerState(self, state):
  176. assert self.debugger is not None, "Init not called"
  177. if not self.listOK:
  178. self.listOK = 1
  179. self.list.Setup(self.debugger)
  180. else:
  181. self.list.Refresh()
  182. def RespondDebuggerData(self):
  183. try:
  184. handle = self.GetChildItem(0)
  185. except win32ui.error:
  186. return # No items
  187. while 1:
  188. item = self.list.ItemFromHandle(handle)
  189. col = self.list.GetBitmapColumn(item)
  190. selCol = self.list.GetSelectedBitmapColumn(item)
  191. if selCol is None: selCol = col
  192. if self.list.GetItemImage(handle)!= (col, selCol):
  193. self.list.SetItemImage(handle, col, selCol)
  194. try:
  195. handle = self.GetNextSiblingItem(handle)
  196. except win32ui.error:
  197. break
  198. class DebuggerListViewWindow(DebuggerWindow):
  199. def __init__(self):
  200. DebuggerWindow.__init__(self, win32ui.CreateListCtrl())
  201. def CreateWindow(self, parent):
  202. list = self
  203. style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_BORDER | commctrl.LVS_EDITLABELS | commctrl.LVS_REPORT
  204. self._obj_.CreateWindow(style, self.GetDefRect(), parent, win32ui.IDC_LIST1)
  205. self.HookMessage(self.OnKeyDown, win32con.WM_KEYDOWN)
  206. self.HookMessage(self.OnKeyDown, win32con.WM_SYSKEYDOWN)
  207. list = self
  208. title, width = self.columns[0]
  209. itemDetails = (commctrl.LVCFMT_LEFT, width, title, 0)
  210. list.InsertColumn(0, itemDetails)
  211. col = 1
  212. for title, width in self.columns[1:]:
  213. col = col + 1
  214. itemDetails = (commctrl.LVCFMT_LEFT, width, title, 0)
  215. list.InsertColumn(col, itemDetails)
  216. parent.HookNotify(self.OnListEndLabelEdit, LVN_ENDLABELEDIT)
  217. parent.HookNotify(self.OnItemRightClick, commctrl.NM_RCLICK)
  218. parent.HookNotify(self.OnItemDoubleClick, commctrl.NM_DBLCLK)
  219. def RespondDebuggerData(self):
  220. pass
  221. def RespondDebuggerState(self, state):
  222. pass
  223. def EditSelected(self):
  224. try:
  225. sel = self.GetNextItem(-1, commctrl.LVNI_SELECTED)
  226. except win32ui.error:
  227. return
  228. self.EditLabel(sel)
  229. def OnKeyDown(self, msg):
  230. key = msg[2]
  231. # If someone starts typing, they probably are trying to edit the text!
  232. if chr(key) in string.ascii_uppercase:
  233. self.EditSelected()
  234. return 0
  235. return DebuggerWindow.OnKeyDown(self, msg)
  236. def OnItemDoubleClick(self, notify_data, extra):
  237. self.EditSelected()
  238. def OnItemRightClick(self, notify_data, extra):
  239. # First select the item we right-clicked on.
  240. pt = self.ScreenToClient(win32api.GetCursorPos())
  241. flags, hItem, subitem = self.HitTest(pt)
  242. if hItem==-1 or commctrl.TVHT_ONITEM & flags==0:
  243. return None
  244. self.SetItemState(hItem, commctrl.LVIS_SELECTED, commctrl.LVIS_SELECTED)
  245. menu = win32ui.CreatePopupMenu()
  246. menu.AppendMenu(win32con.MF_STRING|win32con.MF_ENABLED,1000, "Edit item")
  247. menu.AppendMenu(win32con.MF_STRING|win32con.MF_ENABLED,1001, "Delete item")
  248. dockbar = self.GetParent()
  249. if dockbar.IsFloating():
  250. hook_parent = win32ui.GetMainFrame()
  251. else:
  252. hook_parent = self.GetParentFrame()
  253. hook_parent.HookCommand(self.OnEditItem, 1000)
  254. hook_parent.HookCommand(self.OnDeleteItem, 1001)
  255. menu.TrackPopupMenu(win32api.GetCursorPos()) # track at mouse position.
  256. return None
  257. def OnDeleteItem(self,command, code):
  258. self.DeleteSelected()
  259. def OnEditItem(self, command, code):
  260. self.EditSelected()
  261. class DebuggerBreakpointsWindow(DebuggerListViewWindow):
  262. title = "Breakpoints"
  263. columns = [ ("Condition", 70), ("Location", 1024)]
  264. def SaveState(self):
  265. items = []
  266. for i in range(self.GetItemCount()):
  267. items.append(self.GetItemText(i,0))
  268. items.append(self.GetItemText(i,1))
  269. win32ui.WriteProfileVal("Debugger Windows\\" + self.title, "BreakpointList", "\t".join(items))
  270. win32ui.WriteProfileVal("Debugger Windows\\" + self.title, "Visible", self.IsWindowVisible())
  271. return 1
  272. def OnListEndLabelEdit(self, std, extra):
  273. item = extra[0]
  274. text = item[4]
  275. if text is None: return
  276. item_id = self.GetItem(item[0])[6]
  277. from bdb import Breakpoint
  278. for bplist in Breakpoint.bplist.values():
  279. for bp in bplist:
  280. if id(bp)==item_id:
  281. if text.strip().lower()=="none":
  282. text = None
  283. bp.cond = text
  284. break
  285. self.RespondDebuggerData()
  286. def DeleteSelected(self):
  287. try:
  288. num = self.GetNextItem(-1, commctrl.LVNI_SELECTED)
  289. item_id = self.GetItem(num)[6]
  290. from bdb import Breakpoint
  291. for bplist in list(Breakpoint.bplist.values()):
  292. for bp in bplist:
  293. if id(bp)==item_id:
  294. self.debugger.clear_break(bp.file, bp.line)
  295. break
  296. except win32ui.error:
  297. win32api.MessageBeep()
  298. self.RespondDebuggerData()
  299. def RespondDebuggerData(self):
  300. l = self
  301. l.DeleteAllItems()
  302. index = -1
  303. from bdb import Breakpoint
  304. for bplist in Breakpoint.bplist.values():
  305. for bp in bplist:
  306. baseName = os.path.split(bp.file)[1]
  307. cond = bp.cond
  308. item = index+1, 0, 0, 0, str(cond), 0, id(bp)
  309. index = l.InsertItem(item)
  310. l.SetItemText(index, 1, "%s: %s" % (baseName, bp.line))
  311. class DebuggerWatchWindow(DebuggerListViewWindow):
  312. title = "Watch"
  313. columns = [ ("Expression", 70), ("Value", 1024)]
  314. def CreateWindow(self, parent):
  315. DebuggerListViewWindow.CreateWindow(self, parent)
  316. items = win32ui.GetProfileVal("Debugger Windows\\" + self.title, "Items", "").split("\t")
  317. index = -1
  318. for item in items:
  319. if item:
  320. index = self.InsertItem(index+1, item)
  321. self.InsertItem(index+1, "<New Item>")
  322. def SaveState(self):
  323. items = []
  324. for i in range(self.GetItemCount()-1):
  325. items.append(self.GetItemText(i,0))
  326. win32ui.WriteProfileVal("Debugger Windows\\" + self.title, "Items", "\t".join(items))
  327. win32ui.WriteProfileVal("Debugger Windows\\" + self.title, "Visible", self.IsWindowVisible())
  328. return 1
  329. def OnListEndLabelEdit(self, std, extra):
  330. item = extra[0]
  331. itemno = item[0]
  332. text = item[4]
  333. if text is None: return
  334. self.SetItemText(itemno, 0, text)
  335. if itemno == self.GetItemCount()-1:
  336. self.InsertItem(itemno+1, "<New Item>")
  337. self.RespondDebuggerState(self.debugger.debuggerState)
  338. def DeleteSelected(self):
  339. try:
  340. num = self.GetNextItem(-1, commctrl.LVNI_SELECTED)
  341. if num < self.GetItemCount()-1: # We cant delete the last
  342. self.DeleteItem(num)
  343. except win32ui.error:
  344. win32api.MessageBeep()
  345. def RespondDebuggerState(self, state):
  346. globs = locs = None
  347. if state==DBGSTATE_BREAK:
  348. if self.debugger.curframe:
  349. globs = self.debugger.curframe.f_globals
  350. locs = self.debugger.curframe.f_locals
  351. elif state==DBGSTATE_NOT_DEBUGGING:
  352. import __main__
  353. globs = locs = __main__.__dict__
  354. for i in range(self.GetItemCount()-1):
  355. text = self.GetItemText(i, 0)
  356. if globs is None:
  357. val = ""
  358. else:
  359. try:
  360. val = repr( eval( text, globs, locs) )
  361. except SyntaxError:
  362. val = "Syntax Error"
  363. except:
  364. t, v, tb = sys.exc_info()
  365. val = traceback.format_exception_only(t, v)[0].strip()
  366. tb = None # prevent a cycle.
  367. self.SetItemText(i, 1, val)
  368. def CreateDebuggerDialog(parent, klass):
  369. control = klass()
  370. control.CreateWindow(parent)
  371. return control
  372. DebuggerDialogInfos = (
  373. (0xe810, DebuggerStackWindow, None),
  374. (0xe811, DebuggerBreakpointsWindow, (10, 10)),
  375. (0xe812, DebuggerWatchWindow, None),
  376. )
  377. # Prepare all the "control bars" for this package.
  378. # If control bars are not all loaded when the toolbar-state functions are
  379. # called, things go horribly wrong.
  380. def PrepareControlBars(frame):
  381. style = win32con.WS_CHILD | afxres.CBRS_SIZE_DYNAMIC | afxres.CBRS_TOP | afxres.CBRS_TOOLTIPS | afxres.CBRS_FLYBY
  382. tbd = win32ui.CreateToolBar (frame, style, win32ui.ID_VIEW_TOOLBAR_DBG)
  383. tbd.ModifyStyle(0, commctrl.TBSTYLE_FLAT)
  384. tbd.LoadToolBar(win32ui.IDR_DEBUGGER)
  385. tbd.EnableDocking(afxres.CBRS_ALIGN_ANY)
  386. tbd.SetWindowText("Debugger")
  387. frame.DockControlBar(tbd)
  388. # and the other windows.
  389. for id, klass, float in DebuggerDialogInfos:
  390. try:
  391. frame.GetControlBar(id)
  392. exists=1
  393. except win32ui.error:
  394. exists=0
  395. if exists: continue
  396. bar = pywin.docking.DockingBar.DockingBar()
  397. style=win32con.WS_CHILD | afxres.CBRS_LEFT # don't create visible.
  398. bar.CreateWindow(frame, CreateDebuggerDialog, klass.title, id, style, childCreatorArgs=(klass,))
  399. bar.SetBarStyle( bar.GetBarStyle()|afxres.CBRS_TOOLTIPS|afxres.CBRS_FLYBY|afxres.CBRS_SIZE_DYNAMIC)
  400. bar.EnableDocking(afxres.CBRS_ALIGN_ANY)
  401. if float is None:
  402. frame.DockControlBar(bar)
  403. else:
  404. frame.FloatControlBar(bar, float, afxres.CBRS_ALIGN_ANY)
  405. ## frame.ShowControlBar(bar, 0, 1)
  406. SKIP_NONE=0
  407. SKIP_STEP=1
  408. SKIP_RUN=2
  409. debugger_parent=pdb.Pdb
  410. class Debugger(debugger_parent):
  411. def __init__(self):
  412. self.inited = 0
  413. self.skipBotFrame = SKIP_NONE
  414. self.userbotframe = None
  415. self.frameShutdown = 0
  416. self.pumping = 0
  417. self.debuggerState = DBGSTATE_NOT_DEBUGGING # Assume so, anyway.
  418. self.shownLineCurrent = None # The last filename I highlighted.
  419. self.shownLineCallstack = None # The last filename I highlighted.
  420. self.last_cmd_debugged = ""
  421. self.abortClosed = 0
  422. self.isInitialBreakpoint = 0
  423. debugger_parent.__init__(self)
  424. # See if any break-points have been set in the editor
  425. for doc in editor.editorTemplate.GetDocumentList():
  426. lineNo = -1
  427. while 1:
  428. lineNo = doc.MarkerGetNext(lineNo+1, MARKER_BREAKPOINT)
  429. if lineNo <= 0: break
  430. self.set_break(doc.GetPathName(), lineNo)
  431. self.reset()
  432. self.inForcedGUI = win32ui.GetApp().IsInproc()
  433. self.options = LoadDebuggerOptions()
  434. self.bAtException = self.bAtPostMortem = 0
  435. def __del__(self):
  436. self.close()
  437. def close(self, frameShutdown = 0):
  438. # abortClose indicates if we have total shutdown
  439. # (ie, main window is dieing)
  440. if self.pumping:
  441. # Can stop pump here, as it only posts a message, and
  442. # returns immediately.
  443. if not self.StopDebuggerPump(): # User cancelled close.
  444. return 0
  445. # NOTE - from this point on the close can not be
  446. # stopped - the WM_QUIT message is already in the queue.
  447. self.frameShutdown = frameShutdown
  448. if not self.inited: return 1
  449. self.inited = 0
  450. SetInteractiveContext(None, None)
  451. frame = win32ui.GetMainFrame()
  452. # Hide the debuger toolbars (as they wont normally form part of the main toolbar state.
  453. for id, klass, float in DebuggerDialogInfos:
  454. try:
  455. tb = frame.GetControlBar(id)
  456. if tb.dialog is not None: # We may never have actually been shown.
  457. tb.dialog.SaveState()
  458. frame.ShowControlBar(tb, 0, 1)
  459. except win32ui.error:
  460. pass
  461. self._UnshowCurrentLine()
  462. self.set_quit()
  463. return 1
  464. def StopDebuggerPump(self):
  465. assert self.pumping, "Can't stop the debugger pump if Im not pumping!"
  466. # After stopping a pump, I may never return.
  467. if self.GUIAboutToFinishInteract():
  468. self.pumping = 0
  469. win32ui.StopDebuggerPump() # Posts a message, so we do return.
  470. return 1
  471. return 0
  472. def get_option(self, option):
  473. """Public interface into debugger options
  474. """
  475. try:
  476. return self.options[option]
  477. except KeyError:
  478. raise error("Option %s is not a valid option" % option)
  479. def prep_run(self, cmd):
  480. pass
  481. def done_run(self, cmd=None):
  482. self.RespondDebuggerState(DBGSTATE_NOT_DEBUGGING)
  483. self.close()
  484. def canonic(self, fname):
  485. return os.path.abspath(fname).lower()
  486. def reset(self):
  487. debugger_parent.reset(self)
  488. self.userbotframe = None
  489. self.UpdateAllLineStates()
  490. self._UnshowCurrentLine()
  491. def setup(self, f, t):
  492. debugger_parent.setup(self, f, t)
  493. self.bAtException = t is not None
  494. def set_break(self, filename, lineno, temporary=0, cond = None):
  495. filename = self.canonic(filename)
  496. self.SetLineState(filename, lineno, LINESTATE_BREAKPOINT)
  497. return debugger_parent.set_break(self, filename, lineno, temporary, cond)
  498. def clear_break(self, filename, lineno):
  499. filename = self.canonic(filename)
  500. self.ResetLineState(filename, lineno, LINESTATE_BREAKPOINT)
  501. return debugger_parent.clear_break(self, filename, lineno)
  502. def cmdloop(self):
  503. if self.frameShutdown: return # App in the process of closing - never break in!
  504. self.GUIAboutToBreak()
  505. def print_stack_entry(self, frame):
  506. # We dont want a stack printed - our GUI is better :-)
  507. pass
  508. def user_return(self, frame, return_value):
  509. # Same as parent, just no "print"
  510. # This function is called when a return trap is set here
  511. frame.f_locals['__return__'] = return_value
  512. self.interaction(frame, None)
  513. def user_call(self, frame, args):
  514. # base class has an annoying 'print' that adds no value to us...
  515. if self.stop_here(frame):
  516. self.interaction(frame, None)
  517. def user_exception(self, frame, exc_info):
  518. # This function is called if an exception occurs,
  519. # but only if we are to stop at or just below this level
  520. (exc_type, exc_value, exc_traceback) = exc_info
  521. if self.get_option(OPT_STOP_EXCEPTIONS):
  522. frame.f_locals['__exception__'] = exc_type, exc_value
  523. print("Unhandled exception while debugging...")
  524. # on both py2k and py3k, we may be called with exc_value
  525. # being the args to the exception, or it may already be
  526. # instantiated (IOW, PyErr_Normalize() hasn't been
  527. # called on the args). In py2k this is fine, but in
  528. # py3k, traceback.print_exception fails. So on py3k
  529. # we instantiate an exception instance to print.
  530. if sys.version_info > (3,) and not isinstance(exc_value, BaseException):
  531. # they are args - may be a single item or already a tuple
  532. if not isinstance(exc_value, tuple):
  533. exc_value = (exc_value,)
  534. exc_value = exc_type(*exc_value)
  535. traceback.print_exception(exc_type, exc_value, exc_traceback)
  536. self.interaction(frame, exc_traceback)
  537. def user_line(self, frame):
  538. if frame.f_lineno==0: return
  539. debugger_parent.user_line(self, frame)
  540. def stop_here(self, frame):
  541. if self.isInitialBreakpoint:
  542. self.isInitialBreakpoint = 0
  543. self.set_continue()
  544. return 0
  545. if frame is self.botframe and self.skipBotFrame == SKIP_RUN:
  546. self.set_continue()
  547. return 0
  548. if frame is self.botframe and self.skipBotFrame == SKIP_STEP:
  549. self.set_step()
  550. return 0
  551. return debugger_parent.stop_here(self, frame)
  552. def run(self, cmd,globals=None, locals=None, start_stepping = 1):
  553. if not isinstance(cmd, (str, types.CodeType)):
  554. raise TypeError("Only strings can be run")
  555. self.last_cmd_debugged = cmd
  556. if start_stepping:
  557. self.isInitialBreakpoint = 0
  558. else:
  559. self.isInitialBreakpoint = 1
  560. try:
  561. if globals is None:
  562. import __main__
  563. globals = __main__.__dict__
  564. if locals is None:
  565. locals = globals
  566. self.reset()
  567. self.prep_run(cmd)
  568. sys.settrace(self.trace_dispatch)
  569. if type(cmd) != types.CodeType:
  570. cmd = cmd+'\n'
  571. try:
  572. try:
  573. if start_stepping: self.skipBotFrame = SKIP_STEP
  574. else: self.skipBotFrame = SKIP_RUN
  575. exec(cmd, globals, locals)
  576. except bdb.BdbQuit:
  577. pass
  578. finally:
  579. self.skipBotFrame = SKIP_NONE
  580. self.quitting = 1
  581. sys.settrace(None)
  582. finally:
  583. self.done_run(cmd)
  584. def runeval(self, expr, globals=None, locals=None):
  585. self.prep_run(expr)
  586. try:
  587. debugger_parent.runeval(self, expr, globals, locals)
  588. finally:
  589. self.done_run(expr)
  590. def runexec(self, what, globs=None, locs=None):
  591. self.reset()
  592. sys.settrace(self.trace_dispatch)
  593. try:
  594. try:
  595. exec(what, globs, locs)
  596. except bdb.BdbQuit:
  597. pass
  598. finally:
  599. self.quitting = 1
  600. sys.settrace(None)
  601. def do_set_step(self):
  602. if self.GUIAboutToRun():
  603. self.set_step()
  604. def do_set_next(self):
  605. if self.GUIAboutToRun():
  606. self.set_next(self.curframe)
  607. def do_set_return(self):
  608. if self.GUIAboutToRun():
  609. self.set_return(self.curframe)
  610. def do_set_continue(self):
  611. if self.GUIAboutToRun():
  612. self.set_continue()
  613. def set_quit(self):
  614. ok = 1
  615. if self.pumping:
  616. ok = self.StopDebuggerPump()
  617. if ok:
  618. debugger_parent.set_quit(self)
  619. def _dump_frame_(self, frame,name=None):
  620. if name is None: name = ""
  621. if frame:
  622. if frame.f_code and frame.f_code.co_filename:
  623. fname = os.path.split(frame.f_code.co_filename)[1]
  624. else:
  625. fname = "??"
  626. print(repr(name), fname, frame.f_lineno, frame)
  627. else:
  628. print(repr(name), "None")
  629. def set_trace(self):
  630. # Start debugging from _2_ levels up!
  631. try:
  632. 1 + ''
  633. except:
  634. frame = sys.exc_info()[2].tb_frame.f_back.f_back
  635. self.reset()
  636. self.userbotframe = None
  637. while frame:
  638. # scriptutils.py creates a local variable with name
  639. # '_debugger_stop_frame_', and we dont go past it
  640. # (everything above this is Pythonwin framework code)
  641. if "_debugger_stop_frame_" in frame.f_locals:
  642. self.userbotframe = frame
  643. break
  644. frame.f_trace = self.trace_dispatch
  645. self.botframe = frame
  646. frame = frame.f_back
  647. self.set_step()
  648. sys.settrace(self.trace_dispatch)
  649. def set_cur_frame(self, frame):
  650. # Sets the "current" frame - ie, the frame with focus. This is the
  651. # frame on which "step out" etc actions are taken.
  652. # This may or may not be the top of the stack.
  653. assert frame is not None, "You must pass a valid frame"
  654. self.curframe = frame
  655. for f, index in self.stack:
  656. if f is frame:
  657. self.curindex = index
  658. break
  659. else:
  660. assert 0, "Can't find the frame in the stack."
  661. SetInteractiveContext(frame.f_globals, frame.f_locals)
  662. self.GUIRespondDebuggerData()
  663. self.ShowCurrentLine()
  664. def IsBreak(self):
  665. return self.debuggerState == DBGSTATE_BREAK
  666. def IsDebugging(self):
  667. return self.debuggerState != DBGSTATE_NOT_DEBUGGING
  668. def RespondDebuggerState(self, state):
  669. if state == self.debuggerState: return
  670. if state==DBGSTATE_NOT_DEBUGGING: # Debugger exists, but not doing anything
  671. title = ""
  672. elif state==DBGSTATE_RUNNING: # Code is running under the debugger.
  673. title = " - running"
  674. elif state==DBGSTATE_BREAK: # We are at a breakpoint or stepping or whatever.
  675. if self.bAtException:
  676. if self.bAtPostMortem:
  677. title = " - post mortem exception"
  678. else:
  679. title = " - exception"
  680. else:
  681. title = " - break"
  682. else:
  683. raise error("Invalid debugger state passed!")
  684. win32ui.GetMainFrame().SetWindowText(win32ui.LoadString(win32ui.IDR_MAINFRAME) + title)
  685. if self.debuggerState == DBGSTATE_QUITTING and state != DBGSTATE_NOT_DEBUGGING:
  686. print("Ignoring state change cos Im trying to stop!", state)
  687. return
  688. self.debuggerState = state
  689. try:
  690. frame = win32ui.GetMainFrame()
  691. except win32ui.error:
  692. frame = None
  693. if frame is not None:
  694. for id, klass, float in DebuggerDialogInfos:
  695. cb = win32ui.GetMainFrame().GetControlBar(id).dialog
  696. cb.RespondDebuggerState(state)
  697. # Tell each open editor window about the state transition
  698. for doc in editor.editorTemplate.GetDocumentList():
  699. doc.OnDebuggerStateChange(state)
  700. self.ShowCurrentLine()
  701. #
  702. # GUI debugger interface.
  703. #
  704. def GUICheckInit(self):
  705. if self.inited: return
  706. self.inited = 1
  707. frame = win32ui.GetMainFrame()
  708. # Ensure the debugger windows are attached to the debugger.
  709. for id, klass, float in DebuggerDialogInfos:
  710. w = frame.GetControlBar(id)
  711. w.dialog.Init(self)
  712. # Show toolbar if it was visible during last debug session
  713. # This would be better done using a CDockState, but that class is not wrapped yet
  714. if win32ui.GetProfileVal("Debugger Windows\\" + w.dialog.title, "Visible", 0):
  715. frame.ShowControlBar(w, 1, 1)
  716. # ALWAYS show debugging toolbar, regardless of saved state
  717. tb = frame.GetControlBar(win32ui.ID_VIEW_TOOLBAR_DBG)
  718. frame.ShowControlBar(tb, 1, 1)
  719. self.GUIRespondDebuggerData()
  720. # frame.RecalcLayout()
  721. def GetDebuggerBar(self, barName):
  722. frame = win32ui.GetMainFrame()
  723. for id, klass, float in DebuggerDialogInfos:
  724. if klass.title == barName:
  725. return frame.GetControlBar(id)
  726. assert 0, "Can't find a bar of that name!"
  727. def GUIRespondDebuggerData(self):
  728. if not self.inited: # GUI not inited - no toolbars etc.
  729. return
  730. for id, klass, float in DebuggerDialogInfos:
  731. cb = win32ui.GetMainFrame().GetControlBar(id).dialog
  732. cb.RespondDebuggerData()
  733. def GUIAboutToRun(self):
  734. if not self.StopDebuggerPump():
  735. return 0
  736. self._UnshowCurrentLine()
  737. self.RespondDebuggerState(DBGSTATE_RUNNING)
  738. SetInteractiveContext(None, None)
  739. return 1
  740. def GUIAboutToBreak(self):
  741. "Called as the GUI debugger is about to get context, and take control of the running program."
  742. self.GUICheckInit()
  743. self.RespondDebuggerState(DBGSTATE_BREAK)
  744. self.GUIAboutToInteract()
  745. if self.pumping:
  746. print("!!! Already pumping - outa here")
  747. return
  748. self.pumping = 1
  749. win32ui.StartDebuggerPump() # NOTE - This will NOT return until the user is finished interacting
  750. assert not self.pumping, "Should not be pumping once the pump has finished"
  751. if self.frameShutdown: # User shut down app while debugging
  752. win32ui.GetMainFrame().PostMessage(win32con.WM_CLOSE)
  753. def GUIAboutToInteract(self):
  754. "Called as the GUI is about to perform any interaction with the user"
  755. frame = win32ui.GetMainFrame()
  756. # Remember the enabled state of our main frame
  757. # may be disabled primarily if a modal dialog is displayed.
  758. # Only get at enabled via GetWindowLong.
  759. self.bFrameEnabled = frame.IsWindowEnabled()
  760. self.oldForeground = None
  761. fw = win32ui.GetForegroundWindow()
  762. if fw is not frame:
  763. self.oldForeground = fw
  764. # fw.EnableWindow(0) Leave enabled for now?
  765. self.oldFrameEnableState = frame.IsWindowEnabled()
  766. frame.EnableWindow(1)
  767. if self.inForcedGUI and not frame.IsWindowVisible():
  768. frame.ShowWindow(win32con.SW_SHOW)
  769. frame.UpdateWindow()
  770. if self.curframe:
  771. SetInteractiveContext(self.curframe.f_globals, self.curframe.f_locals)
  772. else:
  773. SetInteractiveContext(None, None)
  774. self.GUIRespondDebuggerData()
  775. def GUIAboutToFinishInteract(self):
  776. """Called as the GUI is about to finish any interaction with the user
  777. Returns non zero if we are allowed to stop interacting"""
  778. if self.oldForeground is not None:
  779. try:
  780. win32ui.GetMainFrame().EnableWindow(self.oldFrameEnableState)
  781. self.oldForeground.EnableWindow(1)
  782. except win32ui.error:
  783. # old window may be dead.
  784. pass
  785. # self.oldForeground.SetForegroundWindow() - fails??
  786. if not self.inForcedGUI:
  787. return 1 # Never a problem, and nothing else to do.
  788. # If we are running a forced GUI, we may never get an opportunity
  789. # to interact again. Therefore we perform a "SaveAll", to makesure that
  790. # any documents are saved before leaving.
  791. for template in win32ui.GetApp().GetDocTemplateList():
  792. for doc in template.GetDocumentList():
  793. if not doc.SaveModified():
  794. return 0
  795. # All documents saved - now hide the app and debugger.
  796. if self.get_option(OPT_HIDE):
  797. frame = win32ui.GetMainFrame()
  798. frame.ShowWindow(win32con.SW_HIDE)
  799. return 1
  800. #
  801. # Pythonwin interface - all stuff to do with showing source files,
  802. # changing line states etc.
  803. #
  804. def ShowLineState(self, fileName, lineNo, lineState):
  805. # Set the state of a line, open if not already
  806. self.ShowLineNo(fileName, lineNo)
  807. self.SetLineState(fileName, lineNo, lineState)
  808. def SetLineState(self, fileName, lineNo, lineState):
  809. # Set the state of a line if the document is open.
  810. doc = editor.editorTemplate.FindOpenDocument(fileName)
  811. if doc is not None:
  812. marker = _LineStateToMarker(lineState)
  813. if not doc.MarkerCheck(lineNo, marker):
  814. doc.MarkerAdd(lineNo, marker)
  815. def ResetLineState(self, fileName, lineNo, lineState):
  816. # Set the state of a line if the document is open.
  817. doc = editor.editorTemplate.FindOpenDocument(fileName)
  818. if doc is not None:
  819. marker = _LineStateToMarker(lineState)
  820. doc.MarkerDelete(lineNo, marker)
  821. def UpdateDocumentLineStates(self, doc):
  822. # Show all lines in their special status color. If the doc is open
  823. # all line states are reset.
  824. doc.MarkerDeleteAll( MARKER_BREAKPOINT )
  825. doc.MarkerDeleteAll( MARKER_CURRENT )
  826. fname = self.canonic(doc.GetPathName())
  827. # Now loop over all break-points
  828. for line in self.breaks.get(fname, []):
  829. doc.MarkerAdd(line, MARKER_BREAKPOINT)
  830. # And the current line if in this document.
  831. if self.shownLineCurrent and fname == self.shownLineCurrent[0]:
  832. lineNo = self.shownLineCurrent[1]
  833. if not doc.MarkerCheck(lineNo, MARKER_CURRENT):
  834. doc.MarkerAdd(lineNo, MARKER_CURRENT)
  835. # if self.shownLineCallstack and fname == self.shownLineCallstack[0]:
  836. # doc.MarkerAdd(self.shownLineCallstack[1], MARKER_CURRENT)
  837. def UpdateAllLineStates(self):
  838. for doc in editor.editorTemplate.GetDocumentList():
  839. self.UpdateDocumentLineStates(doc)
  840. def ShowCurrentLine(self):
  841. # Show the current line. Only ever 1 current line - undoes last current
  842. # The "Current Line" is self.curframe.
  843. # The "Callstack Line" is the top of the stack.
  844. # If current == callstack, only show as current.
  845. self._UnshowCurrentLine() # un-highlight the old one.
  846. if self.curframe:
  847. fileName = self.canonic(self.curframe.f_code.co_filename)
  848. lineNo = self.curframe.f_lineno
  849. self.shownLineCurrent = fileName, lineNo
  850. self.ShowLineState(fileName, lineNo, LINESTATE_CURRENT)
  851. def _UnshowCurrentLine(self):
  852. "Unshow the current line, and forget it"
  853. if self.shownLineCurrent is not None:
  854. fname, lineno = self.shownLineCurrent
  855. self.ResetLineState(fname, lineno, LINESTATE_CURRENT)
  856. self.shownLineCurrent = None
  857. def ShowLineNo( self, filename, lineno ):
  858. wasOpen = editor.editorTemplate.FindOpenDocument(filename) is not None
  859. if os.path.isfile(filename) and scriptutils.JumpToDocument(filename, lineno):
  860. if not wasOpen:
  861. doc = editor.editorTemplate.FindOpenDocument(filename)
  862. if doc is not None:
  863. self.UpdateDocumentLineStates(doc)
  864. return 1
  865. return 0
  866. return 1
  867. else:
  868. # Can't find the source file - linecache may have it?
  869. import linecache
  870. line = linecache.getline(filename, lineno)
  871. print("%s(%d): %s" % (os.path.basename(filename), lineno, line[:-1].expandtabs(4)))
  872. return 0