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