r""" Launch a GDB server with the specified command line, and launches GDB to attach to it. Arguments: args(list): Arguments to the process, similar to :class:`.process`. gdbscript(str): GDB script to run. gdb_args(list): List of additional arguments to pass to G
(args, gdbscript=None, gdb_args=None, exe=None, ssh=None, env=None, port=0, gdbserver_args=None, sysroot=None, api=False, **kwargs)
| 420 | |
| 421 | @LocalContext |
| 422 | def debug(args, gdbscript=None, gdb_args=None, exe=None, ssh=None, env=None, port=0, gdbserver_args=None, sysroot=None, api=False, **kwargs): |
| 423 | r""" |
| 424 | Launch a GDB server with the specified command line, |
| 425 | and launches GDB to attach to it. |
| 426 | |
| 427 | Arguments: |
| 428 | args(list): Arguments to the process, similar to :class:`.process`. |
| 429 | gdbscript(str): GDB script to run. |
| 430 | gdb_args(list): List of additional arguments to pass to GDB. |
| 431 | exe(str): Path to the executable on disk |
| 432 | env(dict): Environment to start the binary in |
| 433 | ssh(:class:`.ssh`): Remote ssh session to use to launch the process. |
| 434 | port(int): Gdb port to use, default: random |
| 435 | gdbserver_args(list): List of additional arguments to pass to gdbserver |
| 436 | sysroot(str): Set an alternate system root. The system root is used to |
| 437 | load absolute shared library symbol files. This is useful to instruct |
| 438 | gdb to load a local version of binaries/libraries instead of downloading |
| 439 | them from the gdbserver, which is faster |
| 440 | api(bool): Enable access to GDB Python API. |
| 441 | |
| 442 | Returns: |
| 443 | :class:`.process` or :class:`.ssh_channel`: A tube connected to the target process. |
| 444 | When ``api=True``, ``gdb`` member of the returned object contains a :class:`Gdb` |
| 445 | instance. |
| 446 | |
| 447 | Notes: |
| 448 | |
| 449 | The debugger is attached automatically, and you can debug everything |
| 450 | from the very beginning. This requires that both ``gdb`` and ``gdbserver`` |
| 451 | are installed on your machine. |
| 452 | |
| 453 | When GDB opens via :func:`debug`, it will initially be stopped on the very first |
| 454 | instruction of the dynamic linker (``ld.so``) for dynamically-linked binaries. |
| 455 | |
| 456 | Only the target binary and the linker will be loaded in memory, so you cannot |
| 457 | set breakpoints on shared library routines like ``malloc`` since ``libc.so`` |
| 458 | has not even been loaded yet. |
| 459 | |
| 460 | There are several ways to handle this: |
| 461 | |
| 462 | 1. Set a breakpoint on the executable's entry point (generally, ``_start``) |
| 463 | - This is only invoked after all of the required shared libraries |
| 464 | are loaded. |
| 465 | - You can generally get the address via the GDB command ``info file``. |
| 466 | 2. Use pending breakpoints via ``set breakpoint pending on`` |
| 467 | - This has the side-effect of setting breakpoints for **every** function |
| 468 | which matches the name. For ``malloc``, this will generally set a |
| 469 | breakpoint in the executable's PLT, in the linker's internal ``malloc``, |
| 470 | and eventaully in ``libc``'s malloc. |
| 471 | 3. Wait for libraries to be loaded with ``set stop-on-solib-event 1`` |
| 472 | - There is no way to stop on any specific library being loaded, and sometimes |
| 473 | multiple libraries are loaded and only a single breakpoint is issued. |
| 474 | - Generally, you just add a few ``continue`` commands until things are set up |
| 475 | the way you want it to be. |
| 476 | |
| 477 | Examples: |
| 478 | |
| 479 | Create a new process, and stop it at 'main' |
no test coverage detected