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

Commit

Permalink
Slightly simplify rt.backtrace.elf
Browse files Browse the repository at this point in the history
  • Loading branch information
kinke committed Oct 14, 2018
1 parent 1b7435c commit 19734f0
Showing 1 changed file with 5 additions and 37 deletions.
42 changes: 5 additions & 37 deletions src/rt/backtrace/elf.d
Expand Up @@ -29,19 +29,12 @@ struct Image

static Image openSelf()
{
version (linux)
{
auto selfPath = "/proc/self/exe".ptr;
}
else version (FreeBSD)
{
char[1024] selfPathBuffer = void;
auto selfPath = getFreeBSDExePath(selfPathBuffer[]);
if (selfPath is null) return false;
}
else version (DragonFlyBSD)
const(char)* selfPath;
foreach (object; SharedObjects)
{
auto selfPath = "/proc/curproc/file".ptr;
// the first object is the main binary
selfPath = object.name().ptr;
break;
}

Image image;
Expand Down Expand Up @@ -94,28 +87,3 @@ struct Image
return base;
}
}

private:

version (FreeBSD)
{
extern (C) int sysctl(const int* name, uint namelen, void* oldp, size_t* oldlenp, const void* newp, size_t newlen) @nogc nothrow;
const(char)* getFreeBSDExePath(char[] buffer) @nogc nothrow
{
enum
{
CTL_KERN = 1,
KERN_PROC = 14,
KERN_PROC_PATHNAME = 12
}

int[4] mib = [CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1];
size_t len = buffer.length;

auto result = sysctl(mib.ptr, mib.length, buffer.ptr, &len, null, 0); // get the length of the path
if (result != 0) return null;
if (len + 1 > buffer.length) return null;
buffer[len] = 0;
return buffer.ptr;
}
}

0 comments on commit 19734f0

Please sign in to comment.