summaryrefslogtreecommitdiff
path: root/source/ucli/accessors.cc
diff options
context:
space:
mode:
Diffstat (limited to 'source/ucli/accessors.cc')
-rw-r--r--source/ucli/accessors.cc150
1 files changed, 81 insertions, 69 deletions
diff --git a/source/ucli/accessors.cc b/source/ucli/accessors.cc
index 621ad95..b31fa53 100644
--- a/source/ucli/accessors.cc
+++ b/source/ucli/accessors.cc
@@ -1,9 +1,9 @@
+#include <elf.h>
#include <fcntl.h>
-#include <gelf.h> // for GElf_Ehdr | Elf
+#include <gelf.h> // for GElf_Ehdr | Elf
+#include <libelf.h>
#include <string.h>
#include <unistd.h>
-#include <elf.h>
-#include <libelf.h>
#include "unwind.h"
@@ -43,7 +43,8 @@ static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, GElf_Shdr *shp,
gelf_getshdr(sec, shp);
str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
- if (!strcmp(name, str)) break;
+ if (!strcmp(name, str))
+ break;
}
return sec;
@@ -56,12 +57,15 @@ static u64 elf_section_offset(int fd, const char *name) {
u64 offset = 0;
elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
- if (elf == NULL) return 0;
+ if (elf == NULL)
+ return 0;
do {
- if (gelf_getehdr(elf, &ehdr) == NULL) break;
+ if (gelf_getehdr(elf, &ehdr) == NULL)
+ break;
- if (!elf_section_by_name(elf, &ehdr, &shdr, name)) break;
+ if (!elf_section_by_name(elf, &ehdr, &shdr, name))
+ break;
offset = shdr.sh_offset;
} while (0);
@@ -103,13 +107,16 @@ ssize_t dso_read(vma *dso, u64 offset, u8 *data, ssize_t size) {
int fd;
fd = dso_data_fd(dso);
- if (fd < 0) return -1;
+ if (fd < 0)
+ return -1;
do {
- if (-1 == lseek(fd, offset, SEEK_SET)) break;
+ if (-1 == lseek(fd, offset, SEEK_SET))
+ break;
ret = read(fd, data, size);
- if (ret <= 0) break;
+ if (ret <= 0)
+ break;
} while (0);
close(fd);
@@ -137,14 +144,15 @@ ssize_t dso__data_read_offset(vma *dso, u64 offset, u8 *data, ssize_t size) {
return r;
}
-#define dw_read(ptr, type, end) \
- ({ \
- type *__p = (type *)ptr; \
- type __v; \
- if ((__p + 1) > (type *)end) return -EINVAL; \
- __v = *__p++; \
- ptr = (typeof(ptr))__p; \
- __v; \
+#define dw_read(ptr, type, end) \
+ ({ \
+ type *__p = (type *)ptr; \
+ type __v; \
+ if ((__p + 1) > (type *)end) \
+ return -EINVAL; \
+ __v = *__p++; \
+ ptr = (typeof(ptr))__p; \
+ __v; \
})
static int __dw_read_encoded_value(u8 **p, u8 *end, u64 *val, u8 encoding) {
@@ -152,43 +160,44 @@ static int __dw_read_encoded_value(u8 **p, u8 *end, u64 *val, u8 encoding) {
*val = 0;
switch (encoding) {
- case DW_EH_PE_omit:
- *val = 0;
- goto out;
- case DW_EH_PE_ptr:
- *val = dw_read(cur, unsigned long, end);
- goto out;
- default:
- break;
+ case DW_EH_PE_omit:
+ *val = 0;
+ goto out;
+ case DW_EH_PE_ptr:
+ *val = dw_read(cur, unsigned long, end);
+ goto out;
+ default:
+ break;
}
switch (encoding & DW_EH_PE_APPL_MASK) {
- case DW_EH_PE_absptr:
- break;
- case DW_EH_PE_pcrel:
- *val = (unsigned long)cur;
- break;
- default:
- return -EINVAL;
+ case DW_EH_PE_absptr:
+ break;
+ case DW_EH_PE_pcrel:
+ *val = (unsigned long)cur;
+ break;
+ default:
+ return -EINVAL;
}
- if ((encoding & 0x07) == 0x00) encoding |= DW_EH_PE_udata4;
+ if ((encoding & 0x07) == 0x00)
+ encoding |= DW_EH_PE_udata4;
switch (encoding & DW_EH_PE_FORMAT_MASK) {
- case DW_EH_PE_sdata4:
- *val += dw_read(cur, s32, end);
- break;
- case DW_EH_PE_udata4:
- *val += dw_read(cur, u32, end);
- break;
- case DW_EH_PE_sdata8:
- *val += dw_read(cur, s64, end);
- break;
- case DW_EH_PE_udata8:
- *val += dw_read(cur, u64, end);
- break;
- default:
- return -EINVAL;
+ case DW_EH_PE_sdata4:
+ *val += dw_read(cur, s32, end);
+ break;
+ case DW_EH_PE_udata4:
+ *val += dw_read(cur, u32, end);
+ break;
+ case DW_EH_PE_sdata8:
+ *val += dw_read(cur, s64, end);
+ break;
+ case DW_EH_PE_udata8:
+ *val += dw_read(cur, u64, end);
+ break;
+ default:
+ return -EINVAL;
}
out:
@@ -196,13 +205,13 @@ out:
return 0;
}
-#define dw_read_encoded_value(ptr, end, enc) \
- ({ \
- u64 __v; \
- if (__dw_read_encoded_value(&ptr, end, &__v, enc)) { \
- return -EINVAL; \
- } \
- __v; \
+#define dw_read_encoded_value(ptr, end, enc) \
+ ({ \
+ u64 __v; \
+ if (__dw_read_encoded_value(&ptr, end, &__v, enc)) { \
+ return -EINVAL; \
+ } \
+ __v; \
})
static int unwind_spec_ehframe(vma *dso, u64 offset, u64 *table_data,
@@ -233,7 +242,8 @@ static int read_unwind_spec(vma *dso, u64 *table_data, u64 *segbase,
if (dso->eh_frame_hdr_offset == 0 && dso->elf_read_error == 0) {
fd = dso_data_fd(dso);
- if (fd < 0) return -EINVAL;
+ if (fd < 0)
+ return -EINVAL;
dso->eh_frame_hdr_offset = elf_section_offset(fd, ".eh_frame_hdr");
close(fd);
@@ -300,7 +310,8 @@ static int get_dyn_info_list_addr(unw_addr_space_t as, unw_word_t *dil_addr,
ssize_t dso__data_read_addr(vma *map, u64 addr, u8 *data, ssize_t size) {
u64 offset;
- if (map->name.size() > 0 && map->name[0] != '/') return 0;
+ if (map->name.size() > 0 && map->name[0] != '/')
+ return 0;
offset = addr - map->start + map->offset;
return dso__data_read_offset(map, offset, data, size);
@@ -336,7 +347,8 @@ static int access_dso_mem(struct unwind_info *ui, unw_word_t addr,
*/
static int reg_value(unw_word_t *valp, struct regs_dump *regs, int id) {
/* we only support 3 registers. RIP, RSP and RBP */
- if (id < 0 || id > 2) return -EINVAL;
+ if (id < 0 || id > 2)
+ return -EINVAL;
*valp = regs->regs[id];
return 0;
@@ -411,17 +423,17 @@ int unwind__arch_reg_id(int regnum) {
int id;
switch (regnum) {
- case UNW_X86_64_RBP:
- id = PERF_REG_BP;
- break;
- case UNW_X86_64_RSP:
- id = PERF_REG_SP;
- break;
- case UNW_X86_64_RIP:
- id = PERF_REG_IP;
- break;
- default:
- return -EINVAL;
+ case UNW_X86_64_RBP:
+ id = PERF_REG_BP;
+ break;
+ case UNW_X86_64_RSP:
+ id = PERF_REG_SP;
+ break;
+ case UNW_X86_64_RIP:
+ id = PERF_REG_IP;
+ break;
+ default:
+ return -EINVAL;
}
return id;