|
1 // Copyright (c) 2006, Google Inc. |
|
2 // All rights reserved. |
|
3 // |
|
4 // Redistribution and use in source and binary forms, with or without |
|
5 // modification, are permitted provided that the following conditions are |
|
6 // met: |
|
7 // |
|
8 // * Redistributions of source code must retain the above copyright |
|
9 // notice, this list of conditions and the following disclaimer. |
|
10 // * Redistributions in binary form must reproduce the above |
|
11 // copyright notice, this list of conditions and the following disclaimer |
|
12 // in the documentation and/or other materials provided with the |
|
13 // distribution. |
|
14 // * Neither the name of Google Inc. nor the names of its |
|
15 // contributors may be used to endorse or promote products derived from |
|
16 // this software without specific prior written permission. |
|
17 // |
|
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 |
|
30 // Tool to upload an exe/dll and its associated symbols to an HTTP server. |
|
31 // The PDB file is located automatically, using the path embedded in the |
|
32 // executable. The upload is sent as a multipart/form-data POST request, |
|
33 // with the following parameters: |
|
34 // code_file: the basename of the module, e.g. "app.exe" |
|
35 // debug_file: the basename of the debugging file, e.g. "app.pdb" |
|
36 // debug_identifier: the debug file's identifier, usually consisting of |
|
37 // the guid and age embedded in the pdb, e.g. |
|
38 // "11111111BBBB3333DDDD555555555555F" |
|
39 // version: the file version of the module, e.g. "1.2.3.4" |
|
40 // os: the operating system that the module was built for, always |
|
41 // "windows" in this implementation. |
|
42 // cpu: the CPU that the module was built for, typically "x86". |
|
43 // symbol_file: the contents of the breakpad-format symbol file |
|
44 |
|
45 #include <Windows.h> |
|
46 #include <DbgHelp.h> |
|
47 #include <WinInet.h> |
|
48 |
|
49 #include <cstdio> |
|
50 #include <map> |
|
51 #include <string> |
|
52 #include <vector> |
|
53 |
|
54 #include "common/windows/string_utils-inl.h" |
|
55 |
|
56 #include "common/windows/http_upload.h" |
|
57 #include "common/windows/pdb_source_line_writer.h" |
|
58 |
|
59 using std::string; |
|
60 using std::wstring; |
|
61 using std::vector; |
|
62 using std::map; |
|
63 using google_breakpad::HTTPUpload; |
|
64 using google_breakpad::PDBModuleInfo; |
|
65 using google_breakpad::PDBSourceLineWriter; |
|
66 using google_breakpad::WindowsStringUtils; |
|
67 |
|
68 // Extracts the file version information for the given filename, |
|
69 // as a string, for example, "1.2.3.4". Returns true on success. |
|
70 static bool GetFileVersionString(const wchar_t *filename, wstring *version) { |
|
71 DWORD handle; |
|
72 DWORD version_size = GetFileVersionInfoSize(filename, &handle); |
|
73 if (version_size < sizeof(VS_FIXEDFILEINFO)) { |
|
74 return false; |
|
75 } |
|
76 |
|
77 vector<char> version_info(version_size); |
|
78 if (!GetFileVersionInfo(filename, handle, version_size, &version_info[0])) { |
|
79 return false; |
|
80 } |
|
81 |
|
82 void *file_info_buffer = NULL; |
|
83 unsigned int file_info_length; |
|
84 if (!VerQueryValue(&version_info[0], L"\\", |
|
85 &file_info_buffer, &file_info_length)) { |
|
86 return false; |
|
87 } |
|
88 |
|
89 // The maximum value of each version component is 65535 (0xffff), |
|
90 // so the max length is 24, including the terminating null. |
|
91 wchar_t ver_string[24]; |
|
92 VS_FIXEDFILEINFO *file_info = |
|
93 reinterpret_cast<VS_FIXEDFILEINFO*>(file_info_buffer); |
|
94 swprintf(ver_string, sizeof(ver_string) / sizeof(ver_string[0]), |
|
95 L"%d.%d.%d.%d", |
|
96 file_info->dwFileVersionMS >> 16, |
|
97 file_info->dwFileVersionMS & 0xffff, |
|
98 file_info->dwFileVersionLS >> 16, |
|
99 file_info->dwFileVersionLS & 0xffff); |
|
100 |
|
101 // remove when VC++7.1 is no longer supported |
|
102 ver_string[sizeof(ver_string) / sizeof(ver_string[0]) - 1] = L'\0'; |
|
103 |
|
104 *version = ver_string; |
|
105 return true; |
|
106 } |
|
107 |
|
108 // Creates a new temporary file and writes the symbol data from the given |
|
109 // exe/dll file to it. Returns the path to the temp file in temp_file_path |
|
110 // and information about the pdb in pdb_info. |
|
111 static bool DumpSymbolsToTempFile(const wchar_t *file, |
|
112 wstring *temp_file_path, |
|
113 PDBModuleInfo *pdb_info) { |
|
114 google_breakpad::PDBSourceLineWriter writer; |
|
115 // Use EXE_FILE to get information out of the exe/dll in addition to the |
|
116 // pdb. The name and version number of the exe/dll are of value, and |
|
117 // there's no way to locate an exe/dll given a pdb. |
|
118 if (!writer.Open(file, PDBSourceLineWriter::EXE_FILE)) { |
|
119 return false; |
|
120 } |
|
121 |
|
122 wchar_t temp_path[_MAX_PATH]; |
|
123 if (GetTempPath(_MAX_PATH, temp_path) == 0) { |
|
124 return false; |
|
125 } |
|
126 |
|
127 wchar_t temp_filename[_MAX_PATH]; |
|
128 if (GetTempFileName(temp_path, L"sym", 0, temp_filename) == 0) { |
|
129 return false; |
|
130 } |
|
131 |
|
132 FILE *temp_file = NULL; |
|
133 #if _MSC_VER >= 1400 // MSVC 2005/8 |
|
134 if (_wfopen_s(&temp_file, temp_filename, L"w") != 0) |
|
135 #else // _MSC_VER >= 1400 |
|
136 // _wfopen_s was introduced in MSVC8. Use _wfopen for earlier environments. |
|
137 // Don't use it with MSVC8 and later, because it's deprecated. |
|
138 if (!(temp_file = _wfopen(temp_filename, L"w"))) |
|
139 #endif // _MSC_VER >= 1400 |
|
140 { |
|
141 return false; |
|
142 } |
|
143 |
|
144 bool success = writer.WriteMap(temp_file); |
|
145 fclose(temp_file); |
|
146 if (!success) { |
|
147 _wunlink(temp_filename); |
|
148 return false; |
|
149 } |
|
150 |
|
151 *temp_file_path = temp_filename; |
|
152 |
|
153 return writer.GetModuleInfo(pdb_info); |
|
154 } |
|
155 |
|
156 void printUsageAndExit() { |
|
157 wprintf(L"Usage: symupload [--timeout NN] <file.exe|file.dll> <symbol upload URL>\n\n"); |
|
158 wprintf(L"Timeout is in milliseconds, or can be 0 to be unlimited\n\n"); |
|
159 wprintf(L"Example:\n\n\tsymupload.exe --timeout 0 chrome.dll http://no.free.symbol.server.for.you\n"); |
|
160 exit(0); |
|
161 } |
|
162 int wmain(int argc, wchar_t *argv[]) { |
|
163 if ((argc != 3) && |
|
164 (argc != 5)) { |
|
165 printUsageAndExit(); |
|
166 } |
|
167 |
|
168 const wchar_t *module, *url; |
|
169 int timeout = -1; |
|
170 if (argc == 3) { |
|
171 module = argv[1]; |
|
172 url = argv[2]; |
|
173 } else { |
|
174 // check for timeout flag |
|
175 if (!wcscmp(L"--timeout", argv[1])) { |
|
176 timeout = _wtoi(argv[2]); |
|
177 module = argv[3]; |
|
178 url = argv[4]; |
|
179 } else { |
|
180 printUsageAndExit(); |
|
181 } |
|
182 } |
|
183 |
|
184 wstring symbol_file; |
|
185 PDBModuleInfo pdb_info; |
|
186 if (!DumpSymbolsToTempFile(module, &symbol_file, &pdb_info)) { |
|
187 fwprintf(stderr, L"Could not get symbol data from %s\n", module); |
|
188 return 1; |
|
189 } |
|
190 |
|
191 wstring code_file = WindowsStringUtils::GetBaseName(wstring(module)); |
|
192 |
|
193 map<wstring, wstring> parameters; |
|
194 parameters[L"code_file"] = code_file; |
|
195 parameters[L"debug_file"] = pdb_info.debug_file; |
|
196 parameters[L"debug_identifier"] = pdb_info.debug_identifier; |
|
197 parameters[L"os"] = L"windows"; // This version of symupload is Windows-only |
|
198 parameters[L"cpu"] = pdb_info.cpu; |
|
199 |
|
200 // Don't make a missing version a hard error. Issue a warning, and let the |
|
201 // server decide whether to reject files without versions. |
|
202 wstring file_version; |
|
203 if (GetFileVersionString(module, &file_version)) { |
|
204 parameters[L"version"] = file_version; |
|
205 } else { |
|
206 fwprintf(stderr, L"Warning: Could not get file version for %s\n", module); |
|
207 } |
|
208 |
|
209 bool success = HTTPUpload::SendRequest(url, parameters, |
|
210 symbol_file, L"symbol_file", |
|
211 timeout == -1 ? NULL : &timeout, |
|
212 NULL, NULL); |
|
213 _wunlink(symbol_file.c_str()); |
|
214 |
|
215 if (!success) { |
|
216 fwprintf(stderr, L"Symbol file upload failed\n"); |
|
217 return 1; |
|
218 } |
|
219 |
|
220 wprintf(L"Uploaded symbols for windows-%s/%s/%s (%s %s)\n", |
|
221 pdb_info.cpu.c_str(), pdb_info.debug_file.c_str(), |
|
222 pdb_info.debug_identifier.c_str(), code_file.c_str(), |
|
223 file_version.c_str()); |
|
224 return 0; |
|
225 } |