summaryrefslogtreecommitdiff
path: root/src/crash-stack/crash-stack-libunw.c
blob: f013e63e98367ceed10427d4bb120028f01b2371 (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
/*
 *
 * Copyright (c) 2016 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.
 *
 * Author: Rafal Pietruch <r.pietruch@samsung.com>
 */
/**
 * @file crash-stack-libunw.c
 * @brief unwinding call stacks, functions specific for archs that use only libunwind
 */
#include "crash-stack.h"
#include <libunwind-ptrace.h>

#include <string.h>

#define MAXPROCNAMELEN 512

void _create_crash_stack(Dwfl *dwfl, Elf *core, pid_t pid, Mappings *mappings, Callstack *callstack)
{
	// reimplemented based on libunwind tests/test-ptrace.c file
	unw_addr_space_t as = 0;
	void *ui = 0;
	do {
		//init before return
		callstack->elems = 0;

		as = unw_create_addr_space(&_UPT_accessors, 0);
		if (!as)
			break;

		ui = _UPT_create(pid);
		if (!ui)
			break;

		unw_cursor_t cursor;
		if (unw_init_remote(&cursor, as, ui) < 0)
			break;

		char proc_name[MAXPROCNAMELEN];
		for (; callstack->elems < sizeof(callstack->proc)/sizeof(callstack->proc[0]);
				 ++callstack->elems) {

			unw_word_t ip;
			if (unw_get_reg(&cursor, UNW_REG_IP, &ip) < 0)
				break;
			callstack->proc[callstack->elems].addr = ip;

			proc_name[0] = '\0';
			unw_word_t off;
			unw_get_proc_name(&cursor, proc_name, sizeof(proc_name), &off);
			if (strlen(proc_name) > 0)
				callstack->proc[callstack->elems].name = strdup(proc_name);
			callstack->proc[callstack->elems].offset = off;

			if (unw_step(&cursor) <= 0)
				break;
		}
	} while (0);

	if (ui)
		_UPT_destroy(ui);
	if (as)
		unw_destroy_addr_space(as);
}