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 // CDB.EXE prints a lot of garbage and there is no argument to pass which | |
18 // would remove all that noise. | |
19 // Using eval feature that evaluates a number in hexa and prints it to stdout | |
epoger
2013/03/07 21:09:09
hexa -> hex
edisonn
2013/03/08 15:50:57
Done.
| |
20 // to mark where the callstack is printed. | |
21 | |
22 #define MARKER_THREAD_CALLSTACK_START_NUMBER "12340000" | |
epoger
2013/03/07 21:09:09
So I guess you just made up these numbers (1234000
edisonn
2013/03/08 15:50:57
Done.
| |
23 #define MARKER_THREAD_CALLSTACK_START "Evaluate expression: 305397760 = 12340000 " | |
24 | |
25 #define MARKER_THREAD_CALLSTACK_STOP_NUMBER "12340001" | |
26 #define MARKER_THREAD_CALLSTACK_STOP "Evaluate expression: 305397761 = 12340001" | |
27 | |
28 #define MARKER_EXCEPTION_CALLSTACK_START_NUMBER "12340002" | |
29 #define MARKER_EXCEPTION_CALLSTACK_START "Evaluate expression: 305397762 = 12340 002" | |
30 | |
31 #define MARKER_EXCEPTION_CALLSTACK_STOP_NUMBER "12340003" | |
32 #define MARKER_EXCEPTION_CALLSTACK_STOP "Evaluate expression: 305397763 = 123400 03" | |
33 | |
34 // k - print stack | |
35 // ? val - evaluarte expression. Used to mark the log file. | |
epoger
2013/03/07 21:09:09
evaluarte -> evaluate
edisonn
2013/03/08 15:50:57
Done.
| |
36 // .ecxr - load exception, if exception was thrown. | |
37 // k - print the resolved stack by .ecxr | |
38 // q - quit cdb.exe | |
39 #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" | |
40 | |
41 char debug_app_name[MAX_PATH] = ""; | |
42 void setAppName(const char* app_name) { | |
43 if (app_name == NULL) { | |
44 app_name = ""; | |
45 } | |
46 strcpy(debug_app_name, app_name); | |
epoger
2013/03/07 21:09:09
throughout this file: please use stncpy to avoid o
edisonn
2013/03/08 15:50:57
Done.
| |
47 } | |
48 | |
49 const char* getAppName() { | |
50 return debug_app_name; | |
51 } | |
52 | |
53 char debug_binaries_path[MAX_PATH] = ""; | |
54 void setBinariesPath(const char* binaries_path) { | |
55 if (binaries_path == NULL) { | |
56 binaries_path = ""; | |
57 } | |
58 strcpy(debug_binaries_path, binaries_path); | |
59 } | |
60 | |
61 const char* getBinariesPath() { | |
62 return debug_binaries_path; | |
63 } | |
64 | |
65 char debug_app_version[100] = ""; | |
66 void setAppVersion(const char* version) { | |
67 if (version == NULL) { | |
68 version = ""; | |
69 } | |
70 strcpy(debug_app_version, version); | |
71 } | |
72 | |
73 const char* getAppVersion() { | |
74 return debug_app_version; | |
75 } | |
76 | |
77 char debug_cdb_path[MAX_PATH] = ""; | |
78 void setCdbPath(const char* path) { | |
79 if (path == NULL) { | |
80 path = ""; | |
81 } | |
82 strcpy(debug_cdb_path, path); | |
83 } | |
84 | |
85 const char* getCdbPath() { | |
86 return debug_cdb_path; | |
87 } | |
88 | |
89 // Not the most performant code, but this will be used only to collect | |
90 // the callstack from a log files, only when the application had failed. | |
epoger
2013/03/07 21:09:09
Please give more information about WHAT this funct
edisonn
2013/03/08 15:50:57
Done.
| |
91 static void printCallstack(const char* filename, | |
92 const char* start, | |
93 const char* stop) { | |
94 FILE* file = fopen(filename, "rt"); | |
95 char line[1000]; | |
96 bool started = false; | |
97 while (fgets(line, sizeof(line), file)) { | |
epoger
2013/03/07 21:09:09
Do we need to worry about lines longer than 1000 c
edisonn
2013/03/08 15:50:57
We will print the frame truncated, or not print it
| |
98 if (!started && strncmp(start, line, strlen(start)) == 0) { | |
99 started = true; | |
100 } else if (started && strncmp(stop, line, strlen(stop)) == 0) { | |
101 break; | |
102 } else if (started) { | |
103 // Filter messages. Calstack lines contain "exe/dll!function" | |
epoger
2013/03/07 21:09:09
Calstack -> Callstack
More importantly, I'm confu
edisonn
2013/03/08 15:50:57
lines are like "ffffeeee ffffdddd render_pdf!proce
| |
104 if (strchr(line, '!') != NULL && strlen(line) > 18) { | |
105 // Remove addresses. 18 = 2 * (8 digit hexa + 1 space). | |
106 printf("%s", line + 18); // fgets includes \n already. | |
107 } | |
108 } | |
109 } | |
110 fclose(file); | |
111 } | |
112 | |
113 int GenerateDumpAndPrintCallstack(EXCEPTION_POINTERS* pExceptionPointers) { | |
epoger
2013/03/07 21:09:09
please document meaning of return value
edisonn
2013/03/08 15:50:57
Done.
| |
114 BOOL bMiniDumpSuccessful; | |
115 char szPath[MAX_PATH]; | |
116 char szFileName[MAX_PATH]; | |
117 char szFileNameOutput[MAX_PATH]; | |
118 const char* szAppName = getAppName(); | |
119 const char* szVersion = getAppVersion(); | |
120 DWORD dwBufferSize = MAX_PATH; | |
121 HANDLE hDumpFile; | |
122 SYSTEMTIME stLocalTime; | |
123 MINIDUMP_EXCEPTION_INFORMATION ExpParam; | |
124 | |
125 GetLocalTime( &stLocalTime ); | |
126 GetTempPath( dwBufferSize, szPath ); | |
127 | |
128 sprintf( szFileName, "%s%s", szPath, szAppName ); | |
129 CreateDirectory( szFileName, NULL ); | |
130 | |
131 sprintf( szFileName, "%s%s\\%s-%04d%02d%02d-%02d%02d%02d-%ld-%ld.dmp", | |
132 szPath, szAppName, szVersion, | |
133 stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay, | |
134 stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond, | |
135 GetCurrentProcessId(), GetCurrentThreadId()); | |
136 | |
137 sprintf( szFileNameOutput, "%s%s\\%s-%04d%02d%02d-%02d%02d%02d-%ld-%ld.out", | |
epoger
2013/03/07 21:09:09
It looks like this formatting string has a lot in
edisonn
2013/03/08 15:50:57
Done.
| |
138 szPath, szAppName, szVersion, | |
139 stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay, | |
140 stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond, | |
141 GetCurrentProcessId(), GetCurrentThreadId()); | |
142 | |
143 hDumpFile = CreateFile(szFileName, | |
144 GENERIC_READ|GENERIC_WRITE, | |
145 FILE_SHARE_WRITE|FILE_SHARE_READ, | |
146 0, | |
147 CREATE_ALWAYS, | |
148 0, | |
149 0); | |
150 | |
151 ExpParam.ThreadId = GetCurrentThreadId(); | |
152 ExpParam.ExceptionPointers = pExceptionPointers; | |
153 ExpParam.ClientPointers = TRUE; | |
154 | |
155 bMiniDumpSuccessful = MiniDumpWriteDump(GetCurrentProcess(), | |
156 GetCurrentProcessId(), | |
157 hDumpFile, | |
158 MiniDumpWithDataSegs, | |
159 &ExpParam, | |
160 NULL, | |
161 NULL); | |
162 | |
163 printf("MiniDump file: %s\n", szFileName); | |
164 printf("App exe and pdb: %s\n", getBinariesPath()); | |
165 | |
166 const char* cdbExePath = getCdbPath(); | |
167 if (cdbExePath && *cdbExePath != '\0') { | |
168 printf("Cdb exe: %s\n", cdbExePath); | |
169 | |
170 char command[MAX_PATH * 4]; | |
171 sprintf(command, "%s -y \"%s\" -i \"%s\" -z \"%s\" -c \"%s\" -kqm >\"%s\ "", | |
172 cdbExePath, | |
173 getBinariesPath(), | |
174 getBinariesPath(), | |
175 szFileName, | |
176 CDB_PRINT_CALLSTACK_CURRENT_THREAD, | |
177 szFileNameOutput); | |
178 system(command); | |
179 | |
180 printf("\nThread Callstack:\n"); | |
181 printCallstack(szFileNameOutput, | |
182 MARKER_THREAD_CALLSTACK_START, | |
183 MARKER_THREAD_CALLSTACK_STOP); | |
184 | |
185 printf("\nException Callstack:\n"); | |
186 printCallstack(szFileNameOutput, | |
187 MARKER_EXCEPTION_CALLSTACK_START, | |
188 MARKER_EXCEPTION_CALLSTACK_STOP); | |
189 } else { | |
190 printf("Warning: CDB path not set up.\n"); | |
191 } | |
192 | |
193 return EXCEPTION_EXECUTE_HANDLER; | |
194 } | |
195 | |
196 /** Sets the debugging variables. Input parameter is app location. | |
197 * e.g out\Debug\render_pdfs.exe | |
198 * This function expects the .pdb file to be in the same directory. | |
199 */ | |
200 void setUpDebuggingFromArgs(const char* vargs0) { | |
201 int i = strlen(vargs0); | |
202 | |
203 if (i >= 4 && | |
204 (vargs0[i - 1] == 'E' || vargs0[i - 1] == 'e') && | |
epoger
2013/03/07 21:09:09
on windows, I think you can use stricmp()
http://
edisonn
2013/03/08 15:50:57
Done.
| |
205 (vargs0[i - 2] == 'X' || vargs0[i - 2] == 'x') && | |
206 (vargs0[i - 3] == 'E' || vargs0[i - 3] == 'e') && | |
207 vargs0[i - 4] == '.') { | |
208 // Ignore .exe | |
209 i -= 4; | |
210 } | |
211 | |
212 int pos_period = i; | |
213 | |
214 // Find last \ in path - this is Windows! | |
215 while (i >= 0 && vargs0[i] != '\\') { | |
216 i--; | |
217 } | |
218 | |
219 int pos_last_slash = i; | |
220 | |
221 char app_name[MAX_PATH]; | |
222 strncpy(app_name, vargs0 + pos_last_slash + 1, pos_period - pos_last_slash - 1); | |
223 app_name[pos_period - pos_last_slash] = '\0'; | |
224 setAppName(app_name); | |
225 | |
226 char binaries_path[MAX_PATH]; | |
227 strncpy(binaries_path, vargs0, pos_last_slash); | |
228 binaries_path[pos_last_slash] = '\0'; | |
229 setBinariesPath(binaries_path); | |
230 | |
231 setAppVersion("1.0"); // Dummy for now, but use revision instead if we use | |
232 // the minidump for anything else other than | |
233 // collecting the callstack. | |
234 | |
235 // cdb.exe is the app used to load the minidump which prints the callstack. | |
236 char cdbExePath[MAX_PATH]; | |
237 #ifdef _WIN64 | |
238 sprintf(cdbExePath, "%s\\x64\\cdb.exe", SK_CDB_PATH); | |
239 #else | |
240 sprintf(cdbExePath, "%s\\cdb.exe", SK_CDB_PATH); | |
241 #endif | |
242 setCdbPath(cdbExePath); | |
243 } | |
244 | |
245 #endif // SK_BUILD_FOR_WIN32 | |
OLD | NEW |