Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

debugger.py 37KB

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