1 // Copyright (c) 2006, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
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
54 #include "common/windows/string_utils-inl.h"
56 #include "common/windows/http_upload.h"
57 #include "common/windows/pdb_source_line_writer.h"
63 using google_breakpad::HTTPUpload;
64 using google_breakpad::PDBModuleInfo;
65 using google_breakpad::PDBSourceLineWriter;
66 using google_breakpad::WindowsStringUtils;
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) {
72 DWORD version_size = GetFileVersionInfoSize(filename, &handle);
73 if (version_size < sizeof(VS_FIXEDFILEINFO)) {
77 vector<char> version_info(version_size);
78 if (!GetFileVersionInfo(filename, handle, version_size, &version_info[0])) {
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)) {
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]),
96 file_info->dwFileVersionMS >> 16,
97 file_info->dwFileVersionMS & 0xffff,
98 file_info->dwFileVersionLS >> 16,
99 file_info->dwFileVersionLS & 0xffff);
101 // remove when VC++7.1 is no longer supported
102 ver_string[sizeof(ver_string) / sizeof(ver_string[0]) - 1] = L'\0';
104 *version = ver_string;
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)) {
122 wchar_t temp_path[_MAX_PATH];
123 if (GetTempPath(_MAX_PATH, temp_path) == 0) {
127 wchar_t temp_filename[_MAX_PATH];
128 if (GetTempFileName(temp_path, L"sym", 0, temp_filename) == 0) {
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
144 bool success = writer.WriteMap(temp_file);
147 _wunlink(temp_filename);
151 *temp_file_path = temp_filename;
153 return writer.GetModuleInfo(pdb_info);
156 __declspec(noreturn) void printUsageAndExit() {
157 wprintf(L"Usage: symupload [--timeout NN] <file.exe|file.dll> "
158 L"<symbol upload URL> [...<symbol upload URLs>]\n\n");
159 wprintf(L"Timeout is in milliseconds, or can be 0 to be unlimited\n\n");
160 wprintf(L"Example:\n\n\tsymupload.exe --timeout 0 chrome.dll "
161 L"http://no.free.symbol.server.for.you\n");
164 int wmain(int argc, wchar_t *argv[]) {
165 const wchar_t *module;
169 if (!wcscmp(L"--timeout", argv[1])) {
170 timeout = _wtoi(argv[2]);
177 if (argc >= currentarg + 2)
178 module = argv[currentarg++];
183 PDBModuleInfo pdb_info;
184 if (!DumpSymbolsToTempFile(module, &symbol_file, &pdb_info)) {
185 fwprintf(stderr, L"Could not get symbol data from %s\n", module);
189 wstring code_file = WindowsStringUtils::GetBaseName(wstring(module));
191 map<wstring, wstring> parameters;
192 parameters[L"code_file"] = code_file;
193 parameters[L"debug_file"] = pdb_info.debug_file;
194 parameters[L"debug_identifier"] = pdb_info.debug_identifier;
195 parameters[L"os"] = L"windows"; // This version of symupload is Windows-only
196 parameters[L"cpu"] = pdb_info.cpu;
198 // Don't make a missing version a hard error. Issue a warning, and let the
199 // server decide whether to reject files without versions.
200 wstring file_version;
201 if (GetFileVersionString(module, &file_version)) {
202 parameters[L"version"] = file_version;
204 fwprintf(stderr, L"Warning: Could not get file version for %s\n", module);
209 while (currentarg < argc) {
210 if (!HTTPUpload::SendRequest(argv[currentarg], parameters,
211 symbol_file, L"symbol_file",
212 timeout == -1 ? NULL : &timeout,
215 fwprintf(stderr, L"Symbol file upload to %s failed\n", argv[currentarg]);
220 _wunlink(symbol_file.c_str());
223 wprintf(L"Uploaded symbols for windows-%s/%s/%s (%s %s)\n",
224 pdb_info.cpu.c_str(), pdb_info.debug_file.c_str(),
225 pdb_info.debug_identifier.c_str(), code_file.c_str(),
226 file_version.c_str());
229 return success ? 0 : 1;