2 GDB extension that adds Cython support.
5 from __future__ import with_statement
19 from lxml import etree
25 from xml.etree import cElementTree as etree
29 from xml.etree import ElementTree as etree
32 # normal cElementTree install
33 import cElementTree as etree
35 # normal ElementTree install
36 import elementtree.ElementTree as etree
39 import pygments.lexers
40 import pygments.formatters
43 sys.stderr.write("Install pygments for colorized source code.\n")
45 if hasattr(gdb, 'string_to_argv'):
46 from gdb import string_to_argv
48 from shlex import split as string_to_argv
50 from Cython.Debugger import libpython
54 PythonObject = 'PythonObject'
56 _data_types = dict(CObject=CObject, PythonObject=PythonObject)
57 _filesystemencoding = sys.getfilesystemencoding() or 'UTF-8'
61 def dont_suppress_errors(function):
63 @functools.wraps(function)
64 def wrapper(*args, **kwargs):
66 return function(*args, **kwargs)
73 def default_selected_gdb_frame(err=True):
74 def decorator(function):
75 @functools.wraps(function)
76 def wrapper(self, frame=None, *args, **kwargs):
78 frame = frame or gdb.selected_frame()
80 raise gdb.GdbError("No frame is currently selected.")
82 if err and frame.name() is None:
83 raise NoFunctionNameInFrameError()
85 return function(self, frame, *args, **kwargs)
89 def require_cython_frame(function):
90 @functools.wraps(function)
91 @require_running_program
92 def wrapper(self, *args, **kwargs):
93 frame = kwargs.get('frame') or gdb.selected_frame()
94 if not self.is_cython_function(frame):
95 raise gdb.GdbError('Selected frame does not correspond with a '
96 'Cython function we know about.')
97 return function(self, *args, **kwargs)
100 def dispatch_on_frame(c_command, python_command=None):
101 def decorator(function):
102 @functools.wraps(function)
103 def wrapper(self, *args, **kwargs):
104 is_cy = self.is_cython_function()
105 is_py = self.is_python_function()
107 if is_cy or (is_py and not python_command):
108 function(self, *args, **kwargs)
110 gdb.execute(python_command)
111 elif self.is_relevant_function():
112 gdb.execute(c_command)
114 raise gdb.GdbError("Not a function cygdb knows about. "
115 "Use the normal GDB commands instead.")
120 def require_running_program(function):
121 @functools.wraps(function)
122 def wrapper(*args, **kwargs):
126 raise gdb.GdbError("No frame is currently selected.")
128 return function(*args, **kwargs)
132 def gdb_function_value_to_unicode(function):
133 @functools.wraps(function)
134 def wrapper(self, string, *args, **kwargs):
135 if isinstance(string, gdb.Value):
136 string = string.string()
138 return function(self, string, *args, **kwargs)
142 # Classes that represent the debug information
143 # Don't rename the parameters of these classes, they come directly from the XML
145 class CythonModule(object):
146 def __init__(self, module_name, filename, c_filename):
147 self.name = module_name
148 self.filename = filename
149 self.c_filename = c_filename
151 # {cython_lineno: min(c_linenos)}
152 self.lineno_cy2c = {}
153 # {c_lineno: cython_lineno}
154 self.lineno_c2cy = {}
157 def qualified_name(self, varname):
158 return '.'.join(self.name, varname)
160 class CythonVariable(object):
162 def __init__(self, name, cname, qualified_name, type):
165 self.qualified_name = qualified_name
168 class CythonFunction(CythonVariable):
177 super(CythonFunction, self).__init__(name,
182 self.pf_cname = pf_cname
183 self.lineno = int(lineno)
186 self.step_into_functions = set()
189 # General purpose classes
191 class CythonBase(object):
193 @default_selected_gdb_frame(err=False)
194 def is_cython_function(self, frame):
195 return frame.name() in self.cy.functions_by_cname
197 @default_selected_gdb_frame(err=False)
198 def is_python_function(self, frame):
200 Tells if a frame is associated with a Python function.
201 If we can't read the Python frame information, don't regard it as such.
203 if frame.name() == 'PyEval_EvalFrameEx':
204 pyframe = libpython.Frame(frame).get_pyop()
205 return pyframe and not pyframe.is_optimized_out()
208 @default_selected_gdb_frame()
209 def get_c_function_name(self, frame):
212 @default_selected_gdb_frame()
213 def get_c_lineno(self, frame):
214 return frame.find_sal().line
216 @default_selected_gdb_frame()
217 def get_cython_function(self, frame):
218 result = self.cy.functions_by_cname.get(frame.name())
220 raise NoCythonFunctionInFrameError()
224 @default_selected_gdb_frame()
225 def get_cython_lineno(self, frame):
227 Get the current Cython line number. Returns 0 if there is no
228 correspondence between the C and Cython code.
230 cyfunc = self.get_cython_function(frame)
231 return cyfunc.module.lineno_c2cy.get(self.get_c_lineno(frame), 0)
233 @default_selected_gdb_frame()
234 def get_source_desc(self, frame):
235 filename = lineno = lexer = None
236 if self.is_cython_function(frame):
237 filename = self.get_cython_function(frame).module.filename
238 lineno = self.get_cython_lineno(frame)
240 lexer = pygments.lexers.CythonLexer(stripall=False)
241 elif self.is_python_function(frame):
242 pyframeobject = libpython.Frame(frame).get_pyop()
244 if not pyframeobject:
245 raise gdb.GdbError('Unable to read information on python frame')
247 filename = pyframeobject.filename()
248 lineno = pyframeobject.current_line_num()
251 lexer = pygments.lexers.PythonLexer(stripall=False)
253 symbol_and_line_obj = frame.find_sal()
254 if not symbol_and_line_obj or not symbol_and_line_obj.symtab:
258 filename = symbol_and_line_obj.symtab.filename
259 lineno = symbol_and_line_obj.line
261 lexer = pygments.lexers.CLexer(stripall=False)
263 return SourceFileDescriptor(filename, lexer), lineno
265 @default_selected_gdb_frame()
266 def get_source_line(self, frame):
267 source_desc, lineno = self.get_source_desc()
268 return source_desc.get_source(lineno)
270 @default_selected_gdb_frame()
271 def is_relevant_function(self, frame):
273 returns whether we care about a frame on the user-level when debugging
277 older_frame = frame.older()
278 if self.is_cython_function(frame) or self.is_python_function(frame):
280 elif older_frame and self.is_cython_function(older_frame):
281 # direct C function call from a Cython function
282 cython_func = self.get_cython_function(older_frame)
283 return name in cython_func.step_into_functions
287 @default_selected_gdb_frame(err=False)
288 def print_stackframe(self, frame, index, is_c=False):
290 Print a C, Cython or Python stack frame and the line of source code
293 # do this to prevent the require_cython_frame decorator from
294 # raising GdbError when calling self.cy.cy_cvalue.invoke()
295 selected_frame = gdb.selected_frame()
299 source_desc, lineno = self.get_source_desc(frame)
300 except NoFunctionNameInFrameError:
301 print '#%-2d Unknown Frame (compile with -g)' % index
304 if not is_c and self.is_python_function(frame):
305 pyframe = libpython.Frame(frame).get_pyop()
306 if pyframe is None or pyframe.is_optimized_out():
307 # print this python function as a C function
308 return self.print_stackframe(frame, index, is_c=True)
310 func_name = pyframe.co_name
311 func_cname = 'PyEval_EvalFrameEx'
313 elif self.is_cython_function(frame):
314 cyfunc = self.get_cython_function(frame)
315 f = lambda arg: self.cy.cy_cvalue.invoke(arg, frame=frame)
317 func_name = cyfunc.name
318 func_cname = cyfunc.cname
319 func_args = [] # [(arg, f(arg)) for arg in cyfunc.arguments]
321 source_desc, lineno = self.get_source_desc(frame)
322 func_name = frame.name()
323 func_cname = func_name
327 gdb_value = gdb.parse_and_eval(func_cname)
331 # Seriously? Why is the address not an int?
332 func_address = int(str(gdb_value.address).split()[0], 0)
334 a = ', '.join('%s=%s' % (name, val) for name, val in func_args)
335 print '#%-2d 0x%016x in %s(%s)' % (index, func_address, func_name, a),
337 if source_desc.filename is not None:
338 print 'at %s:%s' % (source_desc.filename, lineno),
343 print ' ' + source_desc.get_source(lineno)
347 selected_frame.select()
349 def get_remote_cython_globals_dict(self):
350 m = gdb.parse_and_eval('__pyx_m')
353 PyModuleObject = gdb.lookup_type('PyModuleObject')
355 raise gdb.GdbError(textwrap.dedent("""\
356 Unable to lookup type PyModuleObject, did you compile python
357 with debugging support (-g)?"""))
359 m = m.cast(PyModuleObject.pointer())
363 def get_cython_globals_dict(self):
365 Get the Cython globals dict where the remote names are turned into
368 remote_dict = self.get_remote_cython_globals_dict()
369 pyobject_dict = libpython.PyObjectPtr.from_pyobject_ptr(remote_dict)
373 for k, v in pyobject_dict.iteritems():
374 result[k.proxyval(seen)] = v
378 def print_gdb_value(self, name, value, max_name_length=None, prefix=''):
379 if libpython.pretty_printer_lookup(value):
382 typename = '(%s) ' % (value.type,)
384 if max_name_length is None:
385 print '%s%s = %s%s' % (prefix, name, typename, value)
387 print '%s%-*s = %s%s' % (prefix, max_name_length, name, typename,
391 class SourceFileDescriptor(object):
392 def __init__(self, filename, lexer, formatter=None):
393 self.filename = filename
395 self.formatter = formatter
398 return self.filename is not None
401 if pygments and self.lexer and parameters.colorize_code:
402 bg = parameters.terminal_background.value
403 if self.formatter is None:
404 formatter = pygments.formatters.TerminalFormatter(bg=bg)
406 formatter = self.formatter
408 return pygments.highlight(code, self.lexer, formatter)
412 def _get_source(self, start, stop, lex_source, mark_line, lex_entire):
413 with open(self.filename) as f:
414 # to provide "correct" colouring, the entire code needs to be
415 # lexed. However, this makes a lot of things terribly slow, so
416 # we decide not to. Besides, it's unlikely to matter.
418 if lex_source and lex_entire:
419 f = self.lex(f.read()).splitlines()
421 slice = itertools.islice(f, start - 1, stop - 1)
423 for idx, line in enumerate(slice):
424 if start + idx == mark_line:
429 if lex_source and not lex_entire:
430 line = self.lex(line)
432 yield '%s %4d %s' % (prefix, start + idx, line.rstrip())
434 def get_source(self, start, stop=None, lex_source=True, mark_line=0,
436 exc = gdb.GdbError('Unable to retrieve source code')
438 if not self.filename:
441 start = max(start, 1)
447 self._get_source(start, stop, lex_source, mark_line, lex_entire))
454 class CyGDBError(gdb.GdbError):
456 Base class for Cython-command related erorrs
459 def __init__(self, *args):
460 args = args or (self.msg,)
461 super(CyGDBError, self).__init__(*args)
463 class NoCythonFunctionInFrameError(CyGDBError):
465 raised when the user requests the current cython function, which is
468 msg = "Current function is a function cygdb doesn't know about"
470 class NoFunctionNameInFrameError(NoCythonFunctionInFrameError):
472 raised when the name of the C function could not be determined
473 in the current C stack frame
475 msg = ('C function name could not be determined in the current C stack '
481 class CythonParameter(gdb.Parameter):
483 Base class for cython parameters
486 def __init__(self, name, command_class, parameter_class, default=None):
487 self.show_doc = self.set_doc = self.__class__.__doc__
488 super(CythonParameter, self).__init__(name, command_class,
490 if default is not None:
493 def __nonzero__(self):
494 return bool(self.value)
496 __bool__ = __nonzero__ # python 3
498 class CompleteUnqualifiedFunctionNames(CythonParameter):
500 Have 'cy break' complete unqualified function or method names.
503 class ColorizeSourceCode(CythonParameter):
505 Tell cygdb whether to colorize source code.
508 class TerminalBackground(CythonParameter):
510 Tell cygdb about the user's terminal background (light or dark).
513 class CythonParameters(object):
515 Simple container class that might get more functionality in the distant
516 future (mostly to remind us that we're dealing with parameters).
520 self.complete_unqualified = CompleteUnqualifiedFunctionNames(
521 'cy_complete_unqualified',
522 gdb.COMMAND_BREAKPOINTS,
525 self.colorize_code = ColorizeSourceCode(
530 self.terminal_background = TerminalBackground(
531 'cy_terminal_background_color',
536 parameters = CythonParameters()
541 class CythonCommand(gdb.Command, CythonBase):
543 Base class for Cython commands
546 command_class = gdb.COMMAND_NONE
549 def _register(cls, clsname, args, kwargs):
550 if not hasattr(cls, 'completer_class'):
551 return cls(clsname, cls.command_class, *args, **kwargs)
553 return cls(clsname, cls.command_class, cls.completer_class,
557 def register(cls, *args, **kwargs):
558 alias = getattr(cls, 'alias', None)
560 cls._register(cls.alias, args, kwargs)
562 return cls._register(cls.name, args, kwargs)
565 class CyCy(CythonCommand):
567 Invoke a Cython command. Available commands are:
585 command_class = gdb.COMMAND_NONE
586 completer_class = gdb.COMPLETE_COMMAND
588 def __init__(self, *args):
589 super(CythonCommand, self).__init__(*args, prefix=True)
592 import_ = CyImport.register(),
593 break_ = CyBreak.register(),
594 step = CyStep.register(),
595 next = CyNext.register(),
596 run = CyRun.register(),
597 cont = CyCont.register(),
598 finish = CyFinish.register(),
599 up = CyUp.register(),
600 down = CyDown.register(),
601 bt = CyBacktrace.register(),
602 list = CyList.register(),
603 print_ = CyPrint.register(),
604 locals = CyLocals.register(),
605 globals = CyGlobals.register(),
606 exec_ = libpython.FixGdbCommand('cy exec', '-cy-exec'),
607 _exec = CyExec.register(),
608 cy_cname = CyCName('cy_cname'),
609 cy_cvalue = CyCValue('cy_cvalue'),
610 cy_lineno = CyLine('cy_lineno'),
613 for command_name, command in commands.iteritems():
615 setattr(self, command_name, command)
619 # Cython module namespace
620 self.cython_namespace = {}
622 # maps (unique) qualified function names (e.g.
623 # cythonmodule.ClassName.method_name) to the CythonFunction object
624 self.functions_by_qualified_name = {}
626 # unique cnames of Cython functions
627 self.functions_by_cname = {}
629 # map function names like method_name to a list of all such
630 # CythonFunction objects
631 self.functions_by_name = collections.defaultdict(list)
634 class CyImport(CythonCommand):
636 Import debug information outputted by the Cython compiler
637 Example: cy import FILE...
641 command_class = gdb.COMMAND_STATUS
642 completer_class = gdb.COMPLETE_FILENAME
644 def invoke(self, args, from_tty):
645 args = args.encode(_filesystemencoding)
646 for arg in string_to_argv(args):
650 raise gdb.GdbError('Unable to open file %r: %s' %
655 for module in t.getroot():
656 cython_module = CythonModule(**module.attrib)
657 self.cy.cython_namespace[cython_module.name] = cython_module
659 for variable in module.find('Globals'):
661 cython_module.globals[d['name']] = CythonVariable(**d)
663 for function in module.find('Functions'):
664 cython_function = CythonFunction(module=cython_module,
667 # update the global function mappings
668 name = cython_function.name
669 qname = cython_function.qualified_name
671 self.cy.functions_by_name[name].append(cython_function)
672 self.cy.functions_by_qualified_name[
673 cython_function.qualified_name] = cython_function
674 self.cy.functions_by_cname[
675 cython_function.cname] = cython_function
677 d = cython_module.functions[qname] = cython_function
679 for local in function.find('Locals'):
681 cython_function.locals[d['name']] = CythonVariable(**d)
683 for step_into_func in function.find('StepIntoFunctions'):
684 d = step_into_func.attrib
685 cython_function.step_into_functions.add(d['name'])
687 cython_function.arguments.extend(
688 funcarg.tag for funcarg in function.find('Arguments'))
690 for marker in module.find('LineNumberMapping'):
691 cython_lineno = int(marker.attrib['cython_lineno'])
692 c_linenos = map(int, marker.attrib['c_linenos'].split())
693 cython_module.lineno_cy2c[cython_lineno] = min(c_linenos)
694 for c_lineno in c_linenos:
695 cython_module.lineno_c2cy[c_lineno] = cython_lineno
697 self.cy.step.init_breakpoints()
700 class CyBreak(CythonCommand):
702 Set a breakpoint for Cython code using Cython qualified name notation, e.g.:
704 cy break cython_modulename.ClassName.method_name...
708 cy break function_or_method_name...
710 or for a line number:
712 cy break cython_module:lineno...
714 Set a Python breakpoint:
715 Break on any function or method named 'func' in module 'modname'
717 cy break -p modname.func...
719 Break on any function or method named 'func'
725 command_class = gdb.COMMAND_BREAKPOINTS
727 def _break_pyx(self, name):
728 modulename, _, lineno = name.partition(':')
731 cython_module = self.cy.cython_namespace[modulename]
733 cython_module = self.get_cython_function().module
735 if lineno in cython_module.lineno_cy2c:
736 c_lineno = cython_module.lineno_cy2c[lineno]
737 breakpoint = '%s:%s' % (cython_module.c_filename, c_lineno)
738 gdb.execute('break ' + breakpoint)
740 raise GdbError("Not a valid line number. "
741 "Does it contain actual code?")
743 def _break_funcname(self, funcname):
744 func = self.cy.functions_by_qualified_name.get(funcname)
748 funcs = self.cy.functions_by_name.get(funcname)
750 gdb.execute('break ' + funcname)
754 # multiple functions, let the user pick one
755 print 'There are multiple such functions:'
756 for idx, func in enumerate(funcs):
757 print '%3d) %s' % (idx, func.qualified_name)
762 "Select a function, press 'a' for all "
763 "functions or press 'q' or '^D' to quit: ")
767 if result.lower() == 'q':
769 elif result.lower() == 'a':
772 elif (result.isdigit() and
773 0 <= int(result) < len(funcs)):
774 break_funcs = [funcs[int(result)]]
777 print 'Not understood...'
779 break_funcs = [funcs[0]]
781 for func in break_funcs:
782 gdb.execute('break %s' % func.cname)
784 gdb.execute('break %s' % func.pf_cname)
786 def invoke(self, function_names, from_tty):
787 argv = string_to_argv(function_names.encode('UTF-8'))
788 if function_names.startswith('-p'):
790 python_breakpoints = True
792 python_breakpoints = False
794 for funcname in argv:
795 if python_breakpoints:
796 gdb.execute('py-break %s' % funcname)
797 elif ':' in funcname:
798 self._break_pyx(funcname)
800 self._break_funcname(funcname)
802 @dont_suppress_errors
803 def complete(self, text, word):
804 names = self.cy.functions_by_qualified_name
805 if parameters.complete_unqualified:
806 names = itertools.chain(names, self.cy.functions_by_name)
808 words = text.strip().split()
809 if words and '.' in words[-1]:
811 compl = [n for n in self.cy.functions_by_qualified_name
812 if n.startswith(lastword)]
814 seen = set(text[:-len(word)].split())
815 return [n for n in names if n.startswith(word) and n not in seen]
817 if len(lastword) > len(word):
818 # readline sees something (e.g. a '.') as a word boundary, so don't
819 # "recomplete" this prefix
820 strip_prefix_length = len(lastword) - len(word)
821 compl = [n[strip_prefix_length:] for n in compl]
826 class CythonCodeStepper(CythonCommand, libpython.GenericCodeStepper):
828 Base class for CyStep and CyNext. It implements the interface dictated by
829 libpython.GenericCodeStepper.
832 def lineno(self, frame):
833 # Take care of the Python and Cython levels. We need to care for both
834 # as we can't simply dispath to 'py-step', since that would work for
835 # stepping through Python code, but it would not step back into Cython-
836 # related code. The C level should be dispatched to the 'step' command.
837 if self.is_cython_function(frame):
838 return self.get_cython_lineno(frame)
840 return libpython.py_step.lineno(frame)
842 def get_source_line(self, frame):
844 line = super(CythonCodeStepper, self).get_source_line(frame)
848 return line.strip() or None
852 return cls(cls.name, stepinto=getattr(cls, 'stepinto', False))
854 def runtime_break_functions(self):
855 if self.is_cython_function():
856 return self.get_cython_function().step_into_functions
858 def static_break_functions(self):
859 result = ['PyEval_EvalFrameEx']
860 result.extend(self.cy.functions_by_cname)
863 def invoke(self, args, from_tty):
864 if not self.is_cython_function() and not self.is_python_function():
870 self.finish_executing(gdb.execute(command, to_string=True))
875 class CyStep(CythonCodeStepper):
876 "Step through Cython, Python or C code."
882 class CyNext(CythonCodeStepper):
883 "Step-over Python code."
889 class CyRun(CythonCodeStepper):
891 Run a Cython program. This is like the 'run' command, except that it
892 displays Cython or Python source lines as well
897 invoke = CythonCodeStepper.run
902 Continue a Cython program. This is like the 'run' command, except that it
903 displays Cython or Python source lines as well.
907 invoke = CythonCodeStepper.cont
910 class CyFinish(CyRun):
912 Execute until the function returns.
916 invoke = CythonCodeStepper.finish
919 class CyUp(CythonCommand):
921 Go up a Cython, Python or relevant C frame.
926 def invoke(self, *args):
928 gdb.execute(self._command, to_string=True)
929 while not self.is_relevant_function(gdb.selected_frame()):
930 gdb.execute(self._command, to_string=True)
931 except RuntimeError, e:
932 raise gdb.GdbError(*e.args)
934 frame = gdb.selected_frame()
937 frame = frame.older()
940 self.print_stackframe(index=index - 1)
945 Go down a Cython, Python or relevant C frame.
952 class CyBacktrace(CythonCommand):
953 'Print the Cython stack'
956 alias = 'cy backtrace'
957 command_class = gdb.COMMAND_STACK
958 completer_class = gdb.COMPLETE_NONE
960 @require_running_program
961 def invoke(self, args, from_tty):
962 # get the first frame
963 selected_frame = frame = gdb.selected_frame()
965 frame = frame.older()
967 print_all = args == '-a'
975 is_relevant = self.is_relevant_function(frame)
979 if print_all or is_relevant:
980 self.print_stackframe(frame, index)
983 frame = frame.newer()
985 selected_frame.select()
988 class CyList(CythonCommand):
990 List Cython source code. To disable to customize colouring see the cy_*
995 command_class = gdb.COMMAND_FILES
996 completer_class = gdb.COMPLETE_NONE
998 @dispatch_on_frame(c_command='list')
999 def invoke(self, _, from_tty):
1000 sd, lineno = self.get_source_desc()
1001 source = sd.get_source(lineno - 5, lineno + 5, mark_line=lineno,
1006 class CyPrint(CythonCommand):
1008 Print a Cython variable using 'cy-print x' or 'cy-print module.function.x'
1012 command_class = gdb.COMMAND_DATA
1014 def invoke(self, name, from_tty, max_name_length=None):
1015 if self.is_python_function():
1016 return gdb.execute('py-print ' + name)
1017 elif self.is_cython_function():
1018 value = self.cy.cy_cvalue.invoke(name.lstrip('*'))
1021 value = value.dereference()
1025 self.print_gdb_value(name, value, max_name_length)
1027 gdb.execute('print ' + name)
1030 if self.is_cython_function():
1031 f = self.get_cython_function()
1032 return list(itertools.chain(f.locals, f.globals))
1037 sortkey = lambda (name, value): name.lower()
1039 class CyLocals(CythonCommand):
1041 List the locals from the current Cython frame.
1045 command_class = gdb.COMMAND_STACK
1046 completer_class = gdb.COMPLETE_NONE
1048 def _print_if_initialized(self, cyvar, max_name_length, prefix=''):
1050 value = gdb.parse_and_eval(cyvar.cname)
1051 except RuntimeError:
1052 # variable not initialized yet
1055 self.print_gdb_value(cyvar.name, value, max_name_length, prefix)
1057 @dispatch_on_frame(c_command='info locals', python_command='py-locals')
1058 def invoke(self, args, from_tty):
1059 local_cython_vars = self.get_cython_function().locals
1060 max_name_length = len(max(local_cython_vars, key=len))
1061 for name, cyvar in sorted(local_cython_vars.iteritems(), key=sortkey):
1062 self._print_if_initialized(cyvar, max_name_length)
1065 class CyGlobals(CyLocals):
1067 List the globals from the current Cython module.
1071 command_class = gdb.COMMAND_STACK
1072 completer_class = gdb.COMPLETE_NONE
1074 @dispatch_on_frame(c_command='info variables', python_command='py-globals')
1075 def invoke(self, args, from_tty):
1076 global_python_dict = self.get_cython_globals_dict()
1077 module_globals = self.get_cython_function().module.globals
1080 max_globals_dict_len = 0
1082 max_globals_len = len(max(module_globals, key=len))
1083 if global_python_dict:
1084 max_globals_dict_len = len(max(global_python_dict))
1086 max_name_length = max(max_globals_len, max_globals_dict_len)
1089 print 'Python globals:'
1090 for k, v in sorted(global_python_dict.iteritems(), key=sortkey):
1091 v = v.get_truncated_repr(libpython.MAX_OUTPUT_LEN)
1093 print ' %-*s = %s' % (max_name_length, k, v)
1096 for name, cyvar in sorted(module_globals.iteritems(), key=sortkey):
1097 if name not in seen:
1098 self._print_if_initialized(cyvar, max_name_length,
1102 class CyExec(CythonCommand):
1104 command_class = gdb.COMMAND_STACK
1105 completer_class = gdb.COMPLETE_NONE
1107 def _fill_locals_dict(self, executor, local_dict_pointer):
1108 "Fill a remotely allocated dict with values from the Cython C stack"
1109 cython_func = self.get_cython_function()
1111 for name, cyvar in cython_func.locals.iteritems():
1112 if cyvar.type == PythonObject:
1113 # skip unitialized Cython variables
1115 val = gdb.parse_and_eval(cyvar.cname)
1116 except RuntimeError:
1119 # Fortunately, Cython initializes all local (automatic)
1121 if libpython.pointervalue(val) == 0:
1124 pystringp = executor.alloc_pystring(name)
1130 ''' % (local_dict_pointer, pystringp, cyvar.cname)
1132 # PyDict_SetItem doesn't steal our reference
1133 executor.decref(pystringp)
1135 if gdb.parse_and_eval(code) < 0:
1136 gdb.parse_and_eval('PyErr_Print()')
1137 raise gdb.GdbError("Unable to execute Python code.")
1139 def _find_first_cython_or_python_frame(self):
1140 frame = gdb.selected_frame()
1142 if (self.is_cython_function(frame) or
1143 self.is_python_function(frame)):
1146 frame = frame.older()
1148 raise gdb.GdbError("There is no Cython or Python frame on the stack.")
1150 def invoke(self, expr, from_tty):
1151 frame = self._find_first_cython_or_python_frame()
1152 if self.is_python_function(frame):
1153 libpython.py_exec.invoke(expr, from_tty)
1156 executor = libpython.PythonCodeExecutor()
1158 # get the dict of Cython globals and construct a dict in the inferior
1159 # with Cython locals
1160 global_dict = gdb.parse_and_eval(
1161 '(PyObject *) PyModule_GetDict(__pyx_m)')
1162 local_dict = gdb.parse_and_eval('(PyObject *) PyDict_New()')
1165 self._fill_locals_dict(executor, libpython.pointervalue(local_dict))
1166 executor.evalcode(expr, global_dict, local_dict)
1168 executor.decref(libpython.pointervalue(local_dict))
1173 class CyCName(gdb.Function, CythonBase):
1175 Get the C name of a Cython variable in the current context.
1178 print $cy_cname("function")
1179 print $cy_cname("Class.method")
1180 print $cy_cname("module.function")
1183 @require_cython_frame
1184 @gdb_function_value_to_unicode
1185 def invoke(self, cyname, frame=None):
1186 frame = frame or gdb.selected_frame()
1189 if self.is_cython_function(frame):
1190 cython_function = self.get_cython_function(frame)
1191 if cyname in cython_function.locals:
1192 cname = cython_function.locals[cyname].cname
1193 elif cyname in cython_function.module.globals:
1194 cname = cython_function.module.globals[cyname].cname
1196 qname = '%s.%s' % (cython_function.module.name, cyname)
1197 if qname in cython_function.module.functions:
1198 cname = cython_function.module.functions[qname].cname
1201 cname = self.cy.functions_by_qualified_name.get(cyname)
1204 raise gdb.GdbError('No such Cython variable: %s' % cyname)
1209 class CyCValue(CyCName):
1211 Get the value of a Cython variable.
1214 @require_cython_frame
1215 @gdb_function_value_to_unicode
1216 def invoke(self, cyname, frame=None):
1218 cname = super(CyCValue, self).invoke(cyname, frame=frame)
1219 return gdb.parse_and_eval(cname)
1220 except (gdb.GdbError, RuntimeError):
1221 # variable exists but may not have been initialized yet, or may be
1222 # in the globals dict of the Cython module
1223 d = self.get_cython_globals_dict()
1225 return d[cyname]._gdbval
1227 raise gdb.GdbError("Variable %s not initialized yet." % cyname)
1230 class CyLine(gdb.Function, CythonBase):
1232 Get the current Cython line.
1235 @require_cython_frame
1237 return self.get_cython_lineno()
1240 cy = CyCy.register()