summaryrefslogtreecommitdiff
path: root/src/crash-stack/crash-stack-arm.c
blob: 7ad3b9b7c34a7647250a9212e5791a01355c53ac (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
/*
 *
 * 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: Adrian Szyndela <adrian.s@samsung.com>
 */
/**
 * @file crash-stack-arm.c
 * @brief unwinding call stacks, functions specific for ARM
 */
#include "crash-stack.h"
#include "wind/unwarm.h"

#include <string.h>
#include <sys/ptrace.h>
#include <sys/user.h>

static Elf *g_core = NULL;					///< core file handler
static Dwfl *g_dwfl = NULL;					///< dwfl handler
static Mappings *g_mappings = NULL;			///< pointer to mappings database
static pid_t g_pid = 0;						///< PID of analyzed program

/**
 * @brief Important registers for unwinding stack on ARM
 */
struct Regs {
	Dwarf_Addr regs[REGS_REGULAR_NUM];		///< regular registers
	Dwarf_Addr sp;							///< register: stack pointer
	Dwarf_Addr lr;							///< register: link register
	Dwarf_Addr pc;							///< register: program counter
	Dwarf_Addr spsr;						///< register: status register
};

typedef struct Regs Regs;					///< convenience type definition
static Regs g_regs;							///< static storage for target register values

static struct user_regs g_ptrace_registers;	///< static storage for ptrace buffer for registers

void *_crash_stack_get_memory_for_ptrace_registers(size_t *size)
{
	if (NULL != size)
		*size = sizeof(g_ptrace_registers);
	return &g_ptrace_registers;
}

void *_get_place_for_register_value(const char *regname, int regnum)
{
	if (strcmp(regname, "pc") == 0 || REG_PC == regnum)
		return &g_regs.pc;
	else if (strcmp(regname, "sp") == 0 || REG_SP == regnum)
		return &g_regs.sp;
	else if (strcmp(regname, "lr") == 0 || REG_LR == regnum)
		return &g_regs.lr;
	else if (strcmp(regname, "spsr") == 0 || REG_SPSR == regnum)
		return &g_regs.spsr;
	else if (regnum < REGS_REGULAR_NUM)
		return &g_regs.regs[regnum];
	return NULL;
}

void _crash_stack_set_ptrace_registers(void *regbuf)
{
	struct user_regs *registers = regbuf;
	int i;
	for (i = 0; i < sizeof(registers->uregs)/sizeof(registers->uregs[0]); i++) {
		void *regmem = _get_place_for_register_value("", i);
		if (NULL != regmem)
			memcpy(regmem, &registers->uregs[i], sizeof(registers->uregs[i]));
	}
}

/**
 * @brief Callback function for McTernan's unwinder - call stack frame reporting
 *
 * @param data pointer to call stack database
 * @param address return address of the function in the reported call frame
 */
static Boolean __report(void *data, Int32 address)
{
	Callstack *callstack = (Callstack *)(data);
	callstack->tab[callstack->elems++] = address;

	return callstack->elems < MAX_CALLSTACK_LEN ? TRUE : FALSE;
}

/**
 * @brief Helper function for reading values from target memory
 *
 * @param a target address to read from
 * @param v destination address to copy data to
 * @param size of the value
 */
static Boolean __readT(Int32 a, void *v, size_t size)
{
  return _crash_stack_libelf_read_value(g_dwfl, g_core, g_pid, a, v, size, g_mappings);
}

/**
 * @brief Callback function for McTernan's unwinder - reading 32 bits
 *
 * @param a target address to read from
 * @param v destination address to copy data to
 */
static Boolean __readW(Int32 a, Int32 *v)
{
	return __readT(a, v, sizeof(*v));
}

/**
 * @brief Callback function for McTernan's unwinder - reading 16 bits
 *
 * @param a target address to read from
 * @param v destination address to copy data to
 */
static Boolean __readH(Int32 a, Int16 *v)
{
	return __readT(a, v, sizeof(*v));
}

/**
 * @brief Callback function for McTernan's unwinder - reading 8 bits
 *
 * @param a target address to read from
 * @param v destination address to copy data to
 */
static Boolean __readB(Int32 a, Int8 *v)
{
	return __readT(a, v, sizeof(*v));
}

/**
 * @brief Callback function for McTernan's unwinder - trying to find function prologue
 *
 * @param current_pc address from inside of some function from target memory
 */
static Int32 __getProloguePC(Int32 current_pc)
{
    return _crash_stack_libelf_get_prologue_pc(g_dwfl, current_pc, g_mappings);
}

void _create_crash_stack(Dwfl *dwfl, Elf *core, pid_t pid, Mappings *mappings, Callstack *callstack)
{
	UnwindCallbacks callbacks = {
		__report,
		__readW,
		__readH,
		__readB,
		__getProloguePC
#ifdef UNW_DEBUG
			,
		printf
#endif
	};
	UnwState state;

	g_dwfl = dwfl;
	g_core = core;
	g_mappings = mappings;
	g_pid = pid;

	callstack->tab[0] = g_regs.pc;
	callstack->elems = 1;

	UnwInitState(&state, &callbacks, callstack, g_regs.pc, g_regs.sp);
	int i;
	for (i = 0; i < REGS_REGULAR_NUM; i++) {
		state.regData[i].v = g_regs.regs[i];
		state.regData[i].o = REG_VAL_FROM_CONST;
	}
	state.regData[REG_LR].v = g_regs.lr;
	state.regData[REG_LR].o = REG_VAL_FROM_STACK;
	state.regData[REG_SPSR].v = g_regs.spsr;
	state.regData[REG_SPSR].o = REG_VAL_FROM_CONST;

	if (UnwIsAddrThumb(g_regs.pc, g_regs.spsr))
		UnwStartThumb(&state);
	else
		UnwStartArm(&state);
}