Debian Codegraph @

libdrgn/stack_trace.c

raw
1 // Copyright (c) Meta Platforms, Inc. and affiliates.
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3
4 #include <assert.h>
5 #include <ctype.h>
6 #include <elf.h>
7 #include <elfutils/libdw.h>
8 #include <errno.h>
9 #include <inttypes.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "cfi.h"
14 #include "cleanup.h"
15 #include "debug_info.h"
16 #include "drgn_internal.h"
17 #include "dwarf_constants.h"
18 #include "dwarf_info.h"
19 #include "elf_file.h"
20 #include "elf_notes.h"
21 #include "error.h"
22 #include "helpers.h"
23 #include "minmax.h"
24 #include "nstring.h"
25 #include "platform.h"
26 #include "program.h"
27 #include "register_state.h"
28 #include "serialize.h"
29 #include "stack_trace.h"
30 #include "string_builder.h"
31 #include "symbol.h"
32 #include "type.h"
33 #include "util.h"
34
35 static struct drgn_error *
36 drgn_stack_trace_append_frame(struct drgn_stack_trace **trace, size_t *capacity,
37 struct drgn_register_state *regs,
38 Dwarf_Die *scopes, size_t num_scopes,
39 size_t function_scope)
40 {
41 if ((*trace)->num_frames == *capacity) {
42 static const size_t max_capacity =
43 (SIZE_MAX - sizeof(struct drgn_stack_trace)) /
44 sizeof(struct drgn_stack_frame);
45 if (*capacity == max_capacity)
46 return &drgn_enomem;
47 size_t new_capacity;
48 if (*capacity > max_capacity / 2)
49 new_capacity = max_capacity;
50 else
51 new_capacity = 2 * (*capacity);
52 struct drgn_stack_trace *new_trace =
53 realloc((*trace),
54 offsetof(struct drgn_stack_trace,
55 frames[new_capacity]));
56 if (!new_trace)
57 return &drgn_enomem;
58 *trace = new_trace;
59 *capacity = new_capacity;
60 }
61 struct drgn_stack_frame *frame =
62 &(*trace)->frames[(*trace)->num_frames++];
63 frame->regs = regs;
64 frame->scopes = scopes;
65 frame->num_scopes = num_scopes;
66 frame->function_scope = function_scope;
67 return NULL;
68 }
69
70 static void drgn_stack_trace_shrink_to_fit(struct drgn_stack_trace **trace,
71 size_t capacity)
72 {
73 size_t num_frames = (*trace)->num_frames;
74 if (capacity > num_frames) {
75 struct drgn_stack_trace *new_trace =
76 realloc((*trace),
77 offsetof(struct drgn_stack_trace,
78 frames[num_frames]));
79 if (new_trace)
80 *trace = new_trace;
81 }
82 }
83
84 LIBDRGN_PUBLIC void drgn_stack_trace_destroy(struct drgn_stack_trace *trace)
85 {
86 if (trace) {
87 struct drgn_register_state *regs = NULL;
88 for (size_t i = 0; i < trace->num_frames; i++) {
89 if (trace->frames[i].regs != regs) {
90 drgn_register_state_destroy(regs);
91 regs = trace->frames[i].regs;
92 }
93 free(trace->frames[i].scopes);
94 }
95 drgn_register_state_destroy(regs);
96 free(trace);
97 }
98 }
99
100 LIBDRGN_PUBLIC
101 struct drgn_program *drgn_stack_trace_program(struct drgn_stack_trace *trace)
102 {
103 return trace->prog;
104 }
105
106 LIBDRGN_PUBLIC size_t
107 drgn_stack_trace_num_frames(struct drgn_stack_trace *trace)
108 {
109 return trace->num_frames;
110 }
111
112 LIBDRGN_PUBLIC struct drgn_error *
113 drgn_format_stack_trace(struct drgn_stack_trace *trace, char **ret)
114 {
115 struct drgn_error *err;
116 STRING_BUILDER(str);
117 for (size_t frame = 0; frame < trace->num_frames; frame++) {
118 if (!string_builder_appendf(&str, "#%-2zu ", frame))
119 return &drgn_enomem;
120
121 struct drgn_register_state *regs = trace->frames[frame].regs;
122 struct optional_uint64 pc;
123 const char *function_name =
124 drgn_stack_frame_function_name(trace, frame);
125 if (function_name) {
126 if (!string_builder_append(&str, function_name))
127 return &drgn_enomem;
128 } else if ((pc = drgn_register_state_get_pc(regs)).has_value) {
129 _cleanup_symbol_ struct drgn_symbol *sym = NULL;
130 err = drgn_program_find_symbol_by_address_internal(trace->prog,
131 pc.value - !regs->interrupted,
132 &sym);
133 if (err)
134 return err;
135
136 if (sym) {
137 if (!string_builder_appendf(&str,
138 "%s+0x%" PRIx64 "/0x%" PRIx64,
139 sym->name,
140 pc.value - sym->address,
141 sym->size))
142 return &drgn_enomem;
143 } else {
144 if (!string_builder_appendf(&str, "0x%" PRIx64,
145 pc.value))
146 return &drgn_enomem;
147 }
148 } else {
149 if (!string_builder_append(&str, "???"))
150 return &drgn_enomem;
151 }
152
153 int line, column;
154 const char *filename = drgn_stack_frame_source(trace, frame,
155 &line, &column);
156 if (filename && column) {
157 if (!string_builder_appendf(&str, " (%s:%d:%d)",
158 filename, line, column))
159 return &drgn_enomem;
160 } else if (filename) {
161 if (!string_builder_appendf(&str, " (%s:%d)", filename,
162 line))
163 return &drgn_enomem;
164 }
165
166 if (frame != trace->num_frames - 1 &&
167 !string_builder_appendc(&str, '\n'))
168 return &drgn_enomem;
169 }
170 if (!string_builder_null_terminate(&str))
171 return &drgn_enomem;
172 *ret = string_builder_steal(&str);
173 return NULL;
174 }
175
176 LIBDRGN_PUBLIC struct drgn_error *
177 drgn_format_stack_frame(struct drgn_stack_trace *trace, size_t frame, char **ret)
178 {
179 STRING_BUILDER(str);
180 struct drgn_register_state *regs = trace->frames[frame].regs;
181 struct drgn_error *err;
182 if (!string_builder_appendf(&str, "#%zu at ", frame))
183 return &drgn_enomem;
184
185 struct optional_uint64 pc = drgn_register_state_get_pc(regs);
186 if (pc.has_value) {
187 if (!string_builder_appendf(&str, "%#" PRIx64, pc.value))
188 return &drgn_enomem;
189
190 _cleanup_symbol_ struct drgn_symbol *sym;
191 err = drgn_program_find_symbol_by_address_internal(trace->prog,
192 pc.value - !regs->interrupted,
193 &sym);
194 if (err)
195 return err;
196 if (sym && !string_builder_appendf(&str, " (%s+0x%" PRIx64 "/0x%" PRIx64 ")",
197 sym->name, pc.value - sym->address,
198 sym->size))
199 return &drgn_enomem;
200 } else {
201 if (!string_builder_append(&str, "???"))
202 return &drgn_enomem;
203 }
204
205 const char *function_name = drgn_stack_frame_function_name(trace, frame);
206 if (function_name
207 && !string_builder_appendf(&str, " in %s", function_name))
208 return &drgn_enomem;
209
210 int line, column;
211 const char *filename = drgn_stack_frame_source(trace, frame, &line,
212 &column);
213 if (filename && column) {
214 if (!string_builder_appendf(&str, " at %s:%d:%d", filename,
215 line, column))
216 return &drgn_enomem;
217 } else if (filename) {
218 if (!string_builder_appendf(&str, " at %s:%d", filename, line))
219 return &drgn_enomem;
220 }
221
222 if (drgn_stack_frame_is_inline(trace, frame) &&
223 !string_builder_append(&str, " (inlined)"))
224 return &drgn_enomem;
225
226 if (!string_builder_null_terminate(&str))
227 return &drgn_enomem;
228 *ret = string_builder_steal(&str);
229 return NULL;
230 }
231
232 static struct drgn_error *
233 drgn_format_stack_frame_source_impl(struct drgn_stack_trace *trace, size_t frame,
234 struct string_builder *sb)
235 {
236 struct drgn_error *err;
237
238 {
239 _cleanup_free_ char *name = NULL;
240 err = drgn_stack_frame_source_name(trace, frame, &name);
241 if (err)
242 return err;
243
244 if (!string_builder_append(sb, name ? name : "???"))
245 return &drgn_enomem;
246 }
247
248 int line, column;
249 const char *filename = drgn_stack_frame_source(trace, frame, &line,
250 &column);
251 if (filename && column) {
252 if (!string_builder_appendf(sb, " at %s:%d:%d", filename, line,
253 column))
254 return &drgn_enomem;
255 } else if (filename) {
256 if (!string_builder_appendf(sb, " at %s:%d", filename, line))
257 return &drgn_enomem;
258 } else {
259 if (!string_builder_append(sb, " at ??:?"))
260 return &drgn_enomem;
261 }
262 return NULL;
263 }
264
265 LIBDRGN_PUBLIC struct drgn_error *
266 drgn_format_stack_frame_source(struct drgn_stack_trace *trace, size_t frame,
267 char **ret)
268 {
269 struct drgn_error *err;
270 STRING_BUILDER(sb);
271 err = drgn_format_stack_frame_source_impl(trace, frame, &sb);
272 if (err)
273 return err;
274 if (!string_builder_null_terminate(&sb))
275 return &drgn_enomem;
276 *ret = string_builder_steal(&sb);
277 return NULL;
278 }
279
280 LIBDRGN_PUBLIC
281 struct drgn_error *drgn_stack_frame_name(struct drgn_stack_trace *trace,
282 size_t frame, char **ret)
283 {
284 struct drgn_error *err;
285 char *name;
286 const char *function_name = drgn_stack_frame_function_name(trace, frame);
287 if (function_name) {
288 name = strdup(function_name);
289 } else {
290 struct drgn_register_state *regs = trace->frames[frame].regs;
291 struct optional_uint64 pc = drgn_register_state_get_pc(regs);
292 if (pc.has_value) {
293 _cleanup_symbol_ struct drgn_symbol *sym = NULL;
294 err = drgn_program_find_symbol_by_address_internal(trace->prog,
295 pc.value - !regs->interrupted,
296 &sym);
297 if (err)
298 return err;
299 if (sym)
300 name = strdup(sym->name);
301 else if (asprintf(&name, "0x%" PRIx64, pc.value) < 0)
302 name = NULL;
303 } else {
304 name = strdup("???");
305 }
306 }
307 if (!name)
308 return &drgn_enomem;
309 *ret = name;
310 return NULL;
311 }
312
313 LIBDRGN_PUBLIC
314 const char *drgn_stack_frame_function_name(struct drgn_stack_trace *trace,
315 size_t frame)
316 {
317 Dwarf_Die *scopes = trace->frames[frame].scopes;
318 size_t num_scopes = trace->frames[frame].num_scopes;
319 size_t function_scope = trace->frames[frame].function_scope;
320 if (function_scope >= num_scopes)
321 return NULL;
322 return dwarf_diename(&scopes[function_scope]);
323 }
324
325 LIBDRGN_PUBLIC struct drgn_error *
326 drgn_stack_frame_source_name(struct drgn_stack_trace *trace, size_t frame,
327 char **ret)
328 {
329 struct drgn_error *err;
330 char *name = NULL;
331 const char *function_name = drgn_stack_frame_function_name(trace, frame);
332 if (function_name) {
333 name = strdup(function_name);
334 if (!name)
335 return &drgn_enomem;
336 } else {
337 struct drgn_register_state *regs = trace->frames[frame].regs;
338 struct optional_uint64 pc = drgn_register_state_get_pc(regs);
339 if (pc.has_value) {
340 _cleanup_symbol_ struct drgn_symbol *sym = NULL;
341 err = drgn_program_find_symbol_by_address_internal(trace->prog,
342 pc.value - !regs->interrupted,
343 &sym);
344 if (err)
345 return err;
346 if (sym) {
347 name = strdup(sym->name);
348 if (!name)
349 return &drgn_enomem;
350 }
351 }
352 }
353 *ret = name;
354 return NULL;
355 }
356
357 LIBDRGN_PUBLIC bool drgn_stack_frame_is_inline(struct drgn_stack_trace *trace,
358 size_t frame)
359 {
360 Dwarf_Die *scopes = trace->frames[frame].scopes;
361 size_t num_scopes = trace->frames[frame].num_scopes;
362 size_t function_scope = trace->frames[frame].function_scope;
363 return (function_scope < num_scopes &&
364 dwarf_tag(&scopes[function_scope]) ==
365 DW_TAG_inlined_subroutine);
366 }
367
368 LIBDRGN_PUBLIC const char *
369 drgn_stack_frame_source(struct drgn_stack_trace *trace, size_t frame,
370 int *line_ret, int *column_ret)
371 {
372 if (frame > 0 &&
373 trace->frames[frame].regs == trace->frames[frame - 1].regs) {
374 /*
375 * This frame is the caller of an inline frame. Get the call
376 * location from the inlined_subroutine of the callee.
377 */
378 Dwarf_Die *inlined_scopes = trace->frames[frame - 1].scopes;
379 size_t inlined_num_scopes = trace->frames[frame - 1].num_scopes;
380 size_t inlined_function_scope =
381 trace->frames[frame - 1].function_scope;
382 if (inlined_function_scope >= inlined_num_scopes)
383 return NULL;
384 Dwarf_Die *inlined = &inlined_scopes[inlined_function_scope];
385
386 Dwarf_Die inlined_cu;
387 Dwarf_Files *files;
388 if (!dwarf_diecu(inlined, &inlined_cu, NULL, NULL) ||
389 dwarf_getsrcfiles(&inlined_cu, &files, NULL))
390 return NULL;
391
392 Dwarf_Attribute attr;
393 Dwarf_Word value;
394 if (dwarf_formudata(dwarf_attr(inlined, DW_AT_call_file, &attr),
395 &value))
396 return NULL;
397
398 const char *filename = dwarf_filesrc(files, value, NULL, NULL);
399 if (!filename)
400 return NULL;
401 if (line_ret) {
402 if (dwarf_formudata(dwarf_attr(inlined, DW_AT_call_line,
403 &attr), &value))
404 *line_ret = 0;
405 else
406 *line_ret = value;
407 }
408 if (column_ret) {
409 if (dwarf_formudata(dwarf_attr(inlined,
410 DW_AT_call_column,
411 &attr), &value))
412 *column_ret = 0;
413 else
414 *column_ret = value;
415 }
416 return filename;
417 } else if (trace->frames[frame].num_scopes > 0) {
418 struct drgn_register_state *regs = trace->frames[frame].regs;
419 if (!regs->module)
420 return NULL;
421
422 struct optional_uint64 pc = drgn_register_state_get_pc(regs);
423 if (!pc.has_value)
424 return NULL;
425 pc.value -= !regs->interrupted + regs->module->debug_file_bias;
426
427 Dwarf_Die *scopes = trace->frames[frame].scopes;
428 size_t num_scopes = trace->frames[frame].num_scopes;
429 Dwarf_Die cu_die;
430 if (!dwarf_cu_die(scopes[num_scopes - 1].cu, &cu_die, NULL,
431 NULL, NULL, NULL, NULL, NULL))
432 return NULL;
433
434 Dwarf_Line *line = dwarf_getsrc_die(&cu_die, pc.value);
435 if (!line)
436 return NULL;
437 const char *filename = dwarf_linesrc(line, NULL, NULL);
438 if (filename) {
439 if (line_ret)
440 dwarf_lineno(line, line_ret);
441 if (column_ret)
442 dwarf_linecol(line, column_ret);
443 }
444 return filename;
445 } else {
446 return NULL;
447 }
448 }
449
450 LIBDRGN_PUBLIC bool drgn_stack_frame_interrupted(struct drgn_stack_trace *trace,
451 size_t frame)
452 {
453 return trace->frames[frame].regs->interrupted;
454 }
455
456 LIBDRGN_PUBLIC bool drgn_stack_frame_pc(struct drgn_stack_trace *trace,
457 size_t frame, uint64_t *ret)
458 {
459 struct optional_uint64 pc =
460 drgn_register_state_get_pc(trace->frames[frame].regs);
461 if (pc.has_value)
462 *ret = pc.value;
463 return pc.has_value;
464 }
465
466 LIBDRGN_PUBLIC bool drgn_stack_frame_sp(struct drgn_stack_trace *trace,
467 size_t frame, uint64_t *ret)
468 {
469 struct drgn_program *prog = trace->prog;
470 drgn_register_number regno = prog->platform.arch->stack_pointer_regno;
471 struct drgn_register_state *regs = trace->frames[frame].regs;
472 if (!drgn_register_state_has_register(regs, regno))
473 return false;
474 const struct drgn_register_layout *layout =
475 &prog->platform.arch->register_layout[regno];
476 copy_lsbytes(ret, sizeof(*ret), HOST_LITTLE_ENDIAN,
477 &regs->buf[layout->offset], layout->size,
478 drgn_platform_is_little_endian(&prog->platform));
479 return true;
480 }
481
482 LIBDRGN_PUBLIC struct drgn_error *
483 drgn_stack_frame_symbol(struct drgn_stack_trace *trace, size_t frame,
484 struct drgn_symbol **ret)
485 {
486 struct drgn_register_state *regs = trace->frames[frame].regs;
487 struct optional_uint64 pc = drgn_register_state_get_pc(regs);
488 if (!pc.has_value) {
489 return drgn_error_create(DRGN_ERROR_LOOKUP,
490 "program counter is not known at stack frame");
491 }
492 pc.value -= !regs->interrupted;
493 struct drgn_symbol *sym = NULL;
494 struct drgn_error *err;
495 err = drgn_program_find_symbol_by_address_internal(trace->prog, pc.value,
496 &sym);
497 if (err)
498 return err;
499 if (!sym)
500 return drgn_error_symbol_not_found(pc.value);
501 *ret = sym;
502 return NULL;
503 }
504
505 LIBDRGN_PUBLIC struct drgn_error *
506 drgn_stack_frame_locals(struct drgn_stack_trace *trace, size_t frame_i,
507 const char ***names_ret, size_t *count_ret)
508 {
509 struct drgn_stack_frame *frame = &trace->frames[frame_i];
510 if (frame->function_scope >= frame->num_scopes) {
511 *names_ret = NULL;
512 *count_ret = 0;
513 return NULL;
514 }
515 return drgn_dwarf_scopes_names(frame->scopes + frame->function_scope,
516 frame->num_scopes - frame->function_scope,
517 names_ret, count_ret);
518 }
519
520 LIBDRGN_PUBLIC
521 void drgn_stack_frame_locals_destroy(const char **names, size_t count)
522 {
523 free(names);
524 }
525
526 LIBDRGN_PUBLIC struct drgn_error *
527 drgn_stack_frame_find_object(struct drgn_stack_trace *trace, size_t frame_i,
528 const char *name, struct drgn_object *ret)
529 {
530 struct drgn_error *err;
531 struct drgn_stack_frame *frame = &trace->frames[frame_i];
532
533 if (frame->function_scope >= frame->num_scopes)
534 goto not_found;
535
536 Dwarf_Die die, type_die;
537 err = drgn_find_in_dwarf_scopes(frame->scopes, frame->num_scopes, name,
538 &die, &type_die);
539 if (err)
540 return err;
541 if (!die.addr && frame->function_scope == 0) {
542 /*
543 * Scope 0 must be a DW_TAG_inlined_subroutine, and we didn't
544 * find the name in the concrete inlined instance tree. We need
545 * to find the scopes that contain the the abstract instance
546 * root (i.e, the DW_TAG_subprogram definition). (We could do
547 * this ahead of time when unwinding the stack, but for
548 * efficiency we do it lazily.)
549 */
550 Dwarf_Attribute attr_mem, *attr;
551 if (!(attr = dwarf_attr(frame->scopes, DW_AT_abstract_origin,
552 &attr_mem)))
553 goto not_found;
554 Dwarf_Die abstract_origin;
555 if (!dwarf_formref_die(attr, &abstract_origin))
556 return drgn_error_libdw();
557
558 Dwarf_Die *ancestors;
559 size_t num_ancestors;
560 err = drgn_find_die_ancestors(&abstract_origin, &ancestors,
561 &num_ancestors);
562 if (err)
563 return err;
564
565 size_t new_num_scopes = num_ancestors + frame->num_scopes;
566 Dwarf_Die *new_scopes = realloc(ancestors,
567 new_num_scopes *
568 sizeof(*new_scopes));
569 if (!new_scopes) {
570 free(ancestors);
571 return &drgn_enomem;
572 }
573 memcpy(&new_scopes[num_ancestors], frame->scopes,
574 frame->num_scopes * sizeof(*new_scopes));
575 free(frame->scopes);
576 frame->scopes = new_scopes;
577 frame->num_scopes = new_num_scopes;
578 frame->function_scope = num_ancestors;
579
580 /* Look for the name in the new scopes. */
581 err = drgn_find_in_dwarf_scopes(frame->scopes, num_ancestors,
582 name, &die, &type_die);
583 if (err)
584 return err;
585 }
586 if (!die.addr) {
587 not_found:;
588 const char *function_name =
589 drgn_stack_frame_function_name(trace, frame_i);
590 if (function_name) {
591 return drgn_error_format(DRGN_ERROR_LOOKUP,
592 "could not find '%s' in '%s'",
593 name, function_name);
594 } else {
595 return drgn_error_format(DRGN_ERROR_LOOKUP,
596 "could not find '%s'", name);
597 }
598 }
599
600 const struct drgn_register_state *regs = frame->regs;
601 struct drgn_elf_file *file =
602 drgn_module_find_dwarf_file(regs->module,
603 dwarf_cu_getdwarf(die.cu));
604 if (!file) {
605 return drgn_error_create(DRGN_ERROR_OTHER,
606 "couldn't find file containing DIE");
607 }
608 // It doesn't make sense to use the registers if the file has a
609 // different platform than the program.
610 if (!drgn_platforms_equal(&file->platform, &trace->prog->platform))
611 regs = NULL;
612 // If this is an inline frame, then DW_AT_frame_base is in the
613 // containing DW_TAG_subprogram DIE.
614 size_t subprogram_frame_i = frame_i;
615 Dwarf_Die *function_die;
616 for (;;) {
617 struct drgn_stack_frame *subprogram_frame =
618 &trace->frames[subprogram_frame_i];
619 function_die =
620 &subprogram_frame->scopes[subprogram_frame->function_scope];
621 if (dwarf_tag(function_die) == DW_TAG_subprogram)
622 break;
623 subprogram_frame_i++;
624 }
625 return drgn_object_from_dwarf(&trace->prog->dbinfo, file, &die,
626 dwarf_tag(&die) == DW_TAG_enumerator ?
627 &type_die : NULL,
628 function_die, regs, ret);
629 }
630
631 LIBDRGN_PUBLIC bool drgn_stack_frame_register(struct drgn_stack_trace *trace,
632 size_t frame,
633 const struct drgn_register *reg,
634 uint64_t *ret)
635 {
636 struct drgn_program *prog = trace->prog;
637 struct drgn_register_state *regs = trace->frames[frame].regs;
638 if (!drgn_register_state_has_register(regs, reg->regno))
639 return false;
640 const struct drgn_register_layout *layout =
641 &prog->platform.arch->register_layout[reg->regno];
642 if (layout->size > sizeof(*ret))
643 return false;
644 copy_lsbytes(ret, sizeof(*ret), HOST_LITTLE_ENDIAN,
645 &regs->buf[layout->offset], layout->size,
646 drgn_platform_is_little_endian(&prog->platform));
647 return true;
648 }
649
650 static struct drgn_error *
651 drgn_get_stack_trace_obj(struct drgn_object *res,
652 const struct drgn_object *thread_obj,
653 bool *is_pt_regs_ret)
654 {
655 struct drgn_program *prog = drgn_object_program(res);
656
657 struct drgn_type *type = drgn_underlying_type(thread_obj->type);
658 if (drgn_type_kind(type) == DRGN_TYPE_STRUCT &&
659 strcmp(drgn_type_tag(type), "pt_regs") == 0) {
660 *is_pt_regs_ret = true;
661 return drgn_object_read(res, thread_obj);
662 }
663
664 if (drgn_type_kind(type) != DRGN_TYPE_POINTER)
665 goto type_error;
666 type = drgn_underlying_type(drgn_type_type(type).type);
667 if (drgn_type_kind(type) != DRGN_TYPE_STRUCT)
668 goto type_error;
669
670 if ((prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) &&
671 strcmp(drgn_type_tag(type), "task_struct") == 0) {
672 *is_pt_regs_ret = false;
673 return drgn_object_read(res, thread_obj);
674 } else if (strcmp(drgn_type_tag(type), "pt_regs") == 0) {
675 *is_pt_regs_ret = true;
676 /*
677 * If the drgn_object_read() call fails, we're breaking
678 * the rule of not modifying the result on error, but we
679 * don't care in this context.
680 */
681 struct drgn_error *err = drgn_object_dereference(res,
682 thread_obj);
683 if (err)
684 return err;
685 return drgn_object_read(res, res);
686 }
687
688 type_error:
689 return drgn_error_format(DRGN_ERROR_TYPE,
690 "expected struct pt_regs, struct pt_regs *%s, or int",
691 (prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) ?
692 ", struct task_struct *" : "");
693 }
694
695 static struct drgn_error *
696 drgn_get_initial_registers_from_prstatus(struct drgn_program *prog,
697 const struct nstring *prstatus,
698 struct drgn_register_state **ret)
699 {
700 if (!prog->platform.arch->prstatus_get_initial_registers) {
701 return drgn_error_format(DRGN_ERROR_NOT_IMPLEMENTED,
702 "core dump stack unwinding is not supported for %s architecture",
703 prog->platform.arch->name);
704 }
705 return prog->platform.arch->prstatus_get_initial_registers(prog,
706 prstatus->str,
707 prstatus->len,
708 ret);
709 }
710
711 static struct drgn_error *
712 drgn_get_initial_registers_from_kernel_core_dump(struct drgn_program *prog,
713 uint64_t cpu,
714 struct drgn_register_state **ret)
715 {
716 struct drgn_error *err;
717 // For kernel core dumps, we can't look up PRSTATUS notes by PID for
718 // multiple reasons:
719 //
720 // 1. Each CPU has its own idle task, all with PID 0.
721 // 2. Kdump on s390x populates the PID field of the PRSTATUS notes with
722 // the CPU number + 1.
723 // 3. QEMU's dump-guest-memory command does the same.
724 //
725 // Instead, we have to look it up by CPU number. The way to do this
726 // depends on whether the core dump was generated by a real crash (e.g.,
727 // by kdump) or while the kernel was running (e.g., by a hypervisor) as
728 // well as the architecture.
729 //
730 // Note that all of this is inherently racy: cpu_curr() [1], current
731 // [2], registers [3], and the stack pointer [4] are all updated at
732 // different times. This might result in confusing stack traces during a
733 // context switch, but there's not much we can do about that.
734 //
735 // 1: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/sched/core.c?h=v6.6#n6672
736 // 2: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/kernel/process_64.c?h=v6.6#n623
737 // 3: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/entry/entry_64.S?h=v6.6#n267
738 // 4: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/entry/entry_64.S?h=v6.6#n249
739 if (prog->vmcoreinfo.have_crashtime
740 && prog->platform.arch->arch != DRGN_ARCH_S390X) {
741 // If the VMCOREINFO contains CRASHTIME, the core dump is for a
742 // real crash, probably from kdump. Core dumps generated by
743 // kdump should have a PRSTATUS note for each CPU, in order by
744 // CPU number. There are a couple of complications:
745 //
746 // 1. Offline CPUs are skipped.
747 // 2. CPUs that don't respond to the kdump NMI are skipped.
748 //
749 // So, we can't reliably find the note for a given CPU solely
750 // from the note metadata. Instead, we get it directly from
751 // crash_notes, the per-CPU variable in the kernel where the
752 // notes are stored during a crash.
753 //
754 // s390x doesn't use crash_notes.
755 DRGN_OBJECT(tmp, prog);
756 err = drgn_program_find_object(prog, "crash_notes", NULL,
757 DRGN_FIND_OBJECT_ANY, &tmp);
758 if (err)
759 return err;
760 err = linux_helper_per_cpu_ptr(&tmp, &tmp, cpu);
761 if (err)
762 return err;
763 err = drgn_object_dereference(&tmp, &tmp);
764 if (err)
765 return err;
766 err = drgn_object_read(&tmp, &tmp);
767 if (err)
768 return err;
769 if (tmp.encoding != DRGN_OBJECT_ENCODING_BUFFER) {
770 return drgn_error_create(DRGN_ERROR_LOOKUP,
771 "could not parse crash_notes");
772 }
773
774 const void *p = drgn_object_buffer(&tmp);
775 size_t size = drgn_object_size(&tmp);
776 bool bswap = drgn_platform_bswap(&prog->platform);
777 GElf_Nhdr nhdr;
778 const char *name;
779 const void *desc;
780 while (next_elf_note(&p, &size, 4, bswap, &nhdr, &name, &desc)
781 && nhdr.n_namesz > 0) {
782 if (nhdr.n_namesz == sizeof("CORE")
783 && memcmp(name, "CORE", sizeof("CORE")) == 0
784 && nhdr.n_type == NT_PRSTATUS) {
785 struct nstring prstatus = { desc, nhdr.n_descsz };
786 return drgn_get_initial_registers_from_prstatus(prog,
787 &prstatus,
788 ret);
789 }
790 }
791 } else {
792 // Either the VMCOREINFO doesn't contain CRASHTIME, so the core
793 // dump was captured while the kernel was running, or it is from
794 // kdump on s390x. In both cases, we can use the PRSTATUS note
795 // with a PID of CPU number + 1. (This assumes that other
796 // hypervisors do the same thing as QEMU dump-guest-memory,
797 // which may not be the case. QEMU itself as of version 9.1
798 // doesn't do it correctly on ppc64 or s390x:
799 // https://lore.kernel.org/linux-debuggers/[email protected]/T/).
800 struct nstring prstatus;
801 err = drgn_program_find_prstatus(prog, cpu + 1, &prstatus);
802 if (err)
803 return err;
804 if (prstatus.str) {
805 return drgn_get_initial_registers_from_prstatus(prog,
806 &prstatus,
807 ret);
808 }
809 }
810 return drgn_error_format(DRGN_ERROR_LOOKUP,
811 "registers for CPU %" PRIu64 " were not saved",
812 cpu);
813 }
814
815 static struct drgn_error *
816 drgn_get_initial_registers(struct drgn_program *prog, uint32_t tid,
817 const struct drgn_object *thread_obj,
818 struct drgn_register_state **ret)
819 {
820 struct drgn_error *err;
821
822 DRGN_OBJECT(obj, prog);
823 DRGN_OBJECT(tmp, prog);
824
825 /* First, try pt_regs. */
826 if (thread_obj) {
827 bool is_pt_regs;
828 err = drgn_get_stack_trace_obj(&obj, thread_obj, &is_pt_regs);
829 if (err)
830 return err;
831
832 if (is_pt_regs) {
833 assert(obj.encoding == DRGN_OBJECT_ENCODING_BUFFER);
834 assert(obj.kind == DRGN_OBJECT_VALUE);
835 if (!prog->platform.arch->pt_regs_get_initial_registers) {
836 return drgn_error_format(DRGN_ERROR_NOT_IMPLEMENTED,
837 "pt_regs stack unwinding is not supported for %s architecture",
838 prog->platform.arch->name);
839 }
840 return prog->platform.arch->pt_regs_get_initial_registers(&obj,
841 ret);
842 }
843 } else if (prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) {
844 err = drgn_program_find_object(prog, "init_pid_ns", NULL,
845 DRGN_FIND_OBJECT_ANY, &tmp);
846 if (err)
847 return err;
848 err = drgn_object_address_of(&tmp, &tmp);
849 if (err)
850 return err;
851 err = linux_helper_find_task(&obj, &tmp, tid);
852 if (err)
853 return err;
854 bool found;
855 err = drgn_object_bool(&obj, &found);
856 if (err)
857 return err;
858 if (!found) {
859 if (tid == 0) {
860 return drgn_error_create(DRGN_ERROR_LOOKUP,
861 "task not found; "
862 "use stack_trace(idle_task(cpu)) for PID 0");
863 } else {
864 return drgn_error_create(DRGN_ERROR_LOOKUP,
865 "task not found");
866 }
867 }
868 }
869
870 if (prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) {
871 bool on_cpu;
872 err = linux_helper_task_on_cpu(&obj, &on_cpu);
873 if (err)
874 return err;
875 if (on_cpu) {
876 if (prog->flags & DRGN_PROGRAM_IS_LIVE) {
877 return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
878 "cannot unwind stack of running task");
879 }
880 uint64_t cpu;
881 err = linux_helper_task_cpu(&obj, &cpu);
882 if (err)
883 return err;
884 return drgn_get_initial_registers_from_kernel_core_dump(prog,
885 cpu,
886 ret);
887 }
888 if (!prog->platform.arch->linux_kernel_get_initial_registers) {
889 return drgn_error_format(DRGN_ERROR_NOT_IMPLEMENTED,
890 "Linux kernel stack unwinding is not supported for %s architecture",
891 prog->platform.arch->name);
892 }
893 return prog->platform.arch->linux_kernel_get_initial_registers(&obj,
894 ret);
895 } else if (drgn_program_is_userspace_process(prog)) {
896 return drgn_error_create(DRGN_ERROR_NOT_IMPLEMENTED,
897 "stack unwinding is not yet supported for live processes");
898 } else {
899 struct nstring prstatus;
900 err = drgn_program_find_prstatus(prog, tid, &prstatus);
901 if (err)
902 return err;
903 if (!prstatus.str) {
904 return drgn_error_create(DRGN_ERROR_LOOKUP,
905 "thread not found");
906 }
907 return drgn_get_initial_registers_from_prstatus(prog, &prstatus,
908 ret);
909 }
910 }
911
912 static void drgn_add_to_register(void *dst, size_t dst_size, const void *src,
913 size_t src_size, int64_t addend,
914 bool little_endian)
915 {
916 while (addend && dst_size && src_size) {
917 uint64_t uvalue;
918 copy_lsbytes(&uvalue, sizeof(uvalue), HOST_LITTLE_ENDIAN, src,
919 src_size, little_endian);
920 size_t n = min(sizeof(uvalue), src_size);
921 if (little_endian)
922 src = (char *)src + n;
923 src_size -= n;
924
925 bool carry = __builtin_add_overflow(uvalue, (uint64_t)addend,
926 &uvalue);
927 addend = (addend < 0 ? -1 : 0) + carry;
928
929 copy_lsbytes(dst, dst_size, little_endian, &uvalue,
930 sizeof(uvalue), HOST_LITTLE_ENDIAN);
931 n = min(sizeof(uvalue), dst_size);
932 if (little_endian)
933 dst = (char *)dst + n;
934 dst_size -= n;
935 }
936 if (dst != src) {
937 copy_lsbytes(dst, dst_size, little_endian, src, src_size,
938 little_endian);
939 }
940 }
941
942
943 static struct drgn_error *
944 drgn_stack_trace_add_frames(struct drgn_stack_trace **trace,
945 size_t *trace_capacity,
946 struct drgn_register_state *regs)
947 {
948 struct drgn_error *err;
949
950 if (!regs->module) {
951 err = drgn_stack_trace_append_frame(trace, trace_capacity, regs,
952 NULL, 0, 0);
953 goto out;
954 }
955
956 uint64_t pc = regs->_pc - !regs->interrupted;
957 uint64_t bias;
958 Dwarf_Die *scopes;
959 size_t num_scopes;
960 err = drgn_module_find_dwarf_scopes(regs->module, pc, &bias, &scopes,
961 &num_scopes);
962 if (err)
963 goto out;
964 pc -= bias;
965
966 size_t orig_num_frames = (*trace)->num_frames;
967 /*
968 * Walk backwards through scopes, splitting into frames. Stop at index 1
969 * because 0 must be a unit DIE.
970 */
971 size_t frame_end = num_scopes;
972 for (size_t i = num_scopes; i-- > 1;) {
973 bool has_pc;
974 if (i == num_scopes - 1) {
975 /*
976 * The last scope is guaranteed to contain PC, so avoid
977 * a call to dwarf_haspc().
978 */
979 has_pc = true;
980 } else {
981 int r = dwarf_haspc(&scopes[i], pc);
982 if (r < 0) {
983 err = drgn_error_libdw();
984 goto out_scopes;
985 }
986 has_pc = r > 0;
987 }
988 if (has_pc) {
989 Dwarf_Die *frame_scopes;
990 switch (dwarf_tag(&scopes[i])) {
991 case DW_TAG_subprogram:
992 /*
993 * Reuse the original scopes array (shrinking it
994 * if necessary).
995 */
996 if (frame_end == num_scopes ||
997 !(frame_scopes = realloc(scopes,
998 frame_end *
999 sizeof(scopes[i]))))
1000 frame_scopes = scopes;
1001 err = drgn_stack_trace_append_frame(trace,
1002 trace_capacity,
1003 regs,
1004 frame_scopes,
1005 frame_end,
1006 i);
1007 if (err) {
1008 free(frame_scopes);
1009 /*
1010 * We stole scopes for frame_scopes, so
1011 * not out_scopes.
1012 */
1013 goto out;
1014 }
1015 /*
1016 * Added the DW_TAG_subprogram frame. We're
1017 * done.
1018 */
1019 return NULL;
1020 case DW_TAG_inlined_subroutine:
1021 frame_scopes = memdup(&scopes[i],
1022 (frame_end - i) *
1023 sizeof(scopes[i]));
1024 if (!frame_scopes) {
1025 err = &drgn_enomem;
1026 goto out_scopes;
1027 }
1028 err = drgn_stack_trace_append_frame(trace,
1029 trace_capacity,
1030 regs,
1031 frame_scopes,
1032 frame_end - i,
1033 0);
1034 if (err) {
1035 free(frame_scopes);
1036 goto out_scopes;
1037 }
1038 frame_end = i;
1039 break;
1040 default:
1041 break;
1042 }
1043 } else {
1044 /*
1045 * This DIE doesn't contain PC. Ignore it and everything
1046 * after it.
1047 */
1048 frame_end = i;
1049 }
1050 }
1051
1052 /*
1053 * We didn't find a matching DW_TAG_subprogram. Free any matching
1054 * DW_TAG_inlined_subroutine frames we found.
1055 */
1056 for (size_t i = orig_num_frames; i < (*trace)->num_frames; i++)
1057 free((*trace)->frames[i].scopes);
1058 (*trace)->num_frames = orig_num_frames;
1059 /* If we at least found the unit DIE, keep it. */
1060 if (num_scopes > 0) {
1061 Dwarf_Die *frame_scopes;
1062 if (!(frame_scopes = realloc(scopes, sizeof(scopes[0]))))
1063 frame_scopes = scopes;
1064 err = drgn_stack_trace_append_frame(trace, trace_capacity, regs,
1065 frame_scopes, 1, 1);
1066 if (err) {
1067 free(frame_scopes);
1068 goto out;
1069 }
1070 return NULL;
1071 }
1072 /* Otherwise, add a scopeless frame. */
1073 err = drgn_stack_trace_append_frame(trace, trace_capacity, regs, NULL,
1074 0, 0);
1075 out_scopes:
1076 free(scopes);
1077 out:
1078 if (err)
1079 drgn_register_state_destroy(regs);
1080 return err;
1081 }
1082
1083 static struct drgn_error *
1084 drgn_unwind_one_register(struct drgn_program *prog, struct drgn_elf_file *file,
1085 const struct drgn_cfi_rule *rule,
1086 const struct drgn_register_state *regs, void *buf,
1087 size_t size)
1088 {
1089 struct drgn_error *err;
1090 bool little_endian = drgn_platform_is_little_endian(&prog->platform);
1091 SWITCH_ENUM(rule->kind) {
1092 case DRGN_CFI_RULE_UNDEFINED:
1093 return &drgn_not_found;
1094 case DRGN_CFI_RULE_AT_CFA_PLUS_OFFSET: {
1095 struct optional_uint64 cfa = drgn_register_state_get_cfa(regs);
1096 if (!cfa.has_value)
1097 return &drgn_not_found;
1098 err = drgn_program_read_memory(prog, buf,
1099 cfa.value + rule->offset, size,
1100 false);
1101 break;
1102 }
1103 case DRGN_CFI_RULE_CFA_PLUS_OFFSET: {
1104 struct optional_uint64 cfa = drgn_register_state_get_cfa(regs);
1105 if (!cfa.has_value)
1106 return &drgn_not_found;
1107 cfa.value += rule->offset;
1108 copy_lsbytes(buf, size, little_endian, &cfa.value,
1109 sizeof(cfa.value), HOST_LITTLE_ENDIAN);
1110 return NULL;
1111 }
1112 case DRGN_CFI_RULE_AT_REGISTER_PLUS_OFFSET:
1113 case DRGN_CFI_RULE_AT_REGISTER_ADD_OFFSET: {
1114 if (!drgn_register_state_has_register(regs, rule->regno))
1115 return &drgn_not_found;
1116 const struct drgn_register_layout *layout =
1117 &prog->platform.arch->register_layout[rule->regno];
1118 uint64_t address;
1119 copy_lsbytes(&address, sizeof(address), HOST_LITTLE_ENDIAN,
1120 &regs->buf[layout->offset], layout->size,
1121 little_endian);
1122 if (rule->kind == DRGN_CFI_RULE_AT_REGISTER_PLUS_OFFSET)
1123 address += rule->offset;
1124 err = drgn_program_read_memory(prog, buf, address, size, false);
1125 if (!err && rule->kind == DRGN_CFI_RULE_AT_REGISTER_ADD_OFFSET) {
1126 drgn_add_to_register(buf, size, buf, size, rule->offset,
1127 little_endian);
1128 }
1129 break;
1130 }
1131 case DRGN_CFI_RULE_REGISTER_PLUS_OFFSET: {
1132 if (!drgn_register_state_has_register(regs, rule->regno))
1133 return &drgn_not_found;
1134 const struct drgn_register_layout *layout =
1135 &prog->platform.arch->register_layout[rule->regno];
1136 drgn_add_to_register(buf, size, &regs->buf[layout->offset],
1137 layout->size, rule->offset, little_endian);
1138 return NULL;
1139 }
1140 case DRGN_CFI_RULE_AT_DWARF_EXPRESSION:
1141 case DRGN_CFI_RULE_DWARF_EXPRESSION:
1142 // It is possible for file to be NULL when using built-in ORC.
1143 // However, it should be impossible to encounter a DWARF
1144 // expression for built-in ORC.
1145 assert(file != NULL);
1146 err = drgn_eval_cfi_dwarf_expression(prog, file, rule, regs,
1147 buf, size);
1148 break;
1149 case DRGN_CFI_RULE_CONSTANT:
1150 copy_lsbytes(buf, size, little_endian, &rule->constant,
1151 sizeof(rule->constant), HOST_LITTLE_ENDIAN);
1152 return NULL;
1153 default:
1154 UNREACHABLE();
1155 }
1156 /*
1157 * If we couldn't read from memory, leave the register unknown instead
1158 * of failing hard.
1159 */
1160 if (err && err->code == DRGN_ERROR_FAULT) {
1161 drgn_error_destroy(err);
1162 err = &drgn_not_found;
1163 }
1164 return err;
1165 }
1166
1167 static struct drgn_error *drgn_unwind_cfa(struct drgn_program *prog,
1168 struct drgn_elf_file *file,
1169 const struct drgn_cfi_row *row,
1170 struct drgn_register_state *regs)
1171 {
1172 struct drgn_error *err;
1173 struct drgn_cfi_rule rule;
1174 drgn_cfi_row_get_cfa(row, &rule);
1175 uint8_t address_size = drgn_platform_address_size(&prog->platform);
1176 char buf[8];
1177 err = drgn_unwind_one_register(prog, file, &rule, regs, buf,
1178 address_size);
1179 if (!err) {
1180 uint64_t cfa;
1181 copy_lsbytes(&cfa, sizeof(cfa), HOST_LITTLE_ENDIAN, buf,
1182 address_size,
1183 drgn_platform_is_little_endian(&prog->platform));
1184 drgn_register_state_set_cfa(prog, regs, cfa);
1185 } else if (err == &drgn_not_found) {
1186 err = NULL;
1187 }
1188 return err;
1189 }
1190
1191 static struct drgn_error *
1192 drgn_unwind_with_cfi(struct drgn_program *prog, struct drgn_cfi_row **row,
1193 struct drgn_register_state *regs,
1194 struct drgn_register_state **ret)
1195 {
1196 struct drgn_error *err;
1197
1198 if (!regs->module)
1199 return &drgn_not_found;
1200
1201 struct drgn_elf_file *file;
1202 bool interrupted;
1203 drgn_register_number ret_addr_regno;
1204 /* If we found the module, then we must have the PC. */
1205 err = drgn_module_find_cfi(prog, regs->module,
1206 regs->_pc - !regs->interrupted, &file, row,
1207 &interrupted, &ret_addr_regno);
1208 if (err)
1209 return err;
1210
1211 err = drgn_unwind_cfa(prog, file, *row, regs);
1212 if (err)
1213 return err;
1214
1215 size_t num_regs = (*row)->num_regs;
1216 if (num_regs == 0)
1217 return &drgn_stop;
1218
1219 const struct drgn_register_layout *layout =
1220 &prog->platform.arch->register_layout[num_regs - 1];
1221 struct drgn_register_state *unwound =
1222 drgn_register_state_create_impl(layout->offset + layout->size,
1223 num_regs, interrupted);
1224 if (!unwound)
1225 return &drgn_enomem;
1226
1227 bool has_any_register = false;
1228 for (drgn_register_number regno = 0; regno < num_regs; regno++) {
1229 struct drgn_cfi_rule rule;
1230 drgn_cfi_row_get_register(*row, regno, &rule);
1231 layout = &prog->platform.arch->register_layout[regno];
1232 err = drgn_unwind_one_register(prog, file, &rule, regs,
1233 &unwound->buf[layout->offset],
1234 layout->size);
1235 if (!err) {
1236 drgn_register_state_set_has_register(unwound, regno);
1237 has_any_register = true;
1238 } else if (err != &drgn_not_found) {
1239 drgn_register_state_destroy(unwound);
1240 return err;
1241 }
1242 }
1243 if (!has_any_register) {
1244 /* Couldn't unwind any registers. We're done. */
1245 drgn_register_state_destroy(unwound);
1246 return &drgn_stop;
1247 }
1248 if (prog->platform.arch->demangle_cfi_registers)
1249 prog->platform.arch->demangle_cfi_registers(prog, unwound);
1250 if (drgn_register_state_has_register(unwound, ret_addr_regno)) {
1251 layout = &prog->platform.arch->register_layout[ret_addr_regno];
1252 drgn_register_state_set_pc_from_register_impl(prog, unwound,
1253 ret_addr_regno,
1254 layout->offset,
1255 layout->size);
1256 } else {
1257 /* If we could not determine the return address despite having a
1258 * CFI rule for it, then we should try the fallback unwinder. */
1259 struct drgn_cfi_rule rule;
1260 drgn_cfi_row_get_register(*row, ret_addr_regno, &rule);
1261 if (rule.kind != DRGN_CFI_RULE_UNDEFINED) {
1262 drgn_register_state_destroy(unwound);
1263 return &drgn_not_found;
1264 }
1265 }
1266 *ret = unwound;
1267 return NULL;
1268 }
1269
1270 static bool drgn_is_bad_call(const struct drgn_register_state *regs)
1271 {
1272 // If the program counter is 0, it's likely that a NULL function pointer
1273 // was called. Other than that, it's difficult to differentiate a bad
1274 // program counter from a valid program counter that we don't know about
1275 // (e.g., because it's JIT compiled). We can add heuristics in the
1276 // future.
1277 struct optional_uint64 pc = drgn_register_state_get_pc(regs);
1278 return pc.has_value && pc.value == 0;
1279 }
1280
1281 static struct drgn_error *drgn_get_stack_trace(struct drgn_program *prog,
1282 uint32_t tid,
1283 const struct drgn_object *obj,
1284 const struct nstring *prstatus,
1285 struct drgn_stack_trace **ret)
1286 {
1287 struct drgn_error *err;
1288
1289 if (!prog->has_platform) {
1290 return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
1291 "cannot unwind stack without platform");
1292 }
1293
1294 size_t trace_capacity = 1;
1295 struct drgn_stack_trace *trace =
1296 malloc(offsetof(struct drgn_stack_trace,
1297 frames[trace_capacity]));
1298 if (!trace)
1299 return &drgn_enomem;
1300 trace->prog = prog;
1301 trace->num_frames = 0;
1302
1303 struct drgn_cfi_row *row = drgn_empty_cfi_row;
1304
1305 struct drgn_register_state *regs;
1306 if (prstatus) {
1307 err = drgn_get_initial_registers_from_prstatus(prog, prstatus,
1308 &regs);
1309 } else {
1310 err = drgn_get_initial_registers(prog, tid, obj, &regs);
1311 }
1312 if (err)
1313 goto out;
1314
1315 /* Limit iterations so we don't get caught in a loop. */
1316 for (int i = 0; i < 1024; i++) {
1317 err = drgn_stack_trace_add_frames(&trace, &trace_capacity,
1318 regs);
1319 if (err)
1320 goto out;
1321
1322 err = drgn_unwind_with_cfi(prog, &row, regs, &regs);
1323 if (err == &drgn_not_found) {
1324 if (drgn_is_bad_call(regs)
1325 && prog->platform.arch->bad_call_unwind) {
1326 err = prog->platform.arch->bad_call_unwind(prog,
1327 regs,
1328 &regs);
1329 } else {
1330 err = prog->platform.arch->fallback_unwind(prog,
1331 regs,
1332 &regs);
1333 }
1334 }
1335 if (err == &drgn_stop)
1336 break;
1337 else if (err)
1338 goto out;
1339 }
1340
1341 err = NULL;
1342 out:
1343 drgn_cfi_row_destroy(row);
1344 if (err) {
1345 drgn_stack_trace_destroy(trace);
1346 } else {
1347 drgn_stack_trace_shrink_to_fit(&trace, trace_capacity);
1348 *ret = trace;
1349 }
1350 return err;
1351 }
1352
1353 LIBDRGN_PUBLIC struct drgn_error *
1354 drgn_program_stack_trace(struct drgn_program *prog, uint32_t tid,
1355 struct drgn_stack_trace **ret)
1356 {
1357 return drgn_get_stack_trace(prog, tid, NULL, NULL, ret);
1358 }
1359
1360 LIBDRGN_PUBLIC struct drgn_error *
1361 drgn_program_stack_trace_from_pcs(struct drgn_program *prog, const uint64_t *pcs,
1362 size_t pcs_size, struct drgn_stack_trace **ret)
1363 {
1364 struct drgn_stack_trace *trace = malloc_flexible_array(
1365 struct drgn_stack_trace, frames, pcs_size);
1366 struct drgn_error *err;
1367 size_t trace_capacity = pcs_size;
1368
1369 if (!trace)
1370 return &drgn_enomem;
1371
1372 trace->prog = prog;
1373 trace->num_frames = 0;
1374 for (size_t i = 0; i != pcs_size; ++i) {
1375 struct drgn_register_state *regs =
1376 drgn_register_state_create_impl(0, 0, false);
1377 drgn_register_state_set_pc(prog, regs, pcs[i]);
1378
1379 err = drgn_stack_trace_add_frames(&trace, &trace_capacity, regs);
1380 if (err) {
1381 drgn_stack_trace_destroy(trace);
1382 return err;
1383 }
1384 }
1385
1386 drgn_stack_trace_shrink_to_fit(&trace, trace_capacity);
1387 *ret = trace;
1388 return NULL;
1389 }
1390
1391 LIBDRGN_PUBLIC struct drgn_error *
1392 drgn_object_stack_trace(const struct drgn_object *obj,
1393 struct drgn_stack_trace **ret)
1394 {
1395 struct drgn_error *err;
1396
1397 if (drgn_type_kind(drgn_underlying_type(obj->type)) == DRGN_TYPE_INT) {
1398 union drgn_value value;
1399
1400 err = drgn_object_read_integer(obj, &value);
1401 if (err)
1402 return err;
1403 return drgn_get_stack_trace(drgn_object_program(obj),
1404 value.uvalue, NULL, NULL, ret);
1405 } else {
1406 return drgn_get_stack_trace(drgn_object_program(obj), 0, obj,
1407 NULL, ret);
1408 }
1409 }
1410
1411 LIBDRGN_PUBLIC struct drgn_error *
1412 drgn_thread_stack_trace(struct drgn_thread *thread,
1413 struct drgn_stack_trace **ret)
1414 {
1415 return drgn_get_stack_trace(thread->prog, thread->tid,
1416 (thread->prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL)
1417 ? &thread->object : NULL,
1418 thread->prstatus.str ? &thread->prstatus : NULL,
1419 ret);
1420 }
1421
1422 // struct drgn_source_location_list is a lie. struct drgn_stack_trace already
1423 // tracks everything we need for it, so we cast it to a fake type and provide
1424 // functions that operate on that fake type (which mainly wrap stack trace
1425 // functions).
1426 static struct drgn_stack_trace *
1427 locs_to_trace(struct drgn_source_location_list *locs)
1428 {
1429 return (struct drgn_stack_trace *)locs;
1430 }
1431
1432 LIBDRGN_PUBLIC
1433 void drgn_source_location_list_destroy(struct drgn_source_location_list *locs)
1434 {
1435 drgn_stack_trace_destroy(locs_to_trace(locs));
1436 }
1437
1438 LIBDRGN_PUBLIC struct drgn_program *
1439 drgn_source_location_list_program(struct drgn_source_location_list *locs)
1440 {
1441 return drgn_stack_trace_program(locs_to_trace(locs));
1442 }
1443
1444 LIBDRGN_PUBLIC
1445 size_t drgn_source_location_list_length(struct drgn_source_location_list *locs)
1446 {
1447 return locs_to_trace(locs)->num_frames;
1448 }
1449
1450 LIBDRGN_PUBLIC struct drgn_error *
1451 drgn_format_source_location_list(struct drgn_source_location_list *locs,
1452 char **ret)
1453 {
1454 struct drgn_error *err;
1455 struct drgn_stack_trace *trace = locs_to_trace(locs);
1456 STRING_BUILDER(sb);
1457 for (size_t frame = 0; frame < trace->num_frames; frame++) {
1458 if (trace->num_frames > 1
1459 && !string_builder_appendf(&sb, "#%-2zu ", frame))
1460 return &drgn_enomem;
1461
1462 err = drgn_format_stack_frame_source_impl(trace, frame, &sb);
1463 if (err)
1464 return err;
1465
1466 if (frame != trace->num_frames - 1
1467 && !string_builder_appendc(&sb, '\n'))
1468 return &drgn_enomem;
1469 }
1470 if (!string_builder_null_terminate(&sb))
1471 return &drgn_enomem;
1472 *ret = string_builder_steal(&sb);
1473 return NULL;
1474 }
1475
1476 LIBDRGN_PUBLIC struct drgn_error *
1477 drgn_format_source_location_list_at(struct drgn_source_location_list *locs,
1478 size_t i, char **ret)
1479 {
1480 struct drgn_error *err;
1481 STRING_BUILDER(sb);
1482 err = drgn_format_stack_frame_source_impl(locs_to_trace(locs), i, &sb);
1483 if (err)
1484 return err;
1485 if (!string_builder_null_terminate(&sb))
1486 return &drgn_enomem;
1487 *ret = string_builder_steal(&sb);
1488 return NULL;
1489 }
1490
1491 LIBDRGN_PUBLIC const char *
1492 drgn_source_location_list_source_at(struct drgn_source_location_list *locs,
1493 size_t i, int *line_ret, int *column_ret)
1494 {
1495 return drgn_stack_frame_source(locs_to_trace(locs), i, line_ret,
1496 column_ret);
1497 }
1498
1499 LIBDRGN_PUBLIC struct drgn_error *
1500 drgn_source_location_list_name_at(struct drgn_source_location_list *locs,
1501 size_t i, char **ret)
1502 {
1503 return drgn_stack_frame_source_name(locs_to_trace(locs), i, ret);
1504 }
1505
1506 LIBDRGN_PUBLIC struct drgn_error *
1507 drgn_program_source_location(struct drgn_program *prog, uint64_t address,
1508 struct drgn_source_location_list **ret)
1509 {
1510 struct drgn_error *err;
1511 size_t trace_capacity = 1;
1512 struct drgn_stack_trace *trace =
1513 malloc_flexible_array(struct drgn_stack_trace, frames,
1514 trace_capacity);
1515 if (!trace)
1516 return &drgn_enomem;
1517
1518 trace->prog = prog;
1519 trace->num_frames = 0;
1520
1521 struct drgn_register_state *regs =
1522 drgn_register_state_create_impl(0, 0, true);
1523 drgn_register_state_set_pc(prog, regs, address);
1524 err = drgn_stack_trace_add_frames(&trace, &trace_capacity, regs);
1525 if (err) {
1526 drgn_stack_trace_destroy(trace);
1527 return err;
1528 }
1529
1530 if (!trace->frames[0].num_scopes) {
1531 drgn_stack_trace_destroy(trace);
1532 return drgn_error_create(DRGN_ERROR_LOOKUP,
1533 "source code location not found");
1534 }
1535
1536 drgn_stack_trace_shrink_to_fit(&trace, trace_capacity);
1537 *ret = (struct drgn_source_location_list *)trace;
1538 return NULL;
1539 }
1540
1541 struct drgn_error *drgn_parse_addr2line(const char *address_str,
1542 const char **sym_name_ret,
1543 size_t *sym_name_len_ret,
1544 unsigned long long *offset_ret)
1545 {
1546 const char *p = address_str;
1547 while (isspace(*p))
1548 p++;
1549
1550 if (!*p) {
1551 return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
1552 "expected symbol name or address");
1553 }
1554
1555 const char *sym_name = p;
1556 while (*p && !isspace(*p) && *p != '+')
1557 p++;
1558 size_t sym_name_len = p - sym_name;
1559 if (sym_name_len == 0) {
1560 return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
1561 "expected symbol name");
1562 }
1563
1564 while (isspace(*p))
1565 p++;
1566
1567 unsigned long long offset = 0;
1568 char *end;
1569 if (*p == '+') {
1570 p++;
1571 while (isspace(*p))
1572 p++;
1573
1574 if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
1575 p += 2;
1576 if (!isxdigit(*p)) {
1577 return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
1578 "expected symbol offset");
1579 }
1580 errno = 0;
1581 offset = strtoull(p, &end, 16);
1582 } else {
1583 if (!isdigit(*p)) {
1584 return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
1585 "expected symbol offset");
1586 }
1587 errno = 0;
1588 offset = strtoull(p, &end, 10);
1589 }
1590 if (errno == ERANGE) {
1591 return drgn_error_create(DRGN_ERROR_OVERFLOW,
1592 "symbol offset out of range");
1593 } else if (errno) {
1594 return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
1595 "invalid symbol offset");
1596 } else if (end == p) {
1597 return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
1598 "expected symbol offset");
1599 }
1600 p = end;
1601
1602 while (isspace(*p))
1603 p++;
1604
1605 if (*p) {
1606 return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
1607 "unexpected input after symbol offset");
1608 }
1609 } else if (*p) {
1610 return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
1611 "unexpected input after symbol name or address");
1612 }
1613
1614 if (sym_name[0] == '0' && (sym_name[1] == 'x' || sym_name[1] == 'X')
1615 && isxdigit(sym_name[2])) {
1616 errno = 0;
1617 unsigned long long address = strtoull(sym_name + 2, &end, 16);
1618 if (end == sym_name + sym_name_len) {
1619 if (errno == ERANGE) {
1620 return drgn_error_create(DRGN_ERROR_OVERFLOW,
1621 "address out of range");
1622 } else if (!errno) {
1623 sym_name = NULL;
1624 sym_name_len = 0;
1625 offset += address;
1626 }
1627 }
1628 }
1629
1630 *sym_name_ret = sym_name;
1631 *sym_name_len_ret = sym_name_len;
1632 *offset_ret = offset;
1633 return NULL;
1634 }
1635
1636 LIBDRGN_PUBLIC struct drgn_error *
1637 drgn_program_addr2line(struct drgn_program *prog,
1638 const char *address_str,
1639 struct drgn_source_location_list **ret)
1640 {
1641 struct drgn_error *err;
1642 const char *sym_name;
1643 size_t sym_name_len;
1644 unsigned long long offset;
1645 err = drgn_parse_addr2line(address_str, &sym_name, &sym_name_len,
1646 &offset);
1647 if (err)
1648 return err;
1649
1650 uint64_t address = 0;
1651 if (sym_name_len) {
1652 _cleanup_free_ char *sym_name_copy = strndup(sym_name, sym_name_len);
1653 if (!sym_name_copy)
1654 return &drgn_enomem;
1655
1656 _cleanup_symbol_ struct drgn_symbol *sym = NULL;
1657 err = drgn_program_find_symbol_by_name(prog, sym_name_copy, &sym);
1658 if (err)
1659 return err;
1660 address = drgn_symbol_address(sym);
1661 }
1662
1663 return drgn_program_source_location(prog, address + offset, ret);
1664 }