¿Puedo usar GDB para depurar un proceso en ejecución?


En Linux, ¿puedo usar GDB para depurar un proceso que se está ejecutando actualmente?

Author: Justin Ethier, 2010-02-22

7 answers

Sí. Utilice el comando attach. Echa un vistazo a este enlace para obtener más información. Al escribir help attach en una consola GDB se obtiene lo siguiente:

(gdb) help attach

Adjuntar a un proceso o archivo fuera de GDB. Este comando se conecta a otro objetivo, del mismo tipo que el anterior "target "comando ("info files " mostrará su pila de destino). El comando puede tomar como argumento un id de proceso, un nombre de proceso (con un identificador de proceso opcional como sufijo), o un archivo de dispositivo. Para un id de proceso, debe tener permiso para enviar una señal al proceso, y debe tener el mismo uid efectivo que el depurador. Cuando se utiliza "attach " a un proceso existente, el depurador encuentra programa que se ejecuta en el proceso, mirando primero en el trabajo actual directorio, o (si no se encuentra allí) usando la ruta de búsqueda del archivo de origen (ver el comando" directory"). También puede usar el comando" file" especificar el programa, y cargar su tabla de símbolos.


NOTA: Usted puede tener dificultad para adjuntar a un proceso debido a seguridad mejorada en el kernel de Linux - por ejemplo, adjuntar al hijo de un shell desde otro.

Es probable que necesite configurar /proc/sys/kernel/yama/ptrace_scope dependiendo de sus requisitos. Muchos sistemas ahora tienen por defecto 1 o superior.

The sysctl settings (writable only with CAP_SYS_PTRACE) are:

0 - classic ptrace permissions: a process can PTRACE_ATTACH to any other
    process running under the same uid, as long as it is dumpable (i.e.
    did not transition uids, start privileged, or have called
    prctl(PR_SET_DUMPABLE...) already). Similarly, PTRACE_TRACEME is
    unchanged.

1 - restricted ptrace: a process must have a predefined relationship
    with the inferior it wants to call PTRACE_ATTACH on. By default,
    this relationship is that of only its descendants when the above
    classic criteria is also met. To change the relationship, an
    inferior can call prctl(PR_SET_PTRACER, debugger, ...) to declare
    an allowed debugger PID to call PTRACE_ATTACH on the inferior.
    Using PTRACE_TRACEME is unchanged.

2 - admin-only attach: only processes with CAP_SYS_PTRACE may use ptrace
    with PTRACE_ATTACH, or through children calling PTRACE_TRACEME.

3 - no attach: no processes may use ptrace with PTRACE_ATTACH nor via
    PTRACE_TRACEME. Once set, this sysctl value cannot be changed.
 72
Author: Carl Norum,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-13 10:05:15

Puede adjuntar a un proceso en ejecución con gdb -p PID.

 93
Author: Nikolai Fetissov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-02-22 03:42:45

Sí. Puedes hacer:

gdb program_name program_pid

Un atajo sería (suponiendo que solo se esté ejecutando una instancia):

gdb program_name `pidof program_name`
 22
Author: J. Polfer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-04-23 21:49:55

El comando a usar es gdb attach pid donde pid es el id del proceso al que desea adjuntar.

 14
Author: David Kanarek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-02-22 03:42:24

Sí puedes. Supongamos que se está ejecutando un proceso foo...

ps -elf | grep foo

look for the PID number

gdb -a {PID number}
 3
Author: t0mm13b,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-31 03:23:53

Ps-elf no parece mostrar el PID. Recomiendo usar en su lugar:

ps -ld | grep foo
gdb -p PID
 2
Author: Nino Pereira,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-21 16:47:21

Si se quiere adjuntar un proceso, este proceso debe tener el mismo propietario. La raíz es capaz de adjuntar a cualquier proceso.

 1
Author: Milan Kerslager,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-13 15:18:51