summaryrefslogtreecommitdiff
path: root/src/crash-stack/crash-stack-libelf-helpers.c
blob: ab95350e7ed9e4c836092cb72e4c8b02dfe17976 (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
/*
 *
 * 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-libelf-helpers.c
 * @brief unwinding call stacks, functions for accessing libelf for other
 * purposes than unwinding
 */
#include "crash-stack.h"
#include <string.h>
#include <sys/ptrace.h>
#include <errno.h>

bool _crash_stack_libelf_read_value(Dwfl *dwfl, Elf *core, pid_t pid,
		Dwarf_Addr a, void *v, size_t size,
		Mappings *mappings)
{
	Dwfl_Module *module = 0;
	Elf_Data *data = NULL;

	int segment = dwfl_addrsegment(dwfl, a, &module);

	if (module != NULL) {
		Dwarf_Addr start;
		dwfl_module_info(module, NULL, &start, NULL, NULL, NULL, NULL, NULL);

		GElf_Addr bias;
		Elf *elf = dwfl_module_getelf(module, &bias);

		data = elf_getdata_rawchunk(elf, a-start, size, ELF_T_BYTE);
	}
	if (NULL == data && segment != -1) {
		// get data from segment
		GElf_Phdr mem;
		GElf_Phdr *phdr = gelf_getphdr(core, segment, &mem);
		Dwarf_Addr offset_in_segment = a - phdr->p_vaddr;
		if (offset_in_segment < phdr->p_filesz) {
			Dwarf_Addr offset_in_file = phdr->p_offset + offset_in_segment;

			data = elf_getdata_rawchunk(core, offset_in_file, size, ELF_T_BYTE);
		}
	}

	if (NULL == data && module != NULL) {
		const char *name = dwfl_module_info(module, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
		if (name != NULL && name[0] == '[') {
			int i;
			// get module from mappings
			for (i = 0; i < mappings->elems; i++) {
				if (mappings->tab[i].m_start <= a && a < mappings->tab[i].m_end) {
					// compute offset relative to the start of the mapping
					long offset = a - mappings->tab[i].m_start;
					// read from the file, but also account file offset
					data = elf_getdata_rawchunk(mappings->tab[i].m_elf,
							offset + mappings->tab[i].m_offset, size, ELF_T_BYTE);
					break;
				}
			}
		}
	}

	if (data != NULL) {
		memcpy(v, data->d_buf, size);
		return true;
	}

	/* Still no data, but we have a process - read memory with ptrace */
	/* FIXME need to know if we are still in the mapped area */
	/* Bigger issue is that dwfl does not have modules */
	if (pid > 1) {
		long val = ptrace(PTRACE_PEEKDATA, pid, a, NULL);
		if (-1 == val && errno)
			return false;
		memcpy(v, &val, size);
		return true;
	}

	return false;
}

Dwarf_Addr _crash_stack_libelf_get_prologue_pc(Dwfl *dwfl, Dwarf_Addr current_pc, Mappings *mappings)
{
	Dwarf_Addr result = 0;
	Dwfl_Module *module = dwfl_addrmodule(dwfl, current_pc);
	if (module) {
		GElf_Sym sym;
		dwfl_module_addrsym(module, current_pc, &sym, NULL);
		result = sym.st_value;
	}
	if (0 == result) {
		int i;
		for (i=0; i < mappings->elems; i++) {
			if (mappings->tab[i].m_start <= current_pc && current_pc < mappings->tab[i].m_end) {
				/* go through symbols to find the nearest */
				Elf_Scn *scn = NULL;
				Elf *elf = mappings->tab[i].m_elf;
				while ((scn = elf_nextscn(elf, scn)) != NULL) {
					GElf_Shdr shdr_mem;
					GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
					if (shdr != NULL && (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM)) {
						Elf_Data *sdata = elf_getdata(scn, NULL);
						unsigned int nsyms = sdata->d_size / (gelf_getclass(elf) == ELFCLASS32 ?
								sizeof(Elf32_Sym) :
								sizeof(Elf64_Sym));
						unsigned int cnt;
						uintptr_t address_offset = current_pc;
						if (shdr->sh_type == SHT_DYNSYM)
							address_offset -= mappings->tab[i].m_start;
						for (cnt = 0; cnt < nsyms; ++cnt) {
							GElf_Sym sym_mem;
							Elf32_Word xndx;
							GElf_Sym *sym = gelf_getsymshndx(sdata, NULL, cnt, &sym_mem, &xndx);
							if (sym != NULL && sym->st_shndx != SHN_UNDEF) {
								if (sym->st_value <= address_offset && address_offset < sym->st_value + sym->st_size) {
									return sym->st_value;
								}
							}
						}
					}
				}
			}
		}
	}
	return result;
}