6
The current directory should contain a directory named 'cython_debug', or a
7
path to the cython project directory should be given (the parent directory of
10
Additional gdb args can be provided only if a path to the project directory is
23
logger = logging.getLogger(__name__)
26
def make_command_file(path_to_debug_info, prefix_code='',
27
no_import=False, skip_interpreter=False):
29
pattern = os.path.join(path_to_debug_info,
31
'cython_debug_info_*')
32
debug_files = glob.glob(pattern)
35
sys.exit('%s.\nNo debug files were found in %s. Aborting.' % (
36
usage, os.path.abspath(path_to_debug_info)))
38
fd, tempfilename = tempfile.mkstemp()
39
f = os.fdopen(fd, 'w')
42
f.write(textwrap.dedent('''\
43
# This is a gdb command file
44
# See https://sourceware.org/gdb/onlinedocs/gdb/Command-Files.html
46
set breakpoint pending on
51
# Activate virtualenv, if we were launched from one
53
virtualenv = os.getenv('VIRTUAL_ENV')
55
path_to_activate_this_py = os.path.join(virtualenv, 'bin', 'activate_this.py')
56
print("gdb command file: Activating virtualenv: %s; path_to_activate_this_py: %s" % (
57
virtualenv, path_to_activate_this_py))
58
with open(path_to_activate_this_py) as f:
59
exec(f.read(), dict(__file__=path_to_activate_this_py))
60
from Cython.Debugger import libcython, libpython
61
except Exception as ex:
62
from traceback import print_exc
63
print("There was an error in Python code originating from the file ''' + str(__file__) + '''")
64
print("It used the Python interpreter " + str(sys.executable))
71
# don't do this, this overrides file command in .gdbinit
72
# f.write("file %s\n" % sys.executable)
75
if not skip_interpreter:
76
# Point Cygdb to the interpreter that was used to generate
77
# the debugging information.
78
path = os.path.join(path_to_debug_info, "cython_debug", "interpreter")
79
interpreter_file = open(path)
81
interpreter = interpreter_file.read()
83
interpreter_file.close()
84
f.write("file %s\n" % interpreter)
86
f.write('\n'.join('cy import %s\n' % fn for fn in debug_files))
88
if not skip_interpreter:
89
f.write(textwrap.dedent('''\
92
# Check if the Python executable provides a symbol table.
93
if not hasattr(gdb.selected_inferior().progspace, "symbol_file"):
95
"''' + interpreter + ''' was not compiled with debug symbols (or it was "
96
"stripped). Some functionality may not work (properly).\\n")
100
f.write("source .cygdbinit\n")
106
usage = "Usage: cygdb [options] [PATH [-- GDB_ARGUMENTS]]"
108
def main(path_to_debug_info=None, gdb_argv=None, no_import=False):
110
Start the Cython debugger. This tells gdb to import the Cython and Python
111
extensions (libcython.py and libpython.py) and it enables gdb's pending
114
path_to_debug_info is the path to the Cython build directory
115
gdb_argv is the list of options to gdb
116
no_import tells cygdb whether it should import debug information
118
parser = optparse.OptionParser(usage=usage)
119
parser.add_option("--gdb-executable",
120
dest="gdb", default='gdb',
121
help="gdb executable to use [default: gdb]")
122
parser.add_option("--verbose", "-v",
123
dest="verbosity", action="count", default=0,
124
help="Verbose mode. Multiple -v options increase the verbosity")
125
parser.add_option("--skip-interpreter",
126
dest="skip_interpreter", default=False, action="store_true",
127
help="Do not automatically point GDB to the same interpreter "
128
"used to generate debugging information")
130
(options, args) = parser.parse_args()
131
if path_to_debug_info is None:
133
path_to_debug_info = args[0]
135
path_to_debug_info = os.curdir
140
if path_to_debug_info == '--':
143
logging_level = logging.WARN
144
if options.verbosity == 1:
145
logging_level = logging.INFO
146
if options.verbosity >= 2:
147
logging_level = logging.DEBUG
148
logging.basicConfig(level=logging_level)
150
skip_interpreter = options.skip_interpreter
152
logger.info("verbosity = %r", options.verbosity)
153
logger.debug("options = %r; args = %r", options, args)
154
logger.debug("Done parsing command-line options. path_to_debug_info = %r, gdb_argv = %r",
155
path_to_debug_info, gdb_argv)
157
tempfilename = make_command_file(path_to_debug_info,
159
skip_interpreter=skip_interpreter)
160
logger.info("Launching %s with command file: %s and gdb_argv: %s",
161
options.gdb, tempfilename, gdb_argv)
162
with open(tempfilename) as tempfile:
163
logger.debug('Command file (%s) contains: """\n%s"""', tempfilename, tempfile.read())
164
logger.info("Spawning %s...", options.gdb)
165
p = subprocess.Popen([options.gdb, '-command', tempfilename] + gdb_argv)
166
logger.info("Spawned %s (pid %d)", options.gdb, p.pid)
169
logger.debug("Waiting for gdb (pid %d) to exit...", p.pid)
171
logger.debug("Wait for gdb (pid %d) to exit is done. Returned: %r", p.pid, ret)
172
except KeyboardInterrupt:
176
logger.debug("Closing temp command file with fd: %s", tempfile.fileno())
177
logger.debug("Removing temp command file: %s", tempfilename)
178
os.remove(tempfilename)
179
logger.debug("Removed temp command file: %s", tempfilename)