Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit f8aa396

Browse files
authored
Fix rt.backtrace.dwarf (#124)
1 parent 3545b11 commit f8aa396

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
@@ -93,23 +93,37 @@ else
9393
int ret = 0;
9494
foreach (size_t i; 0 .. callstack.length)
9595
{
96-
char[1536] buffer = void; buffer[0] = 0;
97-
char[256] addressBuffer = void; addressBuffer[0] = 0;
96+
char[1536] buffer = void;
97+
size_t bufferLength = 0;
98+
99+
void appendToBuffer(Args...)(const(char)* format, Args args)
100+
{
101+
const count = snprintf(buffer.ptr + bufferLength, buffer.length - bufferLength, format, args);
102+
assert(count >= 0);
103+
bufferLength += count;
104+
if (bufferLength >= buffer.length)
105+
bufferLength = buffer.length - 1;
106+
}
98107

99108
if (locations.length > 0 && locations[i].line != -1)
100-
snprintf(addressBuffer.ptr, addressBuffer.length, "%.*s:%d ", cast(int) locations[i].file.length, locations[i].file.ptr, locations[i].line);
109+
{
110+
appendToBuffer("%.*s:%d ", cast(int) locations[i].file.length, locations[i].file.ptr, locations[i].line);
111+
}
101112
else
102-
addressBuffer[] = "??:? \0";
113+
{
114+
buffer[0 .. 5] = "??:? ";
115+
bufferLength = 5;
116+
}
103117

104118
char[1024] symbolBuffer = void;
105-
int bufferLength;
106119
auto symbol = getDemangledSymbol(frameList[i][0 .. strlen(frameList[i])], symbolBuffer);
107120
if (symbol.length > 0)
108-
bufferLength = snprintf(buffer.ptr, buffer.length, "%s%.*s [0x%x]", addressBuffer.ptr, cast(int) symbol.length, symbol.ptr, callstack[i]);
109-
else
110-
bufferLength = snprintf(buffer.ptr, buffer.length, "%s[0x%x]", addressBuffer.ptr, callstack[i]);
121+
appendToBuffer("%.*s ", cast(int) symbol.length, symbol.ptr);
111122

112-
assert(bufferLength >= 0);
123+
static if (size_t.sizeof == 8)
124+
appendToBuffer("[0x%llx]", callstack[i]);
125+
else
126+
appendToBuffer("[0x%x]", callstack[i]);
113127

114128
auto output = buffer[0 .. bufferLength];
115129
auto pos = i;

0 commit comments

Comments
 (0)