Skip to content

Commit 9ba371e

Browse files
committed
test
1 parent 05a4127 commit 9ba371e

1 file changed

Lines changed: 28 additions & 16 deletions

File tree

cli/stacktrace.cpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,20 @@
3131
void print_stacktrace(FILE* output, int start_idx, bool demangling, int maxdepth, bool omit_above_own)
3232
{
3333
fputs("print_stacktrace\n", output);
34+
3435
// 32 vs. 64bit
35-
#define ADDRESSDISPLAYLENGTH ((sizeof(long)==8)?12:8)
36+
static constexpr auto ADDRESSDISPLAYLENGTH = (sizeof(long) == 8) ? 12 : 8;
37+
3638
void *callstackArray[32]= {nullptr}; // the less resources the better...
3739
fputs("backtrace\n", output);
3840
const int currentdepth = backtrace(callstackArray, static_cast<int>(getArrayLength(callstackArray)));
41+
if (currentdepth == 0) {
42+
fputs("Callstack could not be obtained (backtrace)\n", output);
43+
return;
44+
}
45+
if (currentdepth == getArrayLength(callstackArray)) {
46+
fputs("Callstack might be truncated\n", output);
47+
}
3948
fprintf(output, "currentdepth %d\n", currentdepth);
4049
// set offset to 1 to omit the printing function itself
4150
int offset=start_idx+1; // some entries on top are within our own exception handling code or libc
@@ -49,7 +58,7 @@ void print_stacktrace(FILE* output, int start_idx, bool demangling, int maxdepth
4958
fputs("backtrace_symbols\n", output);
5059
char **symbolStringList = backtrace_symbols(callstackArray, currentdepth);
5160
if (!symbolStringList) {
52-
fputs("Callstack could not be obtained\n", output);
61+
fputs("Callstack could not be obtained (backtrace_symbols)\n", output);
5362
return;
5463
}
5564

@@ -70,19 +79,9 @@ void print_stacktrace(FILE* output, int start_idx, bool demangling, int maxdepth
7079
}
7180
fprintf(output, "offset %d\n", offset);
7281
const char * realnameString = nullptr;
73-
const char * const firstBracketName = strchr(symbolString, '(');
74-
fprintf(output, "firstBracketName %p\n", (void*)firstBracketName);
75-
const char * const firstBracketAddress = strchr(symbolString, '[');
76-
fprintf(output, "firstBracketAddress %p\n", (void*)firstBracketAddress);
77-
const char * const secondBracketAddress = strchr(firstBracketAddress, ']');
78-
fprintf(output, "secondBracketAddress %p\n", (void*)secondBracketAddress);
79-
const char * const beginAddress = firstBracketAddress+3;
80-
fprintf(output, "beginAddress %p\n", (void*)beginAddress);
81-
const int addressLen = int(secondBracketAddress-beginAddress);
82-
fprintf(output, "addressLen %d\n", addressLen);
83-
const int padLen = (ADDRESSDISPLAYLENGTH-addressLen);
84-
fprintf(output, "padLen %d\n", padLen);
85-
if (demangling && firstBracketName) {
82+
if (demangling) {
83+
const char * const firstBracketName = strchr(symbolString, '(');
84+
fprintf(output, "firstBracketName %p\n", (void*)firstBracketName);
8685
const char * const plus = strchr(firstBracketName, '+');
8786
if (plus && (plus>(firstBracketName+1))) {
8887
char input_buffer[1024]= {0};
@@ -97,6 +96,20 @@ void print_stacktrace(FILE* output, int start_idx, bool demangling, int maxdepth
9796
const int ordinal=i-offset;
9897
fprintf(output, "#%-2d 0x",
9998
ordinal);
99+
100+
const char * const firstBracketAddress = strchr(symbolString, '[');
101+
fprintf(output, "firstBracketAddress %p\n", (void*)firstBracketAddress);
102+
const char * const secondBracketAddress = strchr(firstBracketAddress, ']');
103+
fprintf(output, "secondBracketAddress %p\n", (void*)secondBracketAddress);
104+
const int padLen = [&]() {
105+
// TODO: handle firstBracketAddress being NULL
106+
const char * const beginAddress = firstBracketAddress+3;
107+
fprintf(output, "beginAddress %p\n", (void*)beginAddress);
108+
const int addressLen = int(secondBracketAddress-beginAddress);
109+
fprintf(output, "addressLen %d\n", addressLen);
110+
return (ADDRESSDISPLAYLENGTH-addressLen);
111+
}();
112+
fprintf(output, "padLen %d\n", padLen);
100113
if (padLen>0)
101114
fprintf(output, "%0*d",
102115
padLen, 0);
@@ -112,7 +125,6 @@ void print_stacktrace(FILE* output, int start_idx, bool demangling, int maxdepth
112125
}
113126
// NOLINTNEXTLINE(bugprone-multi-level-implicit-pointer-conversion) - code matches the documented usage
114127
free(symbolStringList);
115-
#undef ADDRESSDISPLAYLENGTH
116128
}
117129

118130
#endif // USE_UNIX_BACKTRACE_SUPPORT

0 commit comments

Comments
 (0)