blob: 36665acbd21310a29fb9f0cb10a5033d9731c32f [file] [log] [blame]
Guido van Rossum35f75421998-10-13 23:51:13 +00001import os
2import bdb
3import traceback
4from Tkinter import *
Guido van Rossum504b0bf1999-01-02 21:28:54 +00005from WindowList import ListedToplevel
Guido van Rossum35f75421998-10-13 23:51:13 +00006
Guido van Rossum5188a251998-10-16 18:59:39 +00007import StackViewer
8
Guido van Rossum35f75421998-10-13 23:51:13 +00009
10class Debugger(bdb.Bdb):
Guido van Rossum504b0bf1999-01-02 21:28:54 +000011
Guido van Rossum35e55da1998-10-14 03:43:05 +000012 interacting = 0
Guido van Rossum504b0bf1999-01-02 21:28:54 +000013
Guido van Rossum3fb35151998-10-16 21:09:05 +000014 vstack = vsource = vlocals = vglobals = None
Guido van Rossum504b0bf1999-01-02 21:28:54 +000015
Guido van Rossum35f75421998-10-13 23:51:13 +000016 def __init__(self, pyshell):
17 bdb.Bdb.__init__(self)
18 self.pyshell = pyshell
19 self.make_gui()
Guido van Rossum504b0bf1999-01-02 21:28:54 +000020
Guido van Rossum9016fcf1999-01-11 14:46:55 +000021 def close(self, event=None):
Guido van Rossum35e55da1998-10-14 03:43:05 +000022 if self.interacting:
23 self.top.bell()
24 return
25 self.pyshell.close_debugger()
Guido van Rossum35f75421998-10-13 23:51:13 +000026 self.top.destroy()
Guido van Rossum504b0bf1999-01-02 21:28:54 +000027
Guido van Rossum35e55da1998-10-14 03:43:05 +000028 def run(self, *args):
29 try:
30 self.interacting = 1
31 return apply(bdb.Bdb.run, (self,) + args)
32 finally:
33 self.interacting = 0
Guido van Rossum35f75421998-10-13 23:51:13 +000034
35 def user_line(self, frame):
36 self.interaction(frame)
37
38 def user_return(self, frame, rv):
39 # XXX show rv?
Guido van Rossumb52cfa11998-10-19 02:24:40 +000040 ##self.interaction(frame)
41 pass
Guido van Rossum35f75421998-10-13 23:51:13 +000042
43 def user_exception(self, frame, info):
44 self.interaction(frame, info)
Guido van Rossum504b0bf1999-01-02 21:28:54 +000045
Guido van Rossum35f75421998-10-13 23:51:13 +000046 def make_gui(self):
47 pyshell = self.pyshell
48 self.flist = pyshell.flist
49 self.root = root = pyshell.root
Guido van Rossum504b0bf1999-01-02 21:28:54 +000050 self.top = top =ListedToplevel(root)
51 self.top.wm_title("Debug Control")
52 self.top.wm_iconname("Debug")
Guido van Rossum35e55da1998-10-14 03:43:05 +000053 top.wm_protocol("WM_DELETE_WINDOW", self.close)
Guido van Rossum9016fcf1999-01-11 14:46:55 +000054 self.top.bind("<Escape>", self.close)
Guido van Rossum80d132d1998-10-16 16:12:11 +000055 #
Guido van Rossum35f75421998-10-13 23:51:13 +000056 self.bframe = bframe = Frame(top)
Guido van Rossum35e55da1998-10-14 03:43:05 +000057 self.bframe.pack(anchor="w")
Guido van Rossum35f75421998-10-13 23:51:13 +000058 self.buttons = bl = []
Guido van Rossum80d132d1998-10-16 16:12:11 +000059 #
Guido van Rossum35f75421998-10-13 23:51:13 +000060 self.bcont = b = Button(bframe, text="Go", command=self.cont)
61 bl.append(b)
Guido van Rossum3fb35151998-10-16 21:09:05 +000062 self.bstep = b = Button(bframe, text="Step", command=self.step)
Guido van Rossum35f75421998-10-13 23:51:13 +000063 bl.append(b)
Guido van Rossum80d132d1998-10-16 16:12:11 +000064 self.bnext = b = Button(bframe, text="Over", command=self.next)
Guido van Rossum35f75421998-10-13 23:51:13 +000065 bl.append(b)
Guido van Rossum80d132d1998-10-16 16:12:11 +000066 self.bret = b = Button(bframe, text="Out", command=self.ret)
Guido van Rossum35f75421998-10-13 23:51:13 +000067 bl.append(b)
Guido van Rossum85ef9dc1999-01-28 22:00:30 +000068 self.bret = b = Button(bframe, text="Quit", command=self.quit)
69 bl.append(b)
Guido van Rossum80d132d1998-10-16 16:12:11 +000070 #
Guido van Rossum35f75421998-10-13 23:51:13 +000071 for b in bl:
72 b.configure(state="disabled")
73 b.pack(side="left")
Guido van Rossum80d132d1998-10-16 16:12:11 +000074 #
75 self.cframe = cframe = Frame(bframe)
76 self.cframe.pack(side="left")
77 #
Guido van Rossum3fb35151998-10-16 21:09:05 +000078 if not self.vstack:
79 self.__class__.vstack = BooleanVar(top)
80 self.vstack.set(1)
Guido van Rossum80d132d1998-10-16 16:12:11 +000081 self.bstack = Checkbutton(cframe,
82 text="Stack", command=self.show_stack, variable=self.vstack)
83 self.bstack.grid(row=0, column=0)
Guido van Rossum3fb35151998-10-16 21:09:05 +000084 if not self.vsource:
85 self.__class__.vsource = BooleanVar(top)
Guido van Rossumb52cfa11998-10-19 02:24:40 +000086 ##self.vsource.set(1)
Guido van Rossum80d132d1998-10-16 16:12:11 +000087 self.bsource = Checkbutton(cframe,
88 text="Source", command=self.show_source, variable=self.vsource)
Guido van Rossum6b384831998-10-16 17:16:59 +000089 self.bsource.grid(row=0, column=1)
Guido van Rossum3fb35151998-10-16 21:09:05 +000090 if not self.vlocals:
91 self.__class__.vlocals = BooleanVar(top)
92 self.vlocals.set(1)
Guido van Rossum80d132d1998-10-16 16:12:11 +000093 self.blocals = Checkbutton(cframe,
94 text="Locals", command=self.show_locals, variable=self.vlocals)
95 self.blocals.grid(row=1, column=0)
Guido van Rossum3fb35151998-10-16 21:09:05 +000096 if not self.vglobals:
97 self.__class__.vglobals = BooleanVar(top)
Guido van Rossumb52cfa11998-10-19 02:24:40 +000098 ##self.vglobals.set(1)
Guido van Rossum80d132d1998-10-16 16:12:11 +000099 self.bglobals = Checkbutton(cframe,
100 text="Globals", command=self.show_globals, variable=self.vglobals)
101 self.bglobals.grid(row=1, column=1)
102 #
Guido van Rossum35e55da1998-10-14 03:43:05 +0000103 self.status = Label(top, anchor="w")
104 self.status.pack(anchor="w")
105 self.error = Label(top, anchor="w")
Guido van Rossumb52cfa11998-10-19 02:24:40 +0000106 self.error.pack(anchor="w", fill="x")
Guido van Rossum3fb35151998-10-16 21:09:05 +0000107 self.errorbg = self.error.cget("background")
Guido van Rossum5188a251998-10-16 18:59:39 +0000108 #
109 self.fstack = Frame(top, height=1)
110 self.fstack.pack(expand=1, fill="both")
111 self.flocals = Frame(top)
112 self.flocals.pack(expand=1, fill="both")
113 self.fglobals = Frame(top, height=1)
114 self.fglobals.pack(expand=1, fill="both")
Guido van Rossum3fb35151998-10-16 21:09:05 +0000115 #
116 if self.vstack.get():
117 self.show_stack()
118 if self.vlocals.get():
119 self.show_locals()
120 if self.vglobals.get():
121 self.show_globals()
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000122
Guido van Rossum3fb35151998-10-16 21:09:05 +0000123 frame = None
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000124
Guido van Rossum35f75421998-10-13 23:51:13 +0000125 def interaction(self, frame, info=None):
126 self.frame = frame
127 code = frame.f_code
128 file = code.co_filename
Guido van Rossum35e55da1998-10-14 03:43:05 +0000129 base = os.path.basename(file)
Guido van Rossum35f75421998-10-13 23:51:13 +0000130 lineno = frame.f_lineno
Guido van Rossum3fb35151998-10-16 21:09:05 +0000131 #
Guido van Rossumb7ffca31998-10-15 12:18:49 +0000132 message = "%s:%s" % (base, lineno)
133 if code.co_name != "?":
134 message = "%s: %s()" % (message, code.co_name)
Guido van Rossum35e55da1998-10-14 03:43:05 +0000135 self.status.configure(text=message)
Guido van Rossum3fb35151998-10-16 21:09:05 +0000136 #
Guido van Rossum35f75421998-10-13 23:51:13 +0000137 if info:
138 type, value, tb = info
Guido van Rossum35e55da1998-10-14 03:43:05 +0000139 try:
140 m1 = type.__name__
141 except AttributeError:
142 m1 = "%s" % str(type)
143 if value is not None:
144 try:
145 m1 = "%s: %s" % (m1, str(value))
146 except:
147 pass
Guido van Rossum3fb35151998-10-16 21:09:05 +0000148 bg = "yellow"
Guido van Rossum35e55da1998-10-14 03:43:05 +0000149 else:
150 m1 = ""
Guido van Rossum80d132d1998-10-16 16:12:11 +0000151 tb = None
Guido van Rossum3fb35151998-10-16 21:09:05 +0000152 bg = self.errorbg
153 self.error.configure(text=m1, background=bg)
154 #
Guido van Rossum80d132d1998-10-16 16:12:11 +0000155 sv = self.stackviewer
156 if sv:
157 stack, i = self.get_stack(self.frame, tb)
158 sv.load_stack(stack, i)
Guido van Rossum3fb35151998-10-16 21:09:05 +0000159 #
Guido van Rossumb52cfa11998-10-19 02:24:40 +0000160 self.show_variables(1)
Guido van Rossum3fb35151998-10-16 21:09:05 +0000161 #
Guido van Rossum80d132d1998-10-16 16:12:11 +0000162 if self.vsource.get():
163 self.sync_source_line()
Guido van Rossum3fb35151998-10-16 21:09:05 +0000164 #
Guido van Rossum35f75421998-10-13 23:51:13 +0000165 for b in self.buttons:
166 b.configure(state="normal")
Guido van Rossum3fb35151998-10-16 21:09:05 +0000167 #
Guido van Rossum35f75421998-10-13 23:51:13 +0000168 self.top.tkraise()
169 self.root.mainloop()
Guido van Rossum3fb35151998-10-16 21:09:05 +0000170 #
Guido van Rossum35f75421998-10-13 23:51:13 +0000171 for b in self.buttons:
172 b.configure(state="disabled")
173 self.status.configure(text="")
Guido van Rossum3fb35151998-10-16 21:09:05 +0000174 self.error.configure(text="", background=self.errorbg)
Guido van Rossum35f75421998-10-13 23:51:13 +0000175 self.frame = None
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000176
Guido van Rossum80d132d1998-10-16 16:12:11 +0000177 def sync_source_line(self):
178 frame = self.frame
179 if not frame:
180 return
181 code = frame.f_code
182 file = code.co_filename
183 lineno = frame.f_lineno
184 if file[:1] + file[-1:] != "<>" and os.path.exists(file):
185 edit = self.flist.open(file)
186 if edit:
187 edit.gotoline(lineno)
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000188
Guido van Rossum35f75421998-10-13 23:51:13 +0000189 def cont(self):
190 self.set_continue()
191 self.root.quit()
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000192
Guido van Rossum35f75421998-10-13 23:51:13 +0000193 def step(self):
194 self.set_step()
195 self.root.quit()
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000196
Guido van Rossum35f75421998-10-13 23:51:13 +0000197 def next(self):
198 self.set_next(self.frame)
199 self.root.quit()
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000200
Guido van Rossum35f75421998-10-13 23:51:13 +0000201 def ret(self):
202 self.set_return(self.frame)
203 self.root.quit()
Guido van Rossum80d132d1998-10-16 16:12:11 +0000204
Guido van Rossum85ef9dc1999-01-28 22:00:30 +0000205 def quit(self):
206 self.set_quit()
207 self.root.quit()
208
Guido van Rossum80d132d1998-10-16 16:12:11 +0000209 stackviewer = None
210
211 def show_stack(self):
212 if not self.stackviewer and self.vstack.get():
Guido van Rossum80d132d1998-10-16 16:12:11 +0000213 self.stackviewer = sv = StackViewer.StackViewer(
Guido van Rossum5188a251998-10-16 18:59:39 +0000214 self.fstack, self.flist, self)
Guido van Rossum80d132d1998-10-16 16:12:11 +0000215 if self.frame:
Guido van Rossum6b384831998-10-16 17:16:59 +0000216 stack, i = self.get_stack(self.frame, None)
217 sv.load_stack(stack, i)
Guido van Rossum80d132d1998-10-16 16:12:11 +0000218 else:
219 sv = self.stackviewer
220 if sv and not self.vstack.get():
221 self.stackviewer = None
222 sv.close()
Guido van Rossum5188a251998-10-16 18:59:39 +0000223 self.fstack['height'] = 1
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000224
Guido van Rossum80d132d1998-10-16 16:12:11 +0000225 def show_source(self):
226 if self.vsource.get():
227 self.sync_source_line()
228
229 def show_frame(self, (frame, lineno)):
Guido van Rossum3fb35151998-10-16 21:09:05 +0000230 self.frame = frame
231 self.show_variables()
Guido van Rossum5188a251998-10-16 18:59:39 +0000232
233 localsviewer = None
Guido van Rossum3fb35151998-10-16 21:09:05 +0000234 globalsviewer = None
Guido van Rossum80d132d1998-10-16 16:12:11 +0000235
236 def show_locals(self):
Guido van Rossum5188a251998-10-16 18:59:39 +0000237 lv = self.localsviewer
Guido van Rossum3fb35151998-10-16 21:09:05 +0000238 if self.vlocals.get():
239 if not lv:
240 self.localsviewer = StackViewer.NamespaceViewer(
241 self.flocals, "Locals")
242 else:
243 if lv:
244 self.localsviewer = None
245 lv.close()
246 self.flocals['height'] = 1
247 self.show_variables()
Guido van Rossum5188a251998-10-16 18:59:39 +0000248
Guido van Rossum80d132d1998-10-16 16:12:11 +0000249 def show_globals(self):
Guido van Rossum3fb35151998-10-16 21:09:05 +0000250 gv = self.globalsviewer
251 if self.vglobals.get():
252 if not gv:
253 self.globalsviewer = StackViewer.NamespaceViewer(
254 self.fglobals, "Globals")
255 else:
256 if gv:
257 self.globalsviewer = None
258 gv.close()
259 self.fglobals['height'] = 1
260 self.show_variables()
261
Guido van Rossumb52cfa11998-10-19 02:24:40 +0000262 def show_variables(self, force=0):
Guido van Rossum3fb35151998-10-16 21:09:05 +0000263 lv = self.localsviewer
264 gv = self.globalsviewer
265 frame = self.frame
266 if not frame:
267 ldict = gdict = None
268 else:
269 ldict = frame.f_locals
270 gdict = frame.f_globals
271 if lv and gv and ldict is gdict:
272 ldict = None
273 if lv:
Guido van Rossumb52cfa11998-10-19 02:24:40 +0000274 lv.load_dict(ldict, force)
Guido van Rossum3fb35151998-10-16 21:09:05 +0000275 if gv:
Guido van Rossumb52cfa11998-10-19 02:24:40 +0000276 gv.load_dict(gdict, force)
277
278 def set_breakpoint_here(self, edit):
279 text = edit.text
280 filename = edit.io.filename
281 if not filename:
282 text.bell()
283 return
284 lineno = int(float(text.index("insert")))
285 msg = self.set_break(filename, lineno)
286 if msg:
287 text.bell()
288 return
289 text.tag_add("BREAK", "insert linestart", "insert lineend +1char")
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000290
Guido van Rossumb52cfa11998-10-19 02:24:40 +0000291 # A literal copy of Bdb.set_break() without the print statement at the end
292 def set_break(self, filename, lineno, temporary=0, cond = None):
Guido van Rossum504b0bf1999-01-02 21:28:54 +0000293 import linecache # Import as late as possible
294 line = linecache.getline(filename, lineno)
295 if not line:
296 return 'That line does not exist!'
297 if not self.breaks.has_key(filename):
298 self.breaks[filename] = []
299 list = self.breaks[filename]
300 if not lineno in list:
301 list.append(lineno)
302 bp = bdb.Breakpoint(filename, lineno, temporary, cond)