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