Skip to content

Commit

Permalink
libdrgn: examples: load_debug_info: pass struct drgn_program address …
Browse files Browse the repository at this point in the history
…to --{pre,post}-exec

This is useful for debugging the state of the program after loading
debugging information (e.g., debugging drgn with drgn!). For example:

  load_debug_info --post-exec 'echo drgn -p $1; echo "prog_obj = Object(prog, \"struct drgn_program *\", $2)"; sleep +inf'

Signed-off-by: Omar Sandoval <[email protected]>
  • Loading branch information
osandov committed Aug 4, 2023
1 parent 56d6631 commit 579e688
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions libdrgn/examples/load_debug_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@

extern char **environ;

static struct drgn_error *run_command(const char *which, const char *command)
static struct drgn_error *run_command(const char *which, const char *command,
struct drgn_program *prog)
{
if (!command)
return NULL;

char pid_arg[max_decimal_length(long)];
sprintf(pid_arg, "%ld", (long)getpid());
const char * const argv[] = {"sh", "-c", command, "sh", pid_arg, NULL};
char prog_arg[2 * sizeof(prog) + 3];
sprintf(prog_arg, "%p", prog);
const char * const argv[] = {
"sh", "-c", command, "sh", pid_arg, prog_arg, NULL
};

pid_t pid;
int errnum =
Expand Down Expand Up @@ -75,10 +80,11 @@ noreturn static void usage(bool error)
" -c PATH, --core PATH debug the given core dump\n"
" -p PID, --pid PID debug the running process with the given PID\n"
" -T, --time print how long loading debug info took in seconds\n"
" --pre-exec CMD before loading debug info, execute shell command with\n"
" PID of this process as argument\n"
" --post-exec CMD after loading debug info, execute shell command with\n"
" PID of this process as argument\n"
" --pre-exec CMD before loading debug info, execute the given shell\n"
" command with the PID of this process and the address\n"
" of the struct drgn_program in hexadecimal as arguments\n"
" --post-exec CMD after loading debug info, execute the given shell\n"
" command with the same arguments as --pre-exec\n"
" -h, --help display this help message and exit\n");
exit(error ? EXIT_FAILURE : EXIT_SUCCESS);
}
Expand Down Expand Up @@ -149,7 +155,7 @@ int main(int argc, char **argv)
if (err)
goto out;

err = run_command("pre", pre_exec);
err = run_command("pre", pre_exec, prog);
if (err)
goto out;

Expand All @@ -169,7 +175,7 @@ int main(int argc, char **argv)
goto out;
}

err = run_command("post", post_exec);
err = run_command("post", post_exec, prog);
if (err)
goto out;

Expand Down

0 comments on commit 579e688

Please sign in to comment.