2010-11-26 06:28:50 +03:00
|
|
|
/**********************************************************************
|
|
|
|
|
2012-08-21 12:32:01 +04:00
|
|
|
addr2line.c -
|
2010-11-26 06:28:50 +03:00
|
|
|
|
|
|
|
$Author$
|
|
|
|
|
|
|
|
Copyright (C) 2010 Shinichiro Hamaji
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
2023-06-24 12:18:58 +03:00
|
|
|
#if defined(__clang__) && defined(__has_warning)
|
|
|
|
#if __has_warning("-Wgnu-empty-initializer")
|
2019-01-10 18:42:44 +03:00
|
|
|
#pragma clang diagnostic ignored "-Wgnu-empty-initializer"
|
2023-06-24 12:18:58 +03:00
|
|
|
#endif
|
|
|
|
#if __has_warning("-Wgcc-compat")
|
2018-11-13 05:19:49 +03:00
|
|
|
#pragma clang diagnostic ignored "-Wgcc-compat"
|
2018-11-05 01:36:05 +03:00
|
|
|
#endif
|
2023-06-24 12:18:58 +03:00
|
|
|
#endif
|
2018-11-05 01:36:05 +03:00
|
|
|
|
2020-05-08 12:31:09 +03:00
|
|
|
#include "ruby/internal/config.h"
|
2018-07-31 18:20:54 +03:00
|
|
|
#include "ruby/defines.h"
|
2013-04-05 15:23:36 +04:00
|
|
|
#include "ruby/missing.h"
|
2010-11-26 06:28:50 +03:00
|
|
|
#include "addr2line.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2010-11-27 05:02:43 +03:00
|
|
|
#include <errno.h>
|
2010-11-26 06:28:50 +03:00
|
|
|
|
2019-08-19 17:36:47 +03:00
|
|
|
#ifdef HAVE_LIBPROC_H
|
|
|
|
#include <libproc.h>
|
|
|
|
#endif
|
|
|
|
|
2020-05-08 12:31:09 +03:00
|
|
|
#include "ruby/internal/stdbool.h"
|
2017-12-18 19:17:17 +03:00
|
|
|
|
2018-10-20 09:35:25 +03:00
|
|
|
#if defined(USE_ELF) || defined(HAVE_MACH_O_LOADER_H)
|
2010-11-26 06:28:50 +03:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
2012-08-29 09:37:28 +04:00
|
|
|
#include <stdint.h>
|
2010-11-26 06:28:50 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-11-09 07:34:08 +04:00
|
|
|
/* Make alloca work the best possible way. */
|
|
|
|
#ifdef __GNUC__
|
2016-05-28 04:46:27 +03:00
|
|
|
# ifndef alloca
|
|
|
|
# define alloca __builtin_alloca
|
|
|
|
# endif
|
2012-11-09 07:34:08 +04:00
|
|
|
#else
|
|
|
|
# ifdef HAVE_ALLOCA_H
|
|
|
|
# include <alloca.h>
|
|
|
|
# else
|
|
|
|
# ifdef _AIX
|
|
|
|
#pragma alloca
|
|
|
|
# else
|
|
|
|
# ifndef alloca /* predefined by HP cc +Olibcalls */
|
|
|
|
void *alloca();
|
|
|
|
# endif
|
|
|
|
# endif /* AIX */
|
|
|
|
# endif /* HAVE_ALLOCA_H */
|
2023-08-01 06:11:05 +03:00
|
|
|
# ifndef UNREACHABLE
|
|
|
|
# define UNREACHABLE __builtin_unreachable()
|
|
|
|
# endif
|
|
|
|
# ifndef UNREACHABLE_RETURN
|
|
|
|
# define UNREACHABLE_RETURN(_) __builtin_unreachable()
|
|
|
|
# endif
|
2012-11-09 07:34:08 +04:00
|
|
|
#endif /* __GNUC__ */
|
2011-07-05 09:08:23 +04:00
|
|
|
|
2023-08-01 06:11:05 +03:00
|
|
|
#ifndef UNREACHABLE
|
|
|
|
# define UNREACHABLE abort()
|
|
|
|
#endif
|
|
|
|
#ifndef UNREACHABLE_RETURN
|
|
|
|
# define UNREACHABLE_RETURN(_) return (abort(), (_))
|
|
|
|
#endif
|
|
|
|
|
2014-03-14 10:29:39 +04:00
|
|
|
#ifdef HAVE_DLADDR
|
|
|
|
# include <dlfcn.h>
|
2010-11-26 06:28:50 +03:00
|
|
|
#endif
|
|
|
|
|
2018-10-20 09:35:25 +03:00
|
|
|
#ifdef HAVE_MACH_O_LOADER_H
|
2019-01-31 15:58:33 +03:00
|
|
|
# include <crt_externs.h>
|
2018-11-21 01:40:20 +03:00
|
|
|
# include <mach-o/fat.h>
|
2018-10-20 09:35:25 +03:00
|
|
|
# include <mach-o/loader.h>
|
2018-11-04 18:14:42 +03:00
|
|
|
# include <mach-o/nlist.h>
|
|
|
|
# include <mach-o/stab.h>
|
2018-10-20 09:35:25 +03:00
|
|
|
#endif
|
2010-11-26 06:28:50 +03:00
|
|
|
|
2018-10-20 09:35:25 +03:00
|
|
|
#ifdef USE_ELF
|
|
|
|
# ifdef __OpenBSD__
|
|
|
|
# include <elf_abi.h>
|
|
|
|
# else
|
|
|
|
# include <elf.h>
|
|
|
|
# endif
|
2010-11-26 06:28:50 +03:00
|
|
|
|
2010-11-26 07:39:23 +03:00
|
|
|
#ifndef ElfW
|
2010-11-26 06:28:50 +03:00
|
|
|
# if SIZEOF_VOIDP == 8
|
|
|
|
# define ElfW(x) Elf64##_##x
|
|
|
|
# else
|
|
|
|
# define ElfW(x) Elf32##_##x
|
|
|
|
# endif
|
2010-11-26 07:39:23 +03:00
|
|
|
#endif
|
2014-03-14 10:29:39 +04:00
|
|
|
#ifndef ELF_ST_TYPE
|
|
|
|
# if SIZEOF_VOIDP == 8
|
|
|
|
# define ELF_ST_TYPE ELF64_ST_TYPE
|
|
|
|
# else
|
|
|
|
# define ELF_ST_TYPE ELF32_ST_TYPE
|
|
|
|
# endif
|
|
|
|
#endif
|
2011-11-08 18:31:56 +04:00
|
|
|
#endif
|
2010-11-26 06:28:50 +03:00
|
|
|
|
2018-08-13 09:20:12 +03:00
|
|
|
#ifdef SHF_COMPRESSED
|
2018-08-13 22:12:23 +03:00
|
|
|
# if defined(ELFCOMPRESS_ZLIB) && defined(HAVE_LIBZ)
|
|
|
|
/* FreeBSD 11.0 lacks ELFCOMPRESS_ZLIB */
|
2018-08-13 09:20:12 +03:00
|
|
|
# include <zlib.h>
|
2018-08-13 09:31:37 +03:00
|
|
|
# define SUPPORT_COMPRESSED_DEBUG_LINE
|
2018-08-13 09:20:12 +03:00
|
|
|
# endif
|
|
|
|
#else /* compatibility with glibc < 2.22 */
|
|
|
|
# define SHF_COMPRESSED 0
|
2017-11-19 00:57:38 +03:00
|
|
|
#endif
|
|
|
|
|
2018-10-20 09:35:25 +03:00
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX 4096
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DW_LNS_copy 0x01
|
|
|
|
#define DW_LNS_advance_pc 0x02
|
|
|
|
#define DW_LNS_advance_line 0x03
|
|
|
|
#define DW_LNS_set_file 0x04
|
|
|
|
#define DW_LNS_set_column 0x05
|
|
|
|
#define DW_LNS_negate_stmt 0x06
|
|
|
|
#define DW_LNS_set_basic_block 0x07
|
|
|
|
#define DW_LNS_const_add_pc 0x08
|
|
|
|
#define DW_LNS_fixed_advance_pc 0x09
|
|
|
|
#define DW_LNS_set_prologue_end 0x0a /* DWARF3 */
|
|
|
|
#define DW_LNS_set_epilogue_begin 0x0b /* DWARF3 */
|
|
|
|
#define DW_LNS_set_isa 0x0c /* DWARF3 */
|
|
|
|
|
|
|
|
/* Line number extended opcode name. */
|
|
|
|
#define DW_LNE_end_sequence 0x01
|
|
|
|
#define DW_LNE_set_address 0x02
|
|
|
|
#define DW_LNE_define_file 0x03
|
|
|
|
#define DW_LNE_set_discriminator 0x04 /* DWARF4 */
|
|
|
|
|
2023-07-31 21:04:42 +03:00
|
|
|
#define kprintf(...) fprintf(errout, "" __VA_ARGS__)
|
2013-03-23 20:05:51 +04:00
|
|
|
|
2018-10-14 19:59:15 +03:00
|
|
|
typedef struct line_info {
|
2010-11-26 06:28:50 +03:00
|
|
|
const char *dirname;
|
|
|
|
const char *filename;
|
2014-03-14 10:29:39 +04:00
|
|
|
const char *path; /* object path */
|
2010-11-26 06:28:50 +03:00
|
|
|
int line;
|
|
|
|
|
2014-03-26 08:30:43 +04:00
|
|
|
uintptr_t base_addr;
|
|
|
|
uintptr_t saddr;
|
2014-03-14 10:29:39 +04:00
|
|
|
const char *sname; /* function name */
|
2018-10-14 19:59:15 +03:00
|
|
|
|
|
|
|
struct line_info *next;
|
2010-11-26 06:28:50 +03:00
|
|
|
} line_info_t;
|
2018-10-14 19:59:15 +03:00
|
|
|
|
|
|
|
struct dwarf_section {
|
|
|
|
char *ptr;
|
|
|
|
size_t size;
|
2018-10-20 09:35:25 +03:00
|
|
|
uint64_t flags;
|
2018-10-14 19:59:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct obj_info {
|
2014-04-01 07:38:05 +04:00
|
|
|
const char *path; /* object path */
|
2018-10-14 19:59:15 +03:00
|
|
|
char *mapped;
|
2014-04-01 07:38:05 +04:00
|
|
|
size_t mapped_size;
|
2018-10-14 19:59:15 +03:00
|
|
|
void *uncompressed;
|
2014-04-01 07:38:05 +04:00
|
|
|
uintptr_t base_addr;
|
2018-10-17 02:52:28 +03:00
|
|
|
uintptr_t vmaddr;
|
2018-10-14 19:59:15 +03:00
|
|
|
struct dwarf_section debug_abbrev;
|
|
|
|
struct dwarf_section debug_info;
|
|
|
|
struct dwarf_section debug_line;
|
|
|
|
struct dwarf_section debug_ranges;
|
2022-12-22 16:29:39 +03:00
|
|
|
struct dwarf_section debug_str_offsets;
|
|
|
|
struct dwarf_section debug_addr;
|
2021-03-15 09:22:34 +03:00
|
|
|
struct dwarf_section debug_rnglists;
|
2018-10-14 19:59:15 +03:00
|
|
|
struct dwarf_section debug_str;
|
2022-12-21 16:13:22 +03:00
|
|
|
struct dwarf_section debug_line_str;
|
2018-10-14 19:59:15 +03:00
|
|
|
struct obj_info *next;
|
|
|
|
} obj_info_t;
|
2018-10-21 19:37:24 +03:00
|
|
|
|
2022-12-22 16:29:39 +03:00
|
|
|
#define DWARF_SECTION_COUNT 9
|
2018-10-14 19:59:15 +03:00
|
|
|
|
2018-10-21 19:37:24 +03:00
|
|
|
static struct dwarf_section *
|
|
|
|
obj_dwarf_section_at(obj_info_t *obj, int n)
|
|
|
|
{
|
|
|
|
struct dwarf_section *ary[] = {
|
|
|
|
&obj->debug_abbrev,
|
|
|
|
&obj->debug_info,
|
|
|
|
&obj->debug_line,
|
|
|
|
&obj->debug_ranges,
|
2022-12-22 16:29:39 +03:00
|
|
|
&obj->debug_str_offsets,
|
|
|
|
&obj->debug_addr,
|
2021-03-15 09:22:34 +03:00
|
|
|
&obj->debug_rnglists,
|
2022-12-21 16:13:22 +03:00
|
|
|
&obj->debug_str,
|
|
|
|
&obj->debug_line_str
|
2018-10-21 19:37:24 +03:00
|
|
|
};
|
|
|
|
if (n < 0 || DWARF_SECTION_COUNT <= n) {
|
2023-08-01 06:11:05 +03:00
|
|
|
UNREACHABLE_RETURN(0);
|
2018-10-21 19:37:24 +03:00
|
|
|
}
|
|
|
|
return ary[n];
|
|
|
|
}
|
|
|
|
|
2018-10-14 19:59:15 +03:00
|
|
|
struct debug_section_definition {
|
|
|
|
const char *name;
|
|
|
|
struct dwarf_section *dwarf;
|
2014-04-01 07:38:05 +04:00
|
|
|
};
|
2010-11-26 06:28:50 +03:00
|
|
|
|
|
|
|
/* Avoid consuming stack as this module may be used from signal handler */
|
2019-07-14 14:23:29 +03:00
|
|
|
static char binary_filename[PATH_MAX + 1];
|
2010-11-26 06:28:50 +03:00
|
|
|
|
|
|
|
static unsigned long
|
2021-08-14 04:08:19 +03:00
|
|
|
uleb128(const char **p)
|
2012-03-15 05:39:00 +04:00
|
|
|
{
|
2010-11-26 06:28:50 +03:00
|
|
|
unsigned long r = 0;
|
|
|
|
int s = 0;
|
|
|
|
for (;;) {
|
2021-08-14 04:08:19 +03:00
|
|
|
unsigned char b = (unsigned char)*(*p)++;
|
2010-11-26 06:28:50 +03:00
|
|
|
if (b < 0x80) {
|
2011-03-02 19:14:32 +03:00
|
|
|
r += (unsigned long)b << s;
|
2010-11-26 06:28:50 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
r += (b & 0x7f) << s;
|
|
|
|
s += 7;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
2021-08-14 04:08:19 +03:00
|
|
|
sleb128(const char **p)
|
2012-03-15 05:39:00 +04:00
|
|
|
{
|
2010-11-26 06:28:50 +03:00
|
|
|
long r = 0;
|
|
|
|
int s = 0;
|
|
|
|
for (;;) {
|
2021-08-14 04:08:19 +03:00
|
|
|
unsigned char b = (unsigned char)*(*p)++;
|
2010-11-26 06:28:50 +03:00
|
|
|
if (b < 0x80) {
|
|
|
|
if (b & 0x40) {
|
|
|
|
r -= (0x80 - b) << s;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
r += (b & 0x3f) << s;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
r += (b & 0x7f) << s;
|
|
|
|
s += 7;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
2023-07-31 21:04:42 +03:00
|
|
|
get_nth_dirname(unsigned long dir, const char *p, FILE *errout)
|
2010-11-26 06:28:50 +03:00
|
|
|
{
|
|
|
|
if (!dir--) {
|
|
|
|
return "";
|
|
|
|
}
|
2011-01-12 18:35:07 +03:00
|
|
|
while (dir--) {
|
2010-11-26 06:28:50 +03:00
|
|
|
while (*p) p++;
|
|
|
|
p++;
|
|
|
|
if (!*p) {
|
2013-03-23 20:05:51 +04:00
|
|
|
kprintf("Unexpected directory number %lu in %s\n",
|
2010-11-26 06:28:50 +03:00
|
|
|
dir, binary_filename);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2023-07-31 21:04:42 +03:00
|
|
|
static const char *parse_ver5_debug_line_header(
|
|
|
|
const char *p, int idx, uint8_t format,
|
|
|
|
obj_info_t *obj, const char **out_path,
|
|
|
|
uint64_t *out_directory_index, FILE *errout);
|
2022-12-21 16:18:48 +03:00
|
|
|
|
2010-11-26 06:28:50 +03:00
|
|
|
static void
|
2023-07-31 21:04:42 +03:00
|
|
|
fill_filename(int file, uint8_t format, uint16_t version, const char *include_directories,
|
|
|
|
const char *filenames, line_info_t *line, obj_info_t *obj, FILE *errout)
|
2010-11-26 06:28:50 +03:00
|
|
|
{
|
|
|
|
int i;
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *p = filenames;
|
|
|
|
const char *filename;
|
2010-11-26 06:28:50 +03:00
|
|
|
unsigned long dir;
|
2022-12-21 16:18:48 +03:00
|
|
|
if (version >= 5) {
|
|
|
|
const char *path;
|
|
|
|
uint64_t directory_index = -1;
|
2023-07-31 21:04:42 +03:00
|
|
|
parse_ver5_debug_line_header(filenames, file, format, obj, &path, &directory_index, errout);
|
2022-12-21 16:18:48 +03:00
|
|
|
line->filename = path;
|
2023-07-31 21:04:42 +03:00
|
|
|
parse_ver5_debug_line_header(include_directories, (int)directory_index, format, obj, &path, NULL, errout);
|
2022-12-21 16:18:48 +03:00
|
|
|
line->dirname = path;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (i = 1; i <= file; i++) {
|
|
|
|
filename = p;
|
|
|
|
if (!*p) {
|
|
|
|
/* Need to output binary file name? */
|
|
|
|
kprintf("Unexpected file number %d in %s at %tx\n",
|
|
|
|
file, binary_filename, filenames - obj->mapped);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (*p) p++;
|
|
|
|
p++;
|
|
|
|
dir = uleb128(&p);
|
|
|
|
/* last modified. */
|
|
|
|
uleb128(&p);
|
|
|
|
/* size of the file. */
|
|
|
|
uleb128(&p);
|
|
|
|
|
|
|
|
if (i == file) {
|
|
|
|
line->filename = filename;
|
2023-07-31 21:04:42 +03:00
|
|
|
line->dirname = get_nth_dirname(dir, include_directories, errout);
|
2022-12-21 16:18:48 +03:00
|
|
|
}
|
|
|
|
}
|
2010-11-26 06:28:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-04-01 07:38:05 +04:00
|
|
|
fill_line(int num_traces, void **traces, uintptr_t addr, int file, int line,
|
2022-12-21 16:18:48 +03:00
|
|
|
uint8_t format, uint16_t version, const char *include_directories, const char *filenames,
|
2023-07-31 21:04:42 +03:00
|
|
|
obj_info_t *obj, line_info_t *lines, int offset, FILE *errout)
|
2010-11-26 06:28:50 +03:00
|
|
|
{
|
|
|
|
int i;
|
2018-10-17 02:52:28 +03:00
|
|
|
addr += obj->base_addr - obj->vmaddr;
|
2014-03-26 05:38:05 +04:00
|
|
|
for (i = offset; i < num_traces; i++) {
|
2014-03-26 08:30:43 +04:00
|
|
|
uintptr_t a = (uintptr_t)traces[i];
|
2010-11-26 06:28:50 +03:00
|
|
|
/* We assume one line code doesn't result >100 bytes of native code.
|
|
|
|
We may want more reliable way eventually... */
|
|
|
|
if (addr < a && a < addr + 100) {
|
2023-07-31 21:04:42 +03:00
|
|
|
fill_filename(file, format, version, include_directories, filenames, &lines[i], obj, errout);
|
2010-11-26 06:28:50 +03:00
|
|
|
lines[i].line = line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 03:07:53 +03:00
|
|
|
struct LineNumberProgramHeader {
|
|
|
|
uint64_t unit_length;
|
|
|
|
uint16_t version;
|
|
|
|
uint8_t format; /* 4 or 8 */
|
|
|
|
uint64_t header_length;
|
|
|
|
uint8_t minimum_instruction_length;
|
|
|
|
uint8_t maximum_operations_per_instruction;
|
|
|
|
uint8_t default_is_stmt;
|
2018-10-21 13:46:56 +03:00
|
|
|
int8_t line_base;
|
2018-10-21 03:07:53 +03:00
|
|
|
uint8_t line_range;
|
|
|
|
uint8_t opcode_base;
|
|
|
|
/* uint8_t standard_opcode_lengths[opcode_base-1]; */
|
|
|
|
const char *include_directories;
|
|
|
|
const char *filenames;
|
|
|
|
const char *cu_start;
|
|
|
|
const char *cu_end;
|
|
|
|
};
|
|
|
|
|
2016-10-18 18:09:08 +03:00
|
|
|
static int
|
2023-07-31 21:04:42 +03:00
|
|
|
parse_debug_line_header(obj_info_t *obj, const char **pp, struct LineNumberProgramHeader *header, FILE *errout)
|
2010-11-26 06:28:50 +03:00
|
|
|
{
|
2018-10-21 03:07:53 +03:00
|
|
|
const char *p = *pp;
|
|
|
|
header->unit_length = *(uint32_t *)p;
|
|
|
|
p += sizeof(uint32_t);
|
|
|
|
|
|
|
|
header->format = 4;
|
|
|
|
if (header->unit_length == 0xffffffff) {
|
|
|
|
header->unit_length = *(uint64_t *)p;
|
|
|
|
p += sizeof(uint64_t);
|
|
|
|
header->format = 8;
|
2010-11-26 06:28:50 +03:00
|
|
|
}
|
|
|
|
|
2018-10-21 03:07:53 +03:00
|
|
|
header->cu_end = p + header->unit_length;
|
2010-11-26 06:28:50 +03:00
|
|
|
|
2018-10-21 03:07:53 +03:00
|
|
|
header->version = *(uint16_t *)p;
|
|
|
|
p += sizeof(uint16_t);
|
2022-12-21 16:18:48 +03:00
|
|
|
if (header->version > 5) return -1;
|
|
|
|
|
|
|
|
if (header->version >= 5) {
|
|
|
|
/* address_size = *(uint8_t *)p++; */
|
|
|
|
/* segment_selector_size = *(uint8_t *)p++; */
|
|
|
|
p += 2;
|
|
|
|
}
|
2010-11-26 06:28:50 +03:00
|
|
|
|
2018-10-21 03:07:53 +03:00
|
|
|
header->header_length = header->format == 4 ? *(uint32_t *)p : *(uint64_t *)p;
|
|
|
|
p += header->format;
|
|
|
|
header->cu_start = p + header->header_length;
|
2010-11-26 06:28:50 +03:00
|
|
|
|
2018-10-21 03:07:53 +03:00
|
|
|
header->minimum_instruction_length = *(uint8_t *)p++;
|
2010-11-26 06:28:50 +03:00
|
|
|
|
2018-10-21 03:07:53 +03:00
|
|
|
if (header->version >= 4) {
|
|
|
|
/* maximum_operations_per_instruction = *(uint8_t *)p; */
|
2018-10-21 23:01:22 +03:00
|
|
|
if (*p != 1) return -1; /* For non-VLIW architectures, this field is 1 */
|
2018-10-20 09:18:41 +03:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
2018-10-21 03:07:53 +03:00
|
|
|
header->default_is_stmt = *(uint8_t *)p++;
|
|
|
|
header->line_base = *(int8_t *)p++;
|
|
|
|
header->line_range = *(uint8_t *)p++;
|
|
|
|
header->opcode_base = *(uint8_t *)p++;
|
|
|
|
/* header->standard_opcode_lengths = (uint8_t *)p - 1; */
|
|
|
|
p += header->opcode_base - 1;
|
2010-11-26 06:28:50 +03:00
|
|
|
|
2022-12-21 16:18:48 +03:00
|
|
|
if (header->version >= 5) {
|
|
|
|
header->include_directories = p;
|
2023-07-31 21:04:42 +03:00
|
|
|
p = parse_ver5_debug_line_header(p, -1, header->format, obj, NULL, NULL, errout);
|
2022-12-21 16:18:48 +03:00
|
|
|
header->filenames = p;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
header->include_directories = p;
|
2010-11-26 06:28:50 +03:00
|
|
|
|
2022-12-21 16:18:48 +03:00
|
|
|
/* temporary measure for compress-debug-sections */
|
|
|
|
if (p >= header->cu_end) return -1;
|
2016-10-18 18:09:08 +03:00
|
|
|
|
2022-12-21 16:18:48 +03:00
|
|
|
/* skip include directories */
|
|
|
|
while (*p) {
|
|
|
|
p = memchr(p, '\0', header->cu_end - p);
|
|
|
|
if (!p) return -1;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
p++;
|
2010-11-26 06:28:50 +03:00
|
|
|
|
2022-12-21 16:18:48 +03:00
|
|
|
header->filenames = p;
|
|
|
|
}
|
2010-11-26 06:28:50 +03:00
|
|
|
|
2018-10-21 03:07:53 +03:00
|
|
|
*pp = header->cu_start;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-08-14 04:08:19 +03:00
|
|
|
parse_debug_line_cu(int num_traces, void **traces, const char **debug_line,
|
2023-07-31 21:04:42 +03:00
|
|
|
obj_info_t *obj, line_info_t *lines, int offset, FILE *errout)
|
2018-10-21 03:07:53 +03:00
|
|
|
{
|
|
|
|
const char *p = (const char *)*debug_line;
|
|
|
|
struct LineNumberProgramHeader header;
|
|
|
|
|
|
|
|
/* The registers. */
|
|
|
|
unsigned long addr = 0;
|
|
|
|
unsigned int file = 1;
|
|
|
|
unsigned int line = 1;
|
|
|
|
/* unsigned int column = 0; */
|
|
|
|
int is_stmt;
|
|
|
|
/* int basic_block = 0; */
|
|
|
|
/* int end_sequence = 0; */
|
|
|
|
/* int prologue_end = 0; */
|
|
|
|
/* int epilogue_begin = 0; */
|
|
|
|
/* unsigned int isa = 0; */
|
|
|
|
|
2023-07-31 21:04:42 +03:00
|
|
|
if (parse_debug_line_header(obj, &p, &header, errout))
|
2018-10-21 03:07:53 +03:00
|
|
|
return -1;
|
2018-10-21 19:42:23 +03:00
|
|
|
is_stmt = header.default_is_stmt;
|
2010-11-26 06:28:50 +03:00
|
|
|
|
|
|
|
#define FILL_LINE() \
|
|
|
|
do { \
|
|
|
|
fill_line(num_traces, traces, addr, file, line, \
|
2022-12-21 16:18:48 +03:00
|
|
|
header.format, \
|
|
|
|
header.version, \
|
2021-08-14 04:08:19 +03:00
|
|
|
header.include_directories, \
|
|
|
|
header.filenames, \
|
2023-07-31 21:04:42 +03:00
|
|
|
obj, lines, offset, errout); \
|
2012-05-06 16:39:59 +04:00
|
|
|
/*basic_block = prologue_end = epilogue_begin = 0;*/ \
|
2010-11-26 06:28:50 +03:00
|
|
|
} while (0)
|
|
|
|
|
2018-10-21 03:07:53 +03:00
|
|
|
while (p < header.cu_end) {
|
2010-11-26 06:28:50 +03:00
|
|
|
unsigned long a;
|
|
|
|
unsigned char op = *p++;
|
|
|
|
switch (op) {
|
|
|
|
case DW_LNS_copy:
|
|
|
|
FILL_LINE();
|
|
|
|
break;
|
|
|
|
case DW_LNS_advance_pc:
|
2021-08-14 04:08:19 +03:00
|
|
|
a = uleb128(&p) * header.minimum_instruction_length;
|
2010-11-26 06:28:50 +03:00
|
|
|
addr += a;
|
|
|
|
break;
|
|
|
|
case DW_LNS_advance_line: {
|
2021-08-14 04:08:19 +03:00
|
|
|
long a = sleb128(&p);
|
2010-11-26 06:28:50 +03:00
|
|
|
line += a;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DW_LNS_set_file:
|
2021-08-14 04:08:19 +03:00
|
|
|
file = (unsigned int)uleb128(&p);
|
2010-11-26 06:28:50 +03:00
|
|
|
break;
|
|
|
|
case DW_LNS_set_column:
|
2021-08-14 04:08:19 +03:00
|
|
|
/*column = (unsigned int)*/(void)uleb128(&p);
|
2010-11-26 06:28:50 +03:00
|
|
|
break;
|
|
|
|
case DW_LNS_negate_stmt:
|
|
|
|
is_stmt = !is_stmt;
|
|
|
|
break;
|
|
|
|
case DW_LNS_set_basic_block:
|
2012-05-06 16:39:59 +04:00
|
|
|
/*basic_block = 1; */
|
2010-11-26 06:28:50 +03:00
|
|
|
break;
|
|
|
|
case DW_LNS_const_add_pc:
|
2019-07-15 02:36:57 +03:00
|
|
|
a = ((255UL - header.opcode_base) / header.line_range) *
|
2018-10-21 03:07:53 +03:00
|
|
|
header.minimum_instruction_length;
|
2010-11-26 06:28:50 +03:00
|
|
|
addr += a;
|
|
|
|
break;
|
|
|
|
case DW_LNS_fixed_advance_pc:
|
2021-02-04 11:45:54 +03:00
|
|
|
a = *(uint16_t *)p;
|
|
|
|
p += sizeof(uint16_t);
|
2010-11-26 06:28:50 +03:00
|
|
|
addr += a;
|
|
|
|
break;
|
|
|
|
case DW_LNS_set_prologue_end:
|
2012-05-06 16:39:59 +04:00
|
|
|
/* prologue_end = 1; */
|
2010-11-26 06:28:50 +03:00
|
|
|
break;
|
|
|
|
case DW_LNS_set_epilogue_begin:
|
2012-05-06 16:39:59 +04:00
|
|
|
/* epilogue_begin = 1; */
|
2010-11-26 06:28:50 +03:00
|
|
|
break;
|
|
|
|
case DW_LNS_set_isa:
|
2021-08-14 04:08:19 +03:00
|
|
|
/* isa = (unsigned int)*/(void)uleb128(&p);
|
2010-11-26 06:28:50 +03:00
|
|
|
break;
|
|
|
|
case 0:
|
2021-08-14 04:08:19 +03:00
|
|
|
a = uleb128(&p);
|
2010-11-26 06:28:50 +03:00
|
|
|
op = *p++;
|
|
|
|
switch (op) {
|
|
|
|
case DW_LNE_end_sequence:
|
2012-05-06 16:39:59 +04:00
|
|
|
/* end_sequence = 1; */
|
2010-11-26 06:28:50 +03:00
|
|
|
FILL_LINE();
|
|
|
|
addr = 0;
|
|
|
|
file = 1;
|
|
|
|
line = 1;
|
2012-05-06 16:39:59 +04:00
|
|
|
/* column = 0; */
|
2018-10-21 03:07:53 +03:00
|
|
|
is_stmt = header.default_is_stmt;
|
2012-05-06 16:39:59 +04:00
|
|
|
/* end_sequence = 0; */
|
|
|
|
/* isa = 0; */
|
2010-11-26 06:28:50 +03:00
|
|
|
break;
|
|
|
|
case DW_LNE_set_address:
|
|
|
|
addr = *(unsigned long *)p;
|
|
|
|
p += sizeof(unsigned long);
|
|
|
|
break;
|
|
|
|
case DW_LNE_define_file:
|
2013-03-23 20:05:51 +04:00
|
|
|
kprintf("Unsupported operation in %s\n",
|
2010-11-26 06:28:50 +03:00
|
|
|
binary_filename);
|
|
|
|
break;
|
2010-11-30 11:06:24 +03:00
|
|
|
case DW_LNE_set_discriminator:
|
|
|
|
/* TODO:currently ignore */
|
2021-08-14 04:08:19 +03:00
|
|
|
uleb128(&p);
|
2010-11-30 11:06:24 +03:00
|
|
|
break;
|
2010-11-26 06:28:50 +03:00
|
|
|
default:
|
2013-03-23 20:05:51 +04:00
|
|
|
kprintf("Unknown extended opcode: %d in %s\n",
|
2010-11-26 06:28:50 +03:00
|
|
|
op, binary_filename);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: {
|
2018-10-21 23:01:22 +03:00
|
|
|
uint8_t adjusted_opcode = op - header.opcode_base;
|
|
|
|
uint8_t operation_advance = adjusted_opcode / header.line_range;
|
|
|
|
/* NOTE: this code doesn't support VLIW */
|
|
|
|
addr += operation_advance * header.minimum_instruction_length;
|
|
|
|
line += header.line_base + (adjusted_opcode % header.line_range);
|
2010-11-26 06:28:50 +03:00
|
|
|
FILL_LINE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-21 03:07:53 +03:00
|
|
|
*debug_line = (char *)p;
|
2016-10-18 18:09:08 +03:00
|
|
|
return 0;
|
2010-11-26 06:28:50 +03:00
|
|
|
}
|
|
|
|
|
2016-10-18 18:09:08 +03:00
|
|
|
static int
|
2010-11-26 06:28:50 +03:00
|
|
|
parse_debug_line(int num_traces, void **traces,
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *debug_line, unsigned long size,
|
2023-07-31 21:04:42 +03:00
|
|
|
obj_info_t *obj, line_info_t *lines, int offset, FILE *errout)
|
2010-11-26 06:28:50 +03:00
|
|
|
{
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *debug_line_end = debug_line + size;
|
2010-11-26 06:28:50 +03:00
|
|
|
while (debug_line < debug_line_end) {
|
2023-07-31 21:04:42 +03:00
|
|
|
if (parse_debug_line_cu(num_traces, traces, &debug_line, obj, lines, offset, errout))
|
2016-10-18 18:09:08 +03:00
|
|
|
return -1;
|
2010-11-26 06:28:50 +03:00
|
|
|
}
|
|
|
|
if (debug_line != debug_line_end) {
|
2013-03-23 20:05:51 +04:00
|
|
|
kprintf("Unexpected size of .debug_line in %s\n",
|
2010-11-26 06:28:50 +03:00
|
|
|
binary_filename);
|
|
|
|
}
|
2016-10-18 18:09:08 +03:00
|
|
|
return 0;
|
2010-11-26 06:28:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read file and fill lines */
|
2014-04-14 14:08:04 +04:00
|
|
|
static uintptr_t
|
2014-03-26 08:30:43 +04:00
|
|
|
fill_lines(int num_traces, void **traces, int check_debuglink,
|
2023-07-31 21:04:42 +03:00
|
|
|
obj_info_t **objp, line_info_t *lines, int offset, FILE *errout);
|
2014-04-01 07:38:05 +04:00
|
|
|
|
|
|
|
static void
|
2016-12-26 12:28:46 +03:00
|
|
|
append_obj(obj_info_t **objp)
|
|
|
|
{
|
2014-04-10 11:19:53 +04:00
|
|
|
obj_info_t *newobj = calloc(1, sizeof(obj_info_t));
|
2014-04-01 07:38:05 +04:00
|
|
|
if (*objp) (*objp)->next = newobj;
|
|
|
|
*objp = newobj;
|
|
|
|
}
|
2010-12-01 03:32:19 +03:00
|
|
|
|
2018-10-20 09:35:25 +03:00
|
|
|
#ifdef USE_ELF
|
2020-10-03 18:07:37 +03:00
|
|
|
/* Ideally we should check 4 paths to follow gnu_debuglink:
|
|
|
|
*
|
|
|
|
* - /usr/lib/debug/.build-id/ab/cdef1234.debug
|
|
|
|
* - /usr/bin/ruby.debug
|
|
|
|
* - /usr/bin/.debug/ruby.debug
|
|
|
|
* - /usr/lib/debug/usr/bin/ruby.debug.
|
|
|
|
*
|
|
|
|
* but we handle only two cases for now as the two formats are
|
|
|
|
* used by some linux distributions.
|
|
|
|
*
|
|
|
|
* See GDB's info for detail.
|
|
|
|
* https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
// check the path pattern of "/usr/lib/debug/usr/bin/ruby.debug"
|
2010-12-01 03:32:19 +03:00
|
|
|
static void
|
2016-12-26 12:28:46 +03:00
|
|
|
follow_debuglink(const char *debuglink, int num_traces, void **traces,
|
2023-07-31 21:04:42 +03:00
|
|
|
obj_info_t **objp, line_info_t *lines, int offset, FILE *errout)
|
2010-12-01 03:32:19 +03:00
|
|
|
{
|
|
|
|
static const char global_debug_dir[] = "/usr/lib/debug";
|
2016-12-26 12:28:46 +03:00
|
|
|
const size_t global_debug_dir_len = sizeof(global_debug_dir) - 1;
|
|
|
|
char *p;
|
2014-04-01 07:38:05 +04:00
|
|
|
obj_info_t *o1 = *objp, *o2;
|
2016-12-26 12:28:46 +03:00
|
|
|
size_t len;
|
2010-12-01 03:32:19 +03:00
|
|
|
|
|
|
|
p = strrchr(binary_filename, '/');
|
|
|
|
if (!p) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
p[1] = '\0';
|
|
|
|
|
2016-12-26 12:28:46 +03:00
|
|
|
len = strlen(binary_filename);
|
|
|
|
if (len >= PATH_MAX - global_debug_dir_len)
|
|
|
|
len = PATH_MAX - global_debug_dir_len - 1;
|
|
|
|
memmove(binary_filename + global_debug_dir_len, binary_filename, len);
|
|
|
|
memcpy(binary_filename, global_debug_dir, global_debug_dir_len);
|
|
|
|
len += global_debug_dir_len;
|
|
|
|
strlcpy(binary_filename + len, debuglink, PATH_MAX - len);
|
2010-12-01 03:32:19 +03:00
|
|
|
|
2014-04-01 07:38:05 +04:00
|
|
|
append_obj(objp);
|
|
|
|
o2 = *objp;
|
|
|
|
o2->base_addr = o1->base_addr;
|
|
|
|
o2->path = o1->path;
|
2023-07-31 21:04:42 +03:00
|
|
|
fill_lines(num_traces, traces, 0, objp, lines, offset, errout);
|
2010-12-01 03:32:19 +03:00
|
|
|
}
|
2020-10-03 18:07:37 +03:00
|
|
|
|
|
|
|
// check the path pattern of "/usr/lib/debug/.build-id/ab/cdef1234.debug"
|
|
|
|
static void
|
|
|
|
follow_debuglink_build_id(const char *build_id, size_t build_id_size, int num_traces, void **traces,
|
2023-07-31 21:04:42 +03:00
|
|
|
obj_info_t **objp, line_info_t *lines, int offset, FILE *errout)
|
2020-10-03 18:07:37 +03:00
|
|
|
{
|
|
|
|
static const char global_debug_dir[] = "/usr/lib/debug/.build-id/";
|
|
|
|
const size_t global_debug_dir_len = sizeof(global_debug_dir) - 1;
|
|
|
|
char *p;
|
|
|
|
obj_info_t *o1 = *objp, *o2;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (PATH_MAX < global_debug_dir_len + 1 + build_id_size * 2 + 6) return;
|
|
|
|
|
|
|
|
memcpy(binary_filename, global_debug_dir, global_debug_dir_len);
|
|
|
|
p = binary_filename + global_debug_dir_len;
|
|
|
|
for (i = 0; i < build_id_size; i++) {
|
|
|
|
static const char tbl[] = "0123456789abcdef";
|
|
|
|
unsigned char n = build_id[i];
|
|
|
|
*p++ = tbl[n / 16];
|
|
|
|
*p++ = tbl[n % 16];
|
|
|
|
if (i == 0) *p++ = '/';
|
|
|
|
}
|
|
|
|
strcpy(p, ".debug");
|
|
|
|
|
|
|
|
append_obj(objp);
|
|
|
|
o2 = *objp;
|
|
|
|
o2->base_addr = o1->base_addr;
|
|
|
|
o2->path = o1->path;
|
2023-07-31 21:04:42 +03:00
|
|
|
fill_lines(num_traces, traces, 0, objp, lines, offset, errout);
|
2020-10-03 18:07:37 +03:00
|
|
|
}
|
2018-10-20 09:35:25 +03:00
|
|
|
#endif
|
2010-12-01 03:32:19 +03:00
|
|
|
|
2018-10-14 19:59:15 +03:00
|
|
|
enum
|
|
|
|
{
|
2018-10-17 02:52:28 +03:00
|
|
|
DW_TAG_compile_unit = 0x11,
|
2018-10-14 19:59:15 +03:00
|
|
|
DW_TAG_inlined_subroutine = 0x1d,
|
|
|
|
DW_TAG_subprogram = 0x2e,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Attributes encodings */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
DW_AT_sibling = 0x01,
|
|
|
|
DW_AT_location = 0x02,
|
|
|
|
DW_AT_name = 0x03,
|
|
|
|
/* Reserved 0x04 */
|
|
|
|
/* Reserved 0x05 */
|
|
|
|
/* Reserved 0x06 */
|
|
|
|
/* Reserved 0x07 */
|
|
|
|
/* Reserved 0x08 */
|
|
|
|
DW_AT_ordering = 0x09,
|
|
|
|
/* Reserved 0x0a */
|
|
|
|
DW_AT_byte_size = 0x0b,
|
|
|
|
/* Reserved 0x0c */
|
|
|
|
DW_AT_bit_size = 0x0d,
|
|
|
|
/* Reserved 0x0e */
|
|
|
|
/* Reserved 0x0f */
|
|
|
|
DW_AT_stmt_list = 0x10,
|
|
|
|
DW_AT_low_pc = 0x11,
|
|
|
|
DW_AT_high_pc = 0x12,
|
|
|
|
DW_AT_language = 0x13,
|
|
|
|
/* Reserved 0x14 */
|
|
|
|
DW_AT_discr = 0x15,
|
|
|
|
DW_AT_discr_value = 0x16,
|
|
|
|
DW_AT_visibility = 0x17,
|
|
|
|
DW_AT_import = 0x18,
|
|
|
|
DW_AT_string_length = 0x19,
|
|
|
|
DW_AT_common_reference = 0x1a,
|
|
|
|
DW_AT_comp_dir = 0x1b,
|
|
|
|
DW_AT_const_value = 0x1c,
|
|
|
|
DW_AT_containing_type = 0x1d,
|
|
|
|
DW_AT_default_value = 0x1e,
|
|
|
|
/* Reserved 0x1f */
|
|
|
|
DW_AT_inline = 0x20,
|
|
|
|
DW_AT_is_optional = 0x21,
|
|
|
|
DW_AT_lower_bound = 0x22,
|
|
|
|
/* Reserved 0x23 */
|
|
|
|
/* Reserved 0x24 */
|
|
|
|
DW_AT_producer = 0x25,
|
|
|
|
/* Reserved 0x26 */
|
|
|
|
DW_AT_prototyped = 0x27,
|
|
|
|
/* Reserved 0x28 */
|
|
|
|
/* Reserved 0x29 */
|
|
|
|
DW_AT_return_addr = 0x2a,
|
|
|
|
/* Reserved 0x2b */
|
|
|
|
DW_AT_start_scope = 0x2c,
|
|
|
|
/* Reserved 0x2d */
|
|
|
|
DW_AT_bit_stride = 0x2e,
|
|
|
|
DW_AT_upper_bound = 0x2f,
|
|
|
|
/* Reserved 0x30 */
|
|
|
|
DW_AT_abstract_origin = 0x31,
|
|
|
|
DW_AT_accessibility = 0x32,
|
|
|
|
DW_AT_address_class = 0x33,
|
|
|
|
DW_AT_artificial = 0x34,
|
|
|
|
DW_AT_base_types = 0x35,
|
|
|
|
DW_AT_calling_convention = 0x36,
|
|
|
|
DW_AT_count = 0x37,
|
|
|
|
DW_AT_data_member_location = 0x38,
|
|
|
|
DW_AT_decl_column = 0x39,
|
|
|
|
DW_AT_decl_file = 0x3a,
|
|
|
|
DW_AT_decl_line = 0x3b,
|
|
|
|
DW_AT_declaration = 0x3c,
|
|
|
|
DW_AT_discr_list = 0x3d,
|
|
|
|
DW_AT_encoding = 0x3e,
|
|
|
|
DW_AT_external = 0x3f,
|
|
|
|
DW_AT_frame_base = 0x40,
|
|
|
|
DW_AT_friend = 0x41,
|
|
|
|
DW_AT_identifier_case = 0x42,
|
|
|
|
/* Reserved 0x43 */
|
|
|
|
DW_AT_namelist_item = 0x44,
|
|
|
|
DW_AT_priority = 0x45,
|
|
|
|
DW_AT_segment = 0x46,
|
|
|
|
DW_AT_specification = 0x47,
|
|
|
|
DW_AT_static_link = 0x48,
|
|
|
|
DW_AT_type = 0x49,
|
|
|
|
DW_AT_use_location = 0x4a,
|
|
|
|
DW_AT_variable_parameter = 0x4b,
|
|
|
|
DW_AT_virtuality = 0x4c,
|
|
|
|
DW_AT_vtable_elem_location = 0x4d,
|
|
|
|
DW_AT_allocated = 0x4e,
|
|
|
|
DW_AT_associated = 0x4f,
|
|
|
|
DW_AT_data_location = 0x50,
|
|
|
|
DW_AT_byte_stride = 0x51,
|
|
|
|
DW_AT_entry_pc = 0x52,
|
|
|
|
DW_AT_use_UTF8 = 0x53,
|
|
|
|
DW_AT_extension = 0x54,
|
|
|
|
DW_AT_ranges = 0x55,
|
|
|
|
DW_AT_trampoline = 0x56,
|
|
|
|
DW_AT_call_column = 0x57,
|
|
|
|
DW_AT_call_file = 0x58,
|
|
|
|
DW_AT_call_line = 0x59,
|
|
|
|
DW_AT_description = 0x5a,
|
|
|
|
DW_AT_binary_scale = 0x5b,
|
|
|
|
DW_AT_decimal_scale = 0x5c,
|
|
|
|
DW_AT_small = 0x5d,
|
|
|
|
DW_AT_decimal_sign = 0x5e,
|
|
|
|
DW_AT_digit_count = 0x5f,
|
|
|
|
DW_AT_picture_string = 0x60,
|
|
|
|
DW_AT_mutable = 0x61,
|
|
|
|
DW_AT_threads_scaled = 0x62,
|
|
|
|
DW_AT_explicit = 0x63,
|
|
|
|
DW_AT_object_pointer = 0x64,
|
|
|
|
DW_AT_endianity = 0x65,
|
|
|
|
DW_AT_elemental = 0x66,
|
|
|
|
DW_AT_pure = 0x67,
|
|
|
|
DW_AT_recursive = 0x68,
|
|
|
|
DW_AT_signature = 0x69,
|
|
|
|
DW_AT_main_subprogram = 0x6a,
|
|
|
|
DW_AT_data_bit_offset = 0x6b,
|
|
|
|
DW_AT_const_expr = 0x6c,
|
|
|
|
DW_AT_enum_class = 0x6d,
|
|
|
|
DW_AT_linkage_name = 0x6e,
|
|
|
|
DW_AT_string_length_bit_size = 0x6f,
|
|
|
|
DW_AT_string_length_byte_size = 0x70,
|
|
|
|
DW_AT_rank = 0x71,
|
|
|
|
DW_AT_str_offsets_base = 0x72,
|
|
|
|
DW_AT_addr_base = 0x73,
|
|
|
|
DW_AT_rnglists_base = 0x74,
|
|
|
|
/* Reserved 0x75 */
|
|
|
|
DW_AT_dwo_name = 0x76,
|
|
|
|
DW_AT_reference = 0x77,
|
|
|
|
DW_AT_rvalue_reference = 0x78,
|
|
|
|
DW_AT_macros = 0x79,
|
|
|
|
DW_AT_call_all_calls = 0x7a,
|
|
|
|
DW_AT_call_all_source_calls = 0x7b,
|
|
|
|
DW_AT_call_all_tail_calls = 0x7c,
|
|
|
|
DW_AT_call_return_pc = 0x7d,
|
|
|
|
DW_AT_call_value = 0x7e,
|
|
|
|
DW_AT_call_origin = 0x7f,
|
|
|
|
DW_AT_call_parameter = 0x80,
|
|
|
|
DW_AT_call_pc = 0x81,
|
|
|
|
DW_AT_call_tail_call = 0x82,
|
|
|
|
DW_AT_call_target = 0x83,
|
|
|
|
DW_AT_call_target_clobbered = 0x84,
|
|
|
|
DW_AT_call_data_location = 0x85,
|
|
|
|
DW_AT_call_data_value = 0x86,
|
|
|
|
DW_AT_noreturn = 0x87,
|
|
|
|
DW_AT_alignment = 0x88,
|
|
|
|
DW_AT_export_symbols = 0x89,
|
|
|
|
DW_AT_deleted = 0x8a,
|
|
|
|
DW_AT_defaulted = 0x8b,
|
|
|
|
DW_AT_loclists_base = 0x8c,
|
|
|
|
DW_AT_lo_user = 0x2000,
|
|
|
|
DW_AT_hi_user = 0x3fff
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Attribute form encodings */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
DW_FORM_addr = 0x01,
|
|
|
|
/* Reserved 0x02 */
|
|
|
|
DW_FORM_block2 = 0x03,
|
|
|
|
DW_FORM_block4 = 0x04,
|
|
|
|
DW_FORM_data2 = 0x05,
|
|
|
|
DW_FORM_data4 = 0x06,
|
|
|
|
DW_FORM_data8 = 0x07,
|
|
|
|
DW_FORM_string = 0x08,
|
|
|
|
DW_FORM_block = 0x09,
|
|
|
|
DW_FORM_block1 = 0x0a,
|
|
|
|
DW_FORM_data1 = 0x0b,
|
|
|
|
DW_FORM_flag = 0x0c,
|
|
|
|
DW_FORM_sdata = 0x0d,
|
|
|
|
DW_FORM_strp = 0x0e,
|
|
|
|
DW_FORM_udata = 0x0f,
|
|
|
|
DW_FORM_ref_addr = 0x10,
|
|
|
|
DW_FORM_ref1 = 0x11,
|
|
|
|
DW_FORM_ref2 = 0x12,
|
|
|
|
DW_FORM_ref4 = 0x13,
|
|
|
|
DW_FORM_ref8 = 0x14,
|
|
|
|
DW_FORM_ref_udata = 0x15,
|
|
|
|
DW_FORM_indirect = 0x16,
|
|
|
|
DW_FORM_sec_offset = 0x17,
|
|
|
|
DW_FORM_exprloc = 0x18,
|
|
|
|
DW_FORM_flag_present = 0x19,
|
|
|
|
DW_FORM_strx = 0x1a,
|
|
|
|
DW_FORM_addrx = 0x1b,
|
|
|
|
DW_FORM_ref_sup4 = 0x1c,
|
|
|
|
DW_FORM_strp_sup = 0x1d,
|
|
|
|
DW_FORM_data16 = 0x1e,
|
|
|
|
DW_FORM_line_strp = 0x1f,
|
|
|
|
DW_FORM_ref_sig8 = 0x20,
|
|
|
|
DW_FORM_implicit_const = 0x21,
|
|
|
|
DW_FORM_loclistx = 0x22,
|
|
|
|
DW_FORM_rnglistx = 0x23,
|
|
|
|
DW_FORM_ref_sup8 = 0x24,
|
|
|
|
DW_FORM_strx1 = 0x25,
|
|
|
|
DW_FORM_strx2 = 0x26,
|
|
|
|
DW_FORM_strx3 = 0x27,
|
|
|
|
DW_FORM_strx4 = 0x28,
|
|
|
|
DW_FORM_addrx1 = 0x29,
|
|
|
|
DW_FORM_addrx2 = 0x2a,
|
|
|
|
DW_FORM_addrx3 = 0x2b,
|
2023-04-18 08:47:54 +03:00
|
|
|
DW_FORM_addrx4 = 0x2c,
|
|
|
|
|
|
|
|
/* GNU extensions for referring to .gnu_debugaltlink dwz-compressed info */
|
|
|
|
DW_FORM_GNU_ref_alt = 0x1f20,
|
|
|
|
DW_FORM_GNU_strp_alt = 0x1f21
|
2018-10-14 19:59:15 +03:00
|
|
|
};
|
|
|
|
|
2021-03-15 09:22:34 +03:00
|
|
|
/* Range list entry encodings */
|
|
|
|
enum {
|
|
|
|
DW_RLE_end_of_list = 0x00,
|
|
|
|
DW_RLE_base_addressx = 0x01,
|
|
|
|
DW_RLE_startx_endx = 0x02,
|
|
|
|
DW_RLE_startx_length = 0x03,
|
|
|
|
DW_RLE_offset_pair = 0x04,
|
|
|
|
DW_RLE_base_address = 0x05,
|
|
|
|
DW_RLE_start_end = 0x06,
|
|
|
|
DW_RLE_start_length = 0x07
|
|
|
|
};
|
|
|
|
|
2018-10-14 19:59:15 +03:00
|
|
|
enum {
|
|
|
|
VAL_none = 0,
|
|
|
|
VAL_cstr = 1,
|
|
|
|
VAL_data = 2,
|
|
|
|
VAL_uint = 3,
|
2022-12-22 16:33:06 +03:00
|
|
|
VAL_int = 4,
|
|
|
|
VAL_addr = 5
|
2018-10-14 19:59:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
# define ABBREV_TABLE_SIZE 256
|
|
|
|
typedef struct {
|
|
|
|
obj_info_t *obj;
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *file;
|
2022-12-21 18:51:46 +03:00
|
|
|
uint8_t current_version;
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *current_cu;
|
2018-10-17 02:52:28 +03:00
|
|
|
uint64_t current_low_pc;
|
2022-12-22 16:39:04 +03:00
|
|
|
uint64_t current_str_offsets_base;
|
|
|
|
uint64_t current_addr_base;
|
|
|
|
uint64_t current_rnglists_base;
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *debug_line_cu_end;
|
2022-12-21 16:18:48 +03:00
|
|
|
uint8_t debug_line_format;
|
|
|
|
uint16_t debug_line_version;
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *debug_line_files;
|
|
|
|
const char *debug_line_directories;
|
|
|
|
const char *p;
|
|
|
|
const char *cu_end;
|
|
|
|
const char *pend;
|
|
|
|
const char *q0;
|
|
|
|
const char *q;
|
2019-01-10 18:42:44 +03:00
|
|
|
int format; // 4 or 8
|
2018-10-14 19:59:15 +03:00
|
|
|
uint8_t address_size;
|
|
|
|
int level;
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *abbrev_table[ABBREV_TABLE_SIZE];
|
2018-10-14 19:59:15 +03:00
|
|
|
} DebugInfoReader;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ptrdiff_t pos;
|
|
|
|
int tag;
|
|
|
|
int has_children;
|
|
|
|
} DIE;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
union {
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *ptr;
|
2018-10-14 19:59:15 +03:00
|
|
|
uint64_t uint64;
|
|
|
|
int64_t int64;
|
2022-12-22 16:33:06 +03:00
|
|
|
uint64_t addr_idx;
|
2018-10-14 19:59:15 +03:00
|
|
|
} as;
|
|
|
|
uint64_t off;
|
|
|
|
uint64_t at;
|
|
|
|
uint64_t form;
|
|
|
|
size_t size;
|
|
|
|
int type;
|
|
|
|
} DebugInfoValue;
|
|
|
|
|
2022-10-19 13:49:45 +03:00
|
|
|
#if defined(WORDS_BIGENDIAN)
|
|
|
|
#define MERGE_2INTS(a,b,sz) (((uint64_t)(a)<<sz)|(b))
|
|
|
|
#else
|
2018-10-14 19:59:15 +03:00
|
|
|
#define MERGE_2INTS(a,b,sz) (((uint64_t)(b)<<sz)|(a))
|
2022-10-19 13:49:45 +03:00
|
|
|
#endif
|
2018-10-14 19:59:15 +03:00
|
|
|
|
|
|
|
static uint16_t
|
|
|
|
get_uint16(const uint8_t *p)
|
|
|
|
{
|
|
|
|
return (uint16_t)MERGE_2INTS(p[0],p[1],8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
get_uint32(const uint8_t *p)
|
|
|
|
{
|
|
|
|
return (uint32_t)MERGE_2INTS(get_uint16(p),get_uint16(p+2),16);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
get_uint64(const uint8_t *p)
|
|
|
|
{
|
|
|
|
return MERGE_2INTS(get_uint32(p),get_uint32(p+4),32);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t
|
2021-08-14 04:08:19 +03:00
|
|
|
read_uint8(const char **ptr)
|
2018-10-15 02:40:27 +03:00
|
|
|
{
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *p = *ptr;
|
|
|
|
*ptr = (p + 1);
|
|
|
|
return (uint8_t)*p;
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t
|
2021-08-14 04:08:19 +03:00
|
|
|
read_uint16(const char **ptr)
|
2018-10-15 02:40:27 +03:00
|
|
|
{
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *p = *ptr;
|
|
|
|
*ptr = (p + 2);
|
|
|
|
return get_uint16((const uint8_t *)p);
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t
|
2021-08-14 04:08:19 +03:00
|
|
|
read_uint24(const char **ptr)
|
2018-10-15 02:40:27 +03:00
|
|
|
{
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *p = *ptr;
|
|
|
|
*ptr = (p + 3);
|
|
|
|
return ((uint8_t)*p << 16) | get_uint16((const uint8_t *)p+1);
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t
|
2021-08-14 04:08:19 +03:00
|
|
|
read_uint32(const char **ptr)
|
2018-10-15 02:40:27 +03:00
|
|
|
{
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *p = *ptr;
|
|
|
|
*ptr = (p + 4);
|
|
|
|
return get_uint32((const uint8_t *)p);
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
2021-08-14 04:08:19 +03:00
|
|
|
read_uint64(const char **ptr)
|
2018-10-15 02:40:27 +03:00
|
|
|
{
|
2018-10-14 19:59:15 +03:00
|
|
|
const unsigned char *p = (const unsigned char *)*ptr;
|
|
|
|
*ptr = (char *)(p + 8);
|
|
|
|
return get_uint64(p);
|
|
|
|
}
|
|
|
|
|
2018-10-17 02:52:28 +03:00
|
|
|
static uintptr_t
|
2021-08-14 04:08:19 +03:00
|
|
|
read_uintptr(const char **ptr)
|
2018-10-17 02:52:28 +03:00
|
|
|
{
|
|
|
|
const unsigned char *p = (const unsigned char *)*ptr;
|
|
|
|
*ptr = (char *)(p + SIZEOF_VOIDP);
|
|
|
|
#if SIZEOF_VOIDP == 8
|
|
|
|
return get_uint64(p);
|
|
|
|
#else
|
|
|
|
return get_uint32(p);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-10-15 02:40:27 +03:00
|
|
|
static uint64_t
|
|
|
|
read_uint(DebugInfoReader *reader)
|
|
|
|
{
|
2018-10-21 03:08:02 +03:00
|
|
|
if (reader->format == 4) {
|
|
|
|
return read_uint32(&reader->p);
|
2018-10-14 19:59:15 +03:00
|
|
|
} else { /* 64 bit */
|
2018-10-21 03:08:02 +03:00
|
|
|
return read_uint64(&reader->p);
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 02:40:27 +03:00
|
|
|
static uint64_t
|
2018-10-14 19:59:15 +03:00
|
|
|
read_uleb128(DebugInfoReader *reader)
|
|
|
|
{
|
|
|
|
return uleb128(&reader->p);
|
|
|
|
}
|
|
|
|
|
2018-10-15 02:40:27 +03:00
|
|
|
static int64_t
|
2018-10-14 19:59:15 +03:00
|
|
|
read_sleb128(DebugInfoReader *reader)
|
|
|
|
{
|
|
|
|
return sleb128(&reader->p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
debug_info_reader_init(DebugInfoReader *reader, obj_info_t *obj)
|
|
|
|
{
|
|
|
|
reader->file = obj->mapped;
|
|
|
|
reader->obj = obj;
|
|
|
|
reader->p = obj->debug_info.ptr;
|
|
|
|
reader->pend = obj->debug_info.ptr + obj->debug_info.size;
|
|
|
|
reader->debug_line_cu_end = obj->debug_line.ptr;
|
Show C backtrace appropriately at core dump for GCC 8 or later
When ruby is compiled by GCC 8 or later, some frames of C level
backtrace information lacks.
```
$ ./miniruby -e '1.times { Process.kill(:SEGV, $$) }'
...
-- C level backtrace information
-------------------------------------------
/home/mame/work/ruby-gcc-9/miniruby(rb_vm_bugreport+0x611) [0x558a5fdcbc21] ../ruby/vm_dump.c:758
[0x558a5fbc789a]
/home/mame/work/ruby-gcc-9/miniruby(sigsegv+0x4d) [0x558a5fd1eaed] ../ruby/signal.c:959
/lib/x86_64-linux-gnu/libpthread.so.0(__restore_rt+0x0) [0x7f687e6713c0]
/lib/x86_64-linux-gnu/libc.so.6(kill+0xb) [0x7f687e31355b] ../sysdeps/unix/syscall-template.S:78
/home/mame/work/ruby-gcc-9/miniruby(rb_f_kill+0x350) [0x558a5fd1fe60] ../ruby/signal.c:480
[0x558a5fda50d3]
[0x558a5fdb085c]
[0x558a5fdb0fe7]
[0x558a5fdbae1a]
[0x558a5fdaf484]
/home/mame/work/ruby-gcc-9/miniruby(rb_yield_1+0x29f) [0x558a5fdb2fbf] ../ruby/vm.c:1265
/home/mame/work/ruby-gcc-9/miniruby(int_dotimes+0x5c) [0x558a5fc72f2c] ../ruby/numeric.c:5198
[0x558a5fda50d3]
[0x558a5fdb085c]
[0x558a5fdb0fe7]
[0x558a5fdbaf21]
[0x558a5fdaf484]
/home/mame/work/ruby-gcc-9/miniruby(rb_ec_exec_node+0xed) [0x558a5fbcc4fd] ../ruby/eval.c:317
/home/mame/work/ruby-gcc-9/miniruby(ruby_run_node+0x4f) [0x558a5fbd110f] ../ruby/eval.c:375
/home/mame/work/ruby-gcc-9/miniruby(main+0x73) [0x558a5fb2c083] ../ruby/main.c:50
```
By this one-line change, it shows all locations.
```
$ ./miniruby -e '1.times { Process.kill(:SEGV, $$) }'
...
-- C level backtrace information -------------------------------------------
/home/mame/work/ruby-gcc-9/miniruby(rb_print_backtrace+0x11) [0x558247adec21] ../ruby/vm_dump.c:758
/home/mame/work/ruby-gcc-9/miniruby(rb_vm_bugreport) ../ruby/vm_dump.c:956
/home/mame/work/ruby-gcc-9/miniruby(rb_bug_for_fatal_signal+0x15a) [0x5582478da89a] ../ruby/error.c:773
/home/mame/work/ruby-gcc-9/miniruby(sigsegv+0x4d) [0x558247a31aed] ../ruby/signal.c:959
/lib/x86_64-linux-gnu/libpthread.so.0(__restore_rt+0x0) [0x7f82202f73c0]
/lib/x86_64-linux-gnu/libc.so.6(kill+0xb) [0x7f821ff9955b] ../sysdeps/unix/syscall-template.S:78
/home/mame/work/ruby-gcc-9/miniruby(rb_f_kill+0x350) [0x558247a32e60] ../ruby/signal.c:480
/home/mame/work/ruby-gcc-9/miniruby(vm_call_cfunc_with_frame+0x123) [0x558247ab80d3] ../ruby/vm_insnhelper.c:2821
/home/mame/work/ruby-gcc-9/miniruby(vm_call_method_each_type+0x7c) [0x558247ac385c] ../ruby/vm_insnhelper.c:3324
/home/mame/work/ruby-gcc-9/miniruby(vm_call_method+0xc7) [0x558247ac3fe7] ../ruby/vm_insnhelper.c:3428
/home/mame/work/ruby-gcc-9/miniruby(vm_sendish+0x14) [0x558247acde1a] ../ruby/vm_insnhelper.c:4412
/home/mame/work/ruby-gcc-9/miniruby(vm_exec_core) ../ruby/insns.def:789
/home/mame/work/ruby-gcc-9/miniruby(rb_vm_exec+0x1a4) [0x558247ac2484] ../ruby/vm.c:2165
/home/mame/work/ruby-gcc-9/miniruby(rb_yield_1+0x29f) [0x558247ac5fbf] ../ruby/vm.c:1265
/home/mame/work/ruby-gcc-9/miniruby(int_dotimes+0x5c) [0x558247985f2c] ../ruby/numeric.c:5198
/home/mame/work/ruby-gcc-9/miniruby(vm_call_cfunc_with_frame+0x123) [0x558247ab80d3] ../ruby/vm_insnhelper.c:2821
/home/mame/work/ruby-gcc-9/miniruby(vm_call_method_each_type+0x7c) [0x558247ac385c] ../ruby/vm_insnhelper.c:3324
/home/mame/work/ruby-gcc-9/miniruby(vm_call_method+0xc7) [0x558247ac3fe7] ../ruby/vm_insnhelper.c:3428
/home/mame/work/ruby-gcc-9/miniruby(vm_sendish+0x14) [0x558247acdf21] ../ruby/vm_insnhelper.c:4412
/home/mame/work/ruby-gcc-9/miniruby(vm_exec_core) ../ruby/insns.def:770
/home/mame/work/ruby-gcc-9/miniruby(rb_vm_exec+0x1a4) [0x558247ac2484] ../ruby/vm.c:2165
/home/mame/work/ruby-gcc-9/miniruby(rb_ec_exec_node+0xed) [0x5582478df4fd] ../ruby/eval.c:317
/home/mame/work/ruby-gcc-9/miniruby(ruby_run_node+0x4f) [0x5582478e410f] ../ruby/eval.c:375
/home/mame/work/ruby-gcc-9/miniruby(main+0x73) [0x55824783f083] ../ruby/main.c:50
```
Details:
In short, it is an uninitialized variable bug.
Until GCC 7, all function locations are represented by a pair of
DW_AT_low_pc and DW_AT_high_pc in DWARF information.
But since GCC 8, some functions are split to multiple chunks, which are
represented by DW_AT_ranges.
DW_AT_ranges are represented as offsets from a base address.
According to DWARF specification, it is the base address of the
compilation unit, but GCC seems to use zero as default.
The function "di_read_cu" in addr2line.c had a comment about the fact.
However, the base address wasn't initialized as zero.
2020-12-10 18:09:09 +03:00
|
|
|
reader->current_low_pc = 0;
|
2022-12-22 16:39:04 +03:00
|
|
|
reader->current_str_offsets_base = 0;
|
|
|
|
reader->current_addr_base = 0;
|
|
|
|
reader->current_rnglists_base = 0;
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:22:34 +03:00
|
|
|
static void
|
2021-08-14 04:08:19 +03:00
|
|
|
di_skip_die_attributes(const char **p)
|
2021-03-15 09:22:34 +03:00
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
uint64_t at = uleb128(p);
|
|
|
|
uint64_t form = uleb128(p);
|
|
|
|
if (!at && !form) break;
|
|
|
|
switch (form) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case DW_FORM_implicit_const:
|
|
|
|
sleb128(p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-14 19:59:15 +03:00
|
|
|
static void
|
|
|
|
di_read_debug_abbrev_cu(DebugInfoReader *reader)
|
|
|
|
{
|
|
|
|
uint64_t prev = 0;
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *p = reader->q0;
|
2018-10-14 19:59:15 +03:00
|
|
|
for (;;) {
|
|
|
|
uint64_t abbrev_number = uleb128(&p);
|
|
|
|
if (abbrev_number <= prev) break;
|
|
|
|
if (abbrev_number < ABBREV_TABLE_SIZE) {
|
|
|
|
reader->abbrev_table[abbrev_number] = p;
|
|
|
|
}
|
|
|
|
prev = abbrev_number;
|
|
|
|
uleb128(&p); /* tag */
|
|
|
|
p++; /* has_children */
|
2021-03-15 09:22:34 +03:00
|
|
|
di_skip_die_attributes(&p);
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 03:07:53 +03:00
|
|
|
static int
|
2023-07-31 21:04:42 +03:00
|
|
|
di_read_debug_line_cu(DebugInfoReader *reader, FILE *errout)
|
2018-10-14 19:59:15 +03:00
|
|
|
{
|
2018-10-21 03:07:53 +03:00
|
|
|
const char *p;
|
|
|
|
struct LineNumberProgramHeader header;
|
2018-10-14 19:59:15 +03:00
|
|
|
|
2018-10-21 03:07:53 +03:00
|
|
|
p = (const char *)reader->debug_line_cu_end;
|
2023-07-31 21:04:42 +03:00
|
|
|
if (parse_debug_line_header(reader->obj, &p, &header, errout))
|
2018-10-21 03:07:53 +03:00
|
|
|
return -1;
|
2018-10-14 19:59:15 +03:00
|
|
|
|
2018-10-21 03:07:53 +03:00
|
|
|
reader->debug_line_cu_end = (char *)header.cu_end;
|
2022-12-21 16:18:48 +03:00
|
|
|
reader->debug_line_format = header.format;
|
|
|
|
reader->debug_line_version = header.version;
|
2018-10-21 03:07:53 +03:00
|
|
|
reader->debug_line_directories = (char *)header.include_directories;
|
|
|
|
reader->debug_line_files = (char *)header.filenames;
|
2018-10-14 19:59:15 +03:00
|
|
|
|
2018-10-21 03:07:53 +03:00
|
|
|
return 0;
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
|
2022-12-22 16:33:06 +03:00
|
|
|
static void
|
|
|
|
set_addr_idx_value(DebugInfoValue *v, uint64_t n)
|
|
|
|
{
|
|
|
|
v->as.addr_idx = n;
|
|
|
|
v->type = VAL_addr;
|
|
|
|
}
|
|
|
|
|
2018-10-14 19:59:15 +03:00
|
|
|
static void
|
|
|
|
set_uint_value(DebugInfoValue *v, uint64_t n)
|
|
|
|
{
|
|
|
|
v->as.uint64 = n;
|
|
|
|
v->type = VAL_uint;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_int_value(DebugInfoValue *v, int64_t n)
|
|
|
|
{
|
|
|
|
v->as.int64 = n;
|
|
|
|
v->type = VAL_int;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-08-14 04:08:19 +03:00
|
|
|
set_cstr_value(DebugInfoValue *v, const char *s)
|
2018-10-14 19:59:15 +03:00
|
|
|
{
|
|
|
|
v->as.ptr = s;
|
|
|
|
v->off = 0;
|
|
|
|
v->type = VAL_cstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-08-14 04:08:19 +03:00
|
|
|
set_cstrp_value(DebugInfoValue *v, const char *s, uint64_t off)
|
2018-10-14 19:59:15 +03:00
|
|
|
{
|
|
|
|
v->as.ptr = s;
|
|
|
|
v->off = off;
|
|
|
|
v->type = VAL_cstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-08-14 04:08:19 +03:00
|
|
|
set_data_value(DebugInfoValue *v, const char *s)
|
2018-10-14 19:59:15 +03:00
|
|
|
{
|
|
|
|
v->as.ptr = s;
|
|
|
|
v->type = VAL_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
get_cstr_value(DebugInfoValue *v)
|
|
|
|
{
|
|
|
|
if (v->as.ptr) {
|
|
|
|
return v->as.ptr + v->off;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 16:34:34 +03:00
|
|
|
static const char *
|
|
|
|
resolve_strx(DebugInfoReader *reader, uint64_t idx)
|
|
|
|
{
|
|
|
|
const char *p = reader->obj->debug_str_offsets.ptr + reader->current_str_offsets_base;
|
|
|
|
uint64_t off;
|
|
|
|
if (reader->format == 4) {
|
|
|
|
off = ((uint32_t *)p)[idx];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
off = ((uint64_t *)p)[idx];
|
|
|
|
}
|
|
|
|
return reader->obj->debug_str.ptr + off;
|
|
|
|
}
|
|
|
|
|
2023-08-02 19:43:31 +03:00
|
|
|
static bool
|
|
|
|
debug_info_reader_read_addr_value_member(DebugInfoReader *reader, DebugInfoValue *v, int size)
|
2023-07-31 21:56:53 +03:00
|
|
|
{
|
2023-08-01 06:11:05 +03:00
|
|
|
if (size == 4) {
|
2023-07-31 21:56:53 +03:00
|
|
|
set_uint_value(v, read_uint32(&reader->p));
|
2023-08-01 06:11:05 +03:00
|
|
|
} else if (size == 8) {
|
2023-07-31 21:56:53 +03:00
|
|
|
set_uint_value(v, read_uint64(&reader->p));
|
|
|
|
} else {
|
2023-08-02 19:43:31 +03:00
|
|
|
return false;
|
2023-07-31 21:56:53 +03:00
|
|
|
}
|
2023-08-02 19:43:31 +03:00
|
|
|
return true;
|
2023-07-31 21:56:53 +03:00
|
|
|
}
|
|
|
|
|
2023-08-01 06:11:05 +03:00
|
|
|
#define debug_info_reader_read_addr_value(reader, v, mem) \
|
2023-08-02 19:43:31 +03:00
|
|
|
if (!debug_info_reader_read_addr_value_member((reader), (v), (reader)->mem)) { \
|
|
|
|
kprintf("unknown " #mem ":%d", (reader)->mem); \
|
|
|
|
return false; \
|
|
|
|
}
|
|
|
|
|
2023-08-01 06:11:05 +03:00
|
|
|
|
|
|
|
static bool
|
2023-07-31 21:04:42 +03:00
|
|
|
debug_info_reader_read_value(DebugInfoReader *reader, uint64_t form, DebugInfoValue *v, FILE *errout)
|
2018-10-14 19:59:15 +03:00
|
|
|
{
|
|
|
|
switch (form) {
|
|
|
|
case DW_FORM_addr:
|
2023-08-01 06:11:05 +03:00
|
|
|
debug_info_reader_read_addr_value(reader, v, address_size);
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
case DW_FORM_block2:
|
|
|
|
v->size = read_uint16(&reader->p);
|
|
|
|
set_data_value(v, reader->p);
|
|
|
|
reader->p += v->size;
|
|
|
|
break;
|
|
|
|
case DW_FORM_block4:
|
|
|
|
v->size = read_uint32(&reader->p);
|
|
|
|
set_data_value(v, reader->p);
|
|
|
|
reader->p += v->size;
|
|
|
|
break;
|
|
|
|
case DW_FORM_data2:
|
|
|
|
set_uint_value(v, read_uint16(&reader->p));
|
|
|
|
break;
|
|
|
|
case DW_FORM_data4:
|
|
|
|
set_uint_value(v, read_uint32(&reader->p));
|
|
|
|
break;
|
|
|
|
case DW_FORM_data8:
|
|
|
|
set_uint_value(v, read_uint64(&reader->p));
|
|
|
|
break;
|
|
|
|
case DW_FORM_string:
|
|
|
|
v->size = strlen(reader->p);
|
|
|
|
set_cstr_value(v, reader->p);
|
|
|
|
reader->p += v->size + 1;
|
|
|
|
break;
|
|
|
|
case DW_FORM_block:
|
|
|
|
v->size = uleb128(&reader->p);
|
|
|
|
set_data_value(v, reader->p);
|
|
|
|
reader->p += v->size;
|
|
|
|
break;
|
|
|
|
case DW_FORM_block1:
|
|
|
|
v->size = read_uint8(&reader->p);
|
|
|
|
set_data_value(v, reader->p);
|
|
|
|
reader->p += v->size;
|
|
|
|
break;
|
|
|
|
case DW_FORM_data1:
|
|
|
|
set_uint_value(v, read_uint8(&reader->p));
|
|
|
|
break;
|
|
|
|
case DW_FORM_flag:
|
|
|
|
set_uint_value(v, read_uint8(&reader->p));
|
|
|
|
break;
|
|
|
|
case DW_FORM_sdata:
|
|
|
|
set_int_value(v, read_sleb128(reader));
|
|
|
|
break;
|
|
|
|
case DW_FORM_strp:
|
|
|
|
set_cstrp_value(v, reader->obj->debug_str.ptr, read_uint(reader));
|
|
|
|
break;
|
|
|
|
case DW_FORM_udata:
|
|
|
|
set_uint_value(v, read_uleb128(reader));
|
|
|
|
break;
|
|
|
|
case DW_FORM_ref_addr:
|
2023-07-31 21:56:53 +03:00
|
|
|
if (reader->current_version <= 2) {
|
|
|
|
// DWARF Version 2 specifies that references have
|
|
|
|
// the same size as an address on the target system
|
2023-08-01 06:11:05 +03:00
|
|
|
debug_info_reader_read_addr_value(reader, v, address_size);
|
2018-10-14 19:59:15 +03:00
|
|
|
} else {
|
2023-08-01 06:11:05 +03:00
|
|
|
debug_info_reader_read_addr_value(reader, v, format);
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DW_FORM_ref1:
|
|
|
|
set_uint_value(v, read_uint8(&reader->p));
|
|
|
|
break;
|
|
|
|
case DW_FORM_ref2:
|
|
|
|
set_uint_value(v, read_uint16(&reader->p));
|
|
|
|
break;
|
|
|
|
case DW_FORM_ref4:
|
|
|
|
set_uint_value(v, read_uint32(&reader->p));
|
|
|
|
break;
|
|
|
|
case DW_FORM_ref8:
|
|
|
|
set_uint_value(v, read_uint64(&reader->p));
|
|
|
|
break;
|
|
|
|
case DW_FORM_ref_udata:
|
|
|
|
set_uint_value(v, uleb128(&reader->p));
|
|
|
|
break;
|
|
|
|
case DW_FORM_indirect:
|
2019-07-07 14:16:15 +03:00
|
|
|
/* TODO: read the referred value */
|
2018-10-14 19:59:15 +03:00
|
|
|
set_uint_value(v, uleb128(&reader->p));
|
|
|
|
break;
|
|
|
|
case DW_FORM_sec_offset:
|
|
|
|
set_uint_value(v, read_uint(reader)); /* offset */
|
|
|
|
/* addrptr: debug_addr */
|
|
|
|
/* lineptr: debug_line */
|
|
|
|
/* loclist: debug_loclists */
|
|
|
|
/* loclistptr: debug_loclists */
|
|
|
|
/* macptr: debug_macro */
|
|
|
|
/* rnglist: debug_rnglists */
|
|
|
|
/* rnglistptr: debug_rnglists */
|
|
|
|
/* stroffsetsptr: debug_str_offsets */
|
|
|
|
break;
|
|
|
|
case DW_FORM_exprloc:
|
2018-10-23 15:38:48 +03:00
|
|
|
v->size = (size_t)read_uleb128(reader);
|
2018-10-14 19:59:15 +03:00
|
|
|
set_data_value(v, reader->p);
|
|
|
|
reader->p += v->size;
|
|
|
|
break;
|
|
|
|
case DW_FORM_flag_present:
|
|
|
|
set_uint_value(v, 1);
|
|
|
|
break;
|
|
|
|
case DW_FORM_strx:
|
2022-12-22 16:34:34 +03:00
|
|
|
set_cstr_value(v, resolve_strx(reader, uleb128(&reader->p)));
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
case DW_FORM_addrx:
|
2022-12-22 16:33:06 +03:00
|
|
|
set_addr_idx_value(v, uleb128(&reader->p));
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
case DW_FORM_ref_sup4:
|
|
|
|
set_uint_value(v, read_uint32(&reader->p));
|
|
|
|
break;
|
|
|
|
case DW_FORM_strp_sup:
|
|
|
|
set_uint_value(v, read_uint(reader));
|
2018-10-19 05:01:46 +03:00
|
|
|
/* *p = reader->sup_file + reader->sup_str->sh_offset + ret; */
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
case DW_FORM_data16:
|
|
|
|
v->size = 16;
|
|
|
|
set_data_value(v, reader->p);
|
|
|
|
reader->p += v->size;
|
|
|
|
break;
|
|
|
|
case DW_FORM_line_strp:
|
2022-12-21 16:13:22 +03:00
|
|
|
set_cstrp_value(v, reader->obj->debug_line_str.ptr, read_uint(reader));
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
case DW_FORM_ref_sig8:
|
|
|
|
set_uint_value(v, read_uint64(&reader->p));
|
|
|
|
break;
|
|
|
|
case DW_FORM_implicit_const:
|
|
|
|
set_int_value(v, sleb128(&reader->q));
|
|
|
|
break;
|
|
|
|
case DW_FORM_loclistx:
|
|
|
|
set_uint_value(v, read_uleb128(reader));
|
|
|
|
break;
|
|
|
|
case DW_FORM_rnglistx:
|
|
|
|
set_uint_value(v, read_uleb128(reader));
|
|
|
|
break;
|
|
|
|
case DW_FORM_ref_sup8:
|
|
|
|
set_uint_value(v, read_uint64(&reader->p));
|
|
|
|
break;
|
|
|
|
case DW_FORM_strx1:
|
2022-12-22 16:34:34 +03:00
|
|
|
set_cstr_value(v, resolve_strx(reader, read_uint8(&reader->p)));
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
case DW_FORM_strx2:
|
2022-12-22 16:34:34 +03:00
|
|
|
set_cstr_value(v, resolve_strx(reader, read_uint16(&reader->p)));
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
case DW_FORM_strx3:
|
2022-12-22 16:34:34 +03:00
|
|
|
set_cstr_value(v, resolve_strx(reader, read_uint24(&reader->p)));
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
case DW_FORM_strx4:
|
2022-12-22 16:34:34 +03:00
|
|
|
set_cstr_value(v, resolve_strx(reader, read_uint32(&reader->p)));
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
case DW_FORM_addrx1:
|
2022-12-22 16:33:06 +03:00
|
|
|
set_addr_idx_value(v, read_uint8(&reader->p));
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
case DW_FORM_addrx2:
|
2022-12-22 16:33:06 +03:00
|
|
|
set_addr_idx_value(v, read_uint16(&reader->p));
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
case DW_FORM_addrx3:
|
2022-12-22 16:33:06 +03:00
|
|
|
set_addr_idx_value(v, read_uint24(&reader->p));
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
case DW_FORM_addrx4:
|
2022-12-22 16:33:06 +03:00
|
|
|
set_addr_idx_value(v, read_uint32(&reader->p));
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
2023-04-18 08:47:54 +03:00
|
|
|
/* we have no support for actually reading the real values of these refs out
|
|
|
|
* of the .gnu_debugaltlink dwz-compressed debuginfo at the moment, but "read"
|
|
|
|
* them anyway so that we advance the reader by the right amount. */
|
|
|
|
case DW_FORM_GNU_ref_alt:
|
|
|
|
case DW_FORM_GNU_strp_alt:
|
|
|
|
read_uint(reader);
|
|
|
|
set_uint_value(v, 0);
|
|
|
|
break;
|
2018-10-14 19:59:15 +03:00
|
|
|
case 0:
|
|
|
|
goto fail;
|
|
|
|
break;
|
|
|
|
}
|
2023-08-01 06:11:05 +03:00
|
|
|
return true;
|
2018-10-14 19:59:15 +03:00
|
|
|
|
|
|
|
fail:
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("%d: unsupported form: %#"PRIx64"\n", __LINE__, form);
|
2023-08-01 06:11:05 +03:00
|
|
|
return false;
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* find abbrev in current compilation unit */
|
2021-08-14 04:08:19 +03:00
|
|
|
static const char *
|
2023-07-31 21:04:42 +03:00
|
|
|
di_find_abbrev(DebugInfoReader *reader, uint64_t abbrev_number, FILE *errout)
|
2018-10-15 02:40:27 +03:00
|
|
|
{
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *p;
|
2018-10-14 19:59:15 +03:00
|
|
|
if (abbrev_number < ABBREV_TABLE_SIZE) {
|
|
|
|
return reader->abbrev_table[abbrev_number];
|
|
|
|
}
|
|
|
|
p = reader->abbrev_table[ABBREV_TABLE_SIZE-1];
|
|
|
|
/* skip 255th record */
|
|
|
|
uleb128(&p); /* tag */
|
|
|
|
p++; /* has_children */
|
2021-03-15 09:22:34 +03:00
|
|
|
di_skip_die_attributes(&p);
|
2019-01-10 18:00:40 +03:00
|
|
|
for (uint64_t n = uleb128(&p); abbrev_number != n; n = uleb128(&p)) {
|
2018-10-14 19:59:15 +03:00
|
|
|
if (n == 0) {
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("%d: Abbrev Number %"PRId64" not found\n",__LINE__, abbrev_number);
|
2023-08-01 06:11:05 +03:00
|
|
|
return NULL;
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
uleb128(&p); /* tag */
|
|
|
|
p++; /* has_children */
|
2021-03-15 09:22:34 +03:00
|
|
|
di_skip_die_attributes(&p);
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2018-10-15 02:45:33 +03:00
|
|
|
static void
|
2023-07-31 21:04:42 +03:00
|
|
|
hexdump0(const unsigned char *p, size_t n, FILE *errout)
|
2018-10-15 02:45:33 +03:00
|
|
|
{
|
|
|
|
size_t i;
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F\n");
|
2018-10-15 02:45:33 +03:00
|
|
|
for (i=0; i < n; i++){
|
|
|
|
switch (i & 15) {
|
|
|
|
case 0:
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("%02" PRIdSIZE ": %02X ", i/16, p[i]);
|
2018-10-15 02:45:33 +03:00
|
|
|
break;
|
|
|
|
case 15:
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("%02X\n", p[i]);
|
2018-10-15 02:45:33 +03:00
|
|
|
break;
|
|
|
|
default:
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("%02X ", p[i]);
|
2018-10-15 02:45:33 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((i & 15) != 15) {
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("\n");
|
2018-10-15 02:45:33 +03:00
|
|
|
}
|
|
|
|
}
|
2023-07-31 21:04:42 +03:00
|
|
|
#define hexdump(p,n,e) hexdump0((const unsigned char *)p, n, e)
|
2018-10-15 02:45:33 +03:00
|
|
|
|
2018-10-14 19:59:15 +03:00
|
|
|
static void
|
2023-07-31 21:04:42 +03:00
|
|
|
div_inspect(DebugInfoValue *v, FILE *errout)
|
2018-10-15 02:40:27 +03:00
|
|
|
{
|
2018-10-14 19:59:15 +03:00
|
|
|
switch (v->type) {
|
|
|
|
case VAL_uint:
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("%d: type:%d size:%" PRIxSIZE " v:%"PRIx64"\n",__LINE__,v->type,v->size,v->as.uint64);
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
case VAL_int:
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("%d: type:%d size:%" PRIxSIZE " v:%"PRId64"\n",__LINE__,v->type,v->size,(int64_t)v->as.uint64);
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
case VAL_cstr:
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("%d: type:%d size:%" PRIxSIZE " v:'%s'\n",__LINE__,v->type,v->size,v->as.ptr);
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
case VAL_data:
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("%d: type:%d size:%" PRIxSIZE " v:\n",__LINE__,v->type,v->size);
|
2023-07-31 21:04:42 +03:00
|
|
|
hexdump(v->as.ptr, 16, errout);
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static DIE *
|
2023-07-31 21:04:42 +03:00
|
|
|
di_read_die(DebugInfoReader *reader, DIE *die, FILE *errout)
|
2018-10-15 02:40:27 +03:00
|
|
|
{
|
2018-10-14 19:59:15 +03:00
|
|
|
uint64_t abbrev_number = uleb128(&reader->p);
|
|
|
|
if (abbrev_number == 0) {
|
|
|
|
reader->level--;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-07-31 21:04:42 +03:00
|
|
|
if (!(reader->q = di_find_abbrev(reader, abbrev_number, errout))) return NULL;
|
2018-10-14 19:59:15 +03:00
|
|
|
|
|
|
|
die->pos = reader->p - reader->obj->debug_info.ptr - 1;
|
|
|
|
die->tag = (int)uleb128(&reader->q); /* tag */
|
|
|
|
die->has_children = *reader->q++; /* has_children */
|
|
|
|
if (die->has_children) {
|
|
|
|
reader->level++;
|
|
|
|
}
|
|
|
|
return die;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DebugInfoValue *
|
2023-07-31 21:04:42 +03:00
|
|
|
di_read_record(DebugInfoReader *reader, DebugInfoValue *vp, FILE *errout)
|
2018-10-15 02:40:27 +03:00
|
|
|
{
|
2018-10-14 19:59:15 +03:00
|
|
|
uint64_t at = uleb128(&reader->q);
|
|
|
|
uint64_t form = uleb128(&reader->q);
|
|
|
|
if (!at || !form) return NULL;
|
|
|
|
vp->at = at;
|
|
|
|
vp->form = form;
|
2023-07-31 21:04:42 +03:00
|
|
|
if (!debug_info_reader_read_value(reader, form, vp, errout)) return NULL;
|
2018-10-14 19:59:15 +03:00
|
|
|
return vp;
|
|
|
|
}
|
|
|
|
|
2023-08-01 06:11:05 +03:00
|
|
|
static bool
|
2023-07-31 21:04:42 +03:00
|
|
|
di_skip_records(DebugInfoReader *reader, FILE *errout)
|
2018-10-15 02:40:27 +03:00
|
|
|
{
|
2018-10-14 19:59:15 +03:00
|
|
|
for (;;) {
|
2023-08-25 08:07:09 +03:00
|
|
|
DebugInfoValue v = {{0}};
|
2018-10-14 19:59:15 +03:00
|
|
|
uint64_t at = uleb128(&reader->q);
|
|
|
|
uint64_t form = uleb128(&reader->q);
|
2023-08-01 06:11:05 +03:00
|
|
|
if (!at || !form) return true;
|
2023-07-31 21:04:42 +03:00
|
|
|
if (!debug_info_reader_read_value(reader, form, &v, errout)) return false;
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-22 16:39:04 +03:00
|
|
|
typedef struct addr_header {
|
|
|
|
const char *ptr;
|
|
|
|
uint64_t unit_length;
|
|
|
|
uint8_t format;
|
|
|
|
uint8_t address_size;
|
|
|
|
/* uint8_t segment_selector_size; */
|
|
|
|
} addr_header_t;
|
|
|
|
|
2023-08-01 06:11:05 +03:00
|
|
|
static bool
|
2023-07-31 21:04:42 +03:00
|
|
|
addr_header_init(obj_info_t *obj, addr_header_t *header, FILE *errout)
|
|
|
|
{
|
2022-12-22 16:39:04 +03:00
|
|
|
const char *p = obj->debug_addr.ptr;
|
|
|
|
|
|
|
|
header->ptr = p;
|
|
|
|
|
2023-09-08 06:22:55 +03:00
|
|
|
if (!p) return true;
|
2022-12-22 16:39:04 +03:00
|
|
|
|
|
|
|
header->unit_length = *(uint32_t *)p;
|
|
|
|
p += sizeof(uint32_t);
|
|
|
|
|
|
|
|
header->format = 4;
|
|
|
|
if (header->unit_length == 0xffffffff) {
|
|
|
|
header->unit_length = *(uint64_t *)p;
|
|
|
|
p += sizeof(uint64_t);
|
|
|
|
header->format = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
p += 2; /* version */
|
|
|
|
header->address_size = *p++;
|
2023-08-01 06:11:05 +03:00
|
|
|
if (header->address_size != 4 && header->address_size != 8) {
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("unknown address_size:%d", header->address_size);
|
2023-08-01 06:11:05 +03:00
|
|
|
return false;
|
|
|
|
}
|
2022-12-22 16:39:04 +03:00
|
|
|
p++; /* segment_selector_size */
|
2023-08-01 06:11:05 +03:00
|
|
|
return true;
|
2022-12-22 16:39:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
read_addr(addr_header_t *header, uint64_t addr_base, uint64_t idx) {
|
|
|
|
if (header->address_size == 4) {
|
|
|
|
return ((uint32_t*)(header->ptr + addr_base))[idx];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return ((uint64_t*)(header->ptr + addr_base))[idx];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct rnglists_header {
|
|
|
|
uint64_t unit_length;
|
|
|
|
uint8_t format;
|
|
|
|
uint8_t address_size;
|
|
|
|
uint32_t offset_entry_count;
|
|
|
|
} rnglists_header_t;
|
|
|
|
|
2023-08-01 06:11:05 +03:00
|
|
|
static bool
|
2023-07-31 21:04:42 +03:00
|
|
|
rnglists_header_init(obj_info_t *obj, rnglists_header_t *header, FILE *errout)
|
|
|
|
{
|
2022-12-22 16:39:04 +03:00
|
|
|
const char *p = obj->debug_rnglists.ptr;
|
|
|
|
|
2023-09-08 06:22:55 +03:00
|
|
|
if (!p) return true;
|
2022-12-22 16:39:04 +03:00
|
|
|
|
|
|
|
header->unit_length = *(uint32_t *)p;
|
|
|
|
p += sizeof(uint32_t);
|
|
|
|
|
|
|
|
header->format = 4;
|
|
|
|
if (header->unit_length == 0xffffffff) {
|
|
|
|
header->unit_length = *(uint64_t *)p;
|
|
|
|
p += sizeof(uint64_t);
|
|
|
|
header->format = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
p += 2; /* version */
|
|
|
|
header->address_size = *p++;
|
2023-08-01 06:11:05 +03:00
|
|
|
if (header->address_size != 4 && header->address_size != 8) {
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("unknown address_size:%d", header->address_size);
|
2023-08-01 06:11:05 +03:00
|
|
|
return false;
|
|
|
|
}
|
2022-12-22 16:39:04 +03:00
|
|
|
p++; /* segment_selector_size */
|
|
|
|
header->offset_entry_count = *(uint32_t *)p;
|
2023-08-01 06:11:05 +03:00
|
|
|
return true;
|
2022-12-22 16:39:04 +03:00
|
|
|
}
|
|
|
|
|
2018-10-14 19:59:15 +03:00
|
|
|
typedef struct {
|
|
|
|
uint64_t low_pc;
|
|
|
|
uint64_t high_pc;
|
|
|
|
uint64_t ranges;
|
|
|
|
bool low_pc_set;
|
|
|
|
bool high_pc_set;
|
|
|
|
bool ranges_set;
|
|
|
|
} ranges_t;
|
|
|
|
|
|
|
|
static void
|
2022-12-22 16:41:10 +03:00
|
|
|
ranges_set(ranges_t *ptr, DebugInfoValue *v, addr_header_t *addr_header, uint64_t addr_base)
|
2018-10-15 02:40:27 +03:00
|
|
|
{
|
2022-12-22 16:33:06 +03:00
|
|
|
uint64_t n = 0;
|
|
|
|
if (v->type == VAL_uint) {
|
|
|
|
n = v->as.uint64;
|
|
|
|
}
|
|
|
|
else if (v->type == VAL_addr) {
|
2022-12-22 16:41:10 +03:00
|
|
|
n = read_addr(addr_header, addr_base, v->as.addr_idx);
|
2022-12-22 16:33:06 +03:00
|
|
|
}
|
2018-10-17 02:52:26 +03:00
|
|
|
switch (v->at) {
|
|
|
|
case DW_AT_low_pc:
|
2022-12-22 16:33:06 +03:00
|
|
|
ptr->low_pc = n;
|
2018-10-17 02:52:26 +03:00
|
|
|
ptr->low_pc_set = true;
|
|
|
|
break;
|
|
|
|
case DW_AT_high_pc:
|
|
|
|
if (v->form == DW_FORM_addr) {
|
2022-12-22 16:33:06 +03:00
|
|
|
ptr->high_pc = n;
|
2018-10-17 02:52:26 +03:00
|
|
|
}
|
|
|
|
else {
|
2022-12-22 16:33:06 +03:00
|
|
|
ptr->high_pc = ptr->low_pc + n;
|
2018-10-17 02:52:26 +03:00
|
|
|
}
|
|
|
|
ptr->high_pc_set = true;
|
|
|
|
break;
|
|
|
|
case DW_AT_ranges:
|
2022-12-22 16:33:06 +03:00
|
|
|
ptr->ranges = n;
|
2018-10-17 02:52:26 +03:00
|
|
|
ptr->ranges_set = true;
|
|
|
|
break;
|
|
|
|
}
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:22:34 +03:00
|
|
|
static uint64_t
|
2023-07-31 21:04:42 +03:00
|
|
|
read_dw_form_addr(DebugInfoReader *reader, const char **ptr, FILE *errout)
|
2021-03-15 09:22:34 +03:00
|
|
|
{
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *p = *ptr;
|
2021-08-19 00:01:24 +03:00
|
|
|
*ptr = p + reader->address_size;
|
|
|
|
if (reader->address_size == 4) {
|
2021-03-15 09:22:34 +03:00
|
|
|
return read_uint32(&p);
|
|
|
|
} else {
|
2023-08-01 06:11:05 +03:00
|
|
|
return read_uint64(&p);
|
2021-03-15 09:22:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-14 19:59:15 +03:00
|
|
|
static uintptr_t
|
2023-07-31 21:04:42 +03:00
|
|
|
ranges_include(DebugInfoReader *reader, ranges_t *ptr, uint64_t addr, rnglists_header_t *rnglists_header, FILE *errout)
|
2018-10-15 02:40:27 +03:00
|
|
|
{
|
2018-10-14 19:59:15 +03:00
|
|
|
if (ptr->high_pc_set) {
|
|
|
|
if (ptr->ranges_set || !ptr->low_pc_set) {
|
2023-08-01 06:11:05 +03:00
|
|
|
return UINTPTR_MAX;
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
2018-10-17 02:52:26 +03:00
|
|
|
if (ptr->low_pc <= addr && addr <= ptr->high_pc) {
|
2018-10-23 15:38:48 +03:00
|
|
|
return (uintptr_t)ptr->low_pc;
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ptr->ranges_set) {
|
2018-10-17 02:52:28 +03:00
|
|
|
/* TODO: support base address selection entry */
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *p;
|
2018-10-17 02:52:28 +03:00
|
|
|
uint64_t base = ptr->low_pc_set ? ptr->low_pc : reader->current_low_pc;
|
2021-04-30 17:29:57 +03:00
|
|
|
bool base_valid = true;
|
2022-12-21 18:51:46 +03:00
|
|
|
if (reader->current_version >= 5) {
|
2022-12-22 16:41:10 +03:00
|
|
|
if (rnglists_header->offset_entry_count == 0) {
|
|
|
|
// DW_FORM_sec_offset
|
|
|
|
p = reader->obj->debug_rnglists.ptr + ptr->ranges + reader->current_rnglists_base;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// DW_FORM_rnglistx
|
|
|
|
const char *offset_array = reader->obj->debug_rnglists.ptr + reader->current_rnglists_base;
|
|
|
|
if (rnglists_header->format == 4) {
|
|
|
|
p = offset_array + ((uint32_t *)offset_array)[ptr->ranges];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p = offset_array + ((uint64_t *)offset_array)[ptr->ranges];
|
|
|
|
}
|
|
|
|
}
|
2021-03-15 09:22:34 +03:00
|
|
|
for (;;) {
|
|
|
|
uint8_t rle = read_uint8(&p);
|
2021-04-30 17:29:57 +03:00
|
|
|
uintptr_t from = 0, to = 0;
|
2021-03-15 09:22:34 +03:00
|
|
|
if (rle == DW_RLE_end_of_list) break;
|
|
|
|
switch (rle) {
|
|
|
|
case DW_RLE_base_addressx:
|
|
|
|
uleb128(&p);
|
2021-04-30 17:29:57 +03:00
|
|
|
base_valid = false; /* not supported yet */
|
2021-03-15 09:22:34 +03:00
|
|
|
break;
|
|
|
|
case DW_RLE_startx_endx:
|
|
|
|
uleb128(&p);
|
|
|
|
uleb128(&p);
|
|
|
|
break;
|
|
|
|
case DW_RLE_startx_length:
|
|
|
|
uleb128(&p);
|
|
|
|
uleb128(&p);
|
|
|
|
break;
|
|
|
|
case DW_RLE_offset_pair:
|
2021-04-30 17:29:57 +03:00
|
|
|
if (!base_valid) break;
|
|
|
|
from = (uintptr_t)base + uleb128(&p);
|
|
|
|
to = (uintptr_t)base + uleb128(&p);
|
2021-03-15 09:22:34 +03:00
|
|
|
break;
|
|
|
|
case DW_RLE_base_address:
|
2023-07-31 21:04:42 +03:00
|
|
|
base = read_dw_form_addr(reader, &p, errout);
|
2021-04-30 17:29:57 +03:00
|
|
|
base_valid = true;
|
2021-03-15 09:22:34 +03:00
|
|
|
break;
|
|
|
|
case DW_RLE_start_end:
|
2023-07-31 21:04:42 +03:00
|
|
|
from = (uintptr_t)read_dw_form_addr(reader, &p, errout);
|
|
|
|
to = (uintptr_t)read_dw_form_addr(reader, &p, errout);
|
2021-03-15 09:22:34 +03:00
|
|
|
break;
|
|
|
|
case DW_RLE_start_length:
|
2023-07-31 21:04:42 +03:00
|
|
|
from = (uintptr_t)read_dw_form_addr(reader, &p, errout);
|
2021-04-30 17:29:57 +03:00
|
|
|
to = from + uleb128(&p);
|
2021-03-15 09:22:34 +03:00
|
|
|
break;
|
|
|
|
}
|
2021-04-30 17:29:57 +03:00
|
|
|
if (from <= addr && addr < to) {
|
|
|
|
return from;
|
|
|
|
}
|
2021-03-15 09:22:34 +03:00
|
|
|
}
|
2023-08-01 06:11:05 +03:00
|
|
|
return 0;
|
2021-03-15 09:22:34 +03:00
|
|
|
}
|
|
|
|
p = reader->obj->debug_ranges.ptr + ptr->ranges;
|
2018-10-14 19:59:15 +03:00
|
|
|
for (;;) {
|
2018-10-17 02:52:28 +03:00
|
|
|
uintptr_t from = read_uintptr(&p);
|
|
|
|
uintptr_t to = read_uintptr(&p);
|
2018-10-14 19:59:15 +03:00
|
|
|
if (!from && !to) break;
|
2018-10-17 02:57:47 +03:00
|
|
|
if (from == UINTPTR_MAX) {
|
|
|
|
/* base address selection entry */
|
|
|
|
base = to;
|
|
|
|
}
|
2018-10-20 09:35:25 +03:00
|
|
|
else if (base + from <= addr && addr < base + to) {
|
2021-04-30 17:22:37 +03:00
|
|
|
return (uintptr_t)base + from;
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ptr->low_pc_set) {
|
|
|
|
if (ptr->low_pc == addr) {
|
2018-10-23 15:38:48 +03:00
|
|
|
return (uintptr_t)ptr->low_pc;
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
}
|
2023-08-01 06:11:05 +03:00
|
|
|
return 0;
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static void
|
2023-07-31 21:04:42 +03:00
|
|
|
ranges_inspect(DebugInfoReader *reader, ranges_t *ptr, FILE *errout)
|
2018-10-15 02:40:27 +03:00
|
|
|
{
|
2018-10-14 19:59:15 +03:00
|
|
|
if (ptr->high_pc_set) {
|
|
|
|
if (ptr->ranges_set || !ptr->low_pc_set) {
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("low_pc_set:%d high_pc_set:%d ranges_set:%d\n",ptr->low_pc_set,ptr->high_pc_set,ptr->ranges_set);
|
2023-08-01 06:11:05 +03:00
|
|
|
return;
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("low_pc:%"PRIx64" high_pc:%"PRIx64"\n",ptr->low_pc,ptr->high_pc);
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
else if (ptr->ranges_set) {
|
2018-10-20 09:35:25 +03:00
|
|
|
char *p = reader->obj->debug_ranges.ptr + ptr->ranges;
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("low_pc:%"PRIx64" ranges:%"PRIx64" %lx ",ptr->low_pc,ptr->ranges, p-reader->obj->mapped);
|
2018-10-14 19:59:15 +03:00
|
|
|
for (;;) {
|
2018-10-17 02:52:28 +03:00
|
|
|
uintptr_t from = read_uintptr(&p);
|
|
|
|
uintptr_t to = read_uintptr(&p);
|
2018-10-14 19:59:15 +03:00
|
|
|
if (!from && !to) break;
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("%"PRIx64"-%"PRIx64" ",ptr->low_pc+from,ptr->low_pc+to);
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("\n");
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
else if (ptr->low_pc_set) {
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("low_pc:%"PRIx64"\n",ptr->low_pc);
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
else {
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("empty\n");
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-21 03:07:53 +03:00
|
|
|
static int
|
2023-07-31 21:04:42 +03:00
|
|
|
di_read_cu(DebugInfoReader *reader, FILE *errout)
|
2018-10-17 02:52:28 +03:00
|
|
|
{
|
2018-10-21 03:08:02 +03:00
|
|
|
uint64_t unit_length;
|
|
|
|
uint16_t version;
|
|
|
|
uint64_t debug_abbrev_offset;
|
|
|
|
reader->format = 4;
|
2018-10-17 02:52:28 +03:00
|
|
|
reader->current_cu = reader->p;
|
2018-10-21 03:08:02 +03:00
|
|
|
unit_length = read_uint32(&reader->p);
|
|
|
|
if (unit_length == 0xffffffff) {
|
|
|
|
unit_length = read_uint64(&reader->p);
|
|
|
|
reader->format = 8;
|
|
|
|
}
|
|
|
|
reader->cu_end = reader->p + unit_length;
|
|
|
|
version = read_uint16(&reader->p);
|
2022-12-21 18:51:46 +03:00
|
|
|
reader->current_version = version;
|
2018-10-21 03:08:02 +03:00
|
|
|
if (version > 5) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (version == 5) {
|
|
|
|
/* unit_type = */ read_uint8(&reader->p);
|
|
|
|
reader->address_size = read_uint8(&reader->p);
|
|
|
|
debug_abbrev_offset = read_uint(reader);
|
2018-10-17 02:52:28 +03:00
|
|
|
}
|
2018-10-21 03:08:02 +03:00
|
|
|
else {
|
|
|
|
debug_abbrev_offset = read_uint(reader);
|
|
|
|
reader->address_size = read_uint8(&reader->p);
|
|
|
|
}
|
2023-08-01 06:11:05 +03:00
|
|
|
if (reader->address_size != 4 && reader->address_size != 8) {
|
2023-08-01 10:25:04 +03:00
|
|
|
kprintf("unknown address_size:%d", reader->address_size);
|
2023-08-01 06:11:05 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2018-10-21 03:08:02 +03:00
|
|
|
reader->q0 = reader->obj->debug_abbrev.ptr + debug_abbrev_offset;
|
|
|
|
|
2018-10-17 02:52:28 +03:00
|
|
|
reader->level = 0;
|
|
|
|
di_read_debug_abbrev_cu(reader);
|
2023-07-31 21:04:42 +03:00
|
|
|
if (di_read_debug_line_cu(reader, errout)) return -1;
|
2018-10-17 02:52:28 +03:00
|
|
|
|
|
|
|
do {
|
|
|
|
DIE die;
|
|
|
|
|
2023-07-31 21:04:42 +03:00
|
|
|
if (!di_read_die(reader, &die, errout)) continue;
|
2018-10-17 02:52:28 +03:00
|
|
|
|
|
|
|
if (die.tag != DW_TAG_compile_unit) {
|
2023-07-31 21:04:42 +03:00
|
|
|
if (!di_skip_records(reader, errout)) return -1;
|
2018-10-17 02:52:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-12-22 16:39:04 +03:00
|
|
|
reader->current_str_offsets_base = 0;
|
|
|
|
reader->current_addr_base = 0;
|
|
|
|
reader->current_rnglists_base = 0;
|
|
|
|
|
2023-08-25 08:07:09 +03:00
|
|
|
DebugInfoValue low_pc = {{0}};
|
2018-10-17 02:52:28 +03:00
|
|
|
/* enumerate abbrev */
|
|
|
|
for (;;) {
|
2023-08-25 08:07:09 +03:00
|
|
|
DebugInfoValue v = {{0}};
|
2023-07-31 21:04:42 +03:00
|
|
|
if (!di_read_record(reader, &v, errout)) break;
|
2018-10-17 02:52:28 +03:00
|
|
|
switch (v.at) {
|
|
|
|
case DW_AT_low_pc:
|
2022-12-22 16:39:04 +03:00
|
|
|
// clang may output DW_AT_addr_base after DW_AT_low_pc.
|
|
|
|
// We need to resolve the DW_FORM_addr* after DW_AT_addr_base is parsed.
|
|
|
|
low_pc = v;
|
|
|
|
break;
|
|
|
|
case DW_AT_str_offsets_base:
|
|
|
|
reader->current_str_offsets_base = v.as.uint64;
|
|
|
|
break;
|
|
|
|
case DW_AT_addr_base:
|
|
|
|
reader->current_addr_base = v.as.uint64;
|
|
|
|
break;
|
|
|
|
case DW_AT_rnglists_base:
|
|
|
|
reader->current_rnglists_base = v.as.uint64;
|
2018-10-17 02:52:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-12-22 16:39:04 +03:00
|
|
|
// Resolve the DW_FORM_addr of DW_AT_low_pc
|
|
|
|
switch (low_pc.type) {
|
|
|
|
case VAL_uint:
|
|
|
|
reader->current_low_pc = low_pc.as.uint64;
|
|
|
|
break;
|
|
|
|
case VAL_addr:
|
|
|
|
{
|
2023-08-25 08:07:09 +03:00
|
|
|
addr_header_t header = {0};
|
2023-07-31 21:04:42 +03:00
|
|
|
if (!addr_header_init(reader->obj, &header, errout)) return -1;
|
2022-12-22 16:39:04 +03:00
|
|
|
reader->current_low_pc = read_addr(&header, reader->current_addr_base, low_pc.as.addr_idx);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-10-17 02:52:28 +03:00
|
|
|
} while (0);
|
2023-01-16 22:25:39 +03:00
|
|
|
|
2018-10-21 03:07:53 +03:00
|
|
|
return 0;
|
2018-10-17 02:52:28 +03:00
|
|
|
}
|
|
|
|
|
2018-10-14 19:59:15 +03:00
|
|
|
static void
|
2023-07-31 21:04:42 +03:00
|
|
|
read_abstract_origin(DebugInfoReader *reader, uint64_t form, uint64_t abstract_origin, line_info_t *line, FILE *errout)
|
2018-10-15 02:40:27 +03:00
|
|
|
{
|
2021-08-14 04:08:19 +03:00
|
|
|
const char *p = reader->p;
|
|
|
|
const char *q = reader->q;
|
2018-10-14 19:59:15 +03:00
|
|
|
int level = reader->level;
|
|
|
|
DIE die;
|
|
|
|
|
2021-08-13 16:45:53 +03:00
|
|
|
switch (form) {
|
|
|
|
case DW_FORM_ref1:
|
|
|
|
case DW_FORM_ref2:
|
|
|
|
case DW_FORM_ref4:
|
|
|
|
case DW_FORM_ref8:
|
|
|
|
case DW_FORM_ref_udata:
|
|
|
|
reader->p = reader->current_cu + abstract_origin;
|
|
|
|
break;
|
|
|
|
case DW_FORM_ref_addr:
|
|
|
|
goto finish; /* not supported yet */
|
|
|
|
case DW_FORM_ref_sig8:
|
|
|
|
goto finish; /* not supported yet */
|
|
|
|
case DW_FORM_ref_sup4:
|
|
|
|
case DW_FORM_ref_sup8:
|
|
|
|
goto finish; /* not supported yet */
|
|
|
|
default:
|
|
|
|
goto finish;
|
|
|
|
}
|
2023-07-31 21:04:42 +03:00
|
|
|
if (!di_read_die(reader, &die, errout)) goto finish;
|
2018-10-14 19:59:15 +03:00
|
|
|
|
|
|
|
/* enumerate abbrev */
|
|
|
|
for (;;) {
|
2023-08-25 08:07:09 +03:00
|
|
|
DebugInfoValue v = {{0}};
|
2023-07-31 21:04:42 +03:00
|
|
|
if (!di_read_record(reader, &v, errout)) break;
|
2018-10-14 19:59:15 +03:00
|
|
|
switch (v.at) {
|
|
|
|
case DW_AT_name:
|
|
|
|
line->sname = get_cstr_value(&v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
finish:
|
|
|
|
reader->p = p;
|
|
|
|
reader->q = q;
|
|
|
|
reader->level = level;
|
|
|
|
}
|
|
|
|
|
2023-08-01 06:11:05 +03:00
|
|
|
static bool
|
2018-10-14 19:59:15 +03:00
|
|
|
debug_info_read(DebugInfoReader *reader, int num_traces, void **traces,
|
2023-07-31 21:04:42 +03:00
|
|
|
line_info_t *lines, int offset, FILE *errout)
|
|
|
|
{
|
2022-12-22 16:39:04 +03:00
|
|
|
|
2023-08-25 08:07:09 +03:00
|
|
|
addr_header_t addr_header = {0};
|
2023-07-31 21:04:42 +03:00
|
|
|
if (!addr_header_init(reader->obj, &addr_header, errout)) return false;
|
2022-12-22 16:39:04 +03:00
|
|
|
|
2023-08-25 08:07:09 +03:00
|
|
|
rnglists_header_t rnglists_header = {0};
|
2023-07-31 21:04:42 +03:00
|
|
|
if (!rnglists_header_init(reader->obj, &rnglists_header, errout)) return false;
|
2022-12-22 16:39:04 +03:00
|
|
|
|
2018-10-17 04:29:49 +03:00
|
|
|
while (reader->p < reader->cu_end) {
|
2018-10-14 19:59:15 +03:00
|
|
|
DIE die;
|
2023-08-25 08:07:09 +03:00
|
|
|
ranges_t ranges = {0};
|
|
|
|
line_info_t line = {0};
|
2018-10-14 19:59:15 +03:00
|
|
|
|
2023-07-31 21:04:42 +03:00
|
|
|
if (!di_read_die(reader, &die, errout)) continue;
|
2023-08-01 10:25:04 +03:00
|
|
|
/* kprintf("%d:%tx: <%d>\n",__LINE__,die.pos,reader->level,die.tag); */
|
2018-10-14 19:59:15 +03:00
|
|
|
|
|
|
|
if (die.tag != DW_TAG_subprogram && die.tag != DW_TAG_inlined_subroutine) {
|
|
|
|
skip_die:
|
2023-07-31 21:04:42 +03:00
|
|
|
if (!di_skip_records(reader, errout)) return false;
|
2018-10-14 19:59:15 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enumerate abbrev */
|
|
|
|
for (;;) {
|
2023-08-25 08:07:09 +03:00
|
|
|
DebugInfoValue v = {{0}};
|
2018-10-31 14:08:07 +03:00
|
|
|
/* ptrdiff_t pos = reader->p - reader->p0; */
|
2023-07-31 21:04:42 +03:00
|
|
|
if (!di_read_record(reader, &v, errout)) break;
|
2023-08-01 10:25:04 +03:00
|
|
|
/* kprintf("\n%d:%tx: AT:%lx FORM:%lx\n",__LINE__,pos,v.at,v.form); */
|
2023-07-31 21:04:42 +03:00
|
|
|
/* div_inspect(&v, errout); */
|
2018-10-14 19:59:15 +03:00
|
|
|
switch (v.at) {
|
|
|
|
case DW_AT_name:
|
|
|
|
line.sname = get_cstr_value(&v);
|
|
|
|
break;
|
|
|
|
case DW_AT_call_file:
|
2023-07-31 21:04:42 +03:00
|
|
|
fill_filename((int)v.as.uint64, reader->debug_line_format, reader->debug_line_version, reader->debug_line_directories, reader->debug_line_files, &line, reader->obj, errout);
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
case DW_AT_call_line:
|
|
|
|
line.line = (int)v.as.uint64;
|
|
|
|
break;
|
|
|
|
case DW_AT_low_pc:
|
|
|
|
case DW_AT_high_pc:
|
2018-10-17 02:52:26 +03:00
|
|
|
case DW_AT_ranges:
|
2022-12-22 16:41:10 +03:00
|
|
|
ranges_set(&ranges, &v, &addr_header, reader->current_addr_base);
|
2018-10-14 19:59:15 +03:00
|
|
|
break;
|
|
|
|
case DW_AT_declaration:
|
|
|
|
goto skip_die;
|
|
|
|
case DW_AT_inline:
|
|
|
|
/* 1 or 3 */
|
2018-10-31 14:08:07 +03:00
|
|
|
break; /* goto skip_die; */
|
2018-10-14 19:59:15 +03:00
|
|
|
case DW_AT_abstract_origin:
|
2023-07-31 21:04:42 +03:00
|
|
|
read_abstract_origin(reader, v.form, v.as.uint64, &line, errout);
|
2018-10-31 14:08:07 +03:00
|
|
|
break; /* goto skip_die; */
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
}
|
2023-07-31 21:04:42 +03:00
|
|
|
/* ranges_inspect(reader, &ranges, errout); */
|
2023-08-01 10:25:04 +03:00
|
|
|
/* kprintf("%d:%tx: %x ",__LINE__,diepos,die.tag); */
|
2019-01-10 18:00:40 +03:00
|
|
|
for (int i=offset; i < num_traces; i++) {
|
2018-10-14 19:59:15 +03:00
|
|
|
uintptr_t addr = (uintptr_t)traces[i];
|
2018-10-17 02:52:28 +03:00
|
|
|
uintptr_t offset = addr - reader->obj->base_addr + reader->obj->vmaddr;
|
2023-07-31 21:04:42 +03:00
|
|
|
uintptr_t saddr = ranges_include(reader, &ranges, offset, &rnglists_header, errout);
|
2023-08-01 06:11:05 +03:00
|
|
|
if (saddr == UINTPTR_MAX) return false;
|
2018-10-14 19:59:15 +03:00
|
|
|
if (saddr) {
|
2023-08-02 19:43:31 +03:00
|
|
|
/* kprintf("%d:%tx: %d %lx->%lx %x %s: %s/%s %d %s %s %s\n",__LINE__,die.pos, i,addr,offset, die.tag,line.sname,line.dirname,line.filename,line.line,reader->obj->path,line.sname,lines[i].sname); */
|
2018-10-14 19:59:15 +03:00
|
|
|
if (lines[i].sname) {
|
|
|
|
line_info_t *lp = malloc(sizeof(line_info_t));
|
|
|
|
memcpy(lp, &lines[i], sizeof(line_info_t));
|
|
|
|
lines[i].next = lp;
|
|
|
|
lp->dirname = line.dirname;
|
|
|
|
lp->filename = line.filename;
|
|
|
|
lp->line = line.line;
|
|
|
|
lp->saddr = 0;
|
|
|
|
}
|
|
|
|
lines[i].path = reader->obj->path;
|
|
|
|
lines[i].base_addr = line.base_addr;
|
|
|
|
lines[i].sname = line.sname;
|
2018-10-20 09:35:25 +03:00
|
|
|
lines[i].saddr = saddr + reader->obj->base_addr - reader->obj->vmaddr;
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
}
|
2018-10-17 04:29:49 +03:00
|
|
|
}
|
2023-08-01 06:11:05 +03:00
|
|
|
return true;
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
|
2022-12-21 16:18:48 +03:00
|
|
|
// This function parses the following attributes of Line Number Program Header in DWARF 5:
|
|
|
|
//
|
|
|
|
// * directory_entry_format_count
|
|
|
|
// * directory_entry_format
|
|
|
|
// * directories_count
|
|
|
|
// * directories
|
|
|
|
//
|
|
|
|
// or
|
|
|
|
//
|
|
|
|
// * file_name_entry_format_count
|
|
|
|
// * file_name_entry_format
|
|
|
|
// * file_names_count
|
|
|
|
// * file_names
|
|
|
|
//
|
|
|
|
// It records DW_LNCT_path and DW_LNCT_directory_index at the index "idx".
|
|
|
|
static const char *
|
2023-07-31 21:04:42 +03:00
|
|
|
parse_ver5_debug_line_header(const char *p, int idx, uint8_t format,
|
|
|
|
obj_info_t *obj, const char **out_path,
|
|
|
|
uint64_t *out_directory_index, FILE *errout)
|
|
|
|
{
|
2022-12-21 16:18:48 +03:00
|
|
|
int i, j;
|
|
|
|
int entry_format_count = *(uint8_t *)p++;
|
|
|
|
const char *entry_format = p;
|
|
|
|
|
|
|
|
/* skip the part of entry_format */
|
|
|
|
for (i = 0; i < entry_format_count * 2; i++) uleb128(&p);
|
|
|
|
|
|
|
|
int entry_count = (int)uleb128(&p);
|
|
|
|
|
2023-08-02 19:43:31 +03:00
|
|
|
DebugInfoReader reader = {0};
|
2022-12-21 16:18:48 +03:00
|
|
|
debug_info_reader_init(&reader, obj);
|
|
|
|
reader.format = format;
|
|
|
|
reader.p = p;
|
|
|
|
for (j = 0; j < entry_count; j++) {
|
|
|
|
const char *format = entry_format;
|
|
|
|
for (i = 0; i < entry_format_count; i++) {
|
2023-08-25 08:07:09 +03:00
|
|
|
DebugInfoValue v = {{0}};
|
2022-12-21 16:18:48 +03:00
|
|
|
unsigned long dw_lnct = uleb128(&format);
|
|
|
|
unsigned long dw_form = uleb128(&format);
|
2023-07-31 21:04:42 +03:00
|
|
|
if (!debug_info_reader_read_value(&reader, dw_form, &v, errout)) return 0;
|
2022-12-21 16:18:48 +03:00
|
|
|
if (dw_lnct == 1 /* DW_LNCT_path */ && v.type == VAL_cstr && out_path)
|
|
|
|
*out_path = v.as.ptr + v.off;
|
|
|
|
if (dw_lnct == 2 /* DW_LNCT_directory_index */ && v.type == VAL_uint && out_directory_index)
|
|
|
|
*out_directory_index = v.as.uint64;
|
|
|
|
}
|
2022-12-22 14:26:32 +03:00
|
|
|
if (j == idx) return 0;
|
2022-12-21 16:18:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return reader.p;
|
|
|
|
}
|
|
|
|
|
2018-10-20 09:35:25 +03:00
|
|
|
#ifdef USE_ELF
|
2018-10-14 19:59:15 +03:00
|
|
|
static unsigned long
|
|
|
|
uncompress_debug_section(ElfW(Shdr) *shdr, char *file, char **ptr)
|
2018-10-12 02:58:20 +03:00
|
|
|
{
|
2021-04-29 17:53:55 +03:00
|
|
|
*ptr = NULL;
|
2018-10-14 19:59:15 +03:00
|
|
|
#ifdef SUPPORT_COMPRESSED_DEBUG_LINE
|
|
|
|
ElfW(Chdr) *chdr = (ElfW(Chdr) *)(file + shdr->sh_offset);
|
2018-10-12 02:55:47 +03:00
|
|
|
unsigned long destsize = chdr->ch_size;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (chdr->ch_type != ELFCOMPRESS_ZLIB) {
|
|
|
|
/* unsupported compression type */
|
2018-10-14 19:59:15 +03:00
|
|
|
return 0;
|
2018-10-12 02:55:47 +03:00
|
|
|
}
|
|
|
|
|
2018-10-14 19:59:15 +03:00
|
|
|
*ptr = malloc(destsize);
|
|
|
|
if (!*ptr) return 0;
|
|
|
|
ret = uncompress((Bytef *)*ptr, &destsize,
|
|
|
|
(const Bytef*)chdr + sizeof(ElfW(Chdr)),
|
|
|
|
shdr->sh_size - sizeof(ElfW(Chdr)));
|
2018-10-12 02:55:47 +03:00
|
|
|
if (ret != Z_OK) goto fail;
|
2018-10-14 19:59:15 +03:00
|
|
|
return destsize;
|
2018-10-12 02:55:47 +03:00
|
|
|
|
2018-10-12 02:58:20 +03:00
|
|
|
fail:
|
2018-10-14 19:59:15 +03:00
|
|
|
free(*ptr);
|
2021-04-29 17:53:55 +03:00
|
|
|
*ptr = NULL;
|
2018-10-12 02:58:20 +03:00
|
|
|
#endif
|
2018-10-14 19:59:15 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-01 03:32:19 +03:00
|
|
|
/* read file and fill lines */
|
2014-04-14 14:08:04 +04:00
|
|
|
static uintptr_t
|
2014-03-26 08:30:43 +04:00
|
|
|
fill_lines(int num_traces, void **traces, int check_debuglink,
|
2023-07-31 21:04:42 +03:00
|
|
|
obj_info_t **objp, line_info_t *lines, int offset, FILE *errout)
|
2010-11-26 06:28:50 +03:00
|
|
|
{
|
2014-03-14 10:29:39 +04:00
|
|
|
int i, j;
|
2010-11-26 06:28:50 +03:00
|
|
|
char *shstr;
|
|
|
|
ElfW(Ehdr) *ehdr;
|
2010-12-01 03:32:19 +03:00
|
|
|
ElfW(Shdr) *shdr, *shstr_shdr;
|
2018-10-14 19:59:15 +03:00
|
|
|
ElfW(Shdr) *gnu_debuglink_shdr = NULL;
|
2020-10-03 18:07:37 +03:00
|
|
|
ElfW(Shdr) *note_gnu_build_id = NULL;
|
2010-12-01 03:32:19 +03:00
|
|
|
int fd;
|
2018-10-14 19:59:15 +03:00
|
|
|
off_t filesize;
|
|
|
|
char *file;
|
2014-04-01 02:39:07 +04:00
|
|
|
ElfW(Shdr) *symtab_shdr = NULL, *strtab_shdr = NULL;
|
|
|
|
ElfW(Shdr) *dynsym_shdr = NULL, *dynstr_shdr = NULL;
|
2014-04-01 07:38:05 +04:00
|
|
|
obj_info_t *obj = *objp;
|
2014-04-14 14:08:04 +04:00
|
|
|
uintptr_t dladdr_fbase = 0;
|
2010-12-01 03:32:19 +03:00
|
|
|
|
|
|
|
fd = open(binary_filename, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
2014-04-14 14:08:04 +04:00
|
|
|
goto fail;
|
2010-12-01 03:32:19 +03:00
|
|
|
}
|
|
|
|
filesize = lseek(fd, 0, SEEK_END);
|
2011-03-02 19:14:32 +03:00
|
|
|
if (filesize < 0) {
|
|
|
|
int e = errno;
|
|
|
|
close(fd);
|
2013-03-23 20:05:51 +04:00
|
|
|
kprintf("lseek: %s\n", strerror(e));
|
2014-04-14 14:08:04 +04:00
|
|
|
goto fail;
|
2011-03-02 19:14:32 +03:00
|
|
|
}
|
2012-08-21 17:32:13 +04:00
|
|
|
#if SIZEOF_OFF_T > SIZEOF_SIZE_T
|
|
|
|
if (filesize > (off_t)SIZE_MAX) {
|
2012-08-21 12:32:01 +04:00
|
|
|
close(fd);
|
2013-03-23 20:05:51 +04:00
|
|
|
kprintf("Too large file %s\n", binary_filename);
|
2014-04-14 14:08:04 +04:00
|
|
|
goto fail;
|
2012-08-21 12:32:01 +04:00
|
|
|
}
|
2012-08-21 17:32:13 +04:00
|
|
|
#endif
|
2010-12-01 03:32:19 +03:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
/* async-signal unsafe */
|
2012-08-21 12:32:01 +04:00
|
|
|
file = (char *)mmap(NULL, (size_t)filesize, PROT_READ, MAP_SHARED, fd, 0);
|
2010-12-01 03:32:19 +03:00
|
|
|
if (file == MAP_FAILED) {
|
|
|
|
int e = errno;
|
|
|
|
close(fd);
|
2013-03-23 20:05:51 +04:00
|
|
|
kprintf("mmap: %s\n", strerror(e));
|
2014-04-14 14:08:04 +04:00
|
|
|
goto fail;
|
2010-12-01 03:32:19 +03:00
|
|
|
}
|
2018-07-09 02:43:14 +03:00
|
|
|
close(fd);
|
2010-12-01 03:32:19 +03:00
|
|
|
|
2012-12-21 21:21:12 +04:00
|
|
|
ehdr = (ElfW(Ehdr) *)file;
|
|
|
|
if (memcmp(ehdr->e_ident, "\177ELF", 4) != 0) {
|
|
|
|
/*
|
|
|
|
* Huh? Maybe filename was overridden by setproctitle() and
|
|
|
|
* it match non-elf file.
|
|
|
|
*/
|
2014-04-14 14:08:04 +04:00
|
|
|
goto fail;
|
2012-12-21 21:21:12 +04:00
|
|
|
}
|
2014-04-01 07:38:05 +04:00
|
|
|
obj->mapped = file;
|
|
|
|
obj->mapped_size = (size_t)filesize;
|
2010-11-26 06:28:50 +03:00
|
|
|
|
|
|
|
shdr = (ElfW(Shdr) *)(file + ehdr->e_shoff);
|
|
|
|
|
|
|
|
shstr_shdr = shdr + ehdr->e_shstrndx;
|
|
|
|
shstr = file + shstr_shdr->sh_offset;
|
|
|
|
|
|
|
|
for (i = 0; i < ehdr->e_shnum; i++) {
|
2018-10-20 09:35:25 +03:00
|
|
|
char *section_name = shstr + shdr[i].sh_name;
|
2014-03-27 07:01:59 +04:00
|
|
|
switch (shdr[i].sh_type) {
|
|
|
|
case SHT_STRTAB:
|
|
|
|
if (!strcmp(section_name, ".strtab")) {
|
|
|
|
strtab_shdr = shdr + i;
|
|
|
|
}
|
2014-04-01 02:39:07 +04:00
|
|
|
else if (!strcmp(section_name, ".dynstr")) {
|
|
|
|
dynstr_shdr = shdr + i;
|
|
|
|
}
|
2014-03-27 07:01:59 +04:00
|
|
|
break;
|
|
|
|
case SHT_SYMTAB:
|
|
|
|
/* if (!strcmp(section_name, ".symtab")) */
|
2014-03-14 10:29:39 +04:00
|
|
|
symtab_shdr = shdr + i;
|
2014-03-27 07:01:59 +04:00
|
|
|
break;
|
2014-04-01 02:39:07 +04:00
|
|
|
case SHT_DYNSYM:
|
|
|
|
/* if (!strcmp(section_name, ".dynsym")) */
|
|
|
|
dynsym_shdr = shdr + i;
|
|
|
|
break;
|
2020-10-03 18:07:37 +03:00
|
|
|
case SHT_NOTE:
|
|
|
|
if (!strcmp(section_name, ".note.gnu.build-id")) {
|
|
|
|
note_gnu_build_id = shdr + i;
|
|
|
|
}
|
|
|
|
break;
|
2014-03-27 07:01:59 +04:00
|
|
|
case SHT_PROGBITS:
|
2018-10-14 19:59:15 +03:00
|
|
|
if (!strcmp(section_name, ".gnu_debuglink")) {
|
2014-03-27 07:01:59 +04:00
|
|
|
gnu_debuglink_shdr = shdr + i;
|
|
|
|
}
|
2018-10-14 19:59:15 +03:00
|
|
|
else {
|
|
|
|
const char *debug_section_names[] = {
|
|
|
|
".debug_abbrev",
|
|
|
|
".debug_info",
|
|
|
|
".debug_line",
|
|
|
|
".debug_ranges",
|
2022-12-22 16:29:39 +03:00
|
|
|
".debug_str_offsets",
|
|
|
|
".debug_addr",
|
2021-03-15 09:22:34 +03:00
|
|
|
".debug_rnglists",
|
2022-12-21 16:13:22 +03:00
|
|
|
".debug_str",
|
|
|
|
".debug_line_str"
|
2018-10-14 19:59:15 +03:00
|
|
|
};
|
2018-10-20 09:35:25 +03:00
|
|
|
|
2018-10-14 19:59:15 +03:00
|
|
|
for (j=0; j < DWARF_SECTION_COUNT; j++) {
|
2018-10-20 09:35:25 +03:00
|
|
|
struct dwarf_section *s = obj_dwarf_section_at(obj, j);
|
|
|
|
|
|
|
|
if (strcmp(section_name, debug_section_names[j]) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
s->ptr = file + shdr[i].sh_offset;
|
|
|
|
s->size = shdr[i].sh_size;
|
|
|
|
s->flags = shdr[i].sh_flags;
|
|
|
|
if (s->flags & SHF_COMPRESSED) {
|
|
|
|
s->size = uncompress_debug_section(&shdr[i], file, &s->ptr);
|
|
|
|
if (!s->size) goto fail;
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
2018-10-20 09:35:25 +03:00
|
|
|
break;
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
}
|
2014-03-27 07:01:59 +04:00
|
|
|
break;
|
2014-03-14 10:29:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 07:38:05 +04:00
|
|
|
if (offset == -1) {
|
2014-04-11 08:48:26 +04:00
|
|
|
/* main executable */
|
2014-04-01 07:38:05 +04:00
|
|
|
offset = 0;
|
2014-04-14 14:08:04 +04:00
|
|
|
if (dynsym_shdr && dynstr_shdr) {
|
2014-04-01 07:38:05 +04:00
|
|
|
char *strtab = file + dynstr_shdr->sh_offset;
|
|
|
|
ElfW(Sym) *symtab = (ElfW(Sym) *)(file + dynsym_shdr->sh_offset);
|
|
|
|
int symtab_count = (int)(dynsym_shdr->sh_size / sizeof(ElfW(Sym)));
|
2018-10-14 19:59:15 +03:00
|
|
|
void *handle = dlopen(NULL, RTLD_NOW|RTLD_LOCAL);
|
|
|
|
if (handle) {
|
|
|
|
for (j = 0; j < symtab_count; j++) {
|
|
|
|
ElfW(Sym) *sym = &symtab[j];
|
|
|
|
Dl_info info;
|
|
|
|
void *s;
|
|
|
|
if (ELF_ST_TYPE(sym->st_info) != STT_FUNC || sym->st_size == 0) continue;
|
|
|
|
s = dlsym(handle, strtab + sym->st_name);
|
|
|
|
if (s && dladdr(s, &info)) {
|
2018-10-20 09:35:25 +03:00
|
|
|
obj->base_addr = dladdr_fbase;
|
2018-10-14 19:59:15 +03:00
|
|
|
dladdr_fbase = (uintptr_t)info.dli_fbase;
|
|
|
|
break;
|
|
|
|
}
|
2018-10-12 02:58:21 +03:00
|
|
|
}
|
2018-10-14 19:59:15 +03:00
|
|
|
dlclose(handle);
|
2018-10-12 02:58:21 +03:00
|
|
|
}
|
2014-04-14 14:08:04 +04:00
|
|
|
if (ehdr->e_type == ET_EXEC) {
|
|
|
|
obj->base_addr = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* PIE (position-independent executable) */
|
|
|
|
obj->base_addr = dladdr_fbase;
|
|
|
|
}
|
|
|
|
}
|
2014-04-01 07:38:05 +04:00
|
|
|
}
|
|
|
|
|
2018-10-14 19:59:15 +03:00
|
|
|
if (obj->debug_info.ptr && obj->debug_abbrev.ptr) {
|
|
|
|
DebugInfoReader reader;
|
|
|
|
debug_info_reader_init(&reader, obj);
|
|
|
|
i = 0;
|
|
|
|
while (reader.p < reader.pend) {
|
2023-08-01 10:25:04 +03:00
|
|
|
/* kprintf("%d:%tx: CU[%d]\n", __LINE__, reader.p - reader.obj->debug_info.ptr, i++); */
|
2023-07-31 21:04:42 +03:00
|
|
|
if (di_read_cu(&reader, errout)) goto use_symtab;
|
|
|
|
if (!debug_info_read(&reader, num_traces, traces, lines, offset, errout))
|
2023-08-01 06:11:05 +03:00
|
|
|
goto use_symtab;
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* This file doesn't have dwarf, use symtab or dynsym */
|
2018-10-21 03:08:02 +03:00
|
|
|
use_symtab:
|
2018-10-14 19:59:15 +03:00
|
|
|
if (!symtab_shdr) {
|
|
|
|
/* This file doesn't have symtab, use dynsym instead */
|
|
|
|
symtab_shdr = dynsym_shdr;
|
|
|
|
strtab_shdr = dynstr_shdr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (symtab_shdr && strtab_shdr) {
|
|
|
|
char *strtab = file + strtab_shdr->sh_offset;
|
|
|
|
ElfW(Sym) *symtab = (ElfW(Sym) *)(file + symtab_shdr->sh_offset);
|
|
|
|
int symtab_count = (int)(symtab_shdr->sh_size / sizeof(ElfW(Sym)));
|
|
|
|
for (j = 0; j < symtab_count; j++) {
|
|
|
|
ElfW(Sym) *sym = &symtab[j];
|
|
|
|
uintptr_t saddr = (uintptr_t)sym->st_value + obj->base_addr;
|
|
|
|
if (ELF_ST_TYPE(sym->st_info) != STT_FUNC) continue;
|
|
|
|
for (i = offset; i < num_traces; i++) {
|
|
|
|
uintptr_t d = (uintptr_t)traces[i] - saddr;
|
|
|
|
if (lines[i].line > 0 || d > (uintptr_t)sym->st_size)
|
|
|
|
continue;
|
|
|
|
/* fill symbol name and addr from .symtab */
|
|
|
|
if (!lines[i].sname) lines[i].sname = strtab + sym->st_name;
|
|
|
|
lines[i].saddr = saddr;
|
|
|
|
lines[i].path = obj->path;
|
|
|
|
lines[i].base_addr = obj->base_addr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 09:45:51 +03:00
|
|
|
if (!obj->debug_line.ptr) {
|
2018-10-12 02:55:47 +03:00
|
|
|
/* This file doesn't have .debug_line section,
|
|
|
|
let's check .gnu_debuglink section instead. */
|
|
|
|
if (gnu_debuglink_shdr && check_debuglink) {
|
|
|
|
follow_debuglink(file + gnu_debuglink_shdr->sh_offset,
|
|
|
|
num_traces, traces,
|
2023-07-31 21:04:42 +03:00
|
|
|
objp, lines, offset, errout);
|
2018-10-12 02:55:47 +03:00
|
|
|
}
|
2020-10-03 18:07:37 +03:00
|
|
|
if (note_gnu_build_id && check_debuglink) {
|
|
|
|
ElfW(Nhdr) *nhdr = (ElfW(Nhdr)*) (file + note_gnu_build_id->sh_offset);
|
|
|
|
const char *build_id = (char *)(nhdr + 1) + nhdr->n_namesz;
|
|
|
|
follow_debuglink_build_id(build_id, nhdr->n_descsz,
|
|
|
|
num_traces, traces,
|
2023-07-31 21:04:42 +03:00
|
|
|
objp, lines, offset, errout);
|
2020-10-03 18:07:37 +03:00
|
|
|
}
|
2018-10-12 02:55:47 +03:00
|
|
|
goto finish;
|
|
|
|
}
|
2018-10-12 02:55:44 +03:00
|
|
|
|
2018-10-14 19:59:15 +03:00
|
|
|
if (parse_debug_line(num_traces, traces,
|
|
|
|
obj->debug_line.ptr,
|
|
|
|
obj->debug_line.size,
|
2023-07-31 21:04:42 +03:00
|
|
|
obj, lines, offset, errout) == -1)
|
2018-10-14 19:59:15 +03:00
|
|
|
goto fail;
|
|
|
|
|
2014-04-14 14:08:04 +04:00
|
|
|
finish:
|
|
|
|
return dladdr_fbase;
|
|
|
|
fail:
|
|
|
|
return (uintptr_t)-1;
|
2010-11-26 06:28:50 +03:00
|
|
|
}
|
2018-10-20 09:35:25 +03:00
|
|
|
#else /* Mach-O */
|
|
|
|
/* read file and fill lines */
|
|
|
|
static uintptr_t
|
|
|
|
fill_lines(int num_traces, void **traces, int check_debuglink,
|
2023-07-31 21:04:42 +03:00
|
|
|
obj_info_t **objp, line_info_t *lines, int offset, FILE *errout)
|
2018-10-20 09:35:25 +03:00
|
|
|
{
|
2018-11-09 14:19:23 +03:00
|
|
|
# ifdef __LP64__
|
|
|
|
# define LP(x) x##_64
|
|
|
|
# else
|
|
|
|
# define LP(x) x
|
|
|
|
# endif
|
2018-10-20 09:35:25 +03:00
|
|
|
int fd;
|
|
|
|
off_t filesize;
|
2018-11-21 01:40:20 +03:00
|
|
|
char *file, *p = NULL;
|
2018-10-20 09:35:25 +03:00
|
|
|
obj_info_t *obj = *objp;
|
2018-11-09 14:19:23 +03:00
|
|
|
struct LP(mach_header) *header;
|
2018-10-20 09:35:25 +03:00
|
|
|
uintptr_t dladdr_fbase = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
char *s = binary_filename;
|
|
|
|
char *base = strrchr(binary_filename, '/')+1;
|
|
|
|
size_t max = PATH_MAX;
|
|
|
|
size_t size = strlen(binary_filename);
|
|
|
|
size_t basesize = size - (base - binary_filename);
|
|
|
|
s += size;
|
|
|
|
max -= size;
|
2018-11-04 18:14:42 +03:00
|
|
|
p = s;
|
2018-10-20 09:35:25 +03:00
|
|
|
size = strlcpy(s, ".dSYM/Contents/Resources/DWARF/", max);
|
|
|
|
if (size == 0) goto fail;
|
|
|
|
s += size;
|
|
|
|
max -= size;
|
|
|
|
if (max <= basesize) goto fail;
|
|
|
|
memcpy(s, base, basesize);
|
|
|
|
s[basesize] = 0;
|
|
|
|
|
2018-11-04 18:14:42 +03:00
|
|
|
fd = open(binary_filename, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
*p = 0; /* binary_filename becomes original file name */
|
|
|
|
fd = open(binary_filename, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2018-10-20 09:35:25 +03:00
|
|
|
}
|
2018-11-04 18:14:42 +03:00
|
|
|
|
2018-10-20 09:35:25 +03:00
|
|
|
filesize = lseek(fd, 0, SEEK_END);
|
|
|
|
if (filesize < 0) {
|
|
|
|
int e = errno;
|
|
|
|
close(fd);
|
|
|
|
kprintf("lseek: %s\n", strerror(e));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
#if SIZEOF_OFF_T > SIZEOF_SIZE_T
|
|
|
|
if (filesize > (off_t)SIZE_MAX) {
|
|
|
|
close(fd);
|
|
|
|
kprintf("Too large file %s\n", binary_filename);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
/* async-signal unsafe */
|
|
|
|
file = (char *)mmap(NULL, (size_t)filesize, PROT_READ, MAP_SHARED, fd, 0);
|
|
|
|
if (file == MAP_FAILED) {
|
|
|
|
int e = errno;
|
|
|
|
close(fd);
|
|
|
|
kprintf("mmap: %s\n", strerror(e));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
obj->mapped = file;
|
|
|
|
obj->mapped_size = (size_t)filesize;
|
|
|
|
|
2018-11-09 14:19:23 +03:00
|
|
|
header = (struct LP(mach_header) *)file;
|
2018-11-21 01:40:20 +03:00
|
|
|
if (header->magic == LP(MH_MAGIC)) {
|
|
|
|
/* non universal binary */
|
|
|
|
p = file;
|
|
|
|
}
|
|
|
|
else if (header->magic == FAT_CIGAM) {
|
2019-01-31 15:58:33 +03:00
|
|
|
struct LP(mach_header) *mhp = _NSGetMachExecuteHeader();
|
2018-11-21 01:40:20 +03:00
|
|
|
struct fat_header *fat = (struct fat_header *)file;
|
|
|
|
char *q = file + sizeof(*fat);
|
|
|
|
uint32_t nfat_arch = __builtin_bswap32(fat->nfat_arch);
|
2023-08-01 10:25:04 +03:00
|
|
|
/* kprintf("%d: fat:%s %d\n",__LINE__, binary_filename,nfat_arch); */
|
2018-11-21 01:40:20 +03:00
|
|
|
for (uint32_t i = 0; i < nfat_arch; i++) {
|
|
|
|
struct fat_arch *arch = (struct fat_arch *)q;
|
|
|
|
cpu_type_t cputype = __builtin_bswap32(arch->cputype);
|
|
|
|
cpu_subtype_t cpusubtype = __builtin_bswap32(arch->cpusubtype);
|
|
|
|
uint32_t offset = __builtin_bswap32(arch->offset);
|
2023-08-01 10:25:04 +03:00
|
|
|
/* kprintf("%d: fat %d %x/%x %x/%x\n",__LINE__, i, mhp->cputype,mhp->cpusubtype, cputype,cpusubtype); */
|
2019-01-31 15:58:33 +03:00
|
|
|
if (mhp->cputype == cputype &&
|
2019-06-08 13:46:32 +03:00
|
|
|
(cpu_subtype_t)(mhp->cpusubtype & ~CPU_SUBTYPE_MASK) == cpusubtype) {
|
2018-11-21 01:40:20 +03:00
|
|
|
p = file + offset;
|
|
|
|
file = p;
|
|
|
|
header = (struct LP(mach_header) *)p;
|
|
|
|
if (header->magic == LP(MH_MAGIC)) {
|
|
|
|
goto found_mach_header;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
q += sizeof(*arch);
|
|
|
|
}
|
|
|
|
kprintf("'%s' is not a Mach-O universal binary file!\n",binary_filename);
|
|
|
|
close(fd);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
else {
|
2018-11-09 14:19:23 +03:00
|
|
|
# ifdef __LP64__
|
2024-02-08 05:15:41 +03:00
|
|
|
# define bitsize "64"
|
2018-11-09 14:19:23 +03:00
|
|
|
# else
|
2024-02-08 05:15:41 +03:00
|
|
|
# define bitsize "32"
|
2018-11-09 14:19:23 +03:00
|
|
|
# endif
|
2024-02-08 05:15:41 +03:00
|
|
|
kprintf("'%s' is not a " bitsize
|
2018-11-09 14:19:23 +03:00
|
|
|
"-bit Mach-O file!\n",binary_filename);
|
2024-02-08 05:15:41 +03:00
|
|
|
# undef bitsize
|
2018-10-20 09:35:25 +03:00
|
|
|
close(fd);
|
|
|
|
goto fail;
|
|
|
|
}
|
2018-11-21 01:40:20 +03:00
|
|
|
found_mach_header:
|
|
|
|
p += sizeof(*header);
|
2018-10-20 09:35:25 +03:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < (uint32_t)header->ncmds; i++) {
|
|
|
|
struct load_command *lcmd = (struct load_command *)p;
|
|
|
|
switch (lcmd->cmd) {
|
2018-11-09 14:19:23 +03:00
|
|
|
case LP(LC_SEGMENT):
|
2018-10-20 09:35:25 +03:00
|
|
|
{
|
|
|
|
static const char *debug_section_names[] = {
|
|
|
|
"__debug_abbrev",
|
|
|
|
"__debug_info",
|
|
|
|
"__debug_line",
|
|
|
|
"__debug_ranges",
|
2022-12-22 16:29:39 +03:00
|
|
|
"__debug_str_offsets",
|
|
|
|
"__debug_addr",
|
2021-03-15 09:22:34 +03:00
|
|
|
"__debug_rnglists",
|
2022-12-21 16:13:22 +03:00
|
|
|
"__debug_str",
|
|
|
|
"__debug_line_str",
|
2018-10-20 09:35:25 +03:00
|
|
|
};
|
2018-11-09 14:19:23 +03:00
|
|
|
struct LP(segment_command) *scmd = (struct LP(segment_command) *)lcmd;
|
2018-10-20 09:35:25 +03:00
|
|
|
if (strcmp(scmd->segname, "__TEXT") == 0) {
|
|
|
|
obj->vmaddr = scmd->vmaddr;
|
|
|
|
}
|
|
|
|
else if (strcmp(scmd->segname, "__DWARF") == 0) {
|
2018-11-09 14:19:23 +03:00
|
|
|
p += sizeof(struct LP(segment_command));
|
2018-10-20 09:35:25 +03:00
|
|
|
for (uint64_t i = 0; i < scmd->nsects; i++) {
|
2018-11-09 14:19:23 +03:00
|
|
|
struct LP(section) *sect = (struct LP(section) *)p;
|
|
|
|
p += sizeof(struct LP(section));
|
2018-10-20 09:35:25 +03:00
|
|
|
for (int j=0; j < DWARF_SECTION_COUNT; j++) {
|
|
|
|
struct dwarf_section *s = obj_dwarf_section_at(obj, j);
|
|
|
|
|
|
|
|
if (strcmp(sect->sectname, debug_section_names[j]) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
s->ptr = file + sect->offset;
|
|
|
|
s->size = sect->size;
|
|
|
|
s->flags = sect->flags;
|
|
|
|
if (s->flags & SHF_COMPRESSED) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LC_SYMTAB:
|
|
|
|
{
|
2018-11-04 18:14:42 +03:00
|
|
|
struct symtab_command *cmd = (struct symtab_command *)lcmd;
|
2018-11-09 14:19:23 +03:00
|
|
|
struct LP(nlist) *nl = (struct LP(nlist) *)(file + cmd->symoff);
|
2019-06-08 13:46:32 +03:00
|
|
|
char *strtab = file + cmd->stroff, *sname = 0;
|
2018-10-20 09:35:25 +03:00
|
|
|
uint32_t j;
|
2019-06-08 13:46:32 +03:00
|
|
|
uintptr_t saddr = 0;
|
2021-10-03 06:02:58 +03:00
|
|
|
/* kprintf("[%2d]: %x/symtab %p\n", i, cmd->cmd, (void *)p); */
|
2018-11-04 18:14:42 +03:00
|
|
|
for (j = 0; j < cmd->nsyms; j++) {
|
|
|
|
uintptr_t symsize, d;
|
2018-11-09 14:19:23 +03:00
|
|
|
struct LP(nlist) *e = &nl[j];
|
2018-11-21 01:40:20 +03:00
|
|
|
/* kprintf("[%2d][%4d]: %02x/%x/%x: %s %llx\n", i, j, e->n_type,e->n_sect,e->n_desc,strtab+e->n_un.n_strx,e->n_value); */
|
2018-11-04 18:14:42 +03:00
|
|
|
if (e->n_type != N_FUN) continue;
|
|
|
|
if (e->n_sect) {
|
|
|
|
saddr = (uintptr_t)e->n_value + obj->base_addr - obj->vmaddr;
|
|
|
|
sname = strtab + e->n_un.n_strx;
|
2018-11-21 01:40:20 +03:00
|
|
|
/* kprintf("[%2d][%4d]: %02x/%x/%x: %s %llx\n", i, j, e->n_type,e->n_sect,e->n_desc,strtab+e->n_un.n_strx,e->n_value); */
|
2018-11-04 18:14:42 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (int k = offset; k < num_traces; k++) {
|
|
|
|
d = (uintptr_t)traces[k] - saddr;
|
|
|
|
symsize = e->n_value;
|
|
|
|
/* kprintf("%lx %lx %lx\n",saddr,symsize,traces[k]); */
|
|
|
|
if (lines[k].line > 0 || d > (uintptr_t)symsize)
|
|
|
|
continue;
|
|
|
|
/* fill symbol name and addr from .symtab */
|
|
|
|
if (!lines[k].sname) lines[k].sname = sname;
|
|
|
|
lines[k].saddr = saddr;
|
|
|
|
lines[k].path = obj->path;
|
|
|
|
lines[k].base_addr = obj->base_addr;
|
|
|
|
}
|
2018-10-20 09:35:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p += lcmd->cmdsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj->debug_info.ptr && obj->debug_abbrev.ptr) {
|
|
|
|
DebugInfoReader reader;
|
|
|
|
debug_info_reader_init(&reader, obj);
|
|
|
|
while (reader.p < reader.pend) {
|
2023-07-31 21:04:42 +03:00
|
|
|
if (di_read_cu(&reader, errout)) goto fail;
|
|
|
|
if (!debug_info_read(&reader, num_traces, traces, lines, offset, errout))
|
2023-08-01 06:11:05 +03:00
|
|
|
goto fail;
|
2018-10-20 09:35:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parse_debug_line(num_traces, traces,
|
|
|
|
obj->debug_line.ptr,
|
|
|
|
obj->debug_line.size,
|
2023-07-31 21:04:42 +03:00
|
|
|
obj, lines, offset, errout) == -1)
|
2018-10-20 09:35:25 +03:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return dladdr_fbase;
|
|
|
|
fail:
|
|
|
|
return (uintptr_t)-1;
|
|
|
|
}
|
|
|
|
#endif
|
2010-11-26 06:28:50 +03:00
|
|
|
|
2014-04-01 07:38:05 +04:00
|
|
|
#define HAVE_MAIN_EXE_PATH
|
2021-03-22 21:58:33 +03:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
2014-04-14 14:08:02 +04:00
|
|
|
# include <sys/sysctl.h>
|
|
|
|
#endif
|
2023-09-25 20:07:32 +03:00
|
|
|
/* ssize_t main_exe_path(FILE *errout)
|
2014-04-14 14:08:02 +04:00
|
|
|
*
|
|
|
|
* store the path of the main executable to `binary_filename`,
|
|
|
|
* and returns strlen(binary_filename).
|
|
|
|
* it is NUL terminated.
|
|
|
|
*/
|
2021-02-02 16:05:19 +03:00
|
|
|
#if defined(__linux__) || defined(__NetBSD__)
|
2017-11-16 11:01:26 +03:00
|
|
|
static ssize_t
|
2023-09-25 20:07:32 +03:00
|
|
|
main_exe_path(FILE *errout)
|
2014-04-01 07:38:05 +04:00
|
|
|
{
|
2021-02-02 16:05:19 +03:00
|
|
|
# if defined(__linux__)
|
|
|
|
# define PROC_SELF_EXE "/proc/self/exe"
|
|
|
|
# elif defined(__NetBSD__)
|
|
|
|
# define PROC_SELF_EXE "/proc/curproc/exe"
|
|
|
|
# endif
|
2014-04-01 07:38:05 +04:00
|
|
|
ssize_t len = readlink(PROC_SELF_EXE, binary_filename, PATH_MAX);
|
2019-07-14 11:53:45 +03:00
|
|
|
if (len < 0) return 0;
|
2014-04-01 07:38:05 +04:00
|
|
|
binary_filename[len] = 0;
|
|
|
|
return len;
|
|
|
|
}
|
2021-03-22 21:58:33 +03:00
|
|
|
#elif defined(__FreeBSD__) || defined(__DragonFly__)
|
2017-11-16 11:01:26 +03:00
|
|
|
static ssize_t
|
2023-09-25 20:07:32 +03:00
|
|
|
main_exe_path(FILE *errout)
|
2014-04-14 14:08:02 +04:00
|
|
|
{
|
|
|
|
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
|
|
|
|
size_t len = PATH_MAX;
|
|
|
|
int err = sysctl(mib, 4, binary_filename, &len, NULL, 0);
|
|
|
|
if (err) {
|
|
|
|
kprintf("Can't get the path of ruby");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
len--; /* sysctl sets strlen+1 */
|
|
|
|
return len;
|
|
|
|
}
|
2019-08-19 17:36:47 +03:00
|
|
|
#elif defined(HAVE_LIBPROC_H)
|
|
|
|
static ssize_t
|
2023-09-25 20:07:32 +03:00
|
|
|
main_exe_path(FILE *errout)
|
2019-08-19 17:36:47 +03:00
|
|
|
{
|
|
|
|
int len = proc_pidpath(getpid(), binary_filename, PATH_MAX);
|
|
|
|
if (len == 0) return 0;
|
|
|
|
binary_filename[len] = 0;
|
|
|
|
return len;
|
|
|
|
}
|
2014-04-01 07:38:05 +04:00
|
|
|
#else
|
|
|
|
#undef HAVE_MAIN_EXE_PATH
|
|
|
|
#endif
|
|
|
|
|
2018-10-14 19:59:15 +03:00
|
|
|
static void
|
2023-07-31 21:04:42 +03:00
|
|
|
print_line0(line_info_t *line, void *address, FILE *errout)
|
2018-10-15 02:40:27 +03:00
|
|
|
{
|
2018-10-14 19:59:15 +03:00
|
|
|
uintptr_t addr = (uintptr_t)address;
|
|
|
|
uintptr_t d = addr - line->saddr;
|
|
|
|
if (!address) {
|
|
|
|
/* inlined */
|
|
|
|
if (line->dirname && line->dirname[0]) {
|
|
|
|
kprintf("%s(%s) %s/%s:%d\n", line->path, line->sname, line->dirname, line->filename, line->line);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
kprintf("%s(%s) %s:%d\n", line->path, line->sname, line->filename, line->line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!line->path) {
|
|
|
|
kprintf("[0x%"PRIxPTR"]\n", addr);
|
|
|
|
}
|
2022-05-13 16:32:18 +03:00
|
|
|
else if (!line->sname) {
|
2018-10-14 19:59:15 +03:00
|
|
|
kprintf("%s(0x%"PRIxPTR") [0x%"PRIxPTR"]\n", line->path, addr-line->base_addr, addr);
|
|
|
|
}
|
2022-05-13 16:32:18 +03:00
|
|
|
else if (!line->saddr) {
|
|
|
|
kprintf("%s(%s) [0x%"PRIxPTR"]\n", line->path, line->sname, addr);
|
|
|
|
}
|
2018-10-14 19:59:15 +03:00
|
|
|
else if (line->line <= 0) {
|
|
|
|
kprintf("%s(%s+0x%"PRIxPTR") [0x%"PRIxPTR"]\n", line->path, line->sname,
|
|
|
|
d, addr);
|
|
|
|
}
|
|
|
|
else if (!line->filename) {
|
|
|
|
kprintf("%s(%s+0x%"PRIxPTR") [0x%"PRIxPTR"] ???:%d\n", line->path, line->sname,
|
|
|
|
d, addr, line->line);
|
|
|
|
}
|
|
|
|
else if (line->dirname && line->dirname[0]) {
|
|
|
|
kprintf("%s(%s+0x%"PRIxPTR") [0x%"PRIxPTR"] %s/%s:%d\n", line->path, line->sname,
|
|
|
|
d, addr, line->dirname, line->filename, line->line);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
kprintf("%s(%s+0x%"PRIxPTR") [0x%"PRIxPTR"] %s:%d\n", line->path, line->sname,
|
|
|
|
d, addr, line->filename, line->line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-07-31 21:04:42 +03:00
|
|
|
print_line(line_info_t *line, void *address, FILE *errout)
|
2018-10-15 02:40:27 +03:00
|
|
|
{
|
2023-07-31 21:04:42 +03:00
|
|
|
print_line0(line, address, errout);
|
2018-10-14 19:59:15 +03:00
|
|
|
if (line->next) {
|
2023-07-31 21:04:42 +03:00
|
|
|
print_line(line->next, NULL, errout);
|
2018-10-14 19:59:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-26 06:28:50 +03:00
|
|
|
void
|
2023-07-31 21:04:42 +03:00
|
|
|
rb_dump_backtrace_with_lines(int num_traces, void **traces, FILE *errout)
|
2010-11-26 06:28:50 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
/* async-signal unsafe */
|
2014-03-14 10:29:39 +04:00
|
|
|
line_info_t *lines = (line_info_t *)calloc(num_traces, sizeof(line_info_t));
|
2014-04-01 07:38:05 +04:00
|
|
|
obj_info_t *obj = NULL;
|
2014-04-11 08:48:26 +04:00
|
|
|
/* 2 is NULL + main executable */
|
|
|
|
void **dladdr_fbases = (void **)calloc(num_traces+2, sizeof(void *));
|
2022-12-19 11:15:31 +03:00
|
|
|
|
2014-04-01 07:38:05 +04:00
|
|
|
#ifdef HAVE_MAIN_EXE_PATH
|
2014-04-11 08:48:26 +04:00
|
|
|
char *main_path = NULL; /* used on printing backtrace */
|
2014-04-01 07:38:05 +04:00
|
|
|
ssize_t len;
|
2023-09-25 20:07:32 +03:00
|
|
|
if ((len = main_exe_path(errout)) > 0) {
|
2014-03-24 10:28:32 +04:00
|
|
|
main_path = (char *)alloca(len + 1);
|
2014-04-01 07:38:05 +04:00
|
|
|
if (main_path) {
|
2014-04-14 14:08:04 +04:00
|
|
|
uintptr_t addr;
|
2014-04-01 07:38:05 +04:00
|
|
|
memcpy(main_path, binary_filename, len+1);
|
|
|
|
append_obj(&obj);
|
|
|
|
obj->path = main_path;
|
2023-07-31 21:04:42 +03:00
|
|
|
addr = fill_lines(num_traces, traces, 1, &obj, lines, -1, errout);
|
2014-04-14 14:08:04 +04:00
|
|
|
if (addr != (uintptr_t)-1) {
|
|
|
|
dladdr_fbases[0] = (void *)addr;
|
2014-03-14 10:29:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-11-26 06:28:50 +03:00
|
|
|
|
2014-03-14 10:29:39 +04:00
|
|
|
/* fill source lines by reading dwarf */
|
2010-11-26 06:28:50 +03:00
|
|
|
for (i = 0; i < num_traces; i++) {
|
2014-04-01 07:38:05 +04:00
|
|
|
Dl_info info;
|
2014-03-26 03:55:39 +04:00
|
|
|
if (lines[i].line) continue;
|
2014-04-01 07:38:05 +04:00
|
|
|
if (dladdr(traces[i], &info)) {
|
|
|
|
const char *path;
|
|
|
|
void **p;
|
2014-04-11 08:48:26 +04:00
|
|
|
|
|
|
|
/* skip symbols which is in already checked objects */
|
|
|
|
/* if the binary is strip-ed, this may effect */
|
|
|
|
for (p=dladdr_fbases; *p; p++) {
|
|
|
|
if (*p == info.dli_fbase) {
|
2021-04-30 17:03:02 +03:00
|
|
|
if (info.dli_fname) lines[i].path = info.dli_fname;
|
|
|
|
if (info.dli_sname) lines[i].sname = info.dli_sname;
|
2014-04-01 07:38:05 +04:00
|
|
|
goto next_line;
|
2014-04-11 08:48:26 +04:00
|
|
|
}
|
2014-04-01 07:38:05 +04:00
|
|
|
}
|
2014-04-11 08:48:26 +04:00
|
|
|
*p = info.dli_fbase;
|
|
|
|
|
2014-04-01 07:38:05 +04:00
|
|
|
append_obj(&obj);
|
|
|
|
obj->base_addr = (uintptr_t)info.dli_fbase;
|
|
|
|
path = info.dli_fname;
|
|
|
|
obj->path = path;
|
2021-04-30 17:03:02 +03:00
|
|
|
if (path) lines[i].path = path;
|
|
|
|
if (info.dli_sname) {
|
|
|
|
lines[i].sname = info.dli_sname;
|
|
|
|
lines[i].saddr = (uintptr_t)info.dli_saddr;
|
|
|
|
}
|
2016-12-26 12:28:46 +03:00
|
|
|
strlcpy(binary_filename, path, PATH_MAX);
|
2023-07-31 21:04:42 +03:00
|
|
|
if (fill_lines(num_traces, traces, 1, &obj, lines, i, errout) == (uintptr_t)-1)
|
2016-10-18 18:09:08 +03:00
|
|
|
break;
|
2010-11-26 06:28:50 +03:00
|
|
|
}
|
2014-04-01 07:38:05 +04:00
|
|
|
next_line:
|
|
|
|
continue;
|
2010-11-26 06:28:50 +03:00
|
|
|
}
|
|
|
|
|
2014-03-14 10:29:39 +04:00
|
|
|
/* output */
|
2010-11-26 06:28:50 +03:00
|
|
|
for (i = 0; i < num_traces; i++) {
|
2023-07-31 21:04:42 +03:00
|
|
|
print_line(&lines[i], traces[i], errout);
|
2018-10-14 19:59:15 +03:00
|
|
|
|
2014-03-26 08:30:43 +04:00
|
|
|
/* FreeBSD's backtrace may show _start and so on */
|
2018-10-14 19:59:15 +03:00
|
|
|
if (lines[i].sname && strcmp("main", lines[i].sname) == 0)
|
2014-03-26 08:30:43 +04:00
|
|
|
break;
|
2010-11-26 06:28:50 +03:00
|
|
|
}
|
|
|
|
|
2014-03-14 10:29:39 +04:00
|
|
|
/* free */
|
2014-04-01 07:38:05 +04:00
|
|
|
while (obj) {
|
|
|
|
obj_info_t *o = obj;
|
2018-10-14 19:59:15 +03:00
|
|
|
for (i=0; i < DWARF_SECTION_COUNT; i++) {
|
|
|
|
struct dwarf_section *s = obj_dwarf_section_at(obj, i);
|
2018-10-20 09:35:25 +03:00
|
|
|
if (s->flags & SHF_COMPRESSED) {
|
2018-10-14 19:59:15 +03:00
|
|
|
free(s->ptr);
|
|
|
|
}
|
2018-10-12 02:55:46 +03:00
|
|
|
}
|
2018-10-14 19:59:15 +03:00
|
|
|
if (obj->mapped_size) {
|
|
|
|
munmap(obj->mapped, obj->mapped_size);
|
|
|
|
}
|
|
|
|
obj = o->next;
|
2018-10-12 02:58:20 +03:00
|
|
|
free(o);
|
2018-10-12 02:55:46 +03:00
|
|
|
}
|
2018-10-14 19:59:15 +03:00
|
|
|
for (i = 0; i < num_traces; i++) {
|
|
|
|
line_info_t *line = lines[i].next;
|
|
|
|
while (line) {
|
|
|
|
line_info_t *l = line;
|
|
|
|
line = line->next;
|
|
|
|
free(l);
|
|
|
|
}
|
|
|
|
}
|
2010-11-26 06:28:50 +03:00
|
|
|
free(lines);
|
2014-04-11 08:48:26 +04:00
|
|
|
free(dladdr_fbases);
|
2010-11-26 06:28:50 +03:00
|
|
|
}
|
|
|
|
|
2023-08-01 10:25:04 +03:00
|
|
|
#undef kprintf
|
2013-03-23 20:05:51 +04:00
|
|
|
|
2011-07-08 16:23:10 +04:00
|
|
|
#else /* defined(USE_ELF) */
|
2010-11-27 09:43:30 +03:00
|
|
|
#error not supported
|
|
|
|
#endif
|