Online Gdb Python

Posted : admin On 1/26/2022

GDB introduces a new Python module, named gdb. All methods and classes added by GDB are placed in this module. GDB automatically imports the gdb module for use in all scripts evaluated by the python command. Some types of the gdb module come with a textual representation (accessible through the repr or str functions). These are offered for debugging purposes only, expect them to change over time. Code, Compile, Run and Debug python program online. Write your code in this editor and press 'Run' button to execute it. Create local file geodatabase to hold data and attachments you want to download from ArcGIS Online (called data.gdb in script) Create feature class (called myLayer in script), enable attachments, add globalID's. Add the following field to the feature class -GlobalIDstr, text, length: 50. Create table called MatchTable (called MatchTable in script). OnlineGDB is online IDE with python compiler. Quick and easy way to compile python program online. It supports python3. Online GDB is online ide with compiler and debugger for C/C. Code, Compiler, Run, Debug Share code nippets. OnlineGDB beta online compiler and debugger for c/c code. Forked from null. Debug mode for python program is not yet supported.

Next: Parameters In Python, Previous: Recordings In Python, Up: Python API [Contents][Index]

23.3.2.20 Commands In Python

You can implement new GDB CLI commands in Python. A CLIcommand is implemented using an instance of the gdb.Commandclass, most commonly using a subclass.

Function: Command.__init__(name, command_class[, completer_class[, prefix]])

The object initializer for Command registers the new commandwith GDB. This initializer is normally invoked from thesubclass’ own __init__ method.

name is the name of the command. If name consists ofmultiple words, then the initial words are looked for as prefixcommands. In this case, if one of the prefix commands does not exist,an exception is raised.

There is no support for multi-line commands.

command_class should be one of the ‘COMMAND_’ constantsdefined below. This argument tells GDB how to categorize thenew command in the help system.

completer_class is an optional argument. If given, it should beone of the ‘COMPLETE_’ constants defined below. This argumenttells GDB how to perform completion for this command. If notgiven, GDB will attempt to complete using the object’scomplete method (see below); if no such method is found, anerror will occur when completion is attempted.

prefix is an optional argument. If True, then the newcommand is a prefix command; sub-commands of this command may beregistered.

The help text for the new command is taken from the Pythondocumentation string for the command’s class, if there is one. If nodocumentation string is provided, the default value “This command isnot documented.” is used.

Function: Command.dont_repeat()

By default, a GDB command is repeated when the user enters ablank line at the command prompt. A command can suppress thisbehavior by invoking the dont_repeat method. This is similarto the user command dont-repeat, see dont-repeat.

Function: Command.invoke(argument, from_tty)

This method is called by GDB when this command is invoked.

argument is a string. It is the argument to the command, afterleading and trailing whitespace has been stripped.

from_tty is a boolean argument. When true, this means that thecommand was entered by the user at the terminal; when false it meansthat the command came from elsewhere.

If this method throws an exception, it is turned into a GDBerror call. Otherwise, the return value is ignored.

To break argument up into an argv-like string usegdb.string_to_argv. This function behaves identically toGDB’s internal argument lexer buildargv.It is recommended to use this for consistency.Arguments are separated by spaces and may be quoted.Example:

Function: Command.complete(text, word)

This method is called by GDB when the user attemptscompletion on this command. All forms of completion are handled bythis method, that is, the TAB and M-? key bindings(see Completion), and the complete command (see complete).

The arguments text and word are both strings; textholds the complete command line up to the cursor’s location, whileword holds the last word of the command line; this is computedusing a word-breaking heuristic.

The complete method can return several values:

  • If the return value is a sequence, the contents of the sequence areused as the completions. It is up to complete to ensure that thecontents actually do complete the word. A zero-length sequence isallowed, it means that there were no completions available. Onlystring elements of the sequence are used; other elements in thesequence are ignored.
  • If the return value is one of the ‘COMPLETE_’ constants definedbelow, then the corresponding GDB-internal completionfunction is invoked, and its result is used.
  • All other results are treated as though there were no availablecompletions.

When a new command is registered, it must be declared as a member ofsome general class of commands. This is used to classify top-levelcommands in the on-line help system; note that prefix commands are notlisted under their own category but rather that of their top-levelcommand. The available classifications are represented by constantsdefined in the gdb module:

gdb.COMMAND_NONE

The command does not belong to any particular class. A command inthis category will not be displayed in any of the help categories.

gdb.COMMAND_RUNNING

The command is related to running the inferior. For example,start, step, and continue are in this category.Type help running at the GDB prompt to see a list ofcommands in this category.

gdb.COMMAND_DATA

The command is related to data or variables. For example,call, find, and print are in this category. Typehelp data at the GDB prompt to see a list of commandsin this category.

gdb.COMMAND_STACK

The command has to do with manipulation of the stack. For example,backtrace, frame, and return are in thiscategory. Type help stack at the GDB prompt to see alist of commands in this category.

gdb.COMMAND_FILES

This class is used for file-related commands. For example,file, list and section are in this category.Type help files at the GDB prompt to see a list ofcommands in this category.

gdb.COMMAND_SUPPORT

This should be used for “support facilities”, generally meaningthings that are useful to the user when interacting with GDB,but not related to the state of the inferior. For example,help, make, and shell are in this category. Typehelp support at the GDB prompt to see a list ofcommands in this category.

gdb.COMMAND_STATUS

The command is an ‘info’-related command, that is, related to thestate of GDB itself. For example, info, macro,and show are in this category. Type help status at theGDB prompt to see a list of commands in this category.

gdb.COMMAND_BREAKPOINTS

The command has to do with breakpoints. For example, break,clear, and delete are in this category. Type helpbreakpoints at the GDB prompt to see a list of commands inthis category.

gdb.COMMAND_TRACEPOINTS

The command has to do with tracepoints. For example, trace,actions, and tfind are in this category. Typehelp tracepoints at the GDB prompt to see a list ofcommands in this category.

gdb.COMMAND_TUI

Online Gdb Python Compiler

The command has to do with the text user interface (see TUI).Type help tui at the GDB prompt to see a list ofcommands in this category.

gdb.COMMAND_USER

The command is a general purpose command for the user, and typicallydoes not fit in one of the other categories.Type help user-defined at the GDB prompt to seea list of commands in this category, as well as the list of gdb macros(see Sequences).

gdb.COMMAND_OBSCURE

The command is only used in unusual circumstances, or is not ofgeneral interest to users. For example, checkpoint,fork, and stop are in this category. Type helpobscure at the GDB prompt to see a list of commands in thiscategory.

gdb.COMMAND_MAINTENANCE

The command is only useful to GDB maintainers. Themaintenance and flushregs commands are in this category.Type help internals at the GDB prompt to see a list ofcommands in this category.

A new command can use a predefined completion function, either byspecifying it via an argument at initialization, or by returning itfrom the complete method. These predefined completionconstants are all defined in the gdb module:

gdb.COMPLETE_NONE

This constant means that no completion should be done.

gdb.COMPLETE_FILENAME

This constant means that filename completion should be performed.

gdb.COMPLETE_LOCATION

This constant means that location completion should be done.See Specify Location.

Online Gdb Python
gdb.COMPLETE_COMMAND

This constant means that completion should examine GDBcommand names.

gdb.COMPLETE_SYMBOL

This constant means that completion should be done using symbol namesas the source.

gdb.COMPLETE_EXPRESSION

This constant means that completion should be done on expressions.Often this means completing on symbol names, but some languageparsers also have support for completing on field names.

The following code snippet shows how a trivial CLI command can beimplemented in Python:

The last line instantiates the class, and is necessary to trigger theregistration of the command with GDB. Depending on how thePython code is read into GDB, you may need to import thegdb module explicitly.

Next: Parameters In Python, Previous: Recordings In Python, Up: Python API [Contents][Index]

There are types of bugs that are difficult to debug from within Python:

  • segfaults (not uncaught Python exceptions)
  • hung processes (in cases where you can't get a Python traceback or debug with pdb)

  • out of control daemon processes

In these cases, you can try gdb.

Prerequisites

You need to have gdb on your system and Python debugging extensions. Extensions package includes debugging symbols and adds Python-specific commands into gdb. On a modern Linux system, you can easily install these with:

Fedora:

  • sudo yum install gdb python-debuginfo

Ubuntu:

  • sudo apt-get install gdb python2.7-dbg

Centos*:

  • sudo yum install yum-utils

  • sudo debuginfo-install glibc

  • sudo yum install gdb python-debuginfo

* tested on Centos 7. python-debuginfo is installable after the first two commands.

For gdb support on legacy systems, look at the end of this page.

Running with `gdb`

There are two possible ways:

  1. run python under gdb from the start. Note: the python executable needs to have debug symbols in it which may be another exe python2.7-dbg depending on your system

  2. attach to already running python process

To run python under gdb there are also two ways.

Interactive:

Automatic:

This will run the program til it exits, segfaults or you manually stop execution (using Ctrl+C).

If the process is already running, you can attach to it provided you know the process ID.

Attaching to a running process like this will cause it to stop. You can tell it to continue running with c command.

Debugging process

If your program segfaulted, gdb will automatically pause the program, so you can switch into gdb console to inspect its state. You can also manually interrupt program execution by pressing Ctrl+C in the console.

See the page EasierPythonDebugging for the list of Python helper commands for gdb.

Getting a C Stack Trace

If you are debugging a segfault, this is probably the first thing you want to do.

At the (gdb) prompt, just run the following command:

With luck, this will give some idea of where the problem is occurring and if it doesn't help you fix the problem, it can help someone else track down the problem.

The quality of the results will depend greatly on the amount of debug information available.

Getting a Python Stack Trace

If you have Python extensions installed, you can enter:

to get stack trace with familiar Python source code.

Working With Hung Processes

Online gdb python interpreter

If a process appears hung, it will either be waiting on something (a lock, IO, etc), or be in a busy loop somewhere. In either case, attaching to the process and getting a back trace can help.

If the process is in a busy loop, you may want to continue execution for a bit (using the cont command), then break (Ctrl+C) again and bring up a stack trace.

Online Gdb Python

If the hang occurs in some thread, the following commands may be handy:

Current thread is marked with *. To see where it is in Python code, use py-list:

Online

To see Python code positions for all threads, use:

References

  • http://fedoraproject.org/wiki/Features/EasierPythonDebugging

  • https://github.com/spyder-ide/spyder/wiki/How-to-debug-Spyder-deadlock-freeze-hang

Online gdb python compiler

GDB on Legacy systems

It may happen that you need to use gdb on a legacy system without advanced Python support. In this case you may find the following information useful.

GDB Macros

A set of GDB macros are distributed with Python that aid in debugging the Python process. You can install them by adding the contents of Misc/gdbinit in the Python sources to ~/.gdbinit -- or copy it from Subversion. Be sure to use the correct version for your version of Python or some features will not work.

Note that the new GDB commands this file adds will only work correctly if debugging symbols are available.

Online Gdp Compiler

Depending on how you've compiled Python, some calls may have had their frame pointers (i.e. $fp in GDB) optimised away, which means GDB won't have access to local variables like co that can be inspected for Python callstack information. For example, if you compile using -g -O3 using GCC 4.1, then this occurs. Similarly, with gcc 4.5.2 on Ubuntu (at least) the macros fail for the same reason. The usual symptom is that you'll see the call_function routine appearing to be between PyEval_EvalFrameEx and PyEval_EvalCodeEx, and the macro failing with No symbol 'co' in current context.. There are two work-arounds for the issue:

Online Gdb Python Interpreter

  • Recompiling python with make 'CFLAGS=-g -fno-inline -fno-strict-aliasing' solves this problem.

  • Patching the conditionals for Python frames in the pystack and pystackv routines to ignore frames with where $fp is 0, like so:

Also note that the stop condition for the while-loops in the pystack and pystackv routines were originally designed for the case where you're running the Python interpreter directly, and not running the interpreter withing another program (by loading the shared library and manually bootstrapping the interpreter). So you may need to tweak the while-loops depending on the program you're intending to debug. See, for example, this StackOverflow post for another (putative) stop condition.

Getting Python Stack Traces With GDB Macros

At the gdb prompt, you can get a Python stack trace:

Alternatively, you can get a list of the Python locals along with each stack frame:

More useful macros not in python's gdbinit file

Online Gdb Python

See http://web.archive.org/web/20070915134837/http://www.mashebali.com/?Python_GDB_macros:The_Macros for some more handy python gdb macros.