Back to index

python3.2  3.2.2
Public Member Functions
test.test_gdb.PyBtTests Class Reference
Inheritance diagram for test.test_gdb.PyBtTests:
Inheritance graph
[legend]
Collaboration diagram for test.test_gdb.PyBtTests:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_bt
def test_bt_full
def run_gdb
def get_stack_trace
def get_gdb_repr
def assertEndsWith
def assertMultilineMatches
def get_sample_script

Detailed Description

Definition at line 613 of file test_gdb.py.


Member Function Documentation

def test.test_gdb.DebuggerTests.assertEndsWith (   self,
  actual,
  exp_end 
) [inherited]
Ensure that the given "actual" string ends with "exp_end"

Definition at line 163 of file test_gdb.py.

00163 
00164     def assertEndsWith(self, actual, exp_end):
00165         '''Ensure that the given "actual" string ends with "exp_end"'''
00166         self.assertTrue(actual.endswith(exp_end),
00167                         msg='%r did not end with %r' % (actual, exp_end))

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_gdb.DebuggerTests.assertMultilineMatches (   self,
  actual,
  pattern 
) [inherited]

Definition at line 168 of file test_gdb.py.

00168 
00169     def assertMultilineMatches(self, actual, pattern):
00170         m = re.match(pattern, actual, re.DOTALL)
00171         if not m:
00172             self.fail(msg='%r did not match %r' % (actual, pattern))

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_gdb.DebuggerTests.get_gdb_repr (   self,
  source,
  cmds_after_breakpoint = None,
  import_site = False 
) [inherited]

Definition at line 141 of file test_gdb.py.

00141 
00142                      import_site=False):
00143         # Given an input python source representation of data,
00144         # run "python -c'id(DATA)'" under gdb with a breakpoint on
00145         # builtin_id and scrape out gdb's representation of the "op"
00146         # parameter, and verify that the gdb displays the same string
00147         #
00148         # Verify that the gdb displays the expected string
00149         #
00150         # For a nested structure, the first time we hit the breakpoint will
00151         # give us the top-level structure
00152         gdb_output = self.get_stack_trace(source, breakpoint=BREAKPOINT_FN,
00153                                           cmds_after_breakpoint=cmds_after_breakpoint,
00154                                           import_site=import_site)
00155         # gdb can insert additional '\n' and space characters in various places
00156         # in its output, depending on the width of the terminal it's connected
00157         # to (using its "wrap_here" function)
00158         m = re.match('.*#0\s+builtin_id\s+\(self\=.*,\s+v=\s*(.*?)\)\s+at\s+Python/bltinmodule.c.*',
00159                      gdb_output, re.DOTALL)
00160         if not m:
00161             self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output))
00162         return m.group(1), gdb_output

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 173 of file test_gdb.py.

00173 
00174     def get_sample_script(self):
00175         return findfile('gdb_sample.py')

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_gdb.DebuggerTests.get_stack_trace (   self,
  source = None,
  script = None,
  breakpoint = BREAKPOINT_FN,
  cmds_after_breakpoint = None,
  import_site = False 
) [inherited]
Run 'python -c SOURCE' under gdb with a breakpoint.

Support injecting commands after the breakpoint is reached

Returns the stdout from gdb

cmds_after_breakpoint: if provided, a list of strings: gdb commands

Definition at line 68 of file test_gdb.py.

00068 
00069                         import_site=False):
00070         '''
00071         Run 'python -c SOURCE' under gdb with a breakpoint.
00072 
00073         Support injecting commands after the breakpoint is reached
00074 
00075         Returns the stdout from gdb
00076 
00077         cmds_after_breakpoint: if provided, a list of strings: gdb commands
00078         '''
00079         # We use "set breakpoint pending yes" to avoid blocking with a:
00080         #   Function "foo" not defined.
00081         #   Make breakpoint pending on future shared library load? (y or [n])
00082         # error, which typically happens python is dynamically linked (the
00083         # breakpoints of interest are to be found in the shared library)
00084         # When this happens, we still get:
00085         #   Function "textiowrapper_write" not defined.
00086         # emitted to stderr each time, alas.
00087 
00088         # Initially I had "--eval-command=continue" here, but removed it to
00089         # avoid repeated print breakpoints when traversing hierarchical data
00090         # structures
00091 
00092         # Generate a list of commands in gdb's language:
00093         commands = ['set breakpoint pending yes',
00094                     'break %s' % breakpoint,
00095                     'run']
00096         if cmds_after_breakpoint:
00097             commands += cmds_after_breakpoint
00098         else:
00099             commands += ['backtrace']
00100 
00101         # print commands
00102 
00103         # Use "commands" to generate the arguments with which to invoke "gdb":
00104         args = ["gdb", "--batch"]
00105         args += ['--eval-command=%s' % cmd for cmd in commands]
00106         args += ["--args",
00107                  sys.executable]
00108 
00109         if not import_site:
00110             # -S suppresses the default 'import site'
00111             args += ["-S"]
00112 
00113         if source:
00114             args += ["-c", source]
00115         elif script:
00116             args += [script]
00117 
00118         # print args
00119         # print ' '.join(args)
00120 
00121         # Use "args" to invoke gdb, capturing stdout, stderr:
00122         out, err = self.run_gdb(*args)
00123 
00124         # Ignore some noise on stderr due to the pending breakpoint:
00125         err = err.replace('Function "%s" not defined.\n' % breakpoint, '')
00126         # Ignore some other noise on stderr (http://bugs.python.org/issue8600)
00127         err = err.replace("warning: Unable to find libthread_db matching"
00128                           " inferior's thread library, thread debugging will"
00129                           " not be available.\n",
00130                           '')
00131         err = err.replace("warning: Cannot initialize thread debugging"
00132                           " library: Debugger service failed\n",
00133                           '')
00134 
00135         # Ensure no unexpected error messages:
00136         self.assertEqual(err, '')
00137 
00138         return out

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_gdb.DebuggerTests.run_gdb (   self,
  args 
) [inherited]
Runs gdb with the command line given by *args.

Returns its stdout, stderr

Definition at line 55 of file test_gdb.py.

00055 
00056     def run_gdb(self, *args):
00057         """Runs gdb with the command line given by *args.
00058 
00059         Returns its stdout, stderr
00060         """
00061         out, err = subprocess.Popen(
00062             args, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
00063             ).communicate()
00064         return out.decode('utf-8', 'replace'), err.decode('utf-8', 'replace')

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 614 of file test_gdb.py.

00614 
00615     def test_bt(self):
00616         'Verify that the "py-bt" command works'
00617         bt = self.get_stack_trace(script=self.get_sample_script(),
00618                                   cmds_after_breakpoint=['py-bt'])
00619         self.assertMultilineMatches(bt,
00620                                     r'''^.*
00621 Traceback \(most recent call first\):
00622   File ".*gdb_sample.py", line 10, in baz
00623     id\(42\)
00624   File ".*gdb_sample.py", line 7, in bar
00625     baz\(a, b, c\)
00626   File ".*gdb_sample.py", line 4, in foo
00627     bar\(a, b, c\)
00628   File ".*gdb_sample.py", line 12, in <module>
00629     foo\(1, 2, 3\)
00630 ''')

Here is the call graph for this function:

Definition at line 631 of file test_gdb.py.

00631 
00632     def test_bt_full(self):
00633         'Verify that the "py-bt-full" command works'
00634         bt = self.get_stack_trace(script=self.get_sample_script(),
00635                                   cmds_after_breakpoint=['py-bt-full'])
00636         self.assertMultilineMatches(bt,
00637                                     r'''^.*
00638 #[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
00639     baz\(a, b, c\)
00640 #[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 4, in foo \(a=1, b=2, c=3\)
00641     bar\(a, b, c\)
00642 #[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\)
00643 foo\(1, 2, 3\)
00644 ''')

Here is the call graph for this function:


The documentation for this class was generated from the following file: