Debian Codegraph @

libdrgn/examples/load_debug_info.c

raw
1 // Copyright (c) Meta Platforms, Inc. and affiliates.
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3
4 #include <errno.h>
5 #include <getopt.h>
6 #include <locale.h>
7 #include <spawn.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdnoreturn.h>
11 #include <sys/wait.h>
12 #include <time.h>
13 #include <unistd.h>
14
15 #include "../drgn.h"
16 #include "../util.h"
17
18 extern char **environ;
19
20 static struct drgn_error *run_command(const char *which, const char *command,
21 struct drgn_program *prog)
22 {
23 if (!command)
24 return NULL;
25
26 char pid_arg[max_decimal_length(long)];
27 sprintf(pid_arg, "%ld", (long)getpid());
28 char prog_arg[2 * sizeof(prog) + 3];
29 sprintf(prog_arg, "%p", prog);
30 const char * const argv[] = {
31 "sh", "-c", command, "sh", pid_arg, prog_arg, NULL
32 };
33
34 pid_t pid;
35 int errnum =
36 posix_spawnp(&pid, "sh", NULL, NULL, (char **)argv, environ);
37 if (errnum)
38 return drgn_error_create_os("posix_spawnp", errnum, "sh");
39
40 int wstatus;
41 if (waitpid(pid, &wstatus, 0) < 0)
42 return drgn_error_create_os("waitpid", errno, NULL);
43
44 if (!WIFEXITED(wstatus)) {
45 return drgn_error_format(DRGN_ERROR_OTHER,
46 "%s-exec command exited abnormally: %d",
47 which, wstatus);
48 }
49 if (WEXITSTATUS(wstatus) != 0) {
50 return drgn_error_format(DRGN_ERROR_OTHER,
51 "%s-exec command exited with status %d",
52 which, WEXITSTATUS(wstatus));
53 }
54 return NULL;
55 }
56
57 static inline struct timespec timespec_sub(struct timespec a, struct timespec b)
58 {
59 if (a.tv_nsec < b.tv_nsec) {
60 return (struct timespec){
61 .tv_sec = a.tv_sec - 1 - b.tv_sec,
62 .tv_nsec = a.tv_nsec + 1000000000L - b.tv_nsec,
63 };
64 } else {
65 return (struct timespec){
66 .tv_sec = a.tv_sec - b.tv_sec,
67 .tv_nsec = a.tv_nsec - b.tv_nsec,
68 };
69 }
70 }
71
72 noreturn static void usage(bool error)
73 {
74 fprintf(error ? stderr : stdout,
75 "usage: load_debug_info [OPTION...] [-k|-c CORE|-p PID] [PATH...]\n"
76 "\n"
77 "Example libdrgn program that loads default debug information\n"
78 "\n"
79 "Options:\n"
80 " -k, --kernel debug the running kernel\n"
81 " -c PATH, --core PATH debug the given core dump\n"
82 " -p PID, --pid PID debug the running process with the given PID\n"
83 " -T, --time print how long loading debug info took in seconds\n"
84 " --pre-exec CMD before loading debug info, execute the given shell\n"
85 " command with the PID of this process and the address\n"
86 " of the struct drgn_program in hexadecimal as arguments\n"
87 " --post-exec CMD after loading debug info, execute the given shell\n"
88 " command with the same arguments as --pre-exec\n"
89 " -h, --help display this help message and exit\n");
90 exit(error ? EXIT_FAILURE : EXIT_SUCCESS);
91 }
92
93 int main(int argc, char **argv)
94 {
95 setlocale(LC_ALL, "");
96
97 struct option long_options[] = {
98 {"kernel", no_argument, NULL, 'k'},
99 {"core", required_argument, NULL, 'c'},
100 {"pid", required_argument, NULL, 'p'},
101 {"time", no_argument, NULL, 'T'},
102 {"pre-exec", required_argument, NULL, 'x'},
103 {"post-exec", required_argument, NULL, 'X'},
104 {"help", no_argument, NULL, 'h'},
105 {},
106 };
107 bool kernel = false;
108 const char *core = NULL;
109 const char *pid = NULL;
110 bool print_time = false;
111 const char *pre_exec = NULL;
112 const char *post_exec = NULL;
113 for (;;) {
114 int c = getopt_long(argc, argv, "kc:p:Th", long_options, NULL);
115 if (c == -1)
116 break;
117 switch (c) {
118 case 'k':
119 kernel = true;
120 break;
121 case 'c':
122 core = optarg;
123 break;
124 case 'p':
125 pid = optarg;
126 break;
127 case 'T':
128 print_time = true;
129 break;
130 case 'x':
131 pre_exec = optarg;
132 break;
133 case 'X':
134 post_exec = optarg;
135 break;
136 case 'h':
137 usage(false);
138 default:
139 usage(true);
140 }
141 }
142 if (kernel + !!core + !!pid > 1)
143 usage(true);
144
145 struct drgn_program *prog;
146 struct drgn_error *err = drgn_program_create(NULL, &prog);
147 if (err) {
148 prog = NULL;
149 goto out;
150 }
151
152 if (core)
153 err = drgn_program_set_core_dump(prog, core);
154 else if (pid)
155 err = drgn_program_set_pid(prog, atoi(pid) ?: getpid());
156 else if (kernel)
157 err = drgn_program_set_kernel(prog);
158 if (err)
159 goto out;
160
161 err = run_command("pre", pre_exec, prog);
162 if (err)
163 goto out;
164
165 struct timespec start, end;
166 if (print_time && clock_gettime(CLOCK_MONOTONIC, &start))
167 abort();
168 err = drgn_program_load_debug_info(prog, (const char **)&argv[optind],
169 argc - optind, kernel || core || pid,
170 false);
171 if ((!err || err->code == DRGN_ERROR_MISSING_DEBUG_INFO)
172 && print_time && clock_gettime(CLOCK_MONOTONIC, &end))
173 abort();
174 if (err && err->code == DRGN_ERROR_MISSING_DEBUG_INFO) {
175 drgn_error_fwrite(stderr, err);
176 drgn_error_destroy(err);
177 } else if (err) {
178 goto out;
179 }
180
181 err = run_command("post", post_exec, prog);
182 if (err)
183 goto out;
184
185 if (print_time) {
186 struct timespec diff = timespec_sub(end, start);
187 printf("%lld.%09ld\n", (long long)diff.tv_sec, diff.tv_nsec);
188 }
189
190 out:;
191 int status = err ? EXIT_FAILURE : EXIT_SUCCESS;
192 if (err) {
193 drgn_error_fwrite(stderr, err);
194 drgn_error_destroy(err);
195 }
196 drgn_program_destroy(prog);
197 return status;
198 }