2013-03-08 18:00:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-05-15 13:28:52 +00:00
|
|
|
#include <windows.h>
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/win_dbghelp.h"
|
2018-09-19 15:31:27 +00:00
|
|
|
|
2013-03-08 18:00:16 +00:00
|
|
|
#include <process.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
// Remove prefix addresses. 18 = 2 * (8 digit hexa + 1 space).
|
|
|
|
// e.g. "abcd1234 abcd1234 render_pdf!processInput
|
|
|
|
#define CDB_CALLSTACK_PREFIX (18)
|
|
|
|
|
|
|
|
// CDB.EXE prints a lot of garbage and there is no argument to pass which
|
|
|
|
// would remove all that noise.
|
|
|
|
// Using eval feature that evaluates a number in hex and prints it to stdout
|
|
|
|
// to mark where the callstack is printed.
|
|
|
|
// For example, each thread's callstack will be marked with "12340000" at
|
|
|
|
// the beginning and "12340001" at the end.
|
|
|
|
// We just made up these numbers; they could be anything, as long as they
|
|
|
|
// match up with their decimal equivalents.
|
|
|
|
|
|
|
|
#define MARKER_THREAD_CALLSTACK_START_NUMBER "12340000"
|
|
|
|
#define MARKER_THREAD_CALLSTACK_START "Evaluate expression: 305397760 = 12340000"
|
|
|
|
|
|
|
|
#define MARKER_THREAD_CALLSTACK_STOP_NUMBER "12340001"
|
|
|
|
#define MARKER_THREAD_CALLSTACK_STOP "Evaluate expression: 305397761 = 12340001"
|
|
|
|
|
|
|
|
#define MARKER_EXCEPTION_CALLSTACK_START_NUMBER "12340002"
|
|
|
|
#define MARKER_EXCEPTION_CALLSTACK_START "Evaluate expression: 305397762 = 12340002"
|
|
|
|
|
|
|
|
#define MARKER_EXCEPTION_CALLSTACK_STOP_NUMBER "12340003"
|
|
|
|
#define MARKER_EXCEPTION_CALLSTACK_STOP "Evaluate expression: 305397763 = 12340003"
|
|
|
|
|
|
|
|
// k - print stack
|
|
|
|
// ? val - evaluate expression. Used to mark the log file.
|
|
|
|
// .ecxr - load exception, if exception was thrown.
|
|
|
|
// k - print the resolved stack by .ecxr
|
|
|
|
// q - quit cdb.exe
|
|
|
|
#define CDB_PRINT_CALLSTACK_CURRENT_THREAD "? " MARKER_THREAD_CALLSTACK_START_NUMBER "; k; ? " MARKER_THREAD_CALLSTACK_STOP_NUMBER "; .ecxr; ? " MARKER_EXCEPTION_CALLSTACK_START_NUMBER "; k; ? " MARKER_EXCEPTION_CALLSTACK_STOP_NUMBER "; q"
|
|
|
|
|
|
|
|
static void strncpyOrSetBlank(char* dest, const char* src, size_t len) {
|
2015-08-27 14:41:13 +00:00
|
|
|
const char* srcOrEmptyString = (nullptr == src) ? "" : src;
|
2013-03-08 18:00:16 +00:00
|
|
|
strncpy(dest, srcOrEmptyString, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
char debug_app_name[MAX_PATH] = "";
|
2013-03-09 07:01:15 +00:00
|
|
|
void setAppName(const char* app_name) {
|
2013-03-08 18:00:16 +00:00
|
|
|
strncpyOrSetBlank(debug_app_name, app_name, sizeof(debug_app_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* getAppName() {
|
|
|
|
return debug_app_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
char debug_binaries_path[MAX_PATH] = "";
|
|
|
|
void setBinariesPath(const char* binaries_path) {
|
|
|
|
strncpyOrSetBlank(debug_binaries_path, binaries_path,
|
|
|
|
sizeof(debug_binaries_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* getBinariesPath() {
|
|
|
|
return debug_binaries_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
char debug_app_version[100] = "";
|
|
|
|
void setAppVersion(const char* version) {
|
|
|
|
strncpyOrSetBlank(debug_app_version, version, sizeof(debug_app_version));
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* getAppVersion() {
|
|
|
|
return debug_app_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
char debug_cdb_path[MAX_PATH] = "";
|
|
|
|
void setCdbPath(const char* path) {
|
|
|
|
strncpyOrSetBlank(debug_cdb_path, path, sizeof(debug_cdb_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* getCdbPath() {
|
|
|
|
return debug_cdb_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Print all the lines of a CDB k command whicha are callstacks.
|
|
|
|
* Callstack lines are marked by start and stop markers and they are prefixed
|
|
|
|
* byt 2 hex adresses, which will not be reported.
|
|
|
|
*/
|
|
|
|
static void printCallstack(const char* filename,
|
|
|
|
const char* start,
|
|
|
|
const char* stop) {
|
|
|
|
FILE* file = fopen(filename, "rt");
|
|
|
|
char line[1000];
|
|
|
|
bool started = false;
|
|
|
|
// Not the most performant code, but this will be used only to collect
|
|
|
|
// the callstack from a log files, only when the application had failed.
|
|
|
|
while (fgets(line, sizeof(line), file)) {
|
|
|
|
if (!started && strncmp(start, line, strlen(start)) == 0) {
|
|
|
|
started = true;
|
|
|
|
} else if (started && strncmp(stop, line, strlen(stop)) == 0) {
|
|
|
|
break;
|
|
|
|
} else if (started) {
|
|
|
|
// Filter messages. Calstack lines contain "exe/dll!function"
|
2015-08-27 14:41:13 +00:00
|
|
|
if (strchr(line, '!') != nullptr && strlen(line) > CDB_CALLSTACK_PREFIX) {
|
2013-03-08 18:00:16 +00:00
|
|
|
printf("%s", line + CDB_CALLSTACK_PREFIX); // fgets includes \n already.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BUILD_UNIQUE_FILENAME(var, ext, szPath, szAppName, szVersion, stLocalTime) \
|
|
|
|
sprintf(szFileName, "%s%s\\%s-%04d%02d%02d-%02d%02d%02d-%ld-%ld" ext, \
|
|
|
|
szPath, szAppName, szVersion, \
|
|
|
|
stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay, \
|
|
|
|
stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond, \
|
2013-03-09 07:01:15 +00:00
|
|
|
GetCurrentProcessId(), GetCurrentThreadId());
|
2013-03-08 18:00:16 +00:00
|
|
|
|
|
|
|
// Exception execution handler. Exception is recognized. Transfer control to
|
|
|
|
// the exception handler by executing the __except compound statement,
|
|
|
|
// then continue execution after the __except block.
|
|
|
|
int GenerateDumpAndPrintCallstack(EXCEPTION_POINTERS* pExceptionPointers) {
|
|
|
|
BOOL bMiniDumpSuccessful;
|
2013-03-09 07:01:15 +00:00
|
|
|
char szPath[MAX_PATH];
|
|
|
|
char szFileName[MAX_PATH];
|
|
|
|
char szFileNameOutput[MAX_PATH];
|
2013-03-08 18:00:16 +00:00
|
|
|
const char* szAppName = getAppName();
|
|
|
|
const char* szVersion = getAppVersion();
|
|
|
|
DWORD dwBufferSize = MAX_PATH;
|
|
|
|
HANDLE hDumpFile;
|
|
|
|
SYSTEMTIME stLocalTime;
|
|
|
|
MINIDUMP_EXCEPTION_INFORMATION ExpParam;
|
|
|
|
|
|
|
|
GetLocalTime( &stLocalTime );
|
|
|
|
GetTempPath( dwBufferSize, szPath );
|
|
|
|
|
|
|
|
sprintf( szFileName, "%s%s", szPath, szAppName );
|
2015-08-27 14:41:13 +00:00
|
|
|
CreateDirectory( szFileName, nullptr );
|
2013-03-08 18:00:16 +00:00
|
|
|
|
|
|
|
BUILD_UNIQUE_FILENAME(szFileName, ".dmp", szPath, szAppName, szVersion, stLocalTime);
|
|
|
|
BUILD_UNIQUE_FILENAME(szFileNameOutput, ".out", szPath, szAppName, szVersion, stLocalTime);
|
|
|
|
|
|
|
|
hDumpFile = CreateFile(szFileName,
|
2013-03-09 07:01:15 +00:00
|
|
|
GENERIC_READ|GENERIC_WRITE,
|
2013-03-08 18:00:16 +00:00
|
|
|
FILE_SHARE_WRITE|FILE_SHARE_READ,
|
|
|
|
0,
|
|
|
|
CREATE_ALWAYS,
|
|
|
|
0,
|
|
|
|
0);
|
|
|
|
|
|
|
|
ExpParam.ThreadId = GetCurrentThreadId();
|
|
|
|
ExpParam.ExceptionPointers = pExceptionPointers;
|
|
|
|
ExpParam.ClientPointers = TRUE;
|
|
|
|
|
|
|
|
bMiniDumpSuccessful = MiniDumpWriteDump(GetCurrentProcess(),
|
2013-03-09 07:01:15 +00:00
|
|
|
GetCurrentProcessId(),
|
2013-03-08 18:00:16 +00:00
|
|
|
hDumpFile,
|
|
|
|
MiniDumpWithDataSegs,
|
|
|
|
&ExpParam,
|
2015-08-27 14:41:13 +00:00
|
|
|
nullptr,
|
|
|
|
nullptr);
|
2013-03-08 18:00:16 +00:00
|
|
|
|
|
|
|
printf("MiniDump file: %s\n", szFileName);
|
|
|
|
printf("App exe and pdb: %s\n", getBinariesPath());
|
|
|
|
|
|
|
|
const char* cdbExePath = getCdbPath();
|
|
|
|
if (cdbExePath && *cdbExePath != '\0') {
|
|
|
|
printf("Cdb exe: %s\n", cdbExePath);
|
2013-03-09 07:01:15 +00:00
|
|
|
|
2013-03-08 18:00:16 +00:00
|
|
|
char command[MAX_PATH * 4];
|
|
|
|
sprintf(command, "%s -y \"%s\" -i \"%s\" -z \"%s\" -c \"%s\" -kqm >\"%s\"",
|
|
|
|
cdbExePath,
|
|
|
|
getBinariesPath(),
|
|
|
|
getBinariesPath(),
|
|
|
|
szFileName,
|
|
|
|
CDB_PRINT_CALLSTACK_CURRENT_THREAD,
|
|
|
|
szFileNameOutput);
|
|
|
|
system(command);
|
|
|
|
|
|
|
|
printf("\nThread Callstack:\n");
|
2013-03-09 07:01:15 +00:00
|
|
|
printCallstack(szFileNameOutput,
|
2013-03-08 18:00:16 +00:00
|
|
|
MARKER_THREAD_CALLSTACK_START,
|
|
|
|
MARKER_THREAD_CALLSTACK_STOP);
|
|
|
|
|
2013-03-09 07:01:15 +00:00
|
|
|
printf("\nException Callstack:\n");
|
2013-03-08 18:00:16 +00:00
|
|
|
printCallstack(szFileNameOutput,
|
|
|
|
MARKER_EXCEPTION_CALLSTACK_START,
|
|
|
|
MARKER_EXCEPTION_CALLSTACK_STOP);
|
|
|
|
} else {
|
2013-03-09 07:01:15 +00:00
|
|
|
printf("Warning: CDB path not set up.\n");
|
2013-03-08 18:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return EXCEPTION_EXECUTE_HANDLER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Sets the debugging variables. Input parameter is app location.
|
|
|
|
* e.g out\Debug\render_pdfs.exe
|
|
|
|
* This function expects the .pdb file to be in the same directory.
|
|
|
|
*/
|
|
|
|
void setUpDebuggingFromArgs(const char* vargs0) {
|
2015-01-29 15:13:20 +00:00
|
|
|
size_t i = strlen(vargs0);
|
2013-03-08 18:00:16 +00:00
|
|
|
|
2013-03-08 23:13:33 +00:00
|
|
|
if (i >= 4 && _stricmp(vargs0 - 4, ".exe") == 0) {
|
2013-03-08 18:00:16 +00:00
|
|
|
// Ignore .exe
|
|
|
|
i -= 4;
|
|
|
|
}
|
|
|
|
|
2015-01-29 15:44:24 +00:00
|
|
|
size_t pos_period = i;
|
2013-03-08 18:00:16 +00:00
|
|
|
|
|
|
|
// Find last \ in path - this is Windows!
|
|
|
|
while (i >= 0 && vargs0[i] != '\\') {
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
|
2015-01-29 15:44:24 +00:00
|
|
|
size_t pos_last_slash = i;
|
2013-03-08 18:00:16 +00:00
|
|
|
|
|
|
|
char app_name[MAX_PATH];
|
|
|
|
strncpy(app_name, vargs0 + pos_last_slash + 1, pos_period - pos_last_slash - 1);
|
|
|
|
app_name[pos_period - pos_last_slash] = '\0';
|
|
|
|
setAppName(app_name);
|
|
|
|
|
|
|
|
char binaries_path[MAX_PATH];
|
|
|
|
strncpy(binaries_path, vargs0, pos_last_slash);
|
|
|
|
binaries_path[pos_last_slash] = '\0';
|
|
|
|
setBinariesPath(binaries_path);
|
|
|
|
|
|
|
|
setAppVersion("1.0"); // Dummy for now, but use revision instead if we use
|
|
|
|
// the minidump for anything else other than
|
|
|
|
// collecting the callstack.
|
|
|
|
|
|
|
|
// cdb.exe is the app used to load the minidump which prints the callstack.
|
|
|
|
char cdbExePath[MAX_PATH];
|
|
|
|
#ifdef _WIN64
|
|
|
|
sprintf(cdbExePath, "%s\\x64\\cdb.exe", SK_CDB_PATH);
|
|
|
|
#else
|
|
|
|
sprintf(cdbExePath, "%s\\cdb.exe", SK_CDB_PATH);
|
|
|
|
#endif
|
|
|
|
setCdbPath(cdbExePath);
|
|
|
|
}
|