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

Commit 9e3497d

Browse files
committed
Fix rt.backtrace.dwarf
1 parent 243e690 commit 9e3497d

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/rt/backtrace/dwarf.d

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,37 @@ int traceHandlerOpApplyImpl(const void*[] callstack, scope int delegate(ref size
7979
int ret = 0;
8080
foreach (size_t i; 0 .. callstack.length)
8181
{
82-
char[1536] buffer = void; buffer[0] = 0;
83-
char[256] addressBuffer = void; addressBuffer[0] = 0;
82+
char[1536] buffer = void;
83+
size_t bufferLength = 0;
84+
85+
void appendToBuffer(Args...)(const(char)* format, Args args)
86+
{
87+
const count = snprintf(buffer.ptr + bufferLength, buffer.length - bufferLength, format, args);
88+
assert(count >= 0);
89+
bufferLength += count;
90+
if (bufferLength >= buffer.length)
91+
bufferLength = buffer.length - 1;
92+
}
8493

8594
if (locations.length > 0 && locations[i].line != -1)
86-
snprintf(addressBuffer.ptr, addressBuffer.length, "%.*s:%d ", cast(int) locations[i].file.length, locations[i].file.ptr, locations[i].line);
95+
{
96+
appendToBuffer("%.*s:%d ", cast(int) locations[i].file.length, locations[i].file.ptr, locations[i].line);
97+
}
8798
else
88-
addressBuffer[] = "??:? \0";
99+
{
100+
buffer[0 .. 5] = "??:? ";
101+
bufferLength = 5;
102+
}
89103

90104
char[1024] symbolBuffer = void;
91-
int bufferLength;
92105
auto symbol = getDemangledSymbol(frameList[i][0 .. strlen(frameList[i])], symbolBuffer);
93106
if (symbol.length > 0)
94-
bufferLength = snprintf(buffer.ptr, buffer.length, "%s%.*s [0x%x]", addressBuffer.ptr, cast(int) symbol.length, symbol.ptr, callstack[i]);
95-
else
96-
bufferLength = snprintf(buffer.ptr, buffer.length, "%s[0x%x]", addressBuffer.ptr, callstack[i]);
107+
appendToBuffer("%.*s ", cast(int) symbol.length, symbol.ptr);
97108

98-
assert(bufferLength >= 0);
109+
static if (size_t.sizeof == 8)
110+
appendToBuffer("[0x%llx]", callstack[i]);
111+
else
112+
appendToBuffer("[0x%x]", callstack[i]);
99113

100114
auto output = buffer[0 .. bufferLength];
101115
auto pos = i;

0 commit comments

Comments
 (0)