OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2013 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #ifdef SK_BUILD_FOR_WIN32 |
| 9 |
| 10 #include "windows.h" |
| 11 #include "win_dbghelp.h" |
| 12 #include <process.h> |
| 13 #include <string.h> |
| 14 #include <stdlib.h> |
| 15 #include <stdio.h> |
| 16 |
| 17 // Remove prefix addresses. 18 = 2 * (8 digit hexa + 1 space). |
| 18 // e.g. "abcd1234 abcd1234 render_pdf!processInput |
| 19 #define CDB_CALLSTACK_PREFIX (18) |
| 20 |
| 21 // CDB.EXE prints a lot of garbage and there is no argument to pass which |
| 22 // would remove all that noise. |
| 23 // Using eval feature that evaluates a number in hex and prints it to stdout |
| 24 // to mark where the callstack is printed. |
| 25 // For example, each thread's callstack will be marked with "12340000" at |
| 26 // the beginning and "12340001" at the end. |
| 27 // We just made up these numbers; they could be anything, as long as they |
| 28 // match up with their decimal equivalents. |
| 29 |
| 30 #define MARKER_THREAD_CALLSTACK_START_NUMBER "12340000" |
| 31 #define MARKER_THREAD_CALLSTACK_START "Evaluate expression: 305397760 = 12340000
" |
| 32 |
| 33 #define MARKER_THREAD_CALLSTACK_STOP_NUMBER "12340001" |
| 34 #define MARKER_THREAD_CALLSTACK_STOP "Evaluate expression: 305397761 = 12340001" |
| 35 |
| 36 #define MARKER_EXCEPTION_CALLSTACK_START_NUMBER "12340002" |
| 37 #define MARKER_EXCEPTION_CALLSTACK_START "Evaluate expression: 305397762 = 12340
002" |
| 38 |
| 39 #define MARKER_EXCEPTION_CALLSTACK_STOP_NUMBER "12340003" |
| 40 #define MARKER_EXCEPTION_CALLSTACK_STOP "Evaluate expression: 305397763 = 123400
03" |
| 41 |
| 42 // k - print stack |
| 43 // ? val - evaluate expression. Used to mark the log file. |
| 44 // .ecxr - load exception, if exception was thrown. |
| 45 // k - print the resolved stack by .ecxr |
| 46 // q - quit cdb.exe |
| 47 #define CDB_PRINT_CALLSTACK_CURRENT_THREAD "? " MARKER_THREAD_CALLSTACK_START_NU
MBER "; k; ? " MARKER_THREAD_CALLSTACK_STOP_NUMBER "; .ecxr; ? " MARKER_EXCEPTIO
N_CALLSTACK_START_NUMBER "; k; ? " MARKER_EXCEPTION_CALLSTACK_STOP_NUMBER "; q" |
| 48 |
| 49 static void strncpyOrSetBlank(char* dest, const char* src, size_t len) { |
| 50 const char* srcOrEmptyString = (NULL == src) ? "" : src; |
| 51 strncpy(dest, srcOrEmptyString, len); |
| 52 } |
| 53 |
| 54 char debug_app_name[MAX_PATH] = ""; |
| 55 void setAppName(const char* app_name) { |
| 56 strncpyOrSetBlank(debug_app_name, app_name, sizeof(debug_app_name)); |
| 57 } |
| 58 |
| 59 const char* getAppName() { |
| 60 return debug_app_name; |
| 61 } |
| 62 |
| 63 char debug_binaries_path[MAX_PATH] = ""; |
| 64 void setBinariesPath(const char* binaries_path) { |
| 65 strncpyOrSetBlank(debug_binaries_path, binaries_path, |
| 66 sizeof(debug_binaries_path)); |
| 67 } |
| 68 |
| 69 const char* getBinariesPath() { |
| 70 return debug_binaries_path; |
| 71 } |
| 72 |
| 73 char debug_app_version[100] = ""; |
| 74 void setAppVersion(const char* version) { |
| 75 strncpyOrSetBlank(debug_app_version, version, sizeof(debug_app_version)); |
| 76 } |
| 77 |
| 78 const char* getAppVersion() { |
| 79 return debug_app_version; |
| 80 } |
| 81 |
| 82 char debug_cdb_path[MAX_PATH] = ""; |
| 83 void setCdbPath(const char* path) { |
| 84 strncpyOrSetBlank(debug_cdb_path, path, sizeof(debug_cdb_path)); |
| 85 } |
| 86 |
| 87 const char* getCdbPath() { |
| 88 return debug_cdb_path; |
| 89 } |
| 90 |
| 91 /** Print all the lines of a CDB k command whicha are callstacks. |
| 92 * Callstack lines are marked by start and stop markers and they are prefixed |
| 93 * byt 2 hex adresses, which will not be reported. |
| 94 */ |
| 95 static void printCallstack(const char* filename, |
| 96 const char* start, |
| 97 const char* stop) { |
| 98 FILE* file = fopen(filename, "rt"); |
| 99 char line[1000]; |
| 100 bool started = false; |
| 101 // Not the most performant code, but this will be used only to collect |
| 102 // the callstack from a log files, only when the application had failed. |
| 103 while (fgets(line, sizeof(line), file)) { |
| 104 if (!started && strncmp(start, line, strlen(start)) == 0) { |
| 105 started = true; |
| 106 } else if (started && strncmp(stop, line, strlen(stop)) == 0) { |
| 107 break; |
| 108 } else if (started) { |
| 109 // Filter messages. Calstack lines contain "exe/dll!function" |
| 110 if (strchr(line, '!') != NULL && strlen(line) > CDB_CALLSTACK_PREFIX
) { |
| 111 printf("%s", line + CDB_CALLSTACK_PREFIX); // fgets includes \n
already. |
| 112 } |
| 113 } |
| 114 } |
| 115 fclose(file); |
| 116 } |
| 117 |
| 118 #define BUILD_UNIQUE_FILENAME(var, ext, szPath, szAppName, szVersion, stLocalTim
e) \ |
| 119 sprintf(szFileName, "%s%s\\%s-%04d%02d%02d-%02d%02d%02d-%ld-%ld" ext, \ |
| 120 szPath, szAppName, szVersion, \ |
| 121 stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay, \ |
| 122 stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond, \ |
| 123 GetCurrentProcessId(), GetCurrentThreadId()); |
| 124 |
| 125 // Exception execution handler. Exception is recognized. Transfer control to |
| 126 // the exception handler by executing the __except compound statement, |
| 127 // then continue execution after the __except block. |
| 128 int GenerateDumpAndPrintCallstack(EXCEPTION_POINTERS* pExceptionPointers) { |
| 129 BOOL bMiniDumpSuccessful; |
| 130 char szPath[MAX_PATH]; |
| 131 char szFileName[MAX_PATH]; |
| 132 char szFileNameOutput[MAX_PATH]; |
| 133 const char* szAppName = getAppName(); |
| 134 const char* szVersion = getAppVersion(); |
| 135 DWORD dwBufferSize = MAX_PATH; |
| 136 HANDLE hDumpFile; |
| 137 SYSTEMTIME stLocalTime; |
| 138 MINIDUMP_EXCEPTION_INFORMATION ExpParam; |
| 139 |
| 140 GetLocalTime( &stLocalTime ); |
| 141 GetTempPath( dwBufferSize, szPath ); |
| 142 |
| 143 sprintf( szFileName, "%s%s", szPath, szAppName ); |
| 144 CreateDirectory( szFileName, NULL ); |
| 145 |
| 146 BUILD_UNIQUE_FILENAME(szFileName, ".dmp", szPath, szAppName, szVersion, stLo
calTime); |
| 147 BUILD_UNIQUE_FILENAME(szFileNameOutput, ".out", szPath, szAppName, szVersion
, stLocalTime); |
| 148 |
| 149 hDumpFile = CreateFile(szFileName, |
| 150 GENERIC_READ|GENERIC_WRITE, |
| 151 FILE_SHARE_WRITE|FILE_SHARE_READ, |
| 152 0, |
| 153 CREATE_ALWAYS, |
| 154 0, |
| 155 0); |
| 156 |
| 157 ExpParam.ThreadId = GetCurrentThreadId(); |
| 158 ExpParam.ExceptionPointers = pExceptionPointers; |
| 159 ExpParam.ClientPointers = TRUE; |
| 160 |
| 161 bMiniDumpSuccessful = MiniDumpWriteDump(GetCurrentProcess(), |
| 162 GetCurrentProcessId(), |
| 163 hDumpFile, |
| 164 MiniDumpWithDataSegs, |
| 165 &ExpParam, |
| 166 NULL, |
| 167 NULL); |
| 168 |
| 169 printf("MiniDump file: %s\n", szFileName); |
| 170 printf("App exe and pdb: %s\n", getBinariesPath()); |
| 171 |
| 172 const char* cdbExePath = getCdbPath(); |
| 173 if (cdbExePath && *cdbExePath != '\0') { |
| 174 printf("Cdb exe: %s\n", cdbExePath); |
| 175 |
| 176 char command[MAX_PATH * 4]; |
| 177 sprintf(command, "%s -y \"%s\" -i \"%s\" -z \"%s\" -c \"%s\" -kqm >\"%s\
"", |
| 178 cdbExePath, |
| 179 getBinariesPath(), |
| 180 getBinariesPath(), |
| 181 szFileName, |
| 182 CDB_PRINT_CALLSTACK_CURRENT_THREAD, |
| 183 szFileNameOutput); |
| 184 system(command); |
| 185 |
| 186 printf("\nThread Callstack:\n"); |
| 187 printCallstack(szFileNameOutput, |
| 188 MARKER_THREAD_CALLSTACK_START, |
| 189 MARKER_THREAD_CALLSTACK_STOP); |
| 190 |
| 191 printf("\nException Callstack:\n"); |
| 192 printCallstack(szFileNameOutput, |
| 193 MARKER_EXCEPTION_CALLSTACK_START, |
| 194 MARKER_EXCEPTION_CALLSTACK_STOP); |
| 195 } else { |
| 196 printf("Warning: CDB path not set up.\n"); |
| 197 } |
| 198 |
| 199 return EXCEPTION_EXECUTE_HANDLER; |
| 200 } |
| 201 |
| 202 /** Sets the debugging variables. Input parameter is app location. |
| 203 * e.g out\Debug\render_pdfs.exe |
| 204 * This function expects the .pdb file to be in the same directory. |
| 205 */ |
| 206 void setUpDebuggingFromArgs(const char* vargs0) { |
| 207 int i = strlen(vargs0); |
| 208 |
| 209 if (i >= 4 && stricmp(vargs0 - 4, ".exe") == 0) { |
| 210 // Ignore .exe |
| 211 i -= 4; |
| 212 } |
| 213 |
| 214 int pos_period = i; |
| 215 |
| 216 // Find last \ in path - this is Windows! |
| 217 while (i >= 0 && vargs0[i] != '\\') { |
| 218 i--; |
| 219 } |
| 220 |
| 221 int pos_last_slash = i; |
| 222 |
| 223 char app_name[MAX_PATH]; |
| 224 strncpy(app_name, vargs0 + pos_last_slash + 1, pos_period - pos_last_slash -
1); |
| 225 app_name[pos_period - pos_last_slash] = '\0'; |
| 226 setAppName(app_name); |
| 227 |
| 228 char binaries_path[MAX_PATH]; |
| 229 strncpy(binaries_path, vargs0, pos_last_slash); |
| 230 binaries_path[pos_last_slash] = '\0'; |
| 231 setBinariesPath(binaries_path); |
| 232 |
| 233 setAppVersion("1.0"); // Dummy for now, but use revision instead if we use |
| 234 // the minidump for anything else other than |
| 235 // collecting the callstack. |
| 236 |
| 237 // cdb.exe is the app used to load the minidump which prints the callstack. |
| 238 char cdbExePath[MAX_PATH]; |
| 239 #ifdef _WIN64 |
| 240 sprintf(cdbExePath, "%s\\x64\\cdb.exe", SK_CDB_PATH); |
| 241 #else |
| 242 sprintf(cdbExePath, "%s\\cdb.exe", SK_CDB_PATH); |
| 243 #endif |
| 244 setCdbPath(cdbExePath); |
| 245 } |
| 246 |
| 247 #endif // SK_BUILD_FOR_WIN32 |
OLD | NEW |