Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Fix Issue 18068 - No file names and line numbers in stack trace
Browse files Browse the repository at this point in the history
  • Loading branch information
wilzbach committed May 4, 2018
1 parent 243e690 commit 8be4a1c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/rt/backtrace/dwarf.d
Expand Up @@ -69,8 +69,15 @@ int traceHandlerOpApplyImpl(const void*[] callstack, scope int delegate(ref size
{
// resolve addresses
locations.length = callstack.length;
auto elfObj = image.baseAddress();
foreach(size_t i; 0 .. callstack.length)
locations[i].address = cast(size_t) callstack[i];
{
typeof(elfObj.begin) offset;
if (elfObj.begin <= cast(size_t) callstack[i] && cast(size_t) callstack[i] <= elfObj.end)
offset = elfObj.begin;

locations[i].address = cast(size_t) callstack[i] - offset;
}

resolveAddresses(debugLineSectionData, locations[]);
}
Expand Down
56 changes: 56 additions & 0 deletions src/rt/backtrace/elf.d
Expand Up @@ -58,6 +58,62 @@ struct Image

return null;
}

@property auto baseAddress()
{
version(linux)
{
import core.sys.linux.link;
import core.sys.linux.elf;
}
else version(FreeBSD)
{
import core.sys.freebsd.link_elf;
import core.sys.freebsd.elf;
}
else version(DragonFlyBSD)
{
import core.sys.dragonflybsd.link_elf;
import core.sys.freebsd.elf;
}
struct ElfBaseAddress
{
ElfW!"Addr" begin;
ElfW!"Addr" end;
bool set;
}
ElfBaseAddress elfAddress;

// the DWARF addresses for DSOs are relative
const isDynamicSharedObject = (file.ehdr.e_type == ET_DYN);
if (!isDynamicSharedObject)
return elfAddress;

extern(C) int dl_iterate_phdr_cb_ngc_tracehandler(dl_phdr_info* info, size_t, void* elfObj) @nogc
{
auto obj = cast(ElfBaseAddress*) elfObj;
// only take the first address as this will be the main binary
if (obj.set)
return 0;

obj.set = true;
// search for the executable code segment
foreach (const ref phdr; info.dlpi_phdr[0 .. info.dlpi_phnum])
{
if (phdr.p_type == PT_LOAD && phdr.p_flags & PF_X)
{
obj.begin = info.dlpi_addr + phdr.p_vaddr;
obj.end = obj.begin + phdr.p_memsz;
}
}
// temporarily fall back to this
obj.begin = info.dlpi_addr;
obj.end = obj.begin + info.dlpi_phdr.p_memsz;
return 0;
}
dl_iterate_phdr(&dl_iterate_phdr_cb_ngc_tracehandler, &elfAddress);
return elfAddress;
}
}

private:
Expand Down

0 comments on commit 8be4a1c

Please sign in to comment.