summaryrefslogtreecommitdiffstats
path: root/src/stack.c
blob: 362cc065b255ad616fb90af3dd6f0feccc514f2d (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
/* Unwinding of frames like gstack/pstack.
   Copyright (C) 2013 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 <locale.h>
#include <fcntl.h>
#include ELFUTILS_HEADER(dwfl)

#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;

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 unsigned maxframes = 64;

struct frame
{
  Dwarf_Addr pc;
  bool isactivation;
};

struct frames
{
  unsigned frames;
  struct frame frame[];
};

static Dwfl *dwfl = NULL;

static int
frame_callback (Dwfl_Frame *state, void *arg)
{
  struct frames *frames = (struct frames *) arg;
  unsigned nr = frames->frames;
  if (! dwfl_frame_pc (state, &frames->frame[nr].pc,
		       &frames->frame[nr].isactivation))
    {
      error (0, 0, "%s", dwfl_errmsg (-1));
      return DWARF_CB_ABORT;
    }

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

  return DWARF_CB_OK;
}

static void
print_frames (struct frames *frames)
{
  for (unsigned nr = 0; nr < frames->frames; 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;
      if (mod)
	symname = dwfl_module_addrname (mod, pc_adjusted);

      // 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;

      printf ("#%-2u 0x%0*" PRIx64, nr, width, (uint64_t) pc);

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

      if (symname != NULL)
	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 "+%" PRIx64, start, pc_adjusted - start);
	    }
	}

      if (show_source)
	{
	  Dwfl_Line *lineobj = dwfl_module_getsrc(mod, pc_adjusted);
	  if (lineobj)
	    {
	      int line, col;
	      const char* sname;
	      line = col = -1;
	      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 int
thread_callback (Dwfl_Thread *thread, void *thread_arg)
{
  printf ("TID %ld:\n", (long) dwfl_thread_tid (thread));
  struct frames *frames = (struct frames *) thread_arg;
  frames->frames = 0;
  switch (dwfl_thread_getframes (thread, frame_callback, thread_arg))
    {
    case DWARF_CB_OK:
    case DWARF_CB_ABORT:
      break;
    case -1:
      error (0, 0, "dwfl_thread_getframes: %s", dwfl_errmsg (-1));
      break;
    default:
      abort ();
    }
  print_frames (frames);
  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 ARGP_KEY_INIT:
      state->child_inputs[0] = state->input;
      break;

    case 'm':
      show_module = true;
      break;

    case 's':
      show_source = true;
      break;

    case 'a':
      show_activation = true;
      break;

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

    case 'b':
      show_build_id = 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 1 or higher."));
	  return EINVAL;
	}
      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[] =
    {
      { "activation",  'a', NULL, 0,
	N_("Additionally show frame activation"), 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, module and source)"), 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 (defaults to 64)"), 0 },
      { NULL, 0, NULL, 0, NULL, 0 }
    };

  const struct argp_child children[] =
    {
      { .argp = dwfl_standard_argp () },
      { .argp = NULL },
    };

  const struct argp argp =
    {
      .options = options,
      .parser = parse_opt,
      .doc = N_("\
Print a stack for each thread in a process or core file.\n\
Only real user processes are supported, no kernel or process maps."),
      .children = children
    };

  int remaining;
  argp_parse (&argp, argc, argv, 0, &remaining, &dwfl);
  assert (dwfl != NULL);
  if (remaining != argc)
    error (2, 0, "eu-stack [-a] [-m] [-b] [-s] [-v] [-1] [-n MAXFRAMES]"
	   " [--debuginfo-path=<path>]"
	   " {-p <process id>|--core=<file> [--executable=<file>]|--help}");

  /* dwfl_linux_proc_report has been already called from dwfl_standard_argp's
     parse_opt function.  */
  if (dwfl_report_end (dwfl, NULL, NULL) != 0)
    error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));

  struct frames *frames = malloc (sizeof (struct frames)
				  + sizeof (struct frame) * maxframes);
  frames->frames = 0;

  if (show_one_tid)
    {
      pid_t tid = dwfl_pid (dwfl);
      switch (dwfl_getthread_frames (dwfl, tid, frame_callback, frames))
	{
	case DWARF_CB_OK:
	  break;
	case -1:
	  error (0, 0, "dwfl_getthread_frames (%d): %s", tid,
		 dwfl_errmsg (-1));
	  break;
	default:
	  abort ();
	}
      print_frames (frames);
    }
  else
    {
      switch (dwfl_getthreads (dwfl, thread_callback, frames))
	{
	case DWARF_CB_OK:
	  break;
	case -1:
	  error (0, 0, "dwfl_getthreads: %s", dwfl_errmsg (-1));
	  break;
	default:
	  abort ();
	}
    }
  free (frames);
  dwfl_end (dwfl);

  return 0;
}