| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import locale |
| 7 |
import os |
| 8 |
import re |
| 9 |
import subprocess |
| 10 |
import sys |
| 11 |
import sysconfig |
| 12 |
import textwrap |
| 13 |
import unittest |
| 14 |
|
| 15 |
from test import test_support |
| 16 |
from test.test_support import run_unittest, findfile |
| 17 |
|
| 18 |
|
| 19 |
try: |
| 20 |
import thread |
| 21 |
except ImportError: |
| 22 |
thread = None |
| 23 |
|
| 24 |
def get_gdb_version(): |
| 25 |
try: |
| 26 |
proc = subprocess.Popen(["gdb", "-nx", "--version"], |
| 27 |
stdout=subprocess.PIPE, |
| 28 |
stderr=subprocess.PIPE, |
| 29 |
universal_newlines=True) |
| 30 |
version = proc.communicate()[0] |
| 31 |
except OSError: |
| 32 |
|
| 33 |
|
| 34 |
raise unittest.SkipTest("Couldn't find gdb on the path") |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
match = re.search(r"^GNU gdb.*?\b(\d+)\.(\d+)", version) |
| 42 |
if match is None: |
| 43 |
raise Exception("unable to parse GDB version: %r" % version) |
| 44 |
return (version, int(match.group(1)), int(match.group(2))) |
| 45 |
|
| 46 |
gdb_version, gdb_major_version, gdb_minor_version = get_gdb_version() |
| 47 |
if gdb_major_version < 7: |
| 48 |
raise unittest.SkipTest("gdb versions before 7.0 didn't support python " |
| 49 |
"embedding. Saw %s.%s:\n%s" |
| 50 |
% (gdb_major_version, gdb_minor_version, |
| 51 |
gdb_version)) |
| 52 |
|
| 53 |
if sys.platform.startswith("sunos"): |
| 54 |
raise unittest.SkipTest("test doesn't work very well on Solaris") |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
checkout_hook_path = os.path.join(os.path.dirname(sys.executable), |
| 59 |
'python-gdb.py') |
| 60 |
|
| 61 |
|
| 62 |
def cet_protection(): |
| 63 |
cflags = sysconfig.get_config_var('CFLAGS') |
| 64 |
if not cflags: |
| 65 |
return False |
| 66 |
flags = cflags.split() |
| 67 |
|
| 68 |
|
| 69 |
return (('-mcet' in flags) |
| 70 |
and any((flag.startswith('-fcf-protection') |
| 71 |
and not flag.endswith(("=none", "=return"))) |
| 72 |
for flag in flags)) |
| 73 |
|
| 74 |
|
| 75 |
CET_PROTECTION = cet_protection() |
| 76 |
|
| 77 |
|
| 78 |
def run_gdb(*args, **env_vars): |
| 79 |
"""Runs gdb in batch mode with the additional arguments given by *args. |
| 80 |
|
| 81 |
Returns its (stdout, stderr) |
| 82 |
""" |
| 83 |
if env_vars: |
| 84 |
env = os.environ.copy() |
| 85 |
env.update(env_vars) |
| 86 |
else: |
| 87 |
env = None |
| 88 |
|
| 89 |
|
| 90 |
base_cmd = ('gdb', '--batch', '-nx') |
| 91 |
if (gdb_major_version, gdb_minor_version) >= (7, 4): |
| 92 |
base_cmd += ('-iex', 'add-auto-load-safe-path ' + checkout_hook_path) |
| 93 |
out, err = subprocess.Popen(base_cmd + args, |
| 94 |
|
| 95 |
stdin=subprocess.PIPE, |
| 96 |
stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env, |
| 97 |
).communicate() |
| 98 |
return out, err |
| 99 |
|
| 100 |
if not sysconfig.is_python_build(): |
| 101 |
raise unittest.SkipTest("test_gdb only works on source builds at the moment.") |
| 102 |
|
| 103 |
|
| 104 |
gdbpy_version, _ = run_gdb("--eval-command=python import sys; print(sys.version_info)") |
| 105 |
if not gdbpy_version: |
| 106 |
raise unittest.SkipTest("gdb not built with embedded python support") |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
cmd = ['--args', sys.executable] |
| 111 |
_, gdbpy_errors = run_gdb('--args', sys.executable) |
| 112 |
if "auto-loading has been declined" in gdbpy_errors: |
| 113 |
msg = "gdb security settings prevent use of custom hooks: " |
| 114 |
raise unittest.SkipTest(msg + gdbpy_errors.rstrip()) |
| 115 |
|
| 116 |
def python_is_optimized(): |
| 117 |
cflags = sysconfig.get_config_vars()['PY_CFLAGS'] |
| 118 |
final_opt = "" |
| 119 |
for opt in cflags.split(): |
| 120 |
if opt.startswith('-O'): |
| 121 |
final_opt = opt |
| 122 |
return final_opt not in ('', '-O0', '-Og') |
| 123 |
|
| 124 |
def gdb_has_frame_select(): |
| 125 |
|
| 126 |
stdout, _ = run_gdb("--eval-command=python print(dir(gdb.Frame))") |
| 127 |
m = re.match(r'.*\[(.*)\].*', stdout) |
| 128 |
if not m: |
| 129 |
raise unittest.SkipTest("Unable to parse output from gdb.Frame.select test") |
| 130 |
gdb_frame_dir = m.group(1).split(', ') |
| 131 |
return "'select'" in gdb_frame_dir |
| 132 |
|
| 133 |
HAS_PYUP_PYDOWN = gdb_has_frame_select() |
| 134 |
|
| 135 |
class DebuggerTests(unittest.TestCase): |
| 136 |
|
| 137 |
"""Test that the debugger can debug Python.""" |
| 138 |
|
| 139 |
def get_stack_trace(self, source=None, script=None, |
| 140 |
breakpoint='PyObject_Print', |
| 141 |
cmds_after_breakpoint=None, |
| 142 |
import_site=False): |
| 143 |
''' |
| 144 |
Run 'python -c SOURCE' under gdb with a breakpoint. |
| 145 |
|
| 146 |
Support injecting commands after the breakpoint is reached |
| 147 |
|
| 148 |
Returns the stdout from gdb |
| 149 |
|
| 150 |
cmds_after_breakpoint: if provided, a list of strings: gdb commands |
| 151 |
''' |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
commands = ['set breakpoint pending yes', |
| 167 |
'break %s' % breakpoint, |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
'set print address off', |
| 178 |
|
| 179 |
'run'] |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
if (gdb_major_version, gdb_minor_version) >= (7, 4): |
| 188 |
commands += ['set print entry-values no'] |
| 189 |
|
| 190 |
if cmds_after_breakpoint: |
| 191 |
if CET_PROTECTION: |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
commands += ['next'] |
| 197 |
commands += cmds_after_breakpoint |
| 198 |
else: |
| 199 |
commands += ['backtrace'] |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
args = ["gdb", "--batch", "-nx"] |
| 205 |
args += ['--eval-command=%s' % cmd for cmd in commands] |
| 206 |
args += ["--args", |
| 207 |
sys.executable] |
| 208 |
|
| 209 |
if not import_site: |
| 210 |
|
| 211 |
args += ["-S"] |
| 212 |
|
| 213 |
if source: |
| 214 |
args += ["-c", source] |
| 215 |
elif script: |
| 216 |
args += [script] |
| 217 |
|
| 218 |
|
| 219 |
out, err = run_gdb(*args, PYTHONHASHSEED='0') |
| 220 |
|
| 221 |
for line in err.splitlines(): |
| 222 |
print >>sys.stderr, line |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
if "PC not saved" in err: |
| 230 |
raise unittest.SkipTest("gdb cannot walk the frame object" |
| 231 |
" because the Program Counter is" |
| 232 |
" not present") |
| 233 |
|
| 234 |
return out |
| 235 |
|
| 236 |
def get_gdb_repr(self, source, |
| 237 |
cmds_after_breakpoint=None, |
| 238 |
import_site=False): |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
cmds_after_breakpoint = cmds_after_breakpoint or ["backtrace 1"] |
| 251 |
gdb_output = self.get_stack_trace(source, breakpoint='PyObject_Print', |
| 252 |
cmds_after_breakpoint=cmds_after_breakpoint, |
| 253 |
import_site=import_site) |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
m = re.match('.*#0\s+PyObject_Print\s+\(\s*op\=\s*(.*?),\s+fp=.*\).*', |
| 258 |
gdb_output, re.DOTALL) |
| 259 |
if not m: |
| 260 |
self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output)) |
| 261 |
return m.group(1), gdb_output |
| 262 |
|
| 263 |
def assertEndsWith(self, actual, exp_end): |
| 264 |
'''Ensure that the given "actual" string ends with "exp_end"''' |
| 265 |
self.assertTrue(actual.endswith(exp_end), |
| 266 |
msg='%r did not end with %r' % (actual, exp_end)) |
| 267 |
|
| 268 |
def assertMultilineMatches(self, actual, pattern): |
| 269 |
m = re.match(pattern, actual, re.DOTALL) |
| 270 |
self.assertTrue(m, msg='%r did not match %r' % (actual, pattern)) |
| 271 |
|
| 272 |
def get_sample_script(self): |
| 273 |
return findfile('gdb_sample.py') |
| 274 |
|
| 275 |
|
| 276 |
@unittest.skipIf(python_is_optimized(), |
| 277 |
"Python was compiled with optimizations") |
| 278 |
class PrettyPrintTests(DebuggerTests): |
| 279 |
def test_getting_backtrace(self): |
| 280 |
gdb_output = self.get_stack_trace('print 42') |
| 281 |
self.assertTrue('PyObject_Print' in gdb_output) |
| 282 |
|
| 283 |
def assertGdbRepr(self, val, cmds_after_breakpoint=None): |
| 284 |
|
| 285 |
|
| 286 |
gdb_repr, gdb_output = self.get_gdb_repr('print ' + repr(val), |
| 287 |
cmds_after_breakpoint) |
| 288 |
self.assertEqual(gdb_repr, repr(val)) |
| 289 |
|
| 290 |
def test_int(self): |
| 291 |
'Verify the pretty-printing of various "int" values' |
| 292 |
self.assertGdbRepr(42) |
| 293 |
self.assertGdbRepr(0) |
| 294 |
self.assertGdbRepr(-7) |
| 295 |
self.assertGdbRepr(sys.maxint) |
| 296 |
self.assertGdbRepr(-sys.maxint) |
| 297 |
|
| 298 |
def test_long(self): |
| 299 |
'Verify the pretty-printing of various "long" values' |
| 300 |
self.assertGdbRepr(0L) |
| 301 |
self.assertGdbRepr(1000000000000L) |
| 302 |
self.assertGdbRepr(-1L) |
| 303 |
self.assertGdbRepr(-1000000000000000L) |
| 304 |
|
| 305 |
def test_singletons(self): |
| 306 |
'Verify the pretty-printing of True, False and None' |
| 307 |
self.assertGdbRepr(True) |
| 308 |
self.assertGdbRepr(False) |
| 309 |
self.assertGdbRepr(None) |
| 310 |
|
| 311 |
def test_dicts(self): |
| 312 |
'Verify the pretty-printing of dictionaries' |
| 313 |
self.assertGdbRepr({}) |
| 314 |
self.assertGdbRepr({'foo': 'bar'}) |
| 315 |
self.assertGdbRepr("{'foo': 'bar', 'douglas':42}") |
| 316 |
|
| 317 |
def test_lists(self): |
| 318 |
'Verify the pretty-printing of lists' |
| 319 |
self.assertGdbRepr([]) |
| 320 |
self.assertGdbRepr(range(5)) |
| 321 |
|
| 322 |
def test_strings(self): |
| 323 |
'Verify the pretty-printing of strings' |
| 324 |
self.assertGdbRepr('') |
| 325 |
self.assertGdbRepr('And now for something hopefully the same') |
| 326 |
self.assertGdbRepr('string with embedded NUL here \0 and then some more text') |
| 327 |
self.assertGdbRepr('this is byte 255:\xff and byte 128:\x80') |
| 328 |
|
| 329 |
def test_tuples(self): |
| 330 |
'Verify the pretty-printing of tuples' |
| 331 |
self.assertGdbRepr(tuple()) |
| 332 |
self.assertGdbRepr((1,)) |
| 333 |
self.assertGdbRepr(('foo', 'bar', 'baz')) |
| 334 |
|
| 335 |
def test_unicode(self): |
| 336 |
'Verify the pretty-printing of unicode values' |
| 337 |
|
| 338 |
self.assertGdbRepr(u'') |
| 339 |
|
| 340 |
self.assertGdbRepr(u'hello world') |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
self.assertGdbRepr(u'\u2620') |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
self.assertGdbRepr(u'\u6587\u5b57\u5316\u3051') |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
self.assertGdbRepr(u"\U0001D121") |
| 358 |
|
| 359 |
def test_sets(self): |
| 360 |
'Verify the pretty-printing of sets' |
| 361 |
self.assertGdbRepr(set()) |
| 362 |
rep = self.get_gdb_repr("print set(['a', 'b'])")[0] |
| 363 |
self.assertTrue(rep.startswith("set([")) |
| 364 |
self.assertTrue(rep.endswith("])")) |
| 365 |
self.assertEqual(eval(rep), {'a', 'b'}) |
| 366 |
rep = self.get_gdb_repr("print set([4, 5])")[0] |
| 367 |
self.assertTrue(rep.startswith("set([")) |
| 368 |
self.assertTrue(rep.endswith("])")) |
| 369 |
self.assertEqual(eval(rep), {4, 5}) |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
gdb_repr, gdb_output = self.get_gdb_repr('''s = set(['a','b']) |
| 374 |
s.pop() |
| 375 |
print s''') |
| 376 |
self.assertEqual(gdb_repr, "set(['b'])") |
| 377 |
|
| 378 |
def test_frozensets(self): |
| 379 |
'Verify the pretty-printing of frozensets' |
| 380 |
self.assertGdbRepr(frozenset()) |
| 381 |
rep = self.get_gdb_repr("print frozenset(['a', 'b'])")[0] |
| 382 |
self.assertTrue(rep.startswith("frozenset([")) |
| 383 |
self.assertTrue(rep.endswith("])")) |
| 384 |
self.assertEqual(eval(rep), {'a', 'b'}) |
| 385 |
rep = self.get_gdb_repr("print frozenset([4, 5])")[0] |
| 386 |
self.assertTrue(rep.startswith("frozenset([")) |
| 387 |
self.assertTrue(rep.endswith("])")) |
| 388 |
self.assertEqual(eval(rep), {4, 5}) |
| 389 |
|
| 390 |
def test_exceptions(self): |
| 391 |
|
| 392 |
gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 393 |
try: |
| 394 |
raise RuntimeError("I am an error") |
| 395 |
except RuntimeError, e: |
| 396 |
print e |
| 397 |
''') |
| 398 |
self.assertEqual(gdb_repr, |
| 399 |
"exceptions.RuntimeError('I am an error',)") |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 404 |
try: |
| 405 |
a = 1 // 0 |
| 406 |
except ZeroDivisionError, e: |
| 407 |
print e |
| 408 |
''') |
| 409 |
self.assertEqual(gdb_repr, |
| 410 |
"exceptions.ZeroDivisionError('integer division or modulo by zero',)") |
| 411 |
|
| 412 |
def test_classic_class(self): |
| 413 |
'Verify the pretty-printing of classic class instances' |
| 414 |
gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 415 |
class Foo: |
| 416 |
pass |
| 417 |
foo = Foo() |
| 418 |
foo.an_int = 42 |
| 419 |
print foo''') |
| 420 |
m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr) |
| 421 |
self.assertTrue(m, |
| 422 |
msg='Unexpected classic-class rendering %r' % gdb_repr) |
| 423 |
|
| 424 |
def test_modern_class(self): |
| 425 |
'Verify the pretty-printing of new-style class instances' |
| 426 |
gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 427 |
class Foo(object): |
| 428 |
pass |
| 429 |
foo = Foo() |
| 430 |
foo.an_int = 42 |
| 431 |
print foo''') |
| 432 |
m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr) |
| 433 |
self.assertTrue(m, |
| 434 |
msg='Unexpected new-style class rendering %r' % gdb_repr) |
| 435 |
|
| 436 |
def test_subclassing_list(self): |
| 437 |
'Verify the pretty-printing of an instance of a list subclass' |
| 438 |
gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 439 |
class Foo(list): |
| 440 |
pass |
| 441 |
foo = Foo() |
| 442 |
foo += [1, 2, 3] |
| 443 |
foo.an_int = 42 |
| 444 |
print foo''') |
| 445 |
m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr) |
| 446 |
self.assertTrue(m, |
| 447 |
msg='Unexpected new-style class rendering %r' % gdb_repr) |
| 448 |
|
| 449 |
def test_subclassing_tuple(self): |
| 450 |
'Verify the pretty-printing of an instance of a tuple subclass' |
| 451 |
|
| 452 |
|
| 453 |
gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 454 |
class Foo(tuple): |
| 455 |
pass |
| 456 |
foo = Foo((1, 2, 3)) |
| 457 |
foo.an_int = 42 |
| 458 |
print foo''') |
| 459 |
m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr) |
| 460 |
self.assertTrue(m, |
| 461 |
msg='Unexpected new-style class rendering %r' % gdb_repr) |
| 462 |
|
| 463 |
def assertSane(self, source, corruption, expvalue=None, exptype=None): |
| 464 |
'''Run Python under gdb, corrupting variables in the inferior process |
| 465 |
immediately before taking a backtrace. |
| 466 |
|
| 467 |
Verify that the variable's representation is the expected failsafe |
| 468 |
representation''' |
| 469 |
if corruption: |
| 470 |
cmds_after_breakpoint=[corruption, 'backtrace'] |
| 471 |
else: |
| 472 |
cmds_after_breakpoint=['backtrace'] |
| 473 |
|
| 474 |
gdb_repr, gdb_output = \ |
| 475 |
self.get_gdb_repr(source, |
| 476 |
cmds_after_breakpoint=cmds_after_breakpoint) |
| 477 |
|
| 478 |
if expvalue: |
| 479 |
if gdb_repr == repr(expvalue): |
| 480 |
|
| 481 |
|
| 482 |
return |
| 483 |
|
| 484 |
if exptype: |
| 485 |
pattern = '<' + exptype + ' at remote 0x[0-9a-f]+>' |
| 486 |
else: |
| 487 |
|
| 488 |
|
| 489 |
pattern = '<.* at remote 0x[0-9a-f]+>' |
| 490 |
|
| 491 |
m = re.match(pattern, gdb_repr) |
| 492 |
if not m: |
| 493 |
self.fail('Unexpected gdb representation: %r\n%s' % \ |
| 494 |
(gdb_repr, gdb_output)) |
| 495 |
|
| 496 |
def test_NULL_ptr(self): |
| 497 |
'Ensure that a NULL PyObject* is handled gracefully' |
| 498 |
gdb_repr, gdb_output = ( |
| 499 |
self.get_gdb_repr('print 42', |
| 500 |
cmds_after_breakpoint=['set variable op=0', |
| 501 |
'backtrace']) |
| 502 |
) |
| 503 |
|
| 504 |
self.assertEqual(gdb_repr, '0x0') |
| 505 |
|
| 506 |
def test_NULL_ob_type(self): |
| 507 |
'Ensure that a PyObject* with NULL ob_type is handled gracefully' |
| 508 |
self.assertSane('print 42', |
| 509 |
'set op->ob_type=0') |
| 510 |
|
| 511 |
def test_corrupt_ob_type(self): |
| 512 |
'Ensure that a PyObject* with a corrupt ob_type is handled gracefully' |
| 513 |
self.assertSane('print 42', |
| 514 |
'set op->ob_type=0xDEADBEEF', |
| 515 |
expvalue=42) |
| 516 |
|
| 517 |
def test_corrupt_tp_flags(self): |
| 518 |
'Ensure that a PyObject* with a type with corrupt tp_flags is handled' |
| 519 |
self.assertSane('print 42', |
| 520 |
'set op->ob_type->tp_flags=0x0', |
| 521 |
expvalue=42) |
| 522 |
|
| 523 |
def test_corrupt_tp_name(self): |
| 524 |
'Ensure that a PyObject* with a type with corrupt tp_name is handled' |
| 525 |
self.assertSane('print 42', |
| 526 |
'set op->ob_type->tp_name=0xDEADBEEF', |
| 527 |
expvalue=42) |
| 528 |
|
| 529 |
def test_NULL_instance_dict(self): |
| 530 |
'Ensure that a PyInstanceObject with with a NULL in_dict is handled' |
| 531 |
self.assertSane(''' |
| 532 |
class Foo: |
| 533 |
pass |
| 534 |
foo = Foo() |
| 535 |
foo.an_int = 42 |
| 536 |
print foo''', |
| 537 |
'set ((PyInstanceObject*)op)->in_dict = 0', |
| 538 |
exptype='Foo') |
| 539 |
|
| 540 |
def test_builtins_help(self): |
| 541 |
'Ensure that the new-style class _Helper in site.py can be handled' |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
gdb_repr, gdb_output = self.get_gdb_repr('print __builtins__.help', import_site=True) |
| 546 |
m = re.match(r'<_Helper at remote 0x[0-9a-f]+>', gdb_repr) |
| 547 |
self.assertTrue(m, |
| 548 |
msg='Unexpected rendering %r' % gdb_repr) |
| 549 |
|
| 550 |
def test_selfreferential_list(self): |
| 551 |
'''Ensure that a reference loop involving a list doesn't lead proxyval |
| 552 |
into an infinite loop:''' |
| 553 |
gdb_repr, gdb_output = \ |
| 554 |
self.get_gdb_repr("a = [3, 4, 5] ; a.append(a) ; print a") |
| 555 |
|
| 556 |
self.assertEqual(gdb_repr, '[3, 4, 5, [...]]') |
| 557 |
|
| 558 |
gdb_repr, gdb_output = \ |
| 559 |
self.get_gdb_repr("a = [3, 4, 5] ; b = [a] ; a.append(b) ; print a") |
| 560 |
|
| 561 |
self.assertEqual(gdb_repr, '[3, 4, 5, [[...]]]') |
| 562 |
|
| 563 |
def test_selfreferential_dict(self): |
| 564 |
'''Ensure that a reference loop involving a dict doesn't lead proxyval |
| 565 |
into an infinite loop:''' |
| 566 |
gdb_repr, gdb_output = \ |
| 567 |
self.get_gdb_repr("a = {} ; b = {'bar':a} ; a['foo'] = b ; print a") |
| 568 |
|
| 569 |
self.assertEqual(gdb_repr, "{'foo': {'bar': {...}}}") |
| 570 |
|
| 571 |
def test_selfreferential_old_style_instance(self): |
| 572 |
gdb_repr, gdb_output = \ |
| 573 |
self.get_gdb_repr(''' |
| 574 |
class Foo: |
| 575 |
pass |
| 576 |
foo = Foo() |
| 577 |
foo.an_attr = foo |
| 578 |
print foo''') |
| 579 |
self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>', |
| 580 |
gdb_repr), |
| 581 |
'Unexpected gdb representation: %r\n%s' % \ |
| 582 |
(gdb_repr, gdb_output)) |
| 583 |
|
| 584 |
def test_selfreferential_new_style_instance(self): |
| 585 |
gdb_repr, gdb_output = \ |
| 586 |
self.get_gdb_repr(''' |
| 587 |
class Foo(object): |
| 588 |
pass |
| 589 |
foo = Foo() |
| 590 |
foo.an_attr = foo |
| 591 |
print foo''') |
| 592 |
self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>', |
| 593 |
gdb_repr), |
| 594 |
'Unexpected gdb representation: %r\n%s' % \ |
| 595 |
(gdb_repr, gdb_output)) |
| 596 |
|
| 597 |
gdb_repr, gdb_output = \ |
| 598 |
self.get_gdb_repr(''' |
| 599 |
class Foo(object): |
| 600 |
pass |
| 601 |
a = Foo() |
| 602 |
b = Foo() |
| 603 |
a.an_attr = b |
| 604 |
b.an_attr = a |
| 605 |
print a''') |
| 606 |
self.assertTrue(re.match('<Foo\(an_attr=<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>\) at remote 0x[0-9a-f]+>', |
| 607 |
gdb_repr), |
| 608 |
'Unexpected gdb representation: %r\n%s' % \ |
| 609 |
(gdb_repr, gdb_output)) |
| 610 |
|
| 611 |
def test_truncation(self): |
| 612 |
'Verify that very long output is truncated' |
| 613 |
gdb_repr, gdb_output = self.get_gdb_repr('print range(1000)') |
| 614 |
self.assertEqual(gdb_repr, |
| 615 |
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, " |
| 616 |
"14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, " |
| 617 |
"27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, " |
| 618 |
"40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, " |
| 619 |
"53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, " |
| 620 |
"66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, " |
| 621 |
"79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, " |
| 622 |
"92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, " |
| 623 |
"104, 105, 106, 107, 108, 109, 110, 111, 112, 113, " |
| 624 |
"114, 115, 116, 117, 118, 119, 120, 121, 122, 123, " |
| 625 |
"124, 125, 126, 127, 128, 129, 130, 131, 132, 133, " |
| 626 |
"134, 135, 136, 137, 138, 139, 140, 141, 142, 143, " |
| 627 |
"144, 145, 146, 147, 148, 149, 150, 151, 152, 153, " |
| 628 |
"154, 155, 156, 157, 158, 159, 160, 161, 162, 163, " |
| 629 |
"164, 165, 166, 167, 168, 169, 170, 171, 172, 173, " |
| 630 |
"174, 175, 176, 177, 178, 179, 180, 181, 182, 183, " |
| 631 |
"184, 185, 186, 187, 188, 189, 190, 191, 192, 193, " |
| 632 |
"194, 195, 196, 197, 198, 199, 200, 201, 202, 203, " |
| 633 |
"204, 205, 206, 207, 208, 209, 210, 211, 212, 213, " |
| 634 |
"214, 215, 216, 217, 218, 219, 220, 221, 222, 223, " |
| 635 |
"224, 225, 226...(truncated)") |
| 636 |
self.assertEqual(len(gdb_repr), |
| 637 |
1024 + len('...(truncated)')) |
| 638 |
|
| 639 |
def test_builtin_function(self): |
| 640 |
gdb_repr, gdb_output = self.get_gdb_repr('print len') |
| 641 |
self.assertEqual(gdb_repr, '<built-in function len>') |
| 642 |
|
| 643 |
def test_builtin_method(self): |
| 644 |
gdb_repr, gdb_output = self.get_gdb_repr('import sys; print sys.stdout.readlines') |
| 645 |
self.assertTrue(re.match('<built-in method readlines of file object at remote 0x[0-9a-f]+>', |
| 646 |
gdb_repr), |
| 647 |
'Unexpected gdb representation: %r\n%s' % \ |
| 648 |
(gdb_repr, gdb_output)) |
| 649 |
|
| 650 |
def test_frames(self): |
| 651 |
gdb_output = self.get_stack_trace(''' |
| 652 |
def foo(a, b, c): |
| 653 |
pass |
| 654 |
|
| 655 |
foo(3, 4, 5) |
| 656 |
print foo.__code__''', |
| 657 |
breakpoint='PyObject_Print', |
| 658 |
cmds_after_breakpoint=['print (PyFrameObject*)(((PyCodeObject*)op)->co_zombieframe)'] |
| 659 |
) |
| 660 |
self.assertTrue(re.match(r'.*\s+\$1 =\s+Frame 0x[0-9a-f]+, for file <string>, line 3, in foo \(\)\s+.*', |
| 661 |
gdb_output, |
| 662 |
re.DOTALL), |
| 663 |
'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output)) |
| 664 |
|
| 665 |
@unittest.skipIf(python_is_optimized(), |
| 666 |
"Python was compiled with optimizations") |
| 667 |
class PyListTests(DebuggerTests): |
| 668 |
def assertListing(self, expected, actual): |
| 669 |
self.assertEndsWith(actual, expected) |
| 670 |
|
| 671 |
def test_basic_command(self): |
| 672 |
'Verify that the "py-list" command works' |
| 673 |
bt = self.get_stack_trace(script=self.get_sample_script(), |
| 674 |
cmds_after_breakpoint=['py-list']) |
| 675 |
|
| 676 |
self.assertListing(' 5 \n' |
| 677 |
' 6 def bar(a, b, c):\n' |
| 678 |
' 7 baz(a, b, c)\n' |
| 679 |
' 8 \n' |
| 680 |
' 9 def baz(*args):\n' |
| 681 |
' >10 print(42)\n' |
| 682 |
' 11 \n' |
| 683 |
' 12 foo(1, 2, 3)\n', |
| 684 |
bt) |
| 685 |
|
| 686 |
def test_one_abs_arg(self): |
| 687 |
'Verify the "py-list" command with one absolute argument' |
| 688 |
bt = self.get_stack_trace(script=self.get_sample_script(), |
| 689 |
cmds_after_breakpoint=['py-list 9']) |
| 690 |
|
| 691 |
self.assertListing(' 9 def baz(*args):\n' |
| 692 |
' >10 print(42)\n' |
| 693 |
' 11 \n' |
| 694 |
' 12 foo(1, 2, 3)\n', |
| 695 |
bt) |
| 696 |
|
| 697 |
def test_two_abs_args(self): |
| 698 |
'Verify the "py-list" command with two absolute arguments' |
| 699 |
bt = self.get_stack_trace(script=self.get_sample_script(), |
| 700 |
cmds_after_breakpoint=['py-list 1,3']) |
| 701 |
|
| 702 |
self.assertListing(' 1 # Sample script for use by test_gdb.py\n' |
| 703 |
' 2 \n' |
| 704 |
' 3 def foo(a, b, c):\n', |
| 705 |
bt) |
| 706 |
|
| 707 |
class StackNavigationTests(DebuggerTests): |
| 708 |
@unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
| 709 |
@unittest.skipIf(python_is_optimized(), |
| 710 |
"Python was compiled with optimizations") |
| 711 |
def test_pyup_command(self): |
| 712 |
'Verify that the "py-up" command works' |
| 713 |
bt = self.get_stack_trace(script=self.get_sample_script(), |
| 714 |
cmds_after_breakpoint=['py-up']) |
| 715 |
self.assertMultilineMatches(bt, |
| 716 |
r'''^.* |
| 717 |
#[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\) |
| 718 |
baz\(a, b, c\) |
| 719 |
$''') |
| 720 |
|
| 721 |
@unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
| 722 |
def test_down_at_bottom(self): |
| 723 |
'Verify handling of "py-down" at the bottom of the stack' |
| 724 |
bt = self.get_stack_trace(script=self.get_sample_script(), |
| 725 |
cmds_after_breakpoint=['py-down']) |
| 726 |
self.assertEndsWith(bt, |
| 727 |
'Unable to find a newer python frame\n') |
| 728 |
|
| 729 |
@unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
| 730 |
@unittest.skipIf(python_is_optimized(), |
| 731 |
"Python was compiled with optimizations") |
| 732 |
def test_up_at_top(self): |
| 733 |
'Verify handling of "py-up" at the top of the stack' |
| 734 |
bt = self.get_stack_trace(script=self.get_sample_script(), |
| 735 |
cmds_after_breakpoint=['py-up'] * 4) |
| 736 |
self.assertEndsWith(bt, |
| 737 |
'Unable to find an older python frame\n') |
| 738 |
|
| 739 |
@unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
| 740 |
@unittest.skipIf(python_is_optimized(), |
| 741 |
"Python was compiled with optimizations") |
| 742 |
def test_up_then_down(self): |
| 743 |
'Verify "py-up" followed by "py-down"' |
| 744 |
bt = self.get_stack_trace(script=self.get_sample_script(), |
| 745 |
cmds_after_breakpoint=['py-up', 'py-down']) |
| 746 |
self.assertMultilineMatches(bt, |
| 747 |
r'''^.* |
| 748 |
#[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\) |
| 749 |
baz\(a, b, c\) |
| 750 |
#[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 10, in baz \(args=\(1, 2, 3\)\) |
| 751 |
print\(42\) |
| 752 |
$''') |
| 753 |
|
| 754 |
class PyBtTests(DebuggerTests): |
| 755 |
@unittest.skipIf(python_is_optimized(), |
| 756 |
"Python was compiled with optimizations") |
| 757 |
def test_bt(self): |
| 758 |
'Verify that the "py-bt" command works' |
| 759 |
bt = self.get_stack_trace(script=self.get_sample_script(), |
| 760 |
cmds_after_breakpoint=['py-bt']) |
| 761 |
self.assertMultilineMatches(bt, |
| 762 |
r'''^.* |
| 763 |
Traceback \(most recent call first\): |
| 764 |
File ".*gdb_sample.py", line 10, in baz |
| 765 |
print\(42\) |
| 766 |
File ".*gdb_sample.py", line 7, in bar |
| 767 |
baz\(a, b, c\) |
| 768 |
File ".*gdb_sample.py", line 4, in foo |
| 769 |
bar\(a, b, c\) |
| 770 |
File ".*gdb_sample.py", line 12, in <module> |
| 771 |
foo\(1, 2, 3\) |
| 772 |
''') |
| 773 |
|
| 774 |
@unittest.skipIf(python_is_optimized(), |
| 775 |
"Python was compiled with optimizations") |
| 776 |
def test_bt_full(self): |
| 777 |
'Verify that the "py-bt-full" command works' |
| 778 |
bt = self.get_stack_trace(script=self.get_sample_script(), |
| 779 |
cmds_after_breakpoint=['py-bt-full']) |
| 780 |
self.assertMultilineMatches(bt, |
| 781 |
r'''^.* |
| 782 |
#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\) |
| 783 |
baz\(a, b, c\) |
| 784 |
#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 4, in foo \(a=1, b=2, c=3\) |
| 785 |
bar\(a, b, c\) |
| 786 |
#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\) |
| 787 |
foo\(1, 2, 3\) |
| 788 |
''') |
| 789 |
|
| 790 |
@unittest.skipUnless(thread, |
| 791 |
"Python was compiled without thread support") |
| 792 |
@unittest.skipIf(python_is_optimized(), |
| 793 |
"Python was compiled with optimizations") |
| 794 |
def test_threads(self): |
| 795 |
'Verify that "py-bt" indicates threads that are waiting for the GIL' |
| 796 |
cmd = ''' |
| 797 |
from threading import Thread |
| 798 |
|
| 799 |
class TestThread(Thread): |
| 800 |
# These threads would run forever, but we'll interrupt things with the |
| 801 |
# debugger |
| 802 |
def run(self): |
| 803 |
i = 0 |
| 804 |
while 1: |
| 805 |
i += 1 |
| 806 |
|
| 807 |
t = {} |
| 808 |
for i in range(4): |
| 809 |
t[i] = TestThread() |
| 810 |
t[i].start() |
| 811 |
|
| 812 |
# Trigger a breakpoint on the main thread |
| 813 |
print 42 |
| 814 |
|
| 815 |
''' |
| 816 |
|
| 817 |
gdb_output = self.get_stack_trace(cmd, |
| 818 |
cmds_after_breakpoint=['thread apply all py-bt']) |
| 819 |
self.assertIn('Waiting for the GIL', gdb_output) |
| 820 |
|
| 821 |
|
| 822 |
gdb_output = self.get_stack_trace(cmd, |
| 823 |
cmds_after_breakpoint=['thread apply all py-bt-full']) |
| 824 |
self.assertIn('Waiting for the GIL', gdb_output) |
| 825 |
|
| 826 |
@unittest.skipIf(python_is_optimized(), |
| 827 |
"Python was compiled with optimizations") |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
@unittest.skipUnless(thread, |
| 832 |
"Python was compiled without thread support") |
| 833 |
def test_gc(self): |
| 834 |
'Verify that "py-bt" indicates if a thread is garbage-collecting' |
| 835 |
cmd = ('from gc import collect\n' |
| 836 |
'print 42\n' |
| 837 |
'def foo():\n' |
| 838 |
' collect()\n' |
| 839 |
'def bar():\n' |
| 840 |
' foo()\n' |
| 841 |
'bar()\n') |
| 842 |
|
| 843 |
gdb_output = self.get_stack_trace(cmd, |
| 844 |
cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt'], |
| 845 |
) |
| 846 |
self.assertIn('Garbage-collecting', gdb_output) |
| 847 |
|
| 848 |
|
| 849 |
gdb_output = self.get_stack_trace(cmd, |
| 850 |
cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt-full'], |
| 851 |
) |
| 852 |
self.assertIn('Garbage-collecting', gdb_output) |
| 853 |
|
| 854 |
@unittest.skipIf(python_is_optimized(), |
| 855 |
"Python was compiled with optimizations") |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
@unittest.skipUnless(thread, |
| 860 |
"Python was compiled without thread support") |
| 861 |
def test_pycfunction(self): |
| 862 |
'Verify that "py-bt" displays invocations of PyCFunction instances' |
| 863 |
|
| 864 |
|
| 865 |
cmd = ('from time import gmtime\n' |
| 866 |
'def foo():\n' |
| 867 |
' gmtime(1)\n' |
| 868 |
'def bar():\n' |
| 869 |
' foo()\n' |
| 870 |
'bar()\n') |
| 871 |
|
| 872 |
gdb_output = self.get_stack_trace(cmd, |
| 873 |
breakpoint='time_gmtime', |
| 874 |
cmds_after_breakpoint=['bt', 'py-bt'], |
| 875 |
) |
| 876 |
self.assertIn('<built-in function gmtime', gdb_output) |
| 877 |
|
| 878 |
|
| 879 |
gdb_output = self.get_stack_trace(cmd, |
| 880 |
breakpoint='time_gmtime', |
| 881 |
cmds_after_breakpoint=['py-bt-full'], |
| 882 |
) |
| 883 |
self.assertIn('#1 <built-in function gmtime', gdb_output) |
| 884 |
|
| 885 |
@unittest.skipIf(python_is_optimized(), |
| 886 |
"Python was compiled with optimizations") |
| 887 |
def test_wrapper_call(self): |
| 888 |
cmd = textwrap.dedent(''' |
| 889 |
class MyList(list): |
| 890 |
def __init__(self): |
| 891 |
super(MyList, self).__init__() # wrapper_call() |
| 892 |
|
| 893 |
print("first break point") |
| 894 |
l = MyList() |
| 895 |
''') |
| 896 |
cmds_after_breakpoint = ['break wrapper_call', 'continue'] |
| 897 |
if CET_PROTECTION: |
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
cmds_after_breakpoint.append('next') |
| 902 |
cmds_after_breakpoint.append('py-bt') |
| 903 |
|
| 904 |
|
| 905 |
gdb_output = self.get_stack_trace(cmd, |
| 906 |
cmds_after_breakpoint=cmds_after_breakpoint) |
| 907 |
self.assertRegexpMatches(gdb_output, |
| 908 |
r"<method-wrapper u?'__init__' of MyList object at ") |
| 909 |
|
| 910 |
|
| 911 |
class PyPrintTests(DebuggerTests): |
| 912 |
@unittest.skipIf(python_is_optimized(), |
| 913 |
"Python was compiled with optimizations") |
| 914 |
def test_basic_command(self): |
| 915 |
'Verify that the "py-print" command works' |
| 916 |
bt = self.get_stack_trace(script=self.get_sample_script(), |
| 917 |
cmds_after_breakpoint=['py-print args']) |
| 918 |
self.assertMultilineMatches(bt, |
| 919 |
r".*\nlocal 'args' = \(1, 2, 3\)\n.*") |
| 920 |
|
| 921 |
@unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
| 922 |
@unittest.skipIf(python_is_optimized(), |
| 923 |
"Python was compiled with optimizations") |
| 924 |
def test_print_after_up(self): |
| 925 |
bt = self.get_stack_trace(script=self.get_sample_script(), |
| 926 |
cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a']) |
| 927 |
self.assertMultilineMatches(bt, |
| 928 |
r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*") |
| 929 |
|
| 930 |
@unittest.skipIf(python_is_optimized(), |
| 931 |
"Python was compiled with optimizations") |
| 932 |
def test_printing_global(self): |
| 933 |
bt = self.get_stack_trace(script=self.get_sample_script(), |
| 934 |
cmds_after_breakpoint=['py-print __name__']) |
| 935 |
self.assertMultilineMatches(bt, |
| 936 |
r".*\nglobal '__name__' = '__main__'\n.*") |
| 937 |
|
| 938 |
@unittest.skipIf(python_is_optimized(), |
| 939 |
"Python was compiled with optimizations") |
| 940 |
def test_printing_builtin(self): |
| 941 |
bt = self.get_stack_trace(script=self.get_sample_script(), |
| 942 |
cmds_after_breakpoint=['py-print len']) |
| 943 |
self.assertMultilineMatches(bt, |
| 944 |
r".*\nbuiltin 'len' = <built-in function len>\n.*") |
| 945 |
|
| 946 |
class PyLocalsTests(DebuggerTests): |
| 947 |
@unittest.skipIf(python_is_optimized(), |
| 948 |
"Python was compiled with optimizations") |
| 949 |
def test_basic_command(self): |
| 950 |
bt = self.get_stack_trace(script=self.get_sample_script(), |
| 951 |
cmds_after_breakpoint=['py-locals']) |
| 952 |
self.assertMultilineMatches(bt, |
| 953 |
r".*\nargs = \(1, 2, 3\)\n.*") |
| 954 |
|
| 955 |
@unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
| 956 |
@unittest.skipIf(python_is_optimized(), |
| 957 |
"Python was compiled with optimizations") |
| 958 |
def test_locals_after_up(self): |
| 959 |
bt = self.get_stack_trace(script=self.get_sample_script(), |
| 960 |
cmds_after_breakpoint=['py-up', 'py-locals']) |
| 961 |
self.assertMultilineMatches(bt, |
| 962 |
r".*\na = 1\nb = 2\nc = 3\n.*") |
| 963 |
|
| 964 |
def test_main(): |
| 965 |
if test_support.verbose: |
| 966 |
print("GDB version %s.%s:" % (gdb_major_version, gdb_minor_version)) |
| 967 |
for line in gdb_version.splitlines(): |
| 968 |
print(" " * 4 + line) |
| 969 |
run_unittest(PrettyPrintTests, |
| 970 |
PyListTests, |
| 971 |
StackNavigationTests, |
| 972 |
PyBtTests, |
| 973 |
PyPrintTests, |
| 974 |
PyLocalsTests |
| 975 |
) |
| 976 |
|
| 977 |
if __name__ == "__main__": |
| 978 |
test_main() |