test_gdb.py 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. # 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. import os
  6. import platform
  7. import re
  8. import subprocess
  9. import sys
  10. import sysconfig
  11. import textwrap
  12. import unittest
  13. from test import support
  14. from test.support import findfile, python_is_optimized
  15. def get_gdb_version():
  16. try:
  17. cmd = ["gdb", "-nx", "--version"]
  18. proc = subprocess.Popen(cmd,
  19. stdout=subprocess.PIPE,
  20. stderr=subprocess.PIPE,
  21. universal_newlines=True)
  22. with proc:
  23. version, stderr = proc.communicate()
  24. if proc.returncode:
  25. raise Exception(f"Command {' '.join(cmd)!r} failed "
  26. f"with exit code {proc.returncode}: "
  27. f"stdout={version!r} stderr={stderr!r}")
  28. except OSError:
  29. # This is what "no gdb" looks like. There may, however, be other
  30. # errors that manifest this way too.
  31. raise unittest.SkipTest("Couldn't find gdb on the path")
  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
  35. # 'GNU gdb 6.1.1 [FreeBSD]\n' -> 6.1
  36. # 'GNU gdb (GDB) Fedora (7.5.1-37.fc18)\n' -> 7.5
  37. # 'HP gdb 6.7 for HP Itanium (32 or 64 bit) and target HP-UX 11iv2 and 11iv3.\n' -> 6.7
  38. match = re.search(r"^(?:GNU|HP) gdb.*?\b(\d+)\.(\d+)", version)
  39. if match is None:
  40. raise Exception("unable to parse GDB version: %r" % version)
  41. return (version, int(match.group(1)), int(match.group(2)))
  42. gdb_version, gdb_major_version, gdb_minor_version = get_gdb_version()
  43. if gdb_major_version < 7:
  44. 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,
  47. gdb_version))
  48. if not sysconfig.is_python_build():
  49. raise unittest.SkipTest("test_gdb only works on source builds at the moment.")
  50. if 'Clang' in platform.python_compiler() and sys.platform == 'darwin':
  51. raise unittest.SkipTest("test_gdb doesn't work correctly when python is"
  52. " built with LLVM clang")
  53. if ((sysconfig.get_config_var('PGO_PROF_USE_FLAG') or 'xxx') in
  54. (sysconfig.get_config_var('PY_CORE_CFLAGS') or '')):
  55. raise unittest.SkipTest("test_gdb is not reliable on PGO builds")
  56. # Location of custom hooks file in a repository checkout.
  57. checkout_hook_path = os.path.join(os.path.dirname(sys.executable),
  58. 'python-gdb.py')
  59. PYTHONHASHSEED = '123'
  60. def cet_protection():
  61. cflags = sysconfig.get_config_var('CFLAGS')
  62. if not cflags:
  63. return False
  64. flags = cflags.split()
  65. # True if "-mcet -fcf-protection" options are found, but false
  66. # if "-fcf-protection=none" or "-fcf-protection=return" is found.
  67. return (('-mcet' in flags)
  68. and any((flag.startswith('-fcf-protection')
  69. and not flag.endswith(("=none", "=return")))
  70. for flag in flags))
  71. # Control-flow enforcement technology
  72. CET_PROTECTION = cet_protection()
  73. def run_gdb(*args, **env_vars):
  74. """Runs gdb in --batch mode with the additional arguments given by *args.
  75. Returns its (stdout, stderr) decoded from utf-8 using the replace handler.
  76. """
  77. if env_vars:
  78. env = os.environ.copy()
  79. env.update(env_vars)
  80. else:
  81. env = None
  82. # -nx: Do not execute commands from any .gdbinit initialization files
  83. # (issue #22188)
  84. base_cmd = ('gdb', '--batch', '-nx')
  85. if (gdb_major_version, gdb_minor_version) >= (7, 4):
  86. base_cmd += ('-iex', 'add-auto-load-safe-path ' + checkout_hook_path)
  87. proc = subprocess.Popen(base_cmd + args,
  88. # Redirect stdin to prevent GDB from messing with
  89. # the terminal settings
  90. stdin=subprocess.PIPE,
  91. stdout=subprocess.PIPE,
  92. stderr=subprocess.PIPE,
  93. env=env)
  94. with proc:
  95. out, err = proc.communicate()
  96. return out.decode('utf-8', 'replace'), err.decode('utf-8', 'replace')
  97. # Verify that "gdb" was built with the embedded python support enabled:
  98. gdbpy_version, _ = run_gdb("--eval-command=python import sys; print(sys.version_info)")
  99. if not gdbpy_version:
  100. raise unittest.SkipTest("gdb not built with embedded python support")
  101. if "major=2" in gdbpy_version:
  102. raise unittest.SkipTest("gdb built with Python 2")
  103. # Verify that "gdb" can load our custom hooks, as OS security settings may
  104. # disallow this without a customized .gdbinit.
  105. _, gdbpy_errors = run_gdb('--args', sys.executable)
  106. if "auto-loading has been declined" in gdbpy_errors:
  107. msg = "gdb security settings prevent use of custom hooks: "
  108. raise unittest.SkipTest(msg + gdbpy_errors.rstrip())
  109. def gdb_has_frame_select():
  110. # Does this build of gdb have gdb.Frame.select ?
  111. stdout, _ = run_gdb("--eval-command=python print(dir(gdb.Frame))")
  112. m = re.match(r'.*\[(.*)\].*', stdout)
  113. if not m:
  114. raise unittest.SkipTest("Unable to parse output from gdb.Frame.select test")
  115. gdb_frame_dir = m.group(1).split(', ')
  116. return "'select'" in gdb_frame_dir
  117. HAS_PYUP_PYDOWN = gdb_has_frame_select()
  118. BREAKPOINT_FN='builtin_id'
  119. @unittest.skipIf(support.PGO, "not useful for PGO")
  120. class DebuggerTests(unittest.TestCase):
  121. """Test that the debugger can debug Python."""
  122. def get_stack_trace(self, source=None, script=None,
  123. breakpoint=BREAKPOINT_FN,
  124. cmds_after_breakpoint=None,
  125. import_site=False,
  126. ignore_stderr=False):
  127. '''
  128. Run 'python -c SOURCE' under gdb with a breakpoint.
  129. Support injecting commands after the breakpoint is reached
  130. Returns the stdout from gdb
  131. cmds_after_breakpoint: if provided, a list of strings: gdb commands
  132. '''
  133. # We use "set breakpoint pending yes" to avoid blocking with a:
  134. # Function "foo" not defined.
  135. # Make breakpoint pending on future shared library load? (y or [n])
  136. # error, which typically happens python is dynamically linked (the
  137. # breakpoints of interest are to be found in the shared library)
  138. # When this happens, we still get:
  139. # Function "textiowrapper_write" not defined.
  140. # emitted to stderr each time, alas.
  141. # Initially I had "--eval-command=continue" here, but removed it to
  142. # avoid repeated print breakpoints when traversing hierarchical data
  143. # structures
  144. # Generate a list of commands in gdb's language:
  145. commands = ['set breakpoint pending yes',
  146. 'break %s' % breakpoint,
  147. # The tests assume that the first frame of printed
  148. # backtrace will not contain program counter,
  149. # that is however not guaranteed by gdb
  150. # therefore we need to use 'set print address off' to
  151. # make sure the counter is not there. For example:
  152. # #0 in PyObject_Print ...
  153. # is assumed, but sometimes this can be e.g.
  154. # #0 0x00003fffb7dd1798 in PyObject_Print ...
  155. 'set print address off',
  156. 'run']
  157. # GDB as of 7.4 onwards can distinguish between the
  158. # value of a variable at entry vs current value:
  159. # http://sourceware.org/gdb/onlinedocs/gdb/Variables.html
  160. # which leads to the selftests failing with errors like this:
  161. # AssertionError: 'v@entry=()' != '()'
  162. # Disable this:
  163. if (gdb_major_version, gdb_minor_version) >= (7, 4):
  164. commands += ['set print entry-values no']
  165. if cmds_after_breakpoint:
  166. if CET_PROTECTION:
  167. # bpo-32962: When Python is compiled with -mcet
  168. # -fcf-protection, function arguments are unusable before
  169. # running the first instruction of the function entry point.
  170. # The 'next' command makes the required first step.
  171. commands += ['next']
  172. commands += cmds_after_breakpoint
  173. else:
  174. commands += ['backtrace']
  175. # print commands
  176. # Use "commands" to generate the arguments with which to invoke "gdb":
  177. args = ['--eval-command=%s' % cmd for cmd in commands]
  178. args += ["--args",
  179. sys.executable]
  180. args.extend(subprocess._args_from_interpreter_flags())
  181. if not import_site:
  182. # -S suppresses the default 'import site'
  183. args += ["-S"]
  184. if source:
  185. args += ["-c", source]
  186. elif script:
  187. args += [script]
  188. # Use "args" to invoke gdb, capturing stdout, stderr:
  189. out, err = run_gdb(*args, PYTHONHASHSEED=PYTHONHASHSEED)
  190. if not ignore_stderr:
  191. for line in err.splitlines():
  192. print(line, file=sys.stderr)
  193. # bpo-34007: Sometimes some versions of the shared libraries that
  194. # are part of the traceback are compiled in optimised mode and the
  195. # Program Counter (PC) is not present, not allowing gdb to walk the
  196. # frames back. When this happens, the Python bindings of gdb raise
  197. # an exception, making the test impossible to succeed.
  198. if "PC not saved" in err:
  199. raise unittest.SkipTest("gdb cannot walk the frame object"
  200. " because the Program Counter is"
  201. " not present")
  202. # bpo-40019: Skip the test if gdb failed to read debug information
  203. # because the Python binary is optimized.
  204. for pattern in (
  205. '(frame information optimized out)',
  206. 'Unable to read information on python frame',
  207. ):
  208. if pattern in out:
  209. raise unittest.SkipTest(f"{pattern!r} found in gdb output")
  210. return out
  211. def get_gdb_repr(self, source,
  212. cmds_after_breakpoint=None,
  213. import_site=False):
  214. # Given an input python source representation of data,
  215. # run "python -c'id(DATA)'" under gdb with a breakpoint on
  216. # builtin_id and scrape out gdb's representation of the "op"
  217. # parameter, and verify that the gdb displays the same string
  218. #
  219. # Verify that the gdb displays the expected string
  220. #
  221. # For a nested structure, the first time we hit the breakpoint will
  222. # give us the top-level structure
  223. # NOTE: avoid decoding too much of the traceback as some
  224. # undecodable characters may lurk there in optimized mode
  225. # (issue #19743).
  226. cmds_after_breakpoint = cmds_after_breakpoint or ["backtrace 1"]
  227. gdb_output = self.get_stack_trace(source, breakpoint=BREAKPOINT_FN,
  228. cmds_after_breakpoint=cmds_after_breakpoint,
  229. import_site=import_site)
  230. # gdb can insert additional '\n' and space characters in various places
  231. # in its output, depending on the width of the terminal it's connected
  232. # to (using its "wrap_here" function)
  233. m = re.search(
  234. # Match '#0 builtin_id(self=..., v=...)'
  235. r'#0\s+builtin_id\s+\(self\=.*,\s+v=\s*(.*?)?\)'
  236. # Match ' at Python/bltinmodule.c'.
  237. # bpo-38239: builtin_id() is defined in Python/bltinmodule.c,
  238. # but accept any "Directory\file.c" to support Link Time
  239. # Optimization (LTO).
  240. r'\s+at\s+\S*[A-Za-z]+/[A-Za-z0-9_-]+\.c',
  241. gdb_output, re.DOTALL)
  242. if not m:
  243. self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output))
  244. return m.group(1), gdb_output
  245. def assertEndsWith(self, actual, exp_end):
  246. '''Ensure that the given "actual" string ends with "exp_end"'''
  247. self.assertTrue(actual.endswith(exp_end),
  248. msg='%r did not end with %r' % (actual, exp_end))
  249. def assertMultilineMatches(self, actual, pattern):
  250. m = re.match(pattern, actual, re.DOTALL)
  251. if not m:
  252. self.fail(msg='%r did not match %r' % (actual, pattern))
  253. def get_sample_script(self):
  254. return findfile('gdb_sample.py')
  255. class PrettyPrintTests(DebuggerTests):
  256. def test_getting_backtrace(self):
  257. gdb_output = self.get_stack_trace('id(42)')
  258. self.assertTrue(BREAKPOINT_FN in gdb_output)
  259. def assertGdbRepr(self, val, exp_repr=None):
  260. # Ensure that gdb's rendering of the value in a debugged process
  261. # matches repr(value) in this process:
  262. gdb_repr, gdb_output = self.get_gdb_repr('id(' + ascii(val) + ')')
  263. if not exp_repr:
  264. exp_repr = repr(val)
  265. self.assertEqual(gdb_repr, exp_repr,
  266. ('%r did not equal expected %r; full output was:\n%s'
  267. % (gdb_repr, exp_repr, gdb_output)))
  268. def test_int(self):
  269. 'Verify the pretty-printing of various int values'
  270. self.assertGdbRepr(42)
  271. self.assertGdbRepr(0)
  272. self.assertGdbRepr(-7)
  273. self.assertGdbRepr(1000000000000)
  274. self.assertGdbRepr(-1000000000000000)
  275. def test_singletons(self):
  276. 'Verify the pretty-printing of True, False and None'
  277. self.assertGdbRepr(True)
  278. self.assertGdbRepr(False)
  279. self.assertGdbRepr(None)
  280. def test_dicts(self):
  281. 'Verify the pretty-printing of dictionaries'
  282. self.assertGdbRepr({})
  283. self.assertGdbRepr({'foo': 'bar'}, "{'foo': 'bar'}")
  284. # Python preserves insertion order since 3.6
  285. self.assertGdbRepr({'foo': 'bar', 'douglas': 42}, "{'foo': 'bar', 'douglas': 42}")
  286. def test_lists(self):
  287. 'Verify the pretty-printing of lists'
  288. self.assertGdbRepr([])
  289. self.assertGdbRepr(list(range(5)))
  290. def test_bytes(self):
  291. 'Verify the pretty-printing of bytes'
  292. self.assertGdbRepr(b'')
  293. self.assertGdbRepr(b'And now for something hopefully the same')
  294. self.assertGdbRepr(b'string with embedded NUL here \0 and then some more text')
  295. self.assertGdbRepr(b'this is a tab:\t'
  296. b' this is a slash-N:\n'
  297. b' this is a slash-R:\r'
  298. )
  299. self.assertGdbRepr(b'this is byte 255:\xff and byte 128:\x80')
  300. self.assertGdbRepr(bytes([b for b in range(255)]))
  301. def test_strings(self):
  302. 'Verify the pretty-printing of unicode strings'
  303. # We cannot simply call locale.getpreferredencoding() here,
  304. # as GDB might have been linked against a different version
  305. # of Python with a different encoding and coercion policy
  306. # with respect to PEP 538 and PEP 540.
  307. out, err = run_gdb(
  308. '--eval-command',
  309. 'python import locale; print(locale.getpreferredencoding())')
  310. encoding = out.rstrip()
  311. if err or not encoding:
  312. raise RuntimeError(
  313. f'unable to determine the preferred encoding '
  314. f'of embedded Python in GDB: {err}')
  315. def check_repr(text):
  316. try:
  317. text.encode(encoding)
  318. except UnicodeEncodeError:
  319. self.assertGdbRepr(text, ascii(text))
  320. else:
  321. self.assertGdbRepr(text)
  322. self.assertGdbRepr('')
  323. self.assertGdbRepr('And now for something hopefully the same')
  324. self.assertGdbRepr('string with embedded NUL here \0 and then some more text')
  325. # Test printing a single character:
  326. # U+2620 SKULL AND CROSSBONES
  327. check_repr('\u2620')
  328. # Test printing a Japanese unicode string
  329. # (I believe this reads "mojibake", using 3 characters from the CJK
  330. # Unified Ideographs area, followed by U+3051 HIRAGANA LETTER KE)
  331. check_repr('\u6587\u5b57\u5316\u3051')
  332. # Test a character outside the BMP:
  333. # U+1D121 MUSICAL SYMBOL C CLEF
  334. # This is:
  335. # UTF-8: 0xF0 0x9D 0x84 0xA1
  336. # UTF-16: 0xD834 0xDD21
  337. check_repr(chr(0x1D121))
  338. def test_tuples(self):
  339. 'Verify the pretty-printing of tuples'
  340. self.assertGdbRepr(tuple(), '()')
  341. self.assertGdbRepr((1,), '(1,)')
  342. self.assertGdbRepr(('foo', 'bar', 'baz'))
  343. def test_sets(self):
  344. 'Verify the pretty-printing of sets'
  345. if (gdb_major_version, gdb_minor_version) < (7, 3):
  346. self.skipTest("pretty-printing of sets needs gdb 7.3 or later")
  347. self.assertGdbRepr(set(), "set()")
  348. self.assertGdbRepr(set(['a']), "{'a'}")
  349. # PYTHONHASHSEED is need to get the exact frozenset item order
  350. if not sys.flags.ignore_environment:
  351. self.assertGdbRepr(set(['a', 'b']), "{'a', 'b'}")
  352. self.assertGdbRepr(set([4, 5, 6]), "{4, 5, 6}")
  353. # Ensure that we handle sets containing the "dummy" key value,
  354. # which happens on deletion:
  355. gdb_repr, gdb_output = self.get_gdb_repr('''s = set(['a','b'])
  356. s.remove('a')
  357. id(s)''')
  358. self.assertEqual(gdb_repr, "{'b'}")
  359. def test_frozensets(self):
  360. 'Verify the pretty-printing of frozensets'
  361. if (gdb_major_version, gdb_minor_version) < (7, 3):
  362. self.skipTest("pretty-printing of frozensets needs gdb 7.3 or later")
  363. self.assertGdbRepr(frozenset(), "frozenset()")
  364. self.assertGdbRepr(frozenset(['a']), "frozenset({'a'})")
  365. # PYTHONHASHSEED is need to get the exact frozenset item order
  366. if not sys.flags.ignore_environment:
  367. self.assertGdbRepr(frozenset(['a', 'b']), "frozenset({'a', 'b'})")
  368. self.assertGdbRepr(frozenset([4, 5, 6]), "frozenset({4, 5, 6})")
  369. def test_exceptions(self):
  370. # Test a RuntimeError
  371. gdb_repr, gdb_output = self.get_gdb_repr('''
  372. try:
  373. raise RuntimeError("I am an error")
  374. except RuntimeError as e:
  375. id(e)
  376. ''')
  377. self.assertEqual(gdb_repr,
  378. "RuntimeError('I am an error',)")
  379. # Test division by zero:
  380. gdb_repr, gdb_output = self.get_gdb_repr('''
  381. try:
  382. a = 1 / 0
  383. except ZeroDivisionError as e:
  384. id(e)
  385. ''')
  386. self.assertEqual(gdb_repr,
  387. "ZeroDivisionError('division by zero',)")
  388. def test_modern_class(self):
  389. 'Verify the pretty-printing of new-style class instances'
  390. gdb_repr, gdb_output = self.get_gdb_repr('''
  391. class Foo:
  392. pass
  393. foo = Foo()
  394. foo.an_int = 42
  395. id(foo)''')
  396. m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr)
  397. self.assertTrue(m,
  398. msg='Unexpected new-style class rendering %r' % gdb_repr)
  399. def test_subclassing_list(self):
  400. 'Verify the pretty-printing of an instance of a list subclass'
  401. gdb_repr, gdb_output = self.get_gdb_repr('''
  402. class Foo(list):
  403. pass
  404. foo = Foo()
  405. foo += [1, 2, 3]
  406. foo.an_int = 42
  407. id(foo)''')
  408. m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr)
  409. self.assertTrue(m,
  410. msg='Unexpected new-style class rendering %r' % gdb_repr)
  411. def test_subclassing_tuple(self):
  412. 'Verify the pretty-printing of an instance of a tuple subclass'
  413. # This should exercise the negative tp_dictoffset code in the
  414. # new-style class support
  415. gdb_repr, gdb_output = self.get_gdb_repr('''
  416. class Foo(tuple):
  417. pass
  418. foo = Foo((1, 2, 3))
  419. foo.an_int = 42
  420. id(foo)''')
  421. m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr)
  422. self.assertTrue(m,
  423. msg='Unexpected new-style class rendering %r' % gdb_repr)
  424. def assertSane(self, source, corruption, exprepr=None):
  425. '''Run Python under gdb, corrupting variables in the inferior process
  426. immediately before taking a backtrace.
  427. Verify that the variable's representation is the expected failsafe
  428. representation'''
  429. if corruption:
  430. cmds_after_breakpoint=[corruption, 'backtrace']
  431. else:
  432. cmds_after_breakpoint=['backtrace']
  433. gdb_repr, gdb_output = \
  434. self.get_gdb_repr(source,
  435. cmds_after_breakpoint=cmds_after_breakpoint)
  436. if exprepr:
  437. if gdb_repr == exprepr:
  438. # gdb managed to print the value in spite of the corruption;
  439. # this is good (see http://bugs.python.org/issue8330)
  440. return
  441. # Match anything for the type name; 0xDEADBEEF could point to
  442. # something arbitrary (see http://bugs.python.org/issue8330)
  443. pattern = '<.* at remote 0x-?[0-9a-f]+>'
  444. m = re.match(pattern, gdb_repr)
  445. if not m:
  446. self.fail('Unexpected gdb representation: %r\n%s' % \
  447. (gdb_repr, gdb_output))
  448. def test_NULL_ptr(self):
  449. 'Ensure that a NULL PyObject* is handled gracefully'
  450. gdb_repr, gdb_output = (
  451. self.get_gdb_repr('id(42)',
  452. cmds_after_breakpoint=['set variable v=0',
  453. 'backtrace'])
  454. )
  455. self.assertEqual(gdb_repr, '0x0')
  456. def test_NULL_ob_type(self):
  457. 'Ensure that a PyObject* with NULL ob_type is handled gracefully'
  458. self.assertSane('id(42)',
  459. 'set v->ob_type=0')
  460. def test_corrupt_ob_type(self):
  461. 'Ensure that a PyObject* with a corrupt ob_type is handled gracefully'
  462. self.assertSane('id(42)',
  463. 'set v->ob_type=0xDEADBEEF',
  464. exprepr='42')
  465. def test_corrupt_tp_flags(self):
  466. 'Ensure that a PyObject* with a type with corrupt tp_flags is handled'
  467. self.assertSane('id(42)',
  468. 'set v->ob_type->tp_flags=0x0',
  469. exprepr='42')
  470. def test_corrupt_tp_name(self):
  471. 'Ensure that a PyObject* with a type with corrupt tp_name is handled'
  472. self.assertSane('id(42)',
  473. 'set v->ob_type->tp_name=0xDEADBEEF',
  474. exprepr='42')
  475. def test_builtins_help(self):
  476. 'Ensure that the new-style class _Helper in site.py can be handled'
  477. if sys.flags.no_site:
  478. self.skipTest("need site module, but -S option was used")
  479. # (this was the issue causing tracebacks in
  480. # http://bugs.python.org/issue8032#msg100537 )
  481. gdb_repr, gdb_output = self.get_gdb_repr('id(__builtins__.help)', import_site=True)
  482. m = re.match(r'<_Helper\(\) at remote 0x-?[0-9a-f]+>', gdb_repr)
  483. self.assertTrue(m,
  484. msg='Unexpected rendering %r' % gdb_repr)
  485. def test_selfreferential_list(self):
  486. '''Ensure that a reference loop involving a list doesn't lead proxyval
  487. into an infinite loop:'''
  488. gdb_repr, gdb_output = \
  489. self.get_gdb_repr("a = [3, 4, 5] ; a.append(a) ; id(a)")
  490. self.assertEqual(gdb_repr, '[3, 4, 5, [...]]')
  491. gdb_repr, gdb_output = \
  492. self.get_gdb_repr("a = [3, 4, 5] ; b = [a] ; a.append(b) ; id(a)")
  493. self.assertEqual(gdb_repr, '[3, 4, 5, [[...]]]')
  494. def test_selfreferential_dict(self):
  495. '''Ensure that a reference loop involving a dict doesn't lead proxyval
  496. into an infinite loop:'''
  497. gdb_repr, gdb_output = \
  498. self.get_gdb_repr("a = {} ; b = {'bar':a} ; a['foo'] = b ; id(a)")
  499. self.assertEqual(gdb_repr, "{'foo': {'bar': {...}}}")
  500. def test_selfreferential_old_style_instance(self):
  501. gdb_repr, gdb_output = \
  502. self.get_gdb_repr('''
  503. class Foo:
  504. pass
  505. foo = Foo()
  506. foo.an_attr = foo
  507. id(foo)''')
  508. self.assertTrue(re.match(r'<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>',
  509. gdb_repr),
  510. 'Unexpected gdb representation: %r\n%s' % \
  511. (gdb_repr, gdb_output))
  512. def test_selfreferential_new_style_instance(self):
  513. gdb_repr, gdb_output = \
  514. self.get_gdb_repr('''
  515. class Foo(object):
  516. pass
  517. foo = Foo()
  518. foo.an_attr = foo
  519. id(foo)''')
  520. self.assertTrue(re.match(r'<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>',
  521. gdb_repr),
  522. 'Unexpected gdb representation: %r\n%s' % \
  523. (gdb_repr, gdb_output))
  524. gdb_repr, gdb_output = \
  525. self.get_gdb_repr('''
  526. class Foo(object):
  527. pass
  528. a = Foo()
  529. b = Foo()
  530. a.an_attr = b
  531. b.an_attr = a
  532. id(a)''')
  533. self.assertTrue(re.match(r'<Foo\(an_attr=<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>\) at remote 0x-?[0-9a-f]+>',
  534. gdb_repr),
  535. 'Unexpected gdb representation: %r\n%s' % \
  536. (gdb_repr, gdb_output))
  537. def test_truncation(self):
  538. 'Verify that very long output is truncated'
  539. gdb_repr, gdb_output = self.get_gdb_repr('id(list(range(1000)))')
  540. self.assertEqual(gdb_repr,
  541. "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, "
  542. "14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, "
  543. "27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, "
  544. "40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, "
  545. "53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, "
  546. "66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, "
  547. "79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, "
  548. "92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, "
  549. "104, 105, 106, 107, 108, 109, 110, 111, 112, 113, "
  550. "114, 115, 116, 117, 118, 119, 120, 121, 122, 123, "
  551. "124, 125, 126, 127, 128, 129, 130, 131, 132, 133, "
  552. "134, 135, 136, 137, 138, 139, 140, 141, 142, 143, "
  553. "144, 145, 146, 147, 148, 149, 150, 151, 152, 153, "
  554. "154, 155, 156, 157, 158, 159, 160, 161, 162, 163, "
  555. "164, 165, 166, 167, 168, 169, 170, 171, 172, 173, "
  556. "174, 175, 176, 177, 178, 179, 180, 181, 182, 183, "
  557. "184, 185, 186, 187, 188, 189, 190, 191, 192, 193, "
  558. "194, 195, 196, 197, 198, 199, 200, 201, 202, 203, "
  559. "204, 205, 206, 207, 208, 209, 210, 211, 212, 213, "
  560. "214, 215, 216, 217, 218, 219, 220, 221, 222, 223, "
  561. "224, 225, 226...(truncated)")
  562. self.assertEqual(len(gdb_repr),
  563. 1024 + len('...(truncated)'))
  564. def test_builtin_method(self):
  565. gdb_repr, gdb_output = self.get_gdb_repr('import sys; id(sys.stdout.readlines)')
  566. self.assertTrue(re.match(r'<built-in method readlines of _io.TextIOWrapper object at remote 0x-?[0-9a-f]+>',
  567. gdb_repr),
  568. 'Unexpected gdb representation: %r\n%s' % \
  569. (gdb_repr, gdb_output))
  570. def test_frames(self):
  571. gdb_output = self.get_stack_trace('''
  572. import sys
  573. def foo(a, b, c):
  574. return sys._getframe(0)
  575. f = foo(3, 4, 5)
  576. id(f)''',
  577. breakpoint='builtin_id',
  578. cmds_after_breakpoint=['print (PyFrameObject*)v']
  579. )
  580. self.assertTrue(re.match(r'.*\s+\$1 =\s+Frame 0x-?[0-9a-f]+, for file <string>, line 4, in foo \(a=3.*',
  581. gdb_output,
  582. re.DOTALL),
  583. 'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output))
  584. @unittest.skipIf(python_is_optimized(),
  585. "Python was compiled with optimizations")
  586. class PyListTests(DebuggerTests):
  587. def assertListing(self, expected, actual):
  588. self.assertEndsWith(actual, expected)
  589. def test_basic_command(self):
  590. 'Verify that the "py-list" command works'
  591. bt = self.get_stack_trace(script=self.get_sample_script(),
  592. cmds_after_breakpoint=['py-list'])
  593. self.assertListing(' 5 \n'
  594. ' 6 def bar(a, b, c):\n'
  595. ' 7 baz(a, b, c)\n'
  596. ' 8 \n'
  597. ' 9 def baz(*args):\n'
  598. ' >10 id(42)\n'
  599. ' 11 \n'
  600. ' 12 foo(1, 2, 3)\n',
  601. bt)
  602. def test_one_abs_arg(self):
  603. 'Verify the "py-list" command with one absolute argument'
  604. bt = self.get_stack_trace(script=self.get_sample_script(),
  605. cmds_after_breakpoint=['py-list 9'])
  606. self.assertListing(' 9 def baz(*args):\n'
  607. ' >10 id(42)\n'
  608. ' 11 \n'
  609. ' 12 foo(1, 2, 3)\n',
  610. bt)
  611. def test_two_abs_args(self):
  612. 'Verify the "py-list" command with two absolute arguments'
  613. bt = self.get_stack_trace(script=self.get_sample_script(),
  614. cmds_after_breakpoint=['py-list 1,3'])
  615. self.assertListing(' 1 # Sample script for use by test_gdb.py\n'
  616. ' 2 \n'
  617. ' 3 def foo(a, b, c):\n',
  618. bt)
  619. SAMPLE_WITH_C_CALL = """
  620. from _testcapi import pyobject_fastcall
  621. def foo(a, b, c):
  622. bar(a, b, c)
  623. def bar(a, b, c):
  624. pyobject_fastcall(baz, (a, b, c))
  625. def baz(*args):
  626. id(42)
  627. foo(1, 2, 3)
  628. """
  629. class StackNavigationTests(DebuggerTests):
  630. @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
  631. @unittest.skipIf(python_is_optimized(),
  632. "Python was compiled with optimizations")
  633. def test_pyup_command(self):
  634. 'Verify that the "py-up" command works'
  635. bt = self.get_stack_trace(source=SAMPLE_WITH_C_CALL,
  636. cmds_after_breakpoint=['py-up', 'py-up'])
  637. self.assertMultilineMatches(bt,
  638. r'''^.*
  639. #[0-9]+ Frame 0x-?[0-9a-f]+, for file <string>, line 12, in baz \(args=\(1, 2, 3\)\)
  640. #[0-9]+ <built-in method pyobject_fastcall of module object at remote 0x[0-9a-f]+>
  641. $''')
  642. @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
  643. def test_down_at_bottom(self):
  644. 'Verify handling of "py-down" at the bottom of the stack'
  645. bt = self.get_stack_trace(script=self.get_sample_script(),
  646. cmds_after_breakpoint=['py-down'])
  647. self.assertEndsWith(bt,
  648. 'Unable to find a newer python frame\n')
  649. @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
  650. def test_up_at_top(self):
  651. 'Verify handling of "py-up" at the top of the stack'
  652. bt = self.get_stack_trace(script=self.get_sample_script(),
  653. cmds_after_breakpoint=['py-up'] * 5)
  654. self.assertEndsWith(bt,
  655. 'Unable to find an older python frame\n')
  656. @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
  657. @unittest.skipIf(python_is_optimized(),
  658. "Python was compiled with optimizations")
  659. def test_up_then_down(self):
  660. 'Verify "py-up" followed by "py-down"'
  661. bt = self.get_stack_trace(source=SAMPLE_WITH_C_CALL,
  662. cmds_after_breakpoint=['py-up', 'py-up', 'py-down'])
  663. self.assertMultilineMatches(bt,
  664. r'''^.*
  665. #[0-9]+ Frame 0x-?[0-9a-f]+, for file <string>, line 12, in baz \(args=\(1, 2, 3\)\)
  666. #[0-9]+ <built-in method pyobject_fastcall of module object at remote 0x[0-9a-f]+>
  667. #[0-9]+ Frame 0x-?[0-9a-f]+, for file <string>, line 12, in baz \(args=\(1, 2, 3\)\)
  668. $''')
  669. class PyBtTests(DebuggerTests):
  670. @unittest.skipIf(python_is_optimized(),
  671. "Python was compiled with optimizations")
  672. def test_bt(self):
  673. 'Verify that the "py-bt" command works'
  674. bt = self.get_stack_trace(script=self.get_sample_script(),
  675. cmds_after_breakpoint=['py-bt'])
  676. self.assertMultilineMatches(bt,
  677. r'''^.*
  678. Traceback \(most recent call first\):
  679. <built-in method id of module object .*>
  680. File ".*gdb_sample.py", line 10, in baz
  681. id\(42\)
  682. File ".*gdb_sample.py", line 7, in bar
  683. baz\(a, b, c\)
  684. File ".*gdb_sample.py", line 4, in foo
  685. bar\(a=a, b=b, c=c\)
  686. File ".*gdb_sample.py", line 12, in <module>
  687. foo\(1, 2, 3\)
  688. ''')
  689. @unittest.skipIf(python_is_optimized(),
  690. "Python was compiled with optimizations")
  691. def test_bt_full(self):
  692. 'Verify that the "py-bt-full" command works'
  693. bt = self.get_stack_trace(script=self.get_sample_script(),
  694. cmds_after_breakpoint=['py-bt-full'])
  695. self.assertMultilineMatches(bt,
  696. r'''^.*
  697. #[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
  698. baz\(a, b, c\)
  699. #[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 4, in foo \(a=1, b=2, c=3\)
  700. bar\(a=a, b=b, c=c\)
  701. #[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\)
  702. foo\(1, 2, 3\)
  703. ''')
  704. @unittest.skipIf(python_is_optimized(),
  705. "Python was compiled with optimizations")
  706. def test_threads(self):
  707. 'Verify that "py-bt" indicates threads that are waiting for the GIL'
  708. cmd = '''
  709. from threading import Thread
  710. class TestThread(Thread):
  711. # These threads would run forever, but we'll interrupt things with the
  712. # debugger
  713. def run(self):
  714. i = 0
  715. while 1:
  716. i += 1
  717. t = {}
  718. for i in range(4):
  719. t[i] = TestThread()
  720. t[i].start()
  721. # Trigger a breakpoint on the main thread
  722. id(42)
  723. '''
  724. # Verify with "py-bt":
  725. gdb_output = self.get_stack_trace(cmd,
  726. cmds_after_breakpoint=['thread apply all py-bt'])
  727. self.assertIn('Waiting for the GIL', gdb_output)
  728. # Verify with "py-bt-full":
  729. gdb_output = self.get_stack_trace(cmd,
  730. cmds_after_breakpoint=['thread apply all py-bt-full'])
  731. self.assertIn('Waiting for the GIL', gdb_output)
  732. @unittest.skipIf(python_is_optimized(),
  733. "Python was compiled with optimizations")
  734. # Some older versions of gdb will fail with
  735. # "Cannot find new threads: generic error"
  736. # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
  737. def test_gc(self):
  738. 'Verify that "py-bt" indicates if a thread is garbage-collecting'
  739. cmd = ('from gc import collect\n'
  740. 'id(42)\n'
  741. 'def foo():\n'
  742. ' collect()\n'
  743. 'def bar():\n'
  744. ' foo()\n'
  745. 'bar()\n')
  746. # Verify with "py-bt":
  747. gdb_output = self.get_stack_trace(cmd,
  748. cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt'],
  749. )
  750. self.assertIn('Garbage-collecting', gdb_output)
  751. # Verify with "py-bt-full":
  752. gdb_output = self.get_stack_trace(cmd,
  753. cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt-full'],
  754. )
  755. self.assertIn('Garbage-collecting', gdb_output)
  756. @unittest.skipIf(python_is_optimized(),
  757. "Python was compiled with optimizations")
  758. # Some older versions of gdb will fail with
  759. # "Cannot find new threads: generic error"
  760. # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
  761. #
  762. # gdb will also generate many erroneous errors such as:
  763. # Function "meth_varargs" not defined.
  764. # This is because we are calling functions from an "external" module
  765. # (_testcapimodule) rather than compiled-in functions. It seems difficult
  766. # to suppress these. See also the comment in DebuggerTests.get_stack_trace
  767. def test_pycfunction(self):
  768. 'Verify that "py-bt" displays invocations of PyCFunction instances'
  769. # bpo-46600: If the compiler inlines _null_to_none() in meth_varargs()
  770. # (ex: clang -Og), _null_to_none() is the frame #1. Otherwise,
  771. # meth_varargs() is the frame #1.
  772. expected_frame = r'#(1|2)'
  773. # Various optimizations multiply the code paths by which these are
  774. # called, so test a variety of calling conventions.
  775. for func_name, args in (
  776. ('meth_varargs', ''),
  777. ('meth_varargs_keywords', ''),
  778. ('meth_o', '[]'),
  779. ('meth_noargs', ''),
  780. ('meth_fastcall', ''),
  781. ('meth_fastcall_keywords', ''),
  782. ):
  783. for obj in (
  784. '_testcapi',
  785. '_testcapi.MethClass',
  786. '_testcapi.MethClass()',
  787. '_testcapi.MethStatic()',
  788. # XXX: bound methods don't yet give nice tracebacks
  789. # '_testcapi.MethInstance()',
  790. ):
  791. with self.subTest(f'{obj}.{func_name}'):
  792. cmd = textwrap.dedent(f'''
  793. import _testcapi
  794. def foo():
  795. {obj}.{func_name}({args})
  796. def bar():
  797. foo()
  798. bar()
  799. ''')
  800. # Verify with "py-bt":
  801. gdb_output = self.get_stack_trace(
  802. cmd,
  803. breakpoint=func_name,
  804. cmds_after_breakpoint=['bt', 'py-bt'],
  805. # bpo-45207: Ignore 'Function "meth_varargs" not
  806. # defined.' message in stderr.
  807. ignore_stderr=True,
  808. )
  809. self.assertIn(f'<built-in method {func_name}', gdb_output)
  810. # Verify with "py-bt-full":
  811. gdb_output = self.get_stack_trace(
  812. cmd,
  813. breakpoint=func_name,
  814. cmds_after_breakpoint=['py-bt-full'],
  815. # bpo-45207: Ignore 'Function "meth_varargs" not
  816. # defined.' message in stderr.
  817. ignore_stderr=True,
  818. )
  819. regex = expected_frame
  820. regex += re.escape(f' <built-in method {func_name}')
  821. self.assertRegex(gdb_output, regex)
  822. @unittest.skipIf(python_is_optimized(),
  823. "Python was compiled with optimizations")
  824. def test_wrapper_call(self):
  825. cmd = textwrap.dedent('''
  826. class MyList(list):
  827. def __init__(self):
  828. super().__init__() # wrapper_call()
  829. id("first break point")
  830. l = MyList()
  831. ''')
  832. cmds_after_breakpoint = ['break wrapper_call', 'continue']
  833. if CET_PROTECTION:
  834. # bpo-32962: same case as in get_stack_trace():
  835. # we need an additional 'next' command in order to read
  836. # arguments of the innermost function of the call stack.
  837. cmds_after_breakpoint.append('next')
  838. cmds_after_breakpoint.append('py-bt')
  839. # Verify with "py-bt":
  840. gdb_output = self.get_stack_trace(cmd,
  841. cmds_after_breakpoint=cmds_after_breakpoint)
  842. self.assertRegex(gdb_output,
  843. r"<method-wrapper u?'__init__' of MyList object at ")
  844. class PyPrintTests(DebuggerTests):
  845. @unittest.skipIf(python_is_optimized(),
  846. "Python was compiled with optimizations")
  847. def test_basic_command(self):
  848. 'Verify that the "py-print" command works'
  849. bt = self.get_stack_trace(source=SAMPLE_WITH_C_CALL,
  850. cmds_after_breakpoint=['py-up', 'py-print args'])
  851. self.assertMultilineMatches(bt,
  852. r".*\nlocal 'args' = \(1, 2, 3\)\n.*")
  853. @unittest.skipIf(python_is_optimized(),
  854. "Python was compiled with optimizations")
  855. @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
  856. def test_print_after_up(self):
  857. bt = self.get_stack_trace(source=SAMPLE_WITH_C_CALL,
  858. cmds_after_breakpoint=['py-up', 'py-up', 'py-print c', 'py-print b', 'py-print a'])
  859. self.assertMultilineMatches(bt,
  860. r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*")
  861. @unittest.skipIf(python_is_optimized(),
  862. "Python was compiled with optimizations")
  863. def test_printing_global(self):
  864. bt = self.get_stack_trace(script=self.get_sample_script(),
  865. cmds_after_breakpoint=['py-up', 'py-print __name__'])
  866. self.assertMultilineMatches(bt,
  867. r".*\nglobal '__name__' = '__main__'\n.*")
  868. @unittest.skipIf(python_is_optimized(),
  869. "Python was compiled with optimizations")
  870. def test_printing_builtin(self):
  871. bt = self.get_stack_trace(script=self.get_sample_script(),
  872. cmds_after_breakpoint=['py-up', 'py-print len'])
  873. self.assertMultilineMatches(bt,
  874. r".*\nbuiltin 'len' = <built-in method len of module object at remote 0x-?[0-9a-f]+>\n.*")
  875. class PyLocalsTests(DebuggerTests):
  876. @unittest.skipIf(python_is_optimized(),
  877. "Python was compiled with optimizations")
  878. def test_basic_command(self):
  879. bt = self.get_stack_trace(script=self.get_sample_script(),
  880. cmds_after_breakpoint=['py-up', 'py-locals'])
  881. self.assertMultilineMatches(bt,
  882. r".*\nargs = \(1, 2, 3\)\n.*")
  883. @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
  884. @unittest.skipIf(python_is_optimized(),
  885. "Python was compiled with optimizations")
  886. def test_locals_after_up(self):
  887. bt = self.get_stack_trace(script=self.get_sample_script(),
  888. cmds_after_breakpoint=['py-up', 'py-up', 'py-locals'])
  889. self.assertMultilineMatches(bt,
  890. r'''^.*
  891. Locals for foo
  892. a = 1
  893. b = 2
  894. c = 3
  895. Locals for <module>
  896. .*$''')
  897. def setUpModule():
  898. if support.verbose:
  899. print("GDB version %s.%s:" % (gdb_major_version, gdb_minor_version))
  900. for line in gdb_version.splitlines():
  901. print(" " * 4 + line)
  902. if __name__ == "__main__":
  903. unittest.main()