Skip to content

Commit 313f0aa

Browse files
committed
Added backtrace printing to the tests.
Added couple of traces to the test
1 parent a1b175c commit 313f0aa

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ FOREACH (ODBC_TEST ${ODBC_TESTS})
7878
ADD_EXECUTABLE(odbc_${ODBC_TEST} "${ODBC_TEST}.c" ${COMMON_TEST_SOURCES})
7979
SET_TARGET_PROPERTIES(odbc_${ODBC_TEST} PROPERTIES
8080
LANGUAGE C)
81+
TARGET_LINK_LIBRARIES(odbc_${ODBC_TEST} -rdynamic)
82+
IF (WIN32)
83+
TARGET_LINK_LIBRARIES(odbc_${ODBC_TEST} dbghelp)
84+
ENDIF()
8185
IF (DIRECT_LINK_TESTS)
8286
TARGET_LINK_LIBRARIES(odbc_${ODBC_TEST} maodbc ${PLATFORM_DEPENDENCIES})
8387
ELSE()

test/bulk.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,9 +636,9 @@ ODBC_TEST(t_odbc149)
636636
Val[2].year= 2018;
637637
Val[2].month= 7;
638638
Val[2].day= 27;
639-
639+
diag("About to execute bulk");
640640
CHECK_STMT_RC(Stmt, SQLExecute(Stmt));
641-
641+
diag("Running select to verify bulk results");
642642
OK_SIMPLE_STMT(Stmt, "SELECT id, ts, c, b, w FROM odbc149")
643643
CHECK_STMT_RC(Stmt, SQLBindCol(Stmt, 1, SQL_C_LONG, &idBuf, 0, NULL));
644644
CHECK_STMT_RC(Stmt, SQLBindCol(Stmt, 2, SQL_C_TIMESTAMP, &Val[1], 0, tsLen));

test/tap.h

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,75 @@
3131
#include <stdio.h>
3232
#include <stddef.h>
3333
#include <math.h>
34+
#include <signal.h>
35+
36+
#define MAX_STACK_FRAMES 64
37+
38+
void diag(const char *fstr, ...);
3439

3540
#ifdef _WIN32
3641
# define _WINSOCKAPI_
3742
# include <windows.h>
3843
# include <shlwapi.h>
44+
# include <dbghelp.h>
45+
//# pragma comment(lib, "dbghelp.lib")
46+
3947

4048
char* strcasestr(const char* HayStack, const char* Needle)
4149
{
4250
return StrStrIA(HayStack, Needle);
4351
}
52+
53+
54+
void print_btrace(int sig)
55+
{
56+
void* callstack[MAX_STACK_FRAMES];
57+
SYMBOL_INFO* symbol;
58+
IMAGEHLP_LINE64 line;
59+
HANDLE process = GetCurrentProcess();
60+
61+
diag("Caught signal %d", sig);
62+
diag("Stack Trace:");
63+
SymSetOptions(SYMOPT_LOAD_LINES);
64+
SymInitialize(process, NULL, TRUE);
65+
66+
memset((void*)&line, 0, sizeof(IMAGEHLP_LINE64));
67+
line.SizeOfStruct= sizeof(IMAGEHLP_LINE64);
68+
69+
symbol = (SYMBOL_INFO*)calloc(sizeof(SYMBOL_INFO) + 256, 1);
70+
symbol->MaxNameLen = 255;
71+
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
72+
73+
WORD frames = CaptureStackBackTrace(0, MAX_STACK_FRAMES, callstack, NULL);
74+
75+
// At 0 index will be btrace itself
76+
for (int i = 1; i < frames; ++i)
77+
{
78+
DWORD64 displacement= 0;
79+
SymFromAddr(process, (DWORD64)callstack[i], 0, symbol);
80+
if (!SymGetLineFromAddr64(process, (DWORD64)callstack[i], &displacement, &line))
81+
{
82+
DWORD error= GetLastError();
83+
diag("SymGetLineFromAddr64 returned error : %d", error);
84+
line.FileName= "<no file name>";
85+
}
86+
//stderr?
87+
printf("%d: %s(%s:%u)\n", i, symbol->Name, line.FileName, line.LineNumber);
88+
}
89+
90+
free(symbol);
91+
SymCleanup(process);
92+
exit(1);
93+
}
94+
4495
#else
4596

4697
# include <string.h>
4798
# include <errno.h>
4899
# include <wchar.h>
49100
# include <unistd.h>
101+
# include <execinfo.h>
102+
50103
# include "ma_conv_charset.h"
51104

52105
/* Mimicking of VS' _snprintf */
@@ -111,6 +164,17 @@ int strcpy_s(char *dest, size_t buffer_size, const char *src)
111164
#define _atoi64(str) strtoll((str), NULL, 10)
112165
#define _i64toa(a,b,c) longlong2str((a),(b),(c))
113166

167+
void print_btrace(int sig)
168+
{
169+
void* callstack[MAX_STACK_FRAMES];
170+
int frames = backtrace(callstack, MAX_STACK_FRAMES);
171+
172+
diag("Caught signal %d\n", sig);
173+
diag("Stack Trace:\n");
174+
backtrace_symbols_fd(callstack, frames, STDERR_FILENO);
175+
exit(1);
176+
}
177+
114178
#endif
115179

116180
#include <sql.h>
@@ -307,11 +371,16 @@ void get_env_defaults()
307371
}
308372
}
309373

310-
374+
/* Reads env defaults, command line options, but also it sets functions to process signals(SIGABRT SIGSEGV)
375+
* as all tests run it and run only once. even though not the right place
376+
*/
311377
void get_options(int argc, char **argv)
312378
{
313379
int i;
314380

381+
signal(SIGABRT, print_btrace);
382+
signal(SIGSEGV, print_btrace);
383+
315384
get_env_defaults();
316385

317386
for (i= 1; i < argc; i+= 2) /* "d:u:p:P:s:S:?" */

0 commit comments

Comments
 (0)