blob: 711fb69ebdffcac0c0451d82cdb2ba0f9dac5aa6 [file] [log] [blame]
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00001# Verify that gdb can pretty-print the various PyObject* types
2#
3# The code for testing gdb was adapted from similar work in Unladen Swallow's
4# Lib/test/test_jit_gdb.py
5
Victor Stinner61108332017-02-01 16:29:54 +01006import locale
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00007import os
Lysandros Nikolaou59668aa2018-11-04 22:21:28 +01008import platform
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00009import re
10import subprocess
11import sys
Vinay Sajipf1b34ee2012-05-06 12:03:05 +010012import sysconfig
Victor Stinner61108332017-02-01 16:29:54 +010013import textwrap
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000014import unittest
15
Antoine Pitroud0f3e072013-09-21 23:56:17 +020016from test import support
Benjamin Peterson65c66ab2010-10-29 21:31:35 +000017from test.support import run_unittest, findfile, python_is_optimized
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000018
Victor Stinner5b6b4a82015-09-02 23:19:55 +020019def get_gdb_version():
20 try:
21 proc = subprocess.Popen(["gdb", "-nx", "--version"],
22 stdout=subprocess.PIPE,
Benjamin Petersoncbef66d2016-09-06 10:06:31 -070023 stderr=subprocess.PIPE,
Victor Stinner5b6b4a82015-09-02 23:19:55 +020024 universal_newlines=True)
25 with proc:
26 version = proc.communicate()[0]
27 except OSError:
28 # This is what "no gdb" looks like. There may, however, be other
29 # errors that manifest this way too.
30 raise unittest.SkipTest("Couldn't find gdb on the path")
31
32 # Regex to parse:
33 # 'GNU gdb (GDB; SUSE Linux Enterprise 12) 7.7\n' -> 7.7
34 # 'GNU gdb (GDB) Fedora 7.9.1-17.fc22\n' -> 7.9
Victor Stinner479fea62015-09-03 15:42:26 +020035 # 'GNU gdb 6.1.1 [FreeBSD]\n' -> 6.1
36 # 'GNU gdb (GDB) Fedora (7.5.1-37.fc18)\n' -> 7.5
Victor Stinnera578eb32015-09-15 00:22:55 +020037 match = re.search(r"^GNU gdb.*?\b(\d+)\.(\d+)", version)
Victor Stinner5b6b4a82015-09-02 23:19:55 +020038 if match is None:
39 raise Exception("unable to parse GDB version: %r" % version)
40 return (version, int(match.group(1)), int(match.group(2)))
41
42gdb_version, gdb_major_version, gdb_minor_version = get_gdb_version()
R David Murrayf9333022012-10-27 13:22:41 -040043if gdb_major_version < 7:
Victor Stinner2f3ac1e2015-09-02 23:12:14 +020044 raise unittest.SkipTest("gdb versions before 7.0 didn't support python "
45 "embedding. Saw %s.%s:\n%s"
46 % (gdb_major_version, gdb_minor_version,
Victor Stinner5b6b4a82015-09-02 23:19:55 +020047 gdb_version))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000048
Vinay Sajipf1b34ee2012-05-06 12:03:05 +010049if not sysconfig.is_python_build():
50 raise unittest.SkipTest("test_gdb only works on source builds at the moment.")
51
Lysandros Nikolaou59668aa2018-11-04 22:21:28 +010052if 'Clang' in platform.python_compiler() and sys.platform == 'darwin':
53 raise unittest.SkipTest("test_gdb doesn't work correctly when python is"
54 " built with LLVM clang")
55
R David Murrayf9333022012-10-27 13:22:41 -040056# Location of custom hooks file in a repository checkout.
57checkout_hook_path = os.path.join(os.path.dirname(sys.executable),
58 'python-gdb.py')
59
Victor Stinner51324932013-11-20 12:27:48 +010060PYTHONHASHSEED = '123'
61
Victor Stinner79d21332018-10-09 16:54:04 +020062
63def cet_protection():
64 cflags = sysconfig.get_config_var('CFLAGS')
65 if not cflags:
66 return False
67 flags = cflags.split()
68 # True if "-mcet -fcf-protection" options are found, but false
69 # if "-fcf-protection=none" or "-fcf-protection=return" is found.
70 return (('-mcet' in flags)
71 and any((flag.startswith('-fcf-protection')
72 and not flag.endswith(("=none", "=return")))
73 for flag in flags))
74
75# Control-flow enforcement technology
76CET_PROTECTION = cet_protection()
77
78
R David Murrayf9333022012-10-27 13:22:41 -040079def run_gdb(*args, **env_vars):
80 """Runs gdb in --batch mode with the additional arguments given by *args.
81
82 Returns its (stdout, stderr) decoded from utf-8 using the replace handler.
83 """
84 if env_vars:
85 env = os.environ.copy()
86 env.update(env_vars)
87 else:
88 env = None
Victor Stinner7869a4e2014-08-16 14:38:02 +020089 # -nx: Do not execute commands from any .gdbinit initialization files
90 # (issue #22188)
91 base_cmd = ('gdb', '--batch', '-nx')
R David Murrayf9333022012-10-27 13:22:41 -040092 if (gdb_major_version, gdb_minor_version) >= (7, 4):
93 base_cmd += ('-iex', 'add-auto-load-safe-path ' + checkout_hook_path)
Victor Stinner5b6b4a82015-09-02 23:19:55 +020094 proc = subprocess.Popen(base_cmd + args,
Martin Panter7a5fe6d2016-01-16 05:18:47 +000095 # Redirect stdin to prevent GDB from messing with
96 # the terminal settings
97 stdin=subprocess.PIPE,
Victor Stinner5b6b4a82015-09-02 23:19:55 +020098 stdout=subprocess.PIPE,
99 stderr=subprocess.PIPE,
100 env=env)
101 with proc:
102 out, err = proc.communicate()
R David Murrayf9333022012-10-27 13:22:41 -0400103 return out.decode('utf-8', 'replace'), err.decode('utf-8', 'replace')
104
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000105# Verify that "gdb" was built with the embedded python support enabled:
Antoine Pitroue50240c2013-11-23 17:40:36 +0100106gdbpy_version, _ = run_gdb("--eval-command=python import sys; print(sys.version_info)")
R David Murrayf9333022012-10-27 13:22:41 -0400107if not gdbpy_version:
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000108 raise unittest.SkipTest("gdb not built with embedded python support")
109
Nick Coghlance346872013-09-22 19:38:16 +1000110# Verify that "gdb" can load our custom hooks, as OS security settings may
Raymond Hettinger7ea386e2016-08-25 21:11:50 -0700111# disallow this without a customized .gdbinit.
R David Murrayf9333022012-10-27 13:22:41 -0400112_, gdbpy_errors = run_gdb('--args', sys.executable)
113if "auto-loading has been declined" in gdbpy_errors:
114 msg = "gdb security settings prevent use of custom hooks: "
115 raise unittest.SkipTest(msg + gdbpy_errors.rstrip())
Nick Coghlanbe4e4b52012-06-17 18:57:20 +1000116
Victor Stinner50eb60e2010-04-20 22:32:07 +0000117def gdb_has_frame_select():
118 # Does this build of gdb have gdb.Frame.select ?
R David Murrayf9333022012-10-27 13:22:41 -0400119 stdout, _ = run_gdb("--eval-command=python print(dir(gdb.Frame))")
120 m = re.match(r'.*\[(.*)\].*', stdout)
Victor Stinner50eb60e2010-04-20 22:32:07 +0000121 if not m:
122 raise unittest.SkipTest("Unable to parse output from gdb.Frame.select test")
R David Murrayf9333022012-10-27 13:22:41 -0400123 gdb_frame_dir = m.group(1).split(', ')
124 return "'select'" in gdb_frame_dir
Victor Stinner50eb60e2010-04-20 22:32:07 +0000125
126HAS_PYUP_PYDOWN = gdb_has_frame_select()
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000127
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000128BREAKPOINT_FN='builtin_id'
129
Benjamin Peterson437df902016-09-06 20:22:41 -0700130@unittest.skipIf(support.PGO, "not useful for PGO")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000131class DebuggerTests(unittest.TestCase):
132
133 """Test that the debugger can debug Python."""
134
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000135 def get_stack_trace(self, source=None, script=None,
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000136 breakpoint=BREAKPOINT_FN,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000137 cmds_after_breakpoint=None,
138 import_site=False):
139 '''
140 Run 'python -c SOURCE' under gdb with a breakpoint.
141
142 Support injecting commands after the breakpoint is reached
143
144 Returns the stdout from gdb
145
146 cmds_after_breakpoint: if provided, a list of strings: gdb commands
147 '''
148 # We use "set breakpoint pending yes" to avoid blocking with a:
149 # Function "foo" not defined.
150 # Make breakpoint pending on future shared library load? (y or [n])
151 # error, which typically happens python is dynamically linked (the
152 # breakpoints of interest are to be found in the shared library)
153 # When this happens, we still get:
Victor Stinner67df3a42010-04-21 13:53:05 +0000154 # Function "textiowrapper_write" not defined.
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000155 # emitted to stderr each time, alas.
156
157 # Initially I had "--eval-command=continue" here, but removed it to
158 # avoid repeated print breakpoints when traversing hierarchical data
159 # structures
160
161 # Generate a list of commands in gdb's language:
162 commands = ['set breakpoint pending yes',
163 'break %s' % breakpoint,
Serhiy Storchakafdc99532015-01-31 11:48:52 +0200164
Serhiy Storchakafdc99532015-01-31 11:48:52 +0200165 # The tests assume that the first frame of printed
166 # backtrace will not contain program counter,
167 # that is however not guaranteed by gdb
168 # therefore we need to use 'set print address off' to
169 # make sure the counter is not there. For example:
170 # #0 in PyObject_Print ...
171 # is assumed, but sometimes this can be e.g.
172 # #0 0x00003fffb7dd1798 in PyObject_Print ...
173 'set print address off',
174
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000175 'run']
Serhiy Storchaka17d337b2015-02-06 08:35:20 +0200176
177 # GDB as of 7.4 onwards can distinguish between the
178 # value of a variable at entry vs current value:
179 # http://sourceware.org/gdb/onlinedocs/gdb/Variables.html
180 # which leads to the selftests failing with errors like this:
181 # AssertionError: 'v@entry=()' != '()'
182 # Disable this:
183 if (gdb_major_version, gdb_minor_version) >= (7, 4):
184 commands += ['set print entry-values no']
185
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000186 if cmds_after_breakpoint:
Victor Stinner79d21332018-10-09 16:54:04 +0200187 if CET_PROTECTION:
188 # bpo-32962: When Python is compiled with -mcet
189 # -fcf-protection, function arguments are unusable before
190 # running the first instruction of the function entry point.
191 # The 'next' command makes the required first step.
192 commands += ['next']
Victor Stinner2f9cbaa2018-06-15 22:54:35 +0200193 commands += cmds_after_breakpoint
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000194 else:
195 commands += ['backtrace']
196
197 # print commands
198
199 # Use "commands" to generate the arguments with which to invoke "gdb":
Martin Panter40e102c2015-12-08 21:54:42 +0000200 args = ['--eval-command=%s' % cmd for cmd in commands]
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000201 args += ["--args",
202 sys.executable]
Victor Stinner22756f12016-01-22 14:16:47 +0100203 args.extend(subprocess._args_from_interpreter_flags())
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000204
205 if not import_site:
206 # -S suppresses the default 'import site'
207 args += ["-S"]
208
209 if source:
210 args += ["-c", source]
211 elif script:
212 args += [script]
213
214 # print args
Antoine Pitrouf6eb31f2013-11-24 14:58:17 +0100215 # print (' '.join(args))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000216
217 # Use "args" to invoke gdb, capturing stdout, stderr:
Victor Stinner51324932013-11-20 12:27:48 +0100218 out, err = run_gdb(*args, PYTHONHASHSEED=PYTHONHASHSEED)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000219
Antoine Pitrou81641d62013-05-01 00:15:44 +0200220 errlines = err.splitlines()
221 unexpected_errlines = []
222
223 # Ignore some benign messages on stderr.
224 ignore_patterns = (
225 'Function "%s" not defined.' % breakpoint,
Antoine Pitrou81641d62013-05-01 00:15:44 +0200226 'Do you need "set solib-search-path" or '
227 '"set sysroot"?',
Victor Stinner904f5de2016-03-23 18:32:54 +0100228 # BFD: /usr/lib/debug/(...): unable to initialize decompress
229 # status for section .debug_aranges
230 'BFD: ',
231 # ignore all warnings
232 'warning: ',
Antoine Pitrou81641d62013-05-01 00:15:44 +0200233 )
234 for line in errlines:
Victor Stinnerc53195b2016-03-23 21:08:25 +0100235 if not line:
236 continue
Pablo Galindof2ef51f2018-08-31 23:04:47 +0100237 # bpo34007: Sometimes some versions of the shared libraries that
238 # are part of the traceback are compiled in optimised mode and the
239 # Program Counter (PC) is not present, not allowing gdb to walk the
240 # frames back. When this happens, the Python bindings of gdb raise
241 # an exception, making the test impossible to succeed.
242 if "PC not saved" in line:
243 raise unittest.SkipTest("gdb cannot walk the frame object"
244 " because the Program Counter is"
245 " not present")
Antoine Pitrou81641d62013-05-01 00:15:44 +0200246 if not line.startswith(ignore_patterns):
247 unexpected_errlines.append(line)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000248
249 # Ensure no unexpected error messages:
Antoine Pitrou81641d62013-05-01 00:15:44 +0200250 self.assertEqual(unexpected_errlines, [])
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000251 return out
252
253 def get_gdb_repr(self, source,
254 cmds_after_breakpoint=None,
255 import_site=False):
256 # Given an input python source representation of data,
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000257 # run "python -c'id(DATA)'" under gdb with a breakpoint on
258 # builtin_id and scrape out gdb's representation of the "op"
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000259 # parameter, and verify that the gdb displays the same string
260 #
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000261 # Verify that the gdb displays the expected string
262 #
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000263 # For a nested structure, the first time we hit the breakpoint will
264 # give us the top-level structure
Antoine Pitrouf6eb31f2013-11-24 14:58:17 +0100265
266 # NOTE: avoid decoding too much of the traceback as some
267 # undecodable characters may lurk there in optimized mode
268 # (issue #19743).
269 cmds_after_breakpoint = cmds_after_breakpoint or ["backtrace 1"]
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000270 gdb_output = self.get_stack_trace(source, breakpoint=BREAKPOINT_FN,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000271 cmds_after_breakpoint=cmds_after_breakpoint,
272 import_site=import_site)
273 # gdb can insert additional '\n' and space characters in various places
274 # in its output, depending on the width of the terminal it's connected
275 # to (using its "wrap_here" function)
R David Murray44b548d2016-09-08 13:59:53 -0400276 m = re.match(r'.*#0\s+builtin_id\s+\(self\=.*,\s+v=\s*(.*?)\)\s+at\s+\S*Python/bltinmodule.c.*',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000277 gdb_output, re.DOTALL)
278 if not m:
279 self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output))
280 return m.group(1), gdb_output
281
282 def assertEndsWith(self, actual, exp_end):
283 '''Ensure that the given "actual" string ends with "exp_end"'''
Ezio Melottib3aedd42010-11-20 19:04:17 +0000284 self.assertTrue(actual.endswith(exp_end),
285 msg='%r did not end with %r' % (actual, exp_end))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000286
287 def assertMultilineMatches(self, actual, pattern):
288 m = re.match(pattern, actual, re.DOTALL)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000289 if not m:
290 self.fail(msg='%r did not match %r' % (actual, pattern))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000291
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000292 def get_sample_script(self):
293 return findfile('gdb_sample.py')
294
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000295class PrettyPrintTests(DebuggerTests):
296 def test_getting_backtrace(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000297 gdb_output = self.get_stack_trace('id(42)')
298 self.assertTrue(BREAKPOINT_FN in gdb_output)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000299
Antoine Pitrouf6eb31f2013-11-24 14:58:17 +0100300 def assertGdbRepr(self, val, exp_repr=None):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000301 # Ensure that gdb's rendering of the value in a debugged process
302 # matches repr(value) in this process:
Antoine Pitrouf6eb31f2013-11-24 14:58:17 +0100303 gdb_repr, gdb_output = self.get_gdb_repr('id(' + ascii(val) + ')')
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000304 if not exp_repr:
Victor Stinnera2828252013-11-21 10:25:09 +0100305 exp_repr = repr(val)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000306 self.assertEqual(gdb_repr, exp_repr,
307 ('%r did not equal expected %r; full output was:\n%s'
308 % (gdb_repr, exp_repr, gdb_output)))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000309
310 def test_int(self):
Serhiy Storchaka95949422013-08-27 19:40:23 +0300311 'Verify the pretty-printing of various int values'
Victor Stinnera2828252013-11-21 10:25:09 +0100312 self.assertGdbRepr(42)
313 self.assertGdbRepr(0)
314 self.assertGdbRepr(-7)
315 self.assertGdbRepr(1000000000000)
316 self.assertGdbRepr(-1000000000000000)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000317
318 def test_singletons(self):
319 'Verify the pretty-printing of True, False and None'
Victor Stinnera2828252013-11-21 10:25:09 +0100320 self.assertGdbRepr(True)
321 self.assertGdbRepr(False)
322 self.assertGdbRepr(None)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000323
324 def test_dicts(self):
325 'Verify the pretty-printing of dictionaries'
Victor Stinnera2828252013-11-21 10:25:09 +0100326 self.assertGdbRepr({})
327 self.assertGdbRepr({'foo': 'bar'}, "{'foo': 'bar'}")
INADA Naokid7d2bc82016-11-22 19:40:58 +0900328 # Python preserves insertion order since 3.6
329 self.assertGdbRepr({'foo': 'bar', 'douglas': 42}, "{'foo': 'bar', 'douglas': 42}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000330
331 def test_lists(self):
332 'Verify the pretty-printing of lists'
Victor Stinnera2828252013-11-21 10:25:09 +0100333 self.assertGdbRepr([])
334 self.assertGdbRepr(list(range(5)))
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000335
336 def test_bytes(self):
337 'Verify the pretty-printing of bytes'
338 self.assertGdbRepr(b'')
339 self.assertGdbRepr(b'And now for something hopefully the same')
340 self.assertGdbRepr(b'string with embedded NUL here \0 and then some more text')
341 self.assertGdbRepr(b'this is a tab:\t'
342 b' this is a slash-N:\n'
343 b' this is a slash-R:\r'
344 )
345
346 self.assertGdbRepr(b'this is byte 255:\xff and byte 128:\x80')
347
348 self.assertGdbRepr(bytes([b for b in range(255)]))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000349
350 def test_strings(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000351 'Verify the pretty-printing of unicode strings'
Elvis Pranskevichus7279b512018-09-21 21:13:16 -0400352 # We cannot simply call locale.getpreferredencoding() here,
353 # as GDB might have been linked against a different version
354 # of Python with a different encoding and coercion policy
355 # with respect to PEP 538 and PEP 540.
356 out, err = run_gdb(
357 '--eval-command',
358 'python import locale; print(locale.getpreferredencoding())')
359
360 encoding = out.rstrip()
361 if err or not encoding:
362 raise RuntimeError(
363 f'unable to determine the preferred encoding '
364 f'of embedded Python in GDB: {err}')
365
Victor Stinner150016f2010-05-19 23:04:56 +0000366 def check_repr(text):
367 try:
368 text.encode(encoding)
369 printable = True
370 except UnicodeEncodeError:
Antoine Pitrou4c7c4212010-09-09 20:40:28 +0000371 self.assertGdbRepr(text, ascii(text))
Victor Stinner150016f2010-05-19 23:04:56 +0000372 else:
373 self.assertGdbRepr(text)
374
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000375 self.assertGdbRepr('')
376 self.assertGdbRepr('And now for something hopefully the same')
377 self.assertGdbRepr('string with embedded NUL here \0 and then some more text')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000378
379 # Test printing a single character:
380 # U+2620 SKULL AND CROSSBONES
Victor Stinner150016f2010-05-19 23:04:56 +0000381 check_repr('\u2620')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000382
383 # Test printing a Japanese unicode string
384 # (I believe this reads "mojibake", using 3 characters from the CJK
385 # Unified Ideographs area, followed by U+3051 HIRAGANA LETTER KE)
Victor Stinner150016f2010-05-19 23:04:56 +0000386 check_repr('\u6587\u5b57\u5316\u3051')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000387
388 # Test a character outside the BMP:
389 # U+1D121 MUSICAL SYMBOL C CLEF
390 # This is:
391 # UTF-8: 0xF0 0x9D 0x84 0xA1
392 # UTF-16: 0xD834 0xDD21
Victor Stinner150016f2010-05-19 23:04:56 +0000393 check_repr(chr(0x1D121))
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000394
395 def test_tuples(self):
396 'Verify the pretty-printing of tuples'
Victor Stinner51324932013-11-20 12:27:48 +0100397 self.assertGdbRepr(tuple(), '()')
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000398 self.assertGdbRepr((1,), '(1,)')
399 self.assertGdbRepr(('foo', 'bar', 'baz'))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000400
401 def test_sets(self):
402 'Verify the pretty-printing of sets'
Antoine Pitroua78cccb2013-09-22 00:14:27 +0200403 if (gdb_major_version, gdb_minor_version) < (7, 3):
404 self.skipTest("pretty-printing of sets needs gdb 7.3 or later")
Victor Stinner5a701f02016-01-22 15:04:27 +0100405 self.assertGdbRepr(set(), "set()")
406 self.assertGdbRepr(set(['a']), "{'a'}")
407 # PYTHONHASHSEED is need to get the exact frozenset item order
408 if not sys.flags.ignore_environment:
409 self.assertGdbRepr(set(['a', 'b']), "{'a', 'b'}")
410 self.assertGdbRepr(set([4, 5, 6]), "{4, 5, 6}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000411
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000412 # Ensure that we handle sets containing the "dummy" key value,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000413 # which happens on deletion:
414 gdb_repr, gdb_output = self.get_gdb_repr('''s = set(['a','b'])
Victor Stinner51324932013-11-20 12:27:48 +0100415s.remove('a')
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000416id(s)''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000417 self.assertEqual(gdb_repr, "{'b'}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000418
419 def test_frozensets(self):
420 'Verify the pretty-printing of frozensets'
Antoine Pitroua78cccb2013-09-22 00:14:27 +0200421 if (gdb_major_version, gdb_minor_version) < (7, 3):
422 self.skipTest("pretty-printing of frozensets needs gdb 7.3 or later")
Victor Stinner22756f12016-01-22 14:16:47 +0100423 self.assertGdbRepr(frozenset(), "frozenset()")
424 self.assertGdbRepr(frozenset(['a']), "frozenset({'a'})")
425 # PYTHONHASHSEED is need to get the exact frozenset item order
426 if not sys.flags.ignore_environment:
427 self.assertGdbRepr(frozenset(['a', 'b']), "frozenset({'a', 'b'})")
428 self.assertGdbRepr(frozenset([4, 5, 6]), "frozenset({4, 5, 6})")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000429
430 def test_exceptions(self):
431 # Test a RuntimeError
432 gdb_repr, gdb_output = self.get_gdb_repr('''
433try:
434 raise RuntimeError("I am an error")
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000435except RuntimeError as e:
436 id(e)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000437''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000438 self.assertEqual(gdb_repr,
439 "RuntimeError('I am an error',)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000440
441
442 # Test division by zero:
443 gdb_repr, gdb_output = self.get_gdb_repr('''
444try:
445 a = 1 / 0
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000446except ZeroDivisionError as e:
447 id(e)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000448''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000449 self.assertEqual(gdb_repr,
450 "ZeroDivisionError('division by zero',)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000451
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000452 def test_modern_class(self):
453 'Verify the pretty-printing of new-style class instances'
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000454 gdb_repr, gdb_output = self.get_gdb_repr('''
455class Foo:
456 pass
457foo = Foo()
458foo.an_int = 42
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000459id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100460 m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000461 self.assertTrue(m,
462 msg='Unexpected new-style class rendering %r' % gdb_repr)
463
464 def test_subclassing_list(self):
465 'Verify the pretty-printing of an instance of a list subclass'
466 gdb_repr, gdb_output = self.get_gdb_repr('''
467class Foo(list):
468 pass
469foo = Foo()
470foo += [1, 2, 3]
471foo.an_int = 42
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000472id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100473 m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000474
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000475 self.assertTrue(m,
476 msg='Unexpected new-style class rendering %r' % gdb_repr)
477
478 def test_subclassing_tuple(self):
479 'Verify the pretty-printing of an instance of a tuple subclass'
480 # This should exercise the negative tp_dictoffset code in the
481 # new-style class support
482 gdb_repr, gdb_output = self.get_gdb_repr('''
483class Foo(tuple):
484 pass
485foo = Foo((1, 2, 3))
486foo.an_int = 42
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000487id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100488 m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000489
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000490 self.assertTrue(m,
491 msg='Unexpected new-style class rendering %r' % gdb_repr)
492
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000493 def assertSane(self, source, corruption, exprepr=None):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000494 '''Run Python under gdb, corrupting variables in the inferior process
495 immediately before taking a backtrace.
496
497 Verify that the variable's representation is the expected failsafe
498 representation'''
499 if corruption:
500 cmds_after_breakpoint=[corruption, 'backtrace']
501 else:
502 cmds_after_breakpoint=['backtrace']
503
504 gdb_repr, gdb_output = \
505 self.get_gdb_repr(source,
506 cmds_after_breakpoint=cmds_after_breakpoint)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000507 if exprepr:
508 if gdb_repr == exprepr:
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000509 # gdb managed to print the value in spite of the corruption;
510 # this is good (see http://bugs.python.org/issue8330)
511 return
512
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000513 # Match anything for the type name; 0xDEADBEEF could point to
514 # something arbitrary (see http://bugs.python.org/issue8330)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100515 pattern = '<.* at remote 0x-?[0-9a-f]+>'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000516
517 m = re.match(pattern, gdb_repr)
518 if not m:
519 self.fail('Unexpected gdb representation: %r\n%s' % \
520 (gdb_repr, gdb_output))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000521
522 def test_NULL_ptr(self):
523 'Ensure that a NULL PyObject* is handled gracefully'
524 gdb_repr, gdb_output = (
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000525 self.get_gdb_repr('id(42)',
526 cmds_after_breakpoint=['set variable v=0',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000527 'backtrace'])
528 )
529
Ezio Melottib3aedd42010-11-20 19:04:17 +0000530 self.assertEqual(gdb_repr, '0x0')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000531
532 def test_NULL_ob_type(self):
533 'Ensure that a PyObject* with NULL ob_type is handled gracefully'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000534 self.assertSane('id(42)',
535 'set v->ob_type=0')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000536
537 def test_corrupt_ob_type(self):
538 'Ensure that a PyObject* with a corrupt ob_type is handled gracefully'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000539 self.assertSane('id(42)',
540 'set v->ob_type=0xDEADBEEF',
541 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000542
543 def test_corrupt_tp_flags(self):
544 'Ensure that a PyObject* with a type with corrupt tp_flags is handled'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000545 self.assertSane('id(42)',
546 'set v->ob_type->tp_flags=0x0',
547 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000548
549 def test_corrupt_tp_name(self):
550 'Ensure that a PyObject* with a type with corrupt tp_name is handled'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000551 self.assertSane('id(42)',
552 'set v->ob_type->tp_name=0xDEADBEEF',
553 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000554
555 def test_builtins_help(self):
556 'Ensure that the new-style class _Helper in site.py can be handled'
Victor Stinner22756f12016-01-22 14:16:47 +0100557
558 if sys.flags.no_site:
559 self.skipTest("need site module, but -S option was used")
560
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000561 # (this was the issue causing tracebacks in
562 # http://bugs.python.org/issue8032#msg100537 )
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000563 gdb_repr, gdb_output = self.get_gdb_repr('id(__builtins__.help)', import_site=True)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000564
Antoine Pitrou4d098732011-11-26 01:42:03 +0100565 m = re.match(r'<_Helper at remote 0x-?[0-9a-f]+>', gdb_repr)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000566 self.assertTrue(m,
567 msg='Unexpected rendering %r' % gdb_repr)
568
569 def test_selfreferential_list(self):
570 '''Ensure that a reference loop involving a list doesn't lead proxyval
571 into an infinite loop:'''
572 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000573 self.get_gdb_repr("a = [3, 4, 5] ; a.append(a) ; id(a)")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000574 self.assertEqual(gdb_repr, '[3, 4, 5, [...]]')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000575
576 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000577 self.get_gdb_repr("a = [3, 4, 5] ; b = [a] ; a.append(b) ; id(a)")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000578 self.assertEqual(gdb_repr, '[3, 4, 5, [[...]]]')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000579
580 def test_selfreferential_dict(self):
581 '''Ensure that a reference loop involving a dict doesn't lead proxyval
582 into an infinite loop:'''
583 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000584 self.get_gdb_repr("a = {} ; b = {'bar':a} ; a['foo'] = b ; id(a)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000585
Ezio Melottib3aedd42010-11-20 19:04:17 +0000586 self.assertEqual(gdb_repr, "{'foo': {'bar': {...}}}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000587
588 def test_selfreferential_old_style_instance(self):
589 gdb_repr, gdb_output = \
590 self.get_gdb_repr('''
591class Foo:
592 pass
593foo = Foo()
594foo.an_attr = foo
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000595id(foo)''')
R David Murray44b548d2016-09-08 13:59:53 -0400596 self.assertTrue(re.match(r'<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000597 gdb_repr),
598 'Unexpected gdb representation: %r\n%s' % \
599 (gdb_repr, gdb_output))
600
601 def test_selfreferential_new_style_instance(self):
602 gdb_repr, gdb_output = \
603 self.get_gdb_repr('''
604class Foo(object):
605 pass
606foo = Foo()
607foo.an_attr = foo
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000608id(foo)''')
R David Murray44b548d2016-09-08 13:59:53 -0400609 self.assertTrue(re.match(r'<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000610 gdb_repr),
611 'Unexpected gdb representation: %r\n%s' % \
612 (gdb_repr, gdb_output))
613
614 gdb_repr, gdb_output = \
615 self.get_gdb_repr('''
616class Foo(object):
617 pass
618a = Foo()
619b = Foo()
620a.an_attr = b
621b.an_attr = a
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000622id(a)''')
R David Murray44b548d2016-09-08 13:59:53 -0400623 self.assertTrue(re.match(r'<Foo\(an_attr=<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>\) at remote 0x-?[0-9a-f]+>',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000624 gdb_repr),
625 'Unexpected gdb representation: %r\n%s' % \
626 (gdb_repr, gdb_output))
627
628 def test_truncation(self):
629 'Verify that very long output is truncated'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000630 gdb_repr, gdb_output = self.get_gdb_repr('id(list(range(1000)))')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000631 self.assertEqual(gdb_repr,
632 "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, "
633 "14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, "
634 "27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, "
635 "40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, "
636 "53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, "
637 "66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, "
638 "79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, "
639 "92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, "
640 "104, 105, 106, 107, 108, 109, 110, 111, 112, 113, "
641 "114, 115, 116, 117, 118, 119, 120, 121, 122, 123, "
642 "124, 125, 126, 127, 128, 129, 130, 131, 132, 133, "
643 "134, 135, 136, 137, 138, 139, 140, 141, 142, 143, "
644 "144, 145, 146, 147, 148, 149, 150, 151, 152, 153, "
645 "154, 155, 156, 157, 158, 159, 160, 161, 162, 163, "
646 "164, 165, 166, 167, 168, 169, 170, 171, 172, 173, "
647 "174, 175, 176, 177, 178, 179, 180, 181, 182, 183, "
648 "184, 185, 186, 187, 188, 189, 190, 191, 192, 193, "
649 "194, 195, 196, 197, 198, 199, 200, 201, 202, 203, "
650 "204, 205, 206, 207, 208, 209, 210, 211, 212, 213, "
651 "214, 215, 216, 217, 218, 219, 220, 221, 222, 223, "
652 "224, 225, 226...(truncated)")
653 self.assertEqual(len(gdb_repr),
654 1024 + len('...(truncated)'))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000655
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000656 def test_builtin_method(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000657 gdb_repr, gdb_output = self.get_gdb_repr('import sys; id(sys.stdout.readlines)')
R David Murray44b548d2016-09-08 13:59:53 -0400658 self.assertTrue(re.match(r'<built-in method readlines of _io.TextIOWrapper object at remote 0x-?[0-9a-f]+>',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000659 gdb_repr),
660 'Unexpected gdb representation: %r\n%s' % \
661 (gdb_repr, gdb_output))
662
663 def test_frames(self):
664 gdb_output = self.get_stack_trace('''
665def foo(a, b, c):
666 pass
667
668foo(3, 4, 5)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000669id(foo.__code__)''',
670 breakpoint='builtin_id',
671 cmds_after_breakpoint=['print (PyFrameObject*)(((PyCodeObject*)v)->co_zombieframe)']
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000672 )
R David Murray44b548d2016-09-08 13:59:53 -0400673 self.assertTrue(re.match(r'.*\s+\$1 =\s+Frame 0x-?[0-9a-f]+, for file <string>, line 3, in foo \(\)\s+.*',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000674 gdb_output,
675 re.DOTALL),
676 'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output))
677
Victor Stinnerd2084162011-12-19 13:42:24 +0100678@unittest.skipIf(python_is_optimized(),
679 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000680class PyListTests(DebuggerTests):
681 def assertListing(self, expected, actual):
682 self.assertEndsWith(actual, expected)
683
684 def test_basic_command(self):
685 'Verify that the "py-list" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000686 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000687 cmds_after_breakpoint=['py-list'])
688
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000689 self.assertListing(' 5 \n'
690 ' 6 def bar(a, b, c):\n'
691 ' 7 baz(a, b, c)\n'
692 ' 8 \n'
693 ' 9 def baz(*args):\n'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000694 ' >10 id(42)\n'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000695 ' 11 \n'
696 ' 12 foo(1, 2, 3)\n',
697 bt)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000698
699 def test_one_abs_arg(self):
700 'Verify the "py-list" command with one absolute argument'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000701 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000702 cmds_after_breakpoint=['py-list 9'])
703
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000704 self.assertListing(' 9 def baz(*args):\n'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000705 ' >10 id(42)\n'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000706 ' 11 \n'
707 ' 12 foo(1, 2, 3)\n',
708 bt)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000709
710 def test_two_abs_args(self):
711 'Verify the "py-list" command with two absolute arguments'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000712 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000713 cmds_after_breakpoint=['py-list 1,3'])
714
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000715 self.assertListing(' 1 # Sample script for use by test_gdb.py\n'
716 ' 2 \n'
717 ' 3 def foo(a, b, c):\n',
718 bt)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000719
720class StackNavigationTests(DebuggerTests):
Victor Stinner50eb60e2010-04-20 22:32:07 +0000721 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinnerd2084162011-12-19 13:42:24 +0100722 @unittest.skipIf(python_is_optimized(),
723 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000724 def test_pyup_command(self):
725 'Verify that the "py-up" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000726 bt = self.get_stack_trace(script=self.get_sample_script(),
Victor Stinnere3d75c62016-11-22 22:53:18 +0100727 cmds_after_breakpoint=['py-up', 'py-up'])
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000728 self.assertMultilineMatches(bt,
729 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100730#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000731 baz\(a, b, c\)
732$''')
733
Victor Stinner50eb60e2010-04-20 22:32:07 +0000734 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000735 def test_down_at_bottom(self):
736 'Verify handling of "py-down" at the bottom of the stack'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000737 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000738 cmds_after_breakpoint=['py-down'])
739 self.assertEndsWith(bt,
740 'Unable to find a newer python frame\n')
741
Victor Stinner50eb60e2010-04-20 22:32:07 +0000742 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000743 def test_up_at_top(self):
744 'Verify handling of "py-up" at the top of the stack'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000745 bt = self.get_stack_trace(script=self.get_sample_script(),
Victor Stinnere3d75c62016-11-22 22:53:18 +0100746 cmds_after_breakpoint=['py-up'] * 5)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000747 self.assertEndsWith(bt,
748 'Unable to find an older python frame\n')
749
Victor Stinner50eb60e2010-04-20 22:32:07 +0000750 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinnerd2084162011-12-19 13:42:24 +0100751 @unittest.skipIf(python_is_optimized(),
752 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000753 def test_up_then_down(self):
754 'Verify "py-up" followed by "py-down"'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000755 bt = self.get_stack_trace(script=self.get_sample_script(),
Victor Stinnere3d75c62016-11-22 22:53:18 +0100756 cmds_after_breakpoint=['py-up', 'py-up', 'py-down'])
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000757 self.assertMultilineMatches(bt,
758 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100759#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000760 baz\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100761#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 10, in baz \(args=\(1, 2, 3\)\)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000762 id\(42\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000763$''')
764
765class PyBtTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100766 @unittest.skipIf(python_is_optimized(),
767 "Python was compiled with optimizations")
Victor Stinnere670c882011-05-13 17:40:15 +0200768 def test_bt(self):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000769 'Verify that the "py-bt" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000770 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000771 cmds_after_breakpoint=['py-bt'])
772 self.assertMultilineMatches(bt,
773 r'''^.*
Victor Stinnere670c882011-05-13 17:40:15 +0200774Traceback \(most recent call first\):
Victor Stinnere3d75c62016-11-22 22:53:18 +0100775 <built-in method id of module object .*>
Victor Stinnere670c882011-05-13 17:40:15 +0200776 File ".*gdb_sample.py", line 10, in baz
777 id\(42\)
778 File ".*gdb_sample.py", line 7, in bar
779 baz\(a, b, c\)
780 File ".*gdb_sample.py", line 4, in foo
781 bar\(a, b, c\)
782 File ".*gdb_sample.py", line 12, in <module>
783 foo\(1, 2, 3\)
784''')
785
Victor Stinnerd2084162011-12-19 13:42:24 +0100786 @unittest.skipIf(python_is_optimized(),
787 "Python was compiled with optimizations")
Victor Stinnere670c882011-05-13 17:40:15 +0200788 def test_bt_full(self):
789 'Verify that the "py-bt-full" command works'
790 bt = self.get_stack_trace(script=self.get_sample_script(),
791 cmds_after_breakpoint=['py-bt-full'])
792 self.assertMultilineMatches(bt,
793 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100794#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000795 baz\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100796#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 4, in foo \(a=1, b=2, c=3\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000797 bar\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100798#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\)
Victor Stinnerd2084162011-12-19 13:42:24 +0100799 foo\(1, 2, 3\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000800''')
801
David Malcolm8d37ffa2012-06-27 14:15:34 -0400802 def test_threads(self):
803 'Verify that "py-bt" indicates threads that are waiting for the GIL'
804 cmd = '''
805from threading import Thread
806
807class TestThread(Thread):
808 # These threads would run forever, but we'll interrupt things with the
809 # debugger
810 def run(self):
811 i = 0
812 while 1:
813 i += 1
814
815t = {}
816for i in range(4):
817 t[i] = TestThread()
818 t[i].start()
819
820# Trigger a breakpoint on the main thread
821id(42)
822
823'''
824 # Verify with "py-bt":
825 gdb_output = self.get_stack_trace(cmd,
826 cmds_after_breakpoint=['thread apply all py-bt'])
827 self.assertIn('Waiting for the GIL', gdb_output)
828
829 # Verify with "py-bt-full":
830 gdb_output = self.get_stack_trace(cmd,
831 cmds_after_breakpoint=['thread apply all py-bt-full'])
832 self.assertIn('Waiting for the GIL', gdb_output)
833
834 @unittest.skipIf(python_is_optimized(),
835 "Python was compiled with optimizations")
836 # Some older versions of gdb will fail with
837 # "Cannot find new threads: generic error"
838 # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
David Malcolm8d37ffa2012-06-27 14:15:34 -0400839 def test_gc(self):
840 'Verify that "py-bt" indicates if a thread is garbage-collecting'
841 cmd = ('from gc import collect\n'
842 'id(42)\n'
843 'def foo():\n'
844 ' collect()\n'
845 'def bar():\n'
846 ' foo()\n'
847 'bar()\n')
848 # Verify with "py-bt":
849 gdb_output = self.get_stack_trace(cmd,
850 cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt'],
851 )
852 self.assertIn('Garbage-collecting', gdb_output)
853
854 # Verify with "py-bt-full":
855 gdb_output = self.get_stack_trace(cmd,
856 cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt-full'],
857 )
858 self.assertIn('Garbage-collecting', gdb_output)
859
860 @unittest.skipIf(python_is_optimized(),
861 "Python was compiled with optimizations")
862 # Some older versions of gdb will fail with
863 # "Cannot find new threads: generic error"
864 # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
David Malcolm8d37ffa2012-06-27 14:15:34 -0400865 def test_pycfunction(self):
866 'Verify that "py-bt" displays invocations of PyCFunction instances'
Victor Stinner79644f92015-03-27 15:42:37 +0100867 # Tested function must not be defined with METH_NOARGS or METH_O,
868 # otherwise call_function() doesn't call PyCFunction_Call()
869 cmd = ('from time import gmtime\n'
David Malcolm8d37ffa2012-06-27 14:15:34 -0400870 'def foo():\n'
Victor Stinner79644f92015-03-27 15:42:37 +0100871 ' gmtime(1)\n'
David Malcolm8d37ffa2012-06-27 14:15:34 -0400872 'def bar():\n'
873 ' foo()\n'
874 'bar()\n')
875 # Verify with "py-bt":
876 gdb_output = self.get_stack_trace(cmd,
Victor Stinner79644f92015-03-27 15:42:37 +0100877 breakpoint='time_gmtime',
David Malcolm8d37ffa2012-06-27 14:15:34 -0400878 cmds_after_breakpoint=['bt', 'py-bt'],
879 )
Victor Stinner79644f92015-03-27 15:42:37 +0100880 self.assertIn('<built-in method gmtime', gdb_output)
David Malcolm8d37ffa2012-06-27 14:15:34 -0400881
882 # Verify with "py-bt-full":
883 gdb_output = self.get_stack_trace(cmd,
Victor Stinner79644f92015-03-27 15:42:37 +0100884 breakpoint='time_gmtime',
David Malcolm8d37ffa2012-06-27 14:15:34 -0400885 cmds_after_breakpoint=['py-bt-full'],
886 )
INADA Naoki5566bbb2017-02-03 07:43:03 +0900887 self.assertIn('#2 <built-in method gmtime', gdb_output)
David Malcolm8d37ffa2012-06-27 14:15:34 -0400888
Victor Stinner61108332017-02-01 16:29:54 +0100889 @unittest.skipIf(python_is_optimized(),
890 "Python was compiled with optimizations")
891 def test_wrapper_call(self):
892 cmd = textwrap.dedent('''
893 class MyList(list):
894 def __init__(self):
895 super().__init__() # wrapper_call()
896
Victor Stinnerf94b68a2017-02-01 17:00:32 +0100897 id("first break point")
Victor Stinner61108332017-02-01 16:29:54 +0100898 l = MyList()
899 ''')
Victor Stinner79d21332018-10-09 16:54:04 +0200900 cmds_after_breakpoint = ['break wrapper_call', 'continue']
901 if CET_PROTECTION:
902 # bpo-32962: same case as in get_stack_trace():
903 # we need an additional 'next' command in order to read
904 # arguments of the innermost function of the call stack.
905 cmds_after_breakpoint.append('next')
906 cmds_after_breakpoint.append('py-bt')
907
Victor Stinner61108332017-02-01 16:29:54 +0100908 # Verify with "py-bt":
909 gdb_output = self.get_stack_trace(cmd,
Victor Stinner79d21332018-10-09 16:54:04 +0200910 cmds_after_breakpoint=cmds_after_breakpoint)
Victor Stinner72268ae2017-02-01 18:26:14 +0100911 self.assertRegex(gdb_output,
912 r"<method-wrapper u?'__init__' of MyList object at ")
Victor Stinner61108332017-02-01 16:29:54 +0100913
David Malcolm8d37ffa2012-06-27 14:15:34 -0400914
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000915class PyPrintTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100916 @unittest.skipIf(python_is_optimized(),
917 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000918 def test_basic_command(self):
919 'Verify that the "py-print" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000920 bt = self.get_stack_trace(script=self.get_sample_script(),
Victor Stinnere3d75c62016-11-22 22:53:18 +0100921 cmds_after_breakpoint=['py-up', 'py-print args'])
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000922 self.assertMultilineMatches(bt,
923 r".*\nlocal 'args' = \(1, 2, 3\)\n.*")
924
Vinay Sajip2549f872012-01-04 12:07:30 +0000925 @unittest.skipIf(python_is_optimized(),
926 "Python was compiled with optimizations")
Victor Stinner50eb60e2010-04-20 22:32:07 +0000927 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000928 def test_print_after_up(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000929 bt = self.get_stack_trace(script=self.get_sample_script(),
Victor Stinnere3d75c62016-11-22 22:53:18 +0100930 cmds_after_breakpoint=['py-up', 'py-up', 'py-print c', 'py-print b', 'py-print a'])
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000931 self.assertMultilineMatches(bt,
932 r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*")
933
Victor Stinnerd2084162011-12-19 13:42:24 +0100934 @unittest.skipIf(python_is_optimized(),
935 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000936 def test_printing_global(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000937 bt = self.get_stack_trace(script=self.get_sample_script(),
Victor Stinnere3d75c62016-11-22 22:53:18 +0100938 cmds_after_breakpoint=['py-up', 'py-print __name__'])
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000939 self.assertMultilineMatches(bt,
940 r".*\nglobal '__name__' = '__main__'\n.*")
941
Victor Stinnerd2084162011-12-19 13:42:24 +0100942 @unittest.skipIf(python_is_optimized(),
943 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000944 def test_printing_builtin(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000945 bt = self.get_stack_trace(script=self.get_sample_script(),
Victor Stinnere3d75c62016-11-22 22:53:18 +0100946 cmds_after_breakpoint=['py-up', 'py-print len'])
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000947 self.assertMultilineMatches(bt,
Antoine Pitrou4d098732011-11-26 01:42:03 +0100948 r".*\nbuiltin 'len' = <built-in method len of module object at remote 0x-?[0-9a-f]+>\n.*")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000949
950class PyLocalsTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100951 @unittest.skipIf(python_is_optimized(),
952 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000953 def test_basic_command(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000954 bt = self.get_stack_trace(script=self.get_sample_script(),
Victor Stinnere3d75c62016-11-22 22:53:18 +0100955 cmds_after_breakpoint=['py-up', 'py-locals'])
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000956 self.assertMultilineMatches(bt,
957 r".*\nargs = \(1, 2, 3\)\n.*")
958
Victor Stinner50eb60e2010-04-20 22:32:07 +0000959 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Vinay Sajip2549f872012-01-04 12:07:30 +0000960 @unittest.skipIf(python_is_optimized(),
961 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000962 def test_locals_after_up(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000963 bt = self.get_stack_trace(script=self.get_sample_script(),
Victor Stinnere3d75c62016-11-22 22:53:18 +0100964 cmds_after_breakpoint=['py-up', 'py-up', 'py-locals'])
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000965 self.assertMultilineMatches(bt,
966 r".*\na = 1\nb = 2\nc = 3\n.*")
967
968def test_main():
Antoine Pitroud0f3e072013-09-21 23:56:17 +0200969 if support.verbose:
Victor Stinner2f3ac1e2015-09-02 23:12:14 +0200970 print("GDB version %s.%s:" % (gdb_major_version, gdb_minor_version))
Victor Stinner5b6b4a82015-09-02 23:19:55 +0200971 for line in gdb_version.splitlines():
Antoine Pitroud0f3e072013-09-21 23:56:17 +0200972 print(" " * 4 + line)
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000973 run_unittest(PrettyPrintTests,
974 PyListTests,
975 StackNavigationTests,
976 PyBtTests,
977 PyPrintTests,
978 PyLocalsTests
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000979 )
980
981if __name__ == "__main__":
982 test_main()