• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //  product: the HTTP-friendly product name, e.g. "MyApp"
40 //  version: the file version of the module, e.g. "1.2.3.4"
41 //  os: the operating system that the module was built for, always
42 //      "windows" in this implementation.
43 //  cpu: the CPU that the module was built for, typically "x86".
44 //  symbol_file: the contents of the breakpad-format symbol file
45 
46 #include <windows.h>
47 #include <dbghelp.h>
48 #include <wininet.h>
49 
50 #include <cstdio>
51 #include <map>
52 #include <string>
53 #include <vector>
54 
55 #include "common/windows/string_utils-inl.h"
56 
57 #include "common/windows/http_upload.h"
58 #include "common/windows/pdb_source_line_writer.h"
59 
60 using std::string;
61 using std::wstring;
62 using std::vector;
63 using std::map;
64 using google_breakpad::HTTPUpload;
65 using google_breakpad::PDBModuleInfo;
66 using google_breakpad::PDBSourceLineWriter;
67 using google_breakpad::WindowsStringUtils;
68 
69 // Extracts the file version information for the given filename,
70 // as a string, for example, "1.2.3.4".  Returns true on success.
GetFileVersionString(const wchar_t * filename,wstring * version)71 static bool GetFileVersionString(const wchar_t *filename, wstring *version) {
72   DWORD handle;
73   DWORD version_size = GetFileVersionInfoSize(filename, &handle);
74   if (version_size < sizeof(VS_FIXEDFILEINFO)) {
75     return false;
76   }
77 
78   vector<char> version_info(version_size);
79   if (!GetFileVersionInfo(filename, handle, version_size, &version_info[0])) {
80     return false;
81   }
82 
83   void *file_info_buffer = NULL;
84   unsigned int file_info_length;
85   if (!VerQueryValue(&version_info[0], L"\\",
86                      &file_info_buffer, &file_info_length)) {
87     return false;
88   }
89 
90   // The maximum value of each version component is 65535 (0xffff),
91   // so the max length is 24, including the terminating null.
92   wchar_t ver_string[24];
93   VS_FIXEDFILEINFO *file_info =
94     reinterpret_cast<VS_FIXEDFILEINFO*>(file_info_buffer);
95   swprintf(ver_string, sizeof(ver_string) / sizeof(ver_string[0]),
96            L"%d.%d.%d.%d",
97            file_info->dwFileVersionMS >> 16,
98            file_info->dwFileVersionMS & 0xffff,
99            file_info->dwFileVersionLS >> 16,
100            file_info->dwFileVersionLS & 0xffff);
101 
102   // remove when VC++7.1 is no longer supported
103   ver_string[sizeof(ver_string) / sizeof(ver_string[0]) - 1] = L'\0';
104 
105   *version = ver_string;
106   return true;
107 }
108 
109 // Creates a new temporary file and writes the symbol data from the given
110 // exe/dll file to it.  Returns the path to the temp file in temp_file_path
111 // and information about the pdb in pdb_info.
DumpSymbolsToTempFile(const wchar_t * file,wstring * temp_file_path,PDBModuleInfo * pdb_info)112 static bool DumpSymbolsToTempFile(const wchar_t *file,
113                                   wstring *temp_file_path,
114                                   PDBModuleInfo *pdb_info) {
115   google_breakpad::PDBSourceLineWriter writer;
116   // Use EXE_FILE to get information out of the exe/dll in addition to the
117   // pdb.  The name and version number of the exe/dll are of value, and
118   // there's no way to locate an exe/dll given a pdb.
119   if (!writer.Open(file, PDBSourceLineWriter::EXE_FILE)) {
120     return false;
121   }
122 
123   wchar_t temp_path[_MAX_PATH];
124   if (GetTempPath(_MAX_PATH, temp_path) == 0) {
125     return false;
126   }
127 
128   wchar_t temp_filename[_MAX_PATH];
129   if (GetTempFileName(temp_path, L"sym", 0, temp_filename) == 0) {
130     return false;
131   }
132 
133   FILE *temp_file = NULL;
134 #if _MSC_VER >= 1400  // MSVC 2005/8
135   if (_wfopen_s(&temp_file, temp_filename, L"w") != 0)
136 #else  // _MSC_VER >= 1400
137   // _wfopen_s was introduced in MSVC8.  Use _wfopen for earlier environments.
138   // Don't use it with MSVC8 and later, because it's deprecated.
139   if (!(temp_file = _wfopen(temp_filename, L"w")))
140 #endif  // _MSC_VER >= 1400
141   {
142     return false;
143   }
144 
145   bool success = writer.WriteMap(temp_file);
146   fclose(temp_file);
147   if (!success) {
148     _wunlink(temp_filename);
149     return false;
150   }
151 
152   *temp_file_path = temp_filename;
153 
154   return writer.GetModuleInfo(pdb_info);
155 }
156 
printUsageAndExit()157 __declspec(noreturn) void printUsageAndExit() {
158   wprintf(L"Usage:\n\n"
159           L"    symupload [--timeout NN] [--product product_name] ^\n"
160           L"              <file.exe|file.dll> <symbol upload URL> ^\n"
161           L"              [...<symbol upload URLs>]\n\n");
162   wprintf(L"  - Timeout is in milliseconds, or can be 0 to be unlimited.\n");
163   wprintf(L"  - product_name is an HTTP-friendly product name. It must only\n"
164           L"    contain an ascii subset: alphanumeric and punctuation.\n"
165           L"    This string is case-sensitive.\n\n");
166   wprintf(L"Example:\n\n"
167           L"    symupload.exe --timeout 0 --product Chrome ^\n"
168           L"        chrome.dll http://no.free.symbol.server.for.you\n");
169   exit(0);
170 }
wmain(int argc,wchar_t * argv[])171 int wmain(int argc, wchar_t *argv[]) {
172   const wchar_t *module;
173   const wchar_t *product = nullptr;
174   int timeout = -1;
175   int currentarg = 1;
176   while (argc > currentarg + 1) {
177     if (!wcscmp(L"--timeout", argv[currentarg])) {
178       timeout = _wtoi(argv[currentarg + 1]);
179       currentarg += 2;
180       continue;
181     }
182     if (!wcscmp(L"--product", argv[currentarg])) {
183       product = argv[currentarg + 1];
184       currentarg += 2;
185       continue;
186     }
187     break;
188   }
189 
190   if (argc >= currentarg + 2)
191     module = argv[currentarg++];
192   else
193     printUsageAndExit();
194 
195   wstring symbol_file;
196   PDBModuleInfo pdb_info;
197   if (!DumpSymbolsToTempFile(module, &symbol_file, &pdb_info)) {
198     fwprintf(stderr, L"Could not get symbol data from %s\n", module);
199     return 1;
200   }
201 
202   wstring code_file = WindowsStringUtils::GetBaseName(wstring(module));
203 
204   map<wstring, wstring> parameters;
205   parameters[L"code_file"] = code_file;
206   parameters[L"debug_file"] = pdb_info.debug_file;
207   parameters[L"debug_identifier"] = pdb_info.debug_identifier;
208   parameters[L"os"] = L"windows";  // This version of symupload is Windows-only
209   parameters[L"cpu"] = pdb_info.cpu;
210 
211   // Don't make a missing product name a hard error.  Issue a warning and let
212   // the server decide whether to reject files without product name.
213   if (product) {
214     parameters[L"product"] = product;
215   } else {
216     fwprintf(
217         stderr,
218         L"Warning: No product name (flag --product) was specified for %s\n",
219         module);
220   }
221 
222   // Don't make a missing version a hard error.  Issue a warning, and let the
223   // server decide whether to reject files without versions.
224   wstring file_version;
225   if (GetFileVersionString(module, &file_version)) {
226     parameters[L"version"] = file_version;
227   } else {
228     fwprintf(stderr, L"Warning: Could not get file version for %s\n", module);
229   }
230 
231   bool success = true;
232 
233   while (currentarg < argc) {
234     int response_code;
235     if (!HTTPUpload::SendRequest(argv[currentarg], parameters,
236                                  symbol_file, L"symbol_file",
237                                  timeout == -1 ? NULL : &timeout,
238                                  nullptr, &response_code)) {
239       success = false;
240       fwprintf(stderr,
241                L"Symbol file upload to %s failed. Response code = %ld\n",
242                argv[currentarg], response_code);
243     }
244     currentarg++;
245   }
246 
247   _wunlink(symbol_file.c_str());
248 
249   if (success) {
250     wprintf(L"Uploaded symbols for windows-%s/%s/%s (%s %s)\n",
251             pdb_info.cpu.c_str(), pdb_info.debug_file.c_str(),
252             pdb_info.debug_identifier.c_str(), code_file.c_str(),
253             file_version.c_str());
254   }
255 
256   return success ? 0 : 1;
257 }
258