summaryrefslogtreecommitdiff
path: root/src/crash-stack/crash-stack.c
blob: c4fdd03bc60d123a098f17ccefb1ad32dc92e128 (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
/*
 * Copyright (c) 2016-2018 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Authors: Adrian Szyndela <adrian.s@samsung.com>
 *	    Łukasz Stelmach <l.stelmach@samsung.com>
 *	    Rafał Pietruch <r.pietruch@samsung.com>
 */
#define _GNU_SOURCE 1

/**
 * @file crash-stack.c
 * @brief This file contains Main module of call stack unwinding program
 *
 * Crash-stack is a single purpose program. Its duty is to show call stack
 * of a crashed program. Crash-stack must be called with proper arguments:
 * PID of a crashed program.
 */
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <libelf.h>
#include <linux/prctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <assert.h>

#include "crash-stack.h"

#define LOG_TAG "CRASH_STACK"
#include "shared/log.h"

#define BUF_SIZE (BUFSIZ)
#define HEXA 16
#define PERM_LEN 4
#define ADDR_LEN 33
#define STR_ANONY "[anony]"
#define STR_ANONY_LEN 8

#define STRING_FORMAT_SPECIFIER_WITH_MACRO(macro) "%"#macro"s"
#define STR_FS(macro) STRING_FORMAT_SPECIFIER_WITH_MACRO(macro)

static FILE *outputfile = NULL;		///< global output stream
static FILE *errfile = NULL;		///< global error stream
static FILE *bufferfile = NULL;		///< buffer file for ordering

/**
 * @brief definitions for getopt options: identifiers
 */
enum {
	OPT_PID,
	OPT_TID,
	OPT_SIGNUM,
	OPT_OUTPUTFILE,
	OPT_ERRFILE,
	OPT_PRSTATUS_FD
};

/**
 * @brief definitions for getopt options: full specifications
 */
const struct option opts[] = {
	{ "pid", required_argument, 0, OPT_PID },
	{ "tid", required_argument, 0, OPT_TID },
	{ "sig", required_argument, 0, OPT_SIGNUM },
	{ "output", required_argument, 0, OPT_OUTPUTFILE },
	{ "erroutput", required_argument, 0, OPT_ERRFILE },
	{ "prstatus_fd", required_argument, 0, OPT_PRSTATUS_FD },
	{ 0, 0, 0, 0 }
};

/**
 * @brief container for information from /proc/PID/maps
 */
struct addr_node {
	uintptr_t startaddr;
	uintptr_t endaddr;
	char perm[PERM_LEN+1];
	char *fpath;
	struct addr_node *next;
};

/* helper functions for reading /proc/PID/maps */
static struct addr_node *get_addr_list_from_maps(int fd);
static void free_all_nodes(struct addr_node *start);
static char *fgets_fd(char *str, int len, int fd);

/**
 * @brief Gets registers information for live process
 *
 * @param pid pid of the live process
 * @return 0 on success, -1 otherwise
 */
static int __get_registers_fd(pid_t pid, int fd)
{
	struct elf_prstatus *prstatus = NULL; ///< NT_PRSTATUS of the failed process
	size_t size = 0;
	char *registers = _crash_stack_get_memory_for_registers(&size);

	if (NULL == registers) {
		_E("Cannot get memory for registers (not implemented for this architecture");
		return -1;
	}

	prstatus = mmap(NULL, sizeof(struct elf_prstatus),
			PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

	if (prstatus == MAP_FAILED)
		return -1;

	if (prstatus->pr_pid != pid)
		return -1;

	memcpy(registers, prstatus->pr_reg, size);

	return 0;
}

/**
 * @brief Print signal number causing dump
 */
static void __crash_stack_print_signal(int signo)
{
#define DEF_STR(name) [name] = #name
	const char* const signal_table[] = {
		DEF_STR(SIGHUP),
		DEF_STR(SIGINT),
		DEF_STR(SIGQUIT),
		DEF_STR(SIGILL),
		DEF_STR(SIGTRAP),
		DEF_STR(SIGABRT),
		DEF_STR(SIGBUS),
		DEF_STR(SIGFPE),
		DEF_STR(SIGKILL),
		DEF_STR(SIGUSR1),
		DEF_STR(SIGUSR2),
		DEF_STR(SIGPIPE),
		DEF_STR(SIGSEGV),
		DEF_STR(SIGTERM),
		DEF_STR(SIGSTKFLT),
		DEF_STR(SIGCHLD),
		DEF_STR(SIGCONT),
		DEF_STR(SIGSTOP),
		DEF_STR(SIGTSTP),
		DEF_STR(SIGTTIN),
		DEF_STR(SIGTTOU),
		DEF_STR(SIGURG),
		DEF_STR(SIGXCPU),
		DEF_STR(SIGXFSZ),
		DEF_STR(SIGVTALRM),
		DEF_STR(SIGPROF),
		DEF_STR(SIGWINCH),
		DEF_STR(SIGPOLL),
		DEF_STR(SIGIO),
		DEF_STR(SIGPWR),
		DEF_STR(SIGSYS)
	};
#undef DEF_STR

	if (SIGHUP > signo || signo > SIGSYS) {
		_E("Invalid signal number: %d", signo);
		return;
	}

	printf("Signal: %d\n"
	       "      (%s)\n",
	       signo,
	       signal_table[signo]);
}

/**
 * @brief Prints call stack element to the global outputfile.
 *
 * @param proc_info gathered call stack element
 */
static void __print_proc_info(ProcInfo *proc_info)
{
	if (proc_info->name) {
		fprintf(outputfile, "%s ", proc_info->name);
		if (proc_info->offset >= 0)
			fprintf(outputfile, "+ 0x%x ", proc_info->offset);
	}
	if (sizeof(proc_info->addr) > 4)
		fprintf(outputfile, "(0x%016llx)", (long long)proc_info->addr);
	else
		fprintf(outputfile, "(0x%08x)", (int32_t)proc_info->addr);

	if (proc_info->module_name != 0)
		fprintf(outputfile, " [%s] + 0x%x", proc_info->module_name, proc_info->module_offset);

	fprintf(outputfile, "\n");
}

/**
 * @brief Prints call stack to the global outputfile.
 *
 * @param callstack gathered call stack database
 * @param pid PID of the live process
 */
static void __print_callstack(Callstack *callstack, pid_t pid)
{
	fprintf(outputfile, "\nCallstack Information");
	fprintf(outputfile, " (PID:%d)", pid);
	fprintf(outputfile, "\nCall Stack Count: %zu\n", callstack->elems);

	size_t it;
	for (it = 0; it != callstack->elems; ++it) {
		fprintf(outputfile, "%2zu: ", it);
		__print_proc_info(&callstack->proc[it]);
	}
	fprintf(outputfile, "End of Call Stack\n");
}

void callstack_constructor(Callstack *callstack)
{
	size_t it;
	callstack->elems = 0;
	for (it = 0; it < (int)sizeof(callstack->proc)/sizeof(callstack->proc[0]); ++it) {
		callstack->proc[it].offset = -1;
		callstack->proc[it].name = 0;
		callstack->proc[it].module_name = 0;
	}
}

void callstack_destructor(Callstack *callstack)
{
	size_t it;
	for (it = 0; it < callstack->elems; ++it) {
		free(callstack->proc[it].name);
		free(callstack->proc[it].module_name);
	}
}

/**
 * @brief Print full path of executable file
 */
static void __crash_stack_print_exe(FILE* outputfile, pid_t pid)
{
	int fd, ret;
	char file_path[PATH_MAX];
	char cmd_path[PATH_MAX];

	snprintf(cmd_path, PATH_MAX, "/proc/%d/cmdline", pid);
	if ((fd = open(cmd_path, O_RDONLY)) < 0)
		return;

	if ((ret = read(fd, file_path, sizeof(file_path) - 1)) <= 0) {
		close(fd);
		return;
	}
	file_path[ret] = '\0';

	fprintf(outputfile, "Executable File Path: %s\n", file_path);
	close(fd);
}

/**
 * @brief Print thread information
 *
 * @param outputfile File handle for printing report
 * @param pid PID of the inspected process
 * @param tid TID of the inspected thread
 */
static void __crash_stack_print_threads(FILE* outputfile, pid_t pid, pid_t tid)
{
	int threadnum = 1;
	DIR *dir;
	struct dirent *dentry = NULL;
	char task_path[PATH_MAX];
	struct stat sb;

	snprintf(task_path, PATH_MAX, "/proc/%d/task", pid);
	if (stat(task_path, &sb) == -1)
		return;

	threadnum = sb.st_nlink - 2;

	if (threadnum > 1) {
		fprintf(outputfile, "\nThreads Information\n");
		fprintf(outputfile,
			"Threads: %d\nPID = %d TID = %d\n",
			threadnum, pid, tid);
		/* print thread */
		dir = opendir(task_path);
		if (!dir) {
			_E("opendir(%s): %s", task_path, strerror(errno));
		} else {
			while ((dentry = readdir(dir))) {
				if (strcmp(dentry->d_name, ".") == 0 ||
				    strcmp(dentry->d_name, "..") == 0)
					continue;
				fprintf(outputfile, "%s ", dentry->d_name);
			}
			closedir(dir);
			fprintf(outputfile, "\n");
		}
	}

}

/**
 * @brief Print information about mapped memory regions
 *
 * @param outputfile File handle for printing report.
 * @param pid PID of the inspected process
 */
static void __crash_stack_print_maps(FILE* outputfile, pid_t pid)
{
	char file_path[PATH_MAX];
	struct addr_node *head = NULL;
	struct addr_node *t_node;
	int fd;

	snprintf(file_path, PATH_MAX, "/proc/%d/maps", pid);

	if ((fd = open(file_path, O_RDONLY)) < 0) {
		_E("open(%s): %s", file_path, strerror(errno));
	} else {
		/* parsing the maps to get code segment address*/
		head = get_addr_list_from_maps(fd);
		close(fd);
	}
	if (head == NULL)
		return;

	t_node = head;
	fprintf(outputfile, "\nMaps Information\n");
	while (t_node) {
		if (!strncmp(STR_ANONY, t_node->fpath, STR_ANONY_LEN)) {
			t_node = t_node->next;
		} else {
			fprintf(outputfile, "%16lx %16lx %s %s\n",
				(unsigned long)t_node->startaddr,
				(unsigned long)t_node->endaddr,
				t_node->perm, t_node->fpath);
			t_node = t_node->next;
		}
	}
	fprintf(outputfile, "End of Maps Information\n");
	free_all_nodes(head);
}

static struct addr_node *get_addr_list_from_maps(int fd)
{
	int fpath_len, result;
	uintptr_t saddr;
	uintptr_t eaddr;
	char perm[PERM_LEN+1];
	char path[PATH_MAX+1];
	char addr[ADDR_LEN+1];
	char linebuf[BUF_SIZE];
	struct addr_node *head = NULL;
	struct addr_node *tail = NULL;
	struct addr_node *t_node = NULL;

	/* parsing the maps to get executable code address */
	while (fgets_fd(linebuf, BUF_SIZE, fd) != NULL) {
		memset(path, 0, PATH_MAX+1);
		result = sscanf(linebuf, STR_FS(ADDR_LEN)
				 STR_FS(PERM_LEN)
				 "%*s %*s %*s"
				 STR_FS(PATH_MAX), addr, perm, path);
		if (result < 0)
			continue;
		perm[PERM_LEN] = 0;
		/* rwxp */
		if ((perm[2] == 'x' && path[0] == '/') ||
		    (perm[1] == 'w' && path[0] != '/')) {
			char* addr2 = strchr(addr, '-');
			*(addr2++) = '\0';
			/* add addr node to list */
			saddr = strtoul(addr, NULL, HEXA);
			/* ffff0000-ffff1000 */
			eaddr = strtoul(addr2, NULL, HEXA);
			/* make node and attach to the list */
			t_node = (struct addr_node *)mmap(0, sizeof(struct addr_node),
							  PROT_READ | PROT_WRITE,
							  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
			if (t_node == NULL) {
				_E("mmap(): %s", strerror(errno));
				return NULL;
			}
			memcpy(t_node->perm, perm, PERM_LEN+1);
			t_node->startaddr = saddr;
			t_node->endaddr = eaddr;
			t_node->fpath = NULL;
			fpath_len = strlen(path);
			if (fpath_len > 0) {
				t_node->fpath = (char *)mmap(0, fpath_len + 1,
							     PROT_READ | PROT_WRITE,
							     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
				memset(t_node->fpath, 0, fpath_len + 1);
				memcpy(t_node->fpath, path, fpath_len);
			} else {
				t_node->fpath = (char *)mmap(0, STR_ANONY_LEN,
							     PROT_READ | PROT_WRITE,
							     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
				memset(t_node->fpath, 0, STR_ANONY_LEN);
				memcpy(t_node->fpath, STR_ANONY, STR_ANONY_LEN);
			}
			t_node->next = NULL;
			if (head == NULL || tail == NULL) {
				assert(head == NULL);
				assert(tail == NULL);
				head = t_node;
				tail = t_node;
			} else {
				tail->next = t_node;
				tail = t_node;
			}
		}
	}
	return head;
}

static void free_all_nodes(struct addr_node *start)
{
	struct addr_node *t_node, *n_node;
	int fpath_len;

	if (start == NULL)
		return;
	t_node = start;
	n_node = t_node->next;
	while (t_node) {
		if (t_node->fpath != NULL) {
			fpath_len = strlen(t_node->fpath);
			munmap(t_node->fpath, fpath_len + 1);
		}
		munmap(t_node, sizeof(struct addr_node));
		if (n_node == NULL)
			break;
		t_node = n_node;
		n_node = n_node->next;
	}
}

static char *fgets_fd(char *str, int len, int fd)
{
	char ch;
	register char *cs;
	int num = 0;

	cs = str;
	while (--len > 0 && (num = read(fd, &ch, 1) > 0)) {
		if ((*cs++ = ch) == '\n')
			break;
	}
	*cs = '\0';
	return (num == 0 && cs == str) ? NULL : str;
}

/**
 * @brief Print process and system memory information
 *
 * @param outputfile File handle for printing report.
 * @param pid PID of the inspected process
 */
static void __crash_stack_print_meminfo(FILE* outputfile, pid_t pid)
{
	char infoname[BUF_SIZE];
	char memsize[BUF_SIZE];
	char linebuf[BUF_SIZE];
	char file_path[PATH_MAX];
	int fd;

	fprintf(outputfile, "\nMemory information\n");

	if ((fd = open("/proc/meminfo", O_RDONLY)) < 0) {
		_E("open(/proc/meminfo): %s", strerror(errno));
	} else {
		while (fgets_fd(linebuf, BUF_SIZE, fd) != NULL) {
			sscanf(linebuf, "%16s %16s %*s", infoname, memsize);
			if (strcmp("MemTotal:", infoname) == 0) {
				fprintf(outputfile, "%s %8s KB\n", infoname, memsize);
			} else if (strcmp("MemFree:", infoname) == 0) {
				fprintf(outputfile, "%s	 %8s KB\n", infoname, memsize);
			} else if (strcmp("Buffers:", infoname) == 0) {
				fprintf(outputfile, "%s	 %8s KB\n", infoname, memsize);
			} else if (strcmp("Cached:", infoname) == 0) {
				fprintf(outputfile, "%s	  %8s KB\n", infoname, memsize);
				break;
			}
		}
		close(fd);
	}

	snprintf(file_path, PATH_MAX, "/proc/%d/status", pid);
	if ((fd = open(file_path, O_RDONLY)) < 0) {
		_E("open(%s): %s", file_path, strerror(errno));
	} else {
		while (fgets_fd(linebuf, BUF_SIZE, fd) != NULL) {
			sscanf(linebuf, "%16s %16s %*s", infoname, memsize);
			if (strcmp("VmPeak:", infoname) == 0) {
				fprintf(outputfile, "%s	  %8s KB\n", infoname,
						memsize);
			} else if (strcmp("VmSize:", infoname) == 0) {
				fprintf(outputfile, "%s	  %8s KB\n", infoname,
						memsize);
			} else if (strcmp("VmLck:", infoname) == 0) {
				fprintf(outputfile, "%s	   %8s KB\n", infoname,
						memsize);
			} else if (strcmp("VmPin:", infoname) == 0) {
				fprintf(outputfile, "%s	   %8s KB\n", infoname,
						memsize);
			} else if (strcmp("VmHWM:", infoname) == 0) {
				fprintf(outputfile, "%s	   %8s KB\n",
						infoname, memsize);
			} else if (strcmp("VmRSS:", infoname) == 0) {
				fprintf(outputfile, "%s	   %8s KB\n",
						infoname, memsize);
			} else if (strcmp("VmData:", infoname) == 0) {
				fprintf(outputfile, "%s	  %8s KB\n",
						infoname, memsize);
			} else if (strcmp("VmStk:", infoname) == 0) {
				fprintf(outputfile, "%s	   %8s KB\n",
						infoname, memsize);
			} else if (strcmp("VmExe:", infoname) == 0) {
				fprintf(outputfile, "%s	   %8s KB\n",
						infoname, memsize);
			} else if (strcmp("VmLib:", infoname) == 0) {
				fprintf(outputfile, "%s	   %8s KB\n",
						infoname, memsize);
			} else if (strcmp("VmPTE:", infoname) == 0) {
				fprintf(outputfile, "%s	   %8s KB\n",
						infoname, memsize);
			} else if (strcmp("VmSwap:", infoname) == 0) {
				fprintf(outputfile, "%s	  %8s KB\n",
						infoname, memsize);
				break;
			}
		}
		close(fd);
	}
}

/**
 * @brief Print information saved in buffer (bufferfile)
 *
 * @param bufferfile File handle for reading saved info.
 * @param outputfile File handle for printing report.
 */
static void __print_buffer_info(FILE* bufferfile, FILE *outputfile)
{
	int cnt;
	char buf[1024];

	if (fseek(bufferfile, 0, SEEK_SET) < 0) {
		_E("fseek(): %s", strerror(errno));
		return;
	}
	while (!(feof(bufferfile) || ferror(bufferfile)) && (cnt = fread(buf, sizeof(char), sizeof(buf), bufferfile)) != 0) {
		if (cnt != fwrite(buf, sizeof(char), cnt, outputfile))
			break;
	}
}

/**
 * @brief Main function.
 *
 * Main module accepts should be launched with:
 *
 *     crash-stack --pid pid [--tid tid] [--sig sig]
 *
 * It allows connecting to a live process and displaying its call stack.
 * It might be also used for connecting to a process from system's core dump handler.
 */
int main(int argc, char **argv)
{
	int prstatus_fd = -1;
	int c;
	int signo = 0;
	pid_t pid = 0;
	pid_t tid = 0;
	char bufferfile_path[20] = "/tmp/crash.XXXXXX";
	char *p = NULL;

	while ((c = getopt_long_only(argc, argv, "", opts, NULL)) != -1) {
		switch (c) {
		case OPT_PID:
			pid = atoi(optarg);
			break;
		case OPT_TID:
			tid = atoi(optarg);
			break;
		case OPT_SIGNUM:
			signo = atoi(optarg);
			break;
		case OPT_OUTPUTFILE:
			outputfile = fopen(optarg, "w");
			break;
		case OPT_ERRFILE:
			errfile = fopen(optarg, "w");
			_W("--erroutput is deprecated");
			break;
		case OPT_PRSTATUS_FD:
			prstatus_fd = strtol(optarg, &p, 10);
			if (*p != 0)
				prstatus_fd = -1;
			break;
		}
	}

	if (NULL == errfile) errfile = stderr;
	if (NULL == outputfile) outputfile = stdout;

	if (tid == 0) {
		fprintf(errfile, "TID not provided\n");
		return errno;
	}

	mode_t oldmode = umask(0077);
	if (mkstemp(bufferfile_path) < 0) {
		_E("mkstemp(%s): %s", bufferfile_path, strerror(errno));
		return errno;
	}
	umask(oldmode);

	if ((bufferfile = fopen(bufferfile_path, "w+")) == NULL) {
		_E("fopen(%s): %s", bufferfile_path, strerror(errno));
		return errno;
	}
	unlink(bufferfile_path);

	argc -= optind;

	elf_version(EV_CURRENT);

	/* Executable File Path */
	__crash_stack_print_exe(outputfile, pid);

	/* Signal information */
	__crash_stack_print_signal(signo);

	/* Memory information */
	__crash_stack_print_meminfo(bufferfile, pid);

	/* Threads */
	__crash_stack_print_threads(bufferfile, pid, tid);

	/* Maps information */
	__crash_stack_print_maps(bufferfile, pid);

	if (pid <= 1) {
		_E( "Usage: %s [--output file] [--erroutput file] [--pid <pid> [--tid <tid>]]",
				argv[0]);

		return 1;
	}

	if (-1 == __get_registers_fd(tid, prstatus_fd))
		return 3333;

	/* Unwind call stack */
	Callstack callstack;
	callstack_constructor(&callstack);

	_create_crash_stack(NULL, tid, &callstack,
			    _crash_stack_get_memory_for_registers(NULL));

	/* Print registers */
	_crash_stack_print_regs(outputfile);

	/* Print info */
	__print_buffer_info(bufferfile, outputfile);

	/* Print the results */
	__print_callstack(&callstack, tid);

	/* Clean up */
	callstack_destructor(&callstack);

	return 0;
}