summaryrefslogtreecommitdiffstats
path: root/src/src/stack.c
blob: c277dfd03f775a53a6d4704b1525aa0adac23522 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/* Unwinding of frames like gstack/pstack.
   Copyright (C) 2013-2014 Red Hat, Inc.
   This file is part of elfutils.

   This file is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   elfutils is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <config.h>
#include <assert.h>
#include <argp.h>
#include <error.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <string.h>
#include <locale.h>
#include <fcntl.h>
#include ELFUTILS_HEADER(dwfl)

#include <dwarf.h>
#include <system.h>

/* Name and version of program.  */
static void print_version (FILE *stream, struct argp_state *state);
ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;

/* Bug report address.  */
ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;

/* non-printable argp options.  */
#define OPT_DEBUGINFO	0x100
#define OPT_COREFILE	0x101

static bool show_activation = false;
static bool show_module = false;
static bool show_build_id = false;
static bool show_source = false;
static bool show_one_tid = false;
static bool show_quiet = false;
static bool show_raw = false;
static bool show_modules = false;
static bool show_debugname = false;
static bool show_inlines = false;

static int maxframes = 256;

struct frame
{
  Dwarf_Addr pc;
  bool isactivation;
};

struct frames
{
  int frames;
  int allocated;
  struct frame *frame;
};

static Dwfl *dwfl = NULL;
static pid_t pid = 0;
static int core_fd = -1;
static Elf *core = NULL;
static const char *exec = NULL;
static char *debuginfo_path = NULL;

static const Dwfl_Callbacks proc_callbacks =
  {
    .find_elf = dwfl_linux_proc_find_elf,
    .find_debuginfo = dwfl_standard_find_debuginfo,
    .debuginfo_path = &debuginfo_path,
  };

static const Dwfl_Callbacks core_callbacks =
  {
    .find_elf = dwfl_build_id_find_elf,
    .find_debuginfo = dwfl_standard_find_debuginfo,
    .debuginfo_path = &debuginfo_path,
  };

#ifdef USE_DEMANGLE
static size_t demangle_buffer_len = 0;
static char *demangle_buffer = NULL;
#endif

/* Whether any frames have been shown at all.  Determines exit status.  */
static bool frames_shown = false;

/* Program exit codes. All frames shown without any errors is GOOD.
   Some frames shown with some non-fatal errors is an ERROR.  A fatal
   error or no frames shown at all is BAD.  A command line USAGE exit
   is generated by argp_error.  */
#define EXIT_OK     0
#define EXIT_ERROR  1
#define EXIT_BAD    2
#define EXIT_USAGE 64

static int
get_addr_width (Dwfl_Module *mod)
{
  // Try to find the address wide if possible.
  static int width = 0;
  if (width == 0 && mod)
    {
      Dwarf_Addr bias;
      Elf *elf = dwfl_module_getelf (mod, &bias);
      if (elf)
        {
	  GElf_Ehdr ehdr_mem;
	  GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
	  if (ehdr)
	    width = ehdr->e_ident[EI_CLASS] == ELFCLASS32 ? 8 : 16;
	}
    }
  if (width == 0)
    width = 16;

  return width;
}

static int
module_callback (Dwfl_Module *mod, void **userdata __attribute__((unused)),
		 const char *name, Dwarf_Addr start,
		 void *arg __attribute__((unused)))
{
  /* Forces resolving of main elf and debug files. */
  Dwarf_Addr bias;
  Elf *elf = dwfl_module_getelf (mod, &bias);
  Dwarf *dwarf = dwfl_module_getdwarf (mod, &bias);

  Dwarf_Addr end;
  const char *mainfile;
  const char *debugfile;
  const char *modname = dwfl_module_info (mod, NULL, NULL, &end, NULL,
                                          NULL, &mainfile, &debugfile);
  assert (strcmp (modname, name) == 0);

  int width = get_addr_width (mod);
  printf ("0x%0*" PRIx64 "-0x%0*" PRIx64 " %s\n",
	  width, start, width, end, basename (name));

  const unsigned char *id;
  GElf_Addr id_vaddr;
  int id_len = dwfl_module_build_id (mod, &id, &id_vaddr);
  if (id_len > 0)
    {
      printf ("  [");
      do
	printf ("%02" PRIx8, *id++);
      while (--id_len > 0);
      printf ("]\n");
    }

  if (elf != NULL)
    printf ("  %s\n", mainfile != NULL ? mainfile : "-");
  if (dwarf != NULL)
    printf ("  %s\n", debugfile != NULL ? debugfile : "-");

  return DWARF_CB_OK;
}

static int
frame_callback (Dwfl_Frame *state, void *arg)
{
  struct frames *frames = (struct frames *) arg;
  int nr = frames->frames;
  if (! dwfl_frame_pc (state, &frames->frame[nr].pc,
		       &frames->frame[nr].isactivation))
    return -1;

  frames->frames++;
  if (frames->frames == maxframes)
    return DWARF_CB_ABORT;

  if (frames->frames == frames->allocated)
    {
      frames->allocated *= 2;
      frames->frame = realloc (frames->frame,
			       sizeof (struct frame) * frames->allocated);
      if (frames->frame == NULL)
	error (EXIT_BAD, errno, "realloc frames.frame");
    }

  return DWARF_CB_OK;
}

static const char*
die_name (Dwarf_Die *die)
{
  Dwarf_Attribute attr;
  const char *name;
  name = dwarf_formstring (dwarf_attr_integrate (die,
						 DW_AT_MIPS_linkage_name,
						 &attr)
			   ?: dwarf_attr_integrate (die,
						    DW_AT_linkage_name,
						    &attr));
  if (name == NULL)
    name = dwarf_diename (die);

  return name;
}

static void
print_frame (int nr, Dwarf_Addr pc, bool isactivation,
	     Dwarf_Addr pc_adjusted, Dwfl_Module *mod,
	     const char *symname, Dwarf_Die *cudie,
	     Dwarf_Die *die)
{
  int width = get_addr_width (mod);
  printf ("#%-2u 0x%0*" PRIx64, nr, width, (uint64_t) pc);

  if (show_activation)
    printf ("%4s", ! isactivation ? "- 1" : "");

  if (symname != NULL)
    {
#ifdef USE_DEMANGLE
      // Require GNU v3 ABI by the "_Z" prefix.
      if (! show_raw && symname[0] == '_' && symname[1] == 'Z')
	{
	  int status = -1;
	  char *dsymname = __cxa_demangle (symname, demangle_buffer,
					   &demangle_buffer_len, &status);
	  if (status == 0)
	    symname = demangle_buffer = dsymname;
	}
#endif
      printf (" %s", symname);
    }

  const char* fname;
  Dwarf_Addr start;
  fname = dwfl_module_info(mod, NULL, &start,
			   NULL, NULL, NULL, NULL, NULL);
  if (show_module)
    {
      if (fname != NULL)
	printf (" - %s", fname);
    }

  if (show_build_id)
    {
      const unsigned char *id;
      GElf_Addr id_vaddr;
      int id_len = dwfl_module_build_id (mod, &id, &id_vaddr);
      if (id_len > 0)
	{
	  printf ("\n    [");
	  do
	    printf ("%02" PRIx8, *id++);
	  while (--id_len > 0);
	  printf ("]@0x%0" PRIx64 "+0x%" PRIx64,
		  start, pc_adjusted - start);
	}
    }

  if (show_source)
    {
      int line, col;
      const char* sname;
      line = col = -1;
      sname = NULL;
      if (die != NULL)
	{
	  Dwarf_Files *files;
	  if (dwarf_getsrcfiles (cudie, &files, NULL) == 0)
	    {
	      Dwarf_Attribute attr;
	      Dwarf_Word val;
	      if (dwarf_formudata (dwarf_attr (die, DW_AT_call_file, &attr),
				   &val) == 0)
		{
		  sname = dwarf_filesrc (files, val, NULL, NULL);
		  if (dwarf_formudata (dwarf_attr (die, DW_AT_call_line,
						   &attr), &val) == 0)
		    {
		      line = val;
		      if (dwarf_formudata (dwarf_attr (die, DW_AT_call_column,
						       &attr), &val) == 0)
			col = val;
		    }
		}
	    }
	}
      else
	{
	  Dwfl_Line *lineobj = dwfl_module_getsrc(mod, pc_adjusted);
	  if (lineobj)
	    sname = dwfl_lineinfo (lineobj, NULL, &line, &col, NULL, NULL);
	}

      if (sname != NULL)
	{
	  printf ("\n    %s", sname);
	  if (line > 0)
	    {
	      printf (":%d", line);
	      if (col > 0)
		printf (":%d", col);
	    }
	}
    }
  printf ("\n");
}

static void
print_inline_frames (int *nr, Dwarf_Addr pc, bool isactivation,
		     Dwarf_Addr pc_adjusted, Dwfl_Module *mod,
		     const char *symname, Dwarf_Die *cudie, Dwarf_Die *die)
{
  Dwarf_Die *scopes = NULL;
  int nscopes = dwarf_getscopes_die (die, &scopes);
  if (nscopes > 0)
    {
      /* scopes[0] == die, the lowest level, for which we already have
	 the name.  This is the actual source location where it
	 happened.  */
      print_frame ((*nr)++, pc, isactivation, pc_adjusted, mod, symname,
		   NULL, NULL);

      /* last_scope is the source location where the next frame/function
	 call was done. */
      Dwarf_Die *last_scope = &scopes[0];
      for (int i = 1; i < nscopes && (maxframes == 0 || *nr < maxframes); i++)
	{
	  Dwarf_Die *scope = &scopes[i];
	  int tag = dwarf_tag (scope);
	  if (tag != DW_TAG_inlined_subroutine
	      && tag != DW_TAG_entry_point
	      && tag != DW_TAG_subprogram)
	    continue;

	  symname = die_name (scope);
	  print_frame ((*nr)++, pc, isactivation, pc_adjusted, mod, symname,
		       cudie, last_scope);

	  /* Found the "top-level" in which everything was inlined?  */
	  if (tag == DW_TAG_subprogram)
	    break;

	  last_scope = scope;
	}
    }
  free (scopes);
}

static void
print_frames (struct frames *frames, pid_t tid, int dwflerr, const char *what)
{
  if (frames->frames > 0)
    frames_shown = true;

  printf ("TID %d:\n", tid);
  int frame_nr = 0;
  for (int nr = 0; nr < frames->frames && (maxframes == 0
					   || frame_nr < maxframes); nr++)
    {
      Dwarf_Addr pc = frames->frame[nr].pc;
      bool isactivation = frames->frame[nr].isactivation;
      Dwarf_Addr pc_adjusted = pc - (isactivation ? 0 : 1);

      /* Get PC->SYMNAME.  */
      Dwfl_Module *mod = dwfl_addrmodule (dwfl, pc_adjusted);
      const char *symname = NULL;
      Dwarf_Die die_mem;
      Dwarf_Die *die = NULL;
      Dwarf_Die *cudie = NULL;
      if (mod && ! show_quiet)
	{
	  if (show_debugname)
	    {
	      Dwarf_Addr bias = 0;
	      Dwarf_Die *scopes = NULL;
	      cudie = dwfl_module_addrdie (mod, pc_adjusted, &bias);
	      int nscopes = dwarf_getscopes (cudie, pc_adjusted - bias,
					     &scopes);

	      /* Find the first function-like DIE with a name in scope.  */
	      for (int i = 0; symname == NULL && i < nscopes; i++)
		{
		  Dwarf_Die *scope = &scopes[i];
		  int tag = dwarf_tag (scope);
		  if (tag == DW_TAG_subprogram
		      || tag == DW_TAG_inlined_subroutine
		      || tag == DW_TAG_entry_point)
		    symname = die_name (scope);

		  if (symname != NULL)
		    {
		      die_mem = *scope;
		      die = &die_mem;
		    }
		}
	      free (scopes);
	    }

	  if (symname == NULL)
	    symname = dwfl_module_addrname (mod, pc_adjusted);
	}

      if (show_inlines && die != NULL)
	print_inline_frames (&frame_nr, pc, isactivation, pc_adjusted, mod,
			     symname, cudie, die);
      else
	print_frame (frame_nr++, pc, isactivation, pc_adjusted, mod, symname,
		     NULL, NULL);
    }

  if (frames->frames > 0 && frame_nr == maxframes)
    error (0, 0, "tid %d: shown max number of frames "
	   "(%d, use -n 0 for unlimited)", tid, maxframes);
  else if (dwflerr != 0)
    {
      if (frames->frames > 0)
	{
	  unsigned nr = frames->frames - 1;
	  Dwarf_Addr pc = frames->frame[nr].pc;
	  bool isactivation = frames->frame[nr].isactivation;
	  Dwarf_Addr pc_adjusted = pc - (isactivation ? 0 : 1);
	  Dwfl_Module *mod = dwfl_addrmodule (dwfl, pc_adjusted);
	  const char *mainfile = NULL;
	  const char *modname = dwfl_module_info (mod, NULL, NULL, NULL, NULL,
						  NULL, &mainfile, NULL);
	  if (modname == NULL || modname[0] == '\0')
	    {
	      if (mainfile != NULL)
		modname = mainfile;
	      else
		modname = "<unknown>";
	    }
	  error (0, 0, "%s tid %d at 0x%" PRIx64 " in %s: %s", what, tid,
		 pc_adjusted, modname, dwfl_errmsg (dwflerr));
	}
      else
	error (0, 0, "%s tid %d: %s", what, tid, dwfl_errmsg (dwflerr));
    }
}

static int
thread_callback (Dwfl_Thread *thread, void *thread_arg)
{
  struct frames *frames = (struct frames *) thread_arg;
  pid_t tid = dwfl_thread_tid (thread);
  int err = 0;
  frames->frames = 0;
  switch (dwfl_thread_getframes (thread, frame_callback, thread_arg))
    {
    case DWARF_CB_OK:
    case DWARF_CB_ABORT:
      break;
    case -1:
      err = dwfl_errno ();
      break;
    default:
      abort ();
    }
  print_frames (frames, tid, err, "dwfl_thread_getframes");
  return DWARF_CB_OK;
}

static void
print_version (FILE *stream, struct argp_state *state __attribute__ ((unused)))
{
  fprintf (stream, "stack (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
}

static error_t
parse_opt (int key, char *arg __attribute__ ((unused)),
	   struct argp_state *state)
{
  switch (key)
    {
    case 'p':
      pid = atoi (arg);
      if (pid == 0)
	argp_error (state, N_("-p PID should be a positive process id."));
      break;

    case OPT_COREFILE:
      core_fd = open (arg, O_RDONLY);
      if (core_fd < 0)
	error (EXIT_BAD, errno, N_("Cannot open core file '%s'"), arg);
      elf_version (EV_CURRENT);
      core = elf_begin (core_fd, ELF_C_READ_MMAP, NULL);
      if (core == NULL)
	error (EXIT_BAD, 0, "core '%s' elf_begin: %s", arg, elf_errmsg(-1));
      break;

    case 'e':
      exec = arg;
      break;

    case OPT_DEBUGINFO:
      debuginfo_path = arg;
      break;

    case 'm':
      show_module = true;
      break;

    case 's':
      show_source = true;
      break;

    case 'a':
      show_activation = true;
      break;

    case 'd':
      show_debugname = true;
      break;

    case 'i':
      show_inlines = show_debugname = true;
      break;

    case 'v':
      show_activation = show_source = show_module = show_debugname = true;
      show_inlines = true;
      break;

    case 'b':
      show_build_id = true;
      break;

    case 'q':
      show_quiet = true;
      break;

    case 'r':
      show_raw = true;
      break;

    case '1':
      show_one_tid = true;
      break;

    case 'n':
      maxframes = atoi (arg);
      if (maxframes < 0)
	{
	  argp_error (state, N_("-n MAXFRAMES should be 0 or higher."));
	  return EINVAL;
	}
      break;

    case 'l':
      show_modules = true;
      break;

    case ARGP_KEY_END:
      if (core == NULL && exec != NULL)
	argp_error (state,
		    N_("-e EXEC needs a core given by --core."));

      if (pid == 0 && show_one_tid == true)
	argp_error (state,
		    N_("-1 needs a thread id given by -p."));

      if ((pid == 0 && core == NULL) || (pid != 0 && core != NULL))
	argp_error (state,
		    N_("One of -p PID or --core COREFILE should be given."));

      if (pid != 0)
	{
	  dwfl = dwfl_begin (&proc_callbacks);
	  if (dwfl == NULL)
	    error (EXIT_BAD, 0, "dwfl_begin: %s", dwfl_errmsg (-1));

	  int err = dwfl_linux_proc_report (dwfl, pid);
	  if (err < 0)
	    error (EXIT_BAD, 0, "dwfl_linux_proc_report pid %d: %s", pid,
		   dwfl_errmsg (-1));
	  else if (err > 0)
	    error (EXIT_BAD, err, "dwfl_linux_proc_report pid %d", pid);
	}

      if (core != NULL)
	{
	  dwfl = dwfl_begin (&core_callbacks);
	  if (dwfl == NULL)
	    error (EXIT_BAD, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
	  if (dwfl_core_file_report (dwfl, core, exec) < 0)
	    error (EXIT_BAD, 0, "dwfl_core_file_report: %s", dwfl_errmsg (-1));
	}

      if (dwfl_report_end (dwfl, NULL, NULL) != 0)
	error (EXIT_BAD, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));

      if (pid != 0)
	{
	  int err = dwfl_linux_proc_attach (dwfl, pid, false);
	  if (err < 0)
	    error (EXIT_BAD, 0, "dwfl_linux_proc_attach pid %d: %s", pid,
		   dwfl_errmsg (-1));
	  else if (err > 0)
	    error (EXIT_BAD, err, "dwfl_linux_proc_attach pid %d", pid);
	}

      if (core != NULL)
	{
	  if (dwfl_core_file_attach (dwfl, core) < 0)
	    error (EXIT_BAD, 0, "dwfl_core_file_report: %s", dwfl_errmsg (-1));
	}

      /* Makes sure we are properly attached.  */
      if (dwfl_pid (dwfl) < 0)
	error (EXIT_BAD, 0, "dwfl_pid: %s\n", dwfl_errmsg (-1));
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

int
main (int argc, char **argv)
{
  /* We use no threads here which can interfere with handling a stream.  */
  __fsetlocking (stdin, FSETLOCKING_BYCALLER);
  __fsetlocking (stdout, FSETLOCKING_BYCALLER);
  __fsetlocking (stderr, FSETLOCKING_BYCALLER);

  /* Set locale.  */
  (void) setlocale (LC_ALL, "");

  const struct argp_option options[] =
    {
      { NULL, 0, NULL, 0, N_("Input selection options:"), 0 },
      { "pid", 'p', "PID", 0,
	N_("Show stack of process PID"), 0 },
      { "core", OPT_COREFILE, "COREFILE", 0,
	N_("Show stack found in COREFILE"), 0 },
      {  "executable", 'e', "EXEC", 0, N_("(optional) EXECUTABLE that produced COREFILE"), 0 },
      { "debuginfo-path", OPT_DEBUGINFO, "PATH", 0,
	N_("Search path for separate debuginfo files"), 0 },

      { NULL, 0, NULL, 0, N_("Output selection options:"), 0 },
      { "activation",  'a', NULL, 0,
	N_("Additionally show frame activation"), 0 },
      { "debugname",  'd', NULL, 0,
	N_("Additionally try to lookup DWARF debuginfo name for frame address"),
	0 },
      { "inlines",  'i', NULL, 0,
	N_("Additionally show inlined function frames using DWARF debuginfo if available (implies -d)"), 0 },
      { "module",  'm', NULL, 0,
	N_("Additionally show module file information"), 0 },
      { "source",  's', NULL, 0,
	N_("Additionally show source file information"), 0 },
      { "verbose", 'v', NULL, 0,
	N_("Show all additional information (activation, debugname, inlines, module and source)"), 0 },
      { "quiet", 'q', NULL, 0,
	N_("Do not resolve address to function symbol name"), 0 },
      { "raw", 'r', NULL, 0,
	N_("Show raw function symbol names, do not try to demangle names"), 0 },
      { "build-id",  'b', NULL, 0,
	N_("Show module build-id, load address and pc offset"), 0 },
      { NULL, '1', NULL, 0,
	N_("Show the backtrace of only one thread"), 0 },
      { NULL, 'n', "MAXFRAMES", 0,
	N_("Show at most MAXFRAMES per thread (default 256, use 0 for unlimited)"), 0 },
      { "list-modules", 'l', NULL, 0,
	N_("Show module memory map with build-id, elf and debug files detected"), 0 },
      { NULL, 0, NULL, 0, NULL, 0 }
    };

  const struct argp argp =
    {
      .options = options,
      .parser = parse_opt,
      .doc = N_("Print a stack for each thread in a process or core file.\v\
Program exits with return code 0 if all frames were shown without \
any errors.  If some frames were shown, but there were some non-fatal \
errors, possibly causing an incomplete backtrace, the program exits \
with return code 1.  If no frames could be shown, or a fatal error \
occured the program exits with return code 2.  If the program was \
invoked with bad or missing arguments it will exit with return code 64.")
    };

  argp_parse (&argp, argc, argv, 0, NULL, NULL);

  if (show_modules)
    {
      printf ("PID %d - %s module memory map\n", dwfl_pid (dwfl),
	      pid != 0 ? "process" : "core");
      if (dwfl_getmodules (dwfl, module_callback, NULL, 0) != 0)
	error (EXIT_BAD, 0, "dwfl_getmodules: %s", dwfl_errmsg (-1));
    }

  struct frames frames;
  /* When maxframes is zero, then 2048 is just the initial allocation
     that will be increased using realloc in framecallback ().  */
  frames.allocated = maxframes == 0 ? 2048 : maxframes;
  frames.frames = 0;
  frames.frame = malloc (sizeof (struct frame) * frames.allocated);
  if (frames.frame == NULL)
    error (EXIT_BAD, errno, "malloc frames.frame");

  if (show_one_tid)
    {
      int err = 0;
      switch (dwfl_getthread_frames (dwfl, pid, frame_callback, &frames))
	{
	case DWARF_CB_OK:
	case DWARF_CB_ABORT:
	  break;
	case -1:
	  err = dwfl_errno ();
	  break;
	default:
	  abort ();
	}
      print_frames (&frames, pid, err, "dwfl_getthread_frames");
    }
  else
    {
      printf ("PID %d - %s\n", dwfl_pid (dwfl), pid != 0 ? "process" : "core");
      switch (dwfl_getthreads (dwfl, thread_callback, &frames))
	{
	case DWARF_CB_OK:
	case DWARF_CB_ABORT:
	  break;
	case -1:
	  error (0, 0, "dwfl_getthreads: %s", dwfl_errmsg (-1));
	  break;
	default:
	  abort ();
	}
    }
  free (frames.frame);
  dwfl_end (dwfl);

  if (core != NULL)
    elf_end (core);

  if (core_fd != -1)
    close (core_fd);

#ifdef USE_DEMANGLE
  free (demangle_buffer);
#endif

  if (! frames_shown)
    error (EXIT_BAD, 0, N_("Couldn't show any frames."));

  return error_message_count != 0 ? EXIT_ERROR : EXIT_OK;
}