michael@0: // Copyright (c) 2006, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // Tool to upload an exe/dll and its associated symbols to an HTTP server. michael@0: // The PDB file is located automatically, using the path embedded in the michael@0: // executable. The upload is sent as a multipart/form-data POST request, michael@0: // with the following parameters: michael@0: // code_file: the basename of the module, e.g. "app.exe" michael@0: // debug_file: the basename of the debugging file, e.g. "app.pdb" michael@0: // debug_identifier: the debug file's identifier, usually consisting of michael@0: // the guid and age embedded in the pdb, e.g. michael@0: // "11111111BBBB3333DDDD555555555555F" michael@0: // version: the file version of the module, e.g. "1.2.3.4" michael@0: // os: the operating system that the module was built for, always michael@0: // "windows" in this implementation. michael@0: // cpu: the CPU that the module was built for, typically "x86". michael@0: // symbol_file: the contents of the breakpad-format symbol file michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "common/windows/string_utils-inl.h" michael@0: michael@0: #include "common/windows/http_upload.h" michael@0: #include "common/windows/pdb_source_line_writer.h" michael@0: michael@0: using std::string; michael@0: using std::wstring; michael@0: using std::vector; michael@0: using std::map; michael@0: using google_breakpad::HTTPUpload; michael@0: using google_breakpad::PDBModuleInfo; michael@0: using google_breakpad::PDBSourceLineWriter; michael@0: using google_breakpad::WindowsStringUtils; michael@0: michael@0: // Extracts the file version information for the given filename, michael@0: // as a string, for example, "1.2.3.4". Returns true on success. michael@0: static bool GetFileVersionString(const wchar_t *filename, wstring *version) { michael@0: DWORD handle; michael@0: DWORD version_size = GetFileVersionInfoSize(filename, &handle); michael@0: if (version_size < sizeof(VS_FIXEDFILEINFO)) { michael@0: return false; michael@0: } michael@0: michael@0: vector version_info(version_size); michael@0: if (!GetFileVersionInfo(filename, handle, version_size, &version_info[0])) { michael@0: return false; michael@0: } michael@0: michael@0: void *file_info_buffer = NULL; michael@0: unsigned int file_info_length; michael@0: if (!VerQueryValue(&version_info[0], L"\\", michael@0: &file_info_buffer, &file_info_length)) { michael@0: return false; michael@0: } michael@0: michael@0: // The maximum value of each version component is 65535 (0xffff), michael@0: // so the max length is 24, including the terminating null. michael@0: wchar_t ver_string[24]; michael@0: VS_FIXEDFILEINFO *file_info = michael@0: reinterpret_cast(file_info_buffer); michael@0: swprintf(ver_string, sizeof(ver_string) / sizeof(ver_string[0]), michael@0: L"%d.%d.%d.%d", michael@0: file_info->dwFileVersionMS >> 16, michael@0: file_info->dwFileVersionMS & 0xffff, michael@0: file_info->dwFileVersionLS >> 16, michael@0: file_info->dwFileVersionLS & 0xffff); michael@0: michael@0: // remove when VC++7.1 is no longer supported michael@0: ver_string[sizeof(ver_string) / sizeof(ver_string[0]) - 1] = L'\0'; michael@0: michael@0: *version = ver_string; michael@0: return true; michael@0: } michael@0: michael@0: // Creates a new temporary file and writes the symbol data from the given michael@0: // exe/dll file to it. Returns the path to the temp file in temp_file_path michael@0: // and information about the pdb in pdb_info. michael@0: static bool DumpSymbolsToTempFile(const wchar_t *file, michael@0: wstring *temp_file_path, michael@0: PDBModuleInfo *pdb_info) { michael@0: google_breakpad::PDBSourceLineWriter writer; michael@0: // Use EXE_FILE to get information out of the exe/dll in addition to the michael@0: // pdb. The name and version number of the exe/dll are of value, and michael@0: // there's no way to locate an exe/dll given a pdb. michael@0: if (!writer.Open(file, PDBSourceLineWriter::EXE_FILE)) { michael@0: return false; michael@0: } michael@0: michael@0: wchar_t temp_path[_MAX_PATH]; michael@0: if (GetTempPath(_MAX_PATH, temp_path) == 0) { michael@0: return false; michael@0: } michael@0: michael@0: wchar_t temp_filename[_MAX_PATH]; michael@0: if (GetTempFileName(temp_path, L"sym", 0, temp_filename) == 0) { michael@0: return false; michael@0: } michael@0: michael@0: FILE *temp_file = NULL; michael@0: #if _MSC_VER >= 1400 // MSVC 2005/8 michael@0: if (_wfopen_s(&temp_file, temp_filename, L"w") != 0) michael@0: #else // _MSC_VER >= 1400 michael@0: // _wfopen_s was introduced in MSVC8. Use _wfopen for earlier environments. michael@0: // Don't use it with MSVC8 and later, because it's deprecated. michael@0: if (!(temp_file = _wfopen(temp_filename, L"w"))) michael@0: #endif // _MSC_VER >= 1400 michael@0: { michael@0: return false; michael@0: } michael@0: michael@0: bool success = writer.WriteMap(temp_file); michael@0: fclose(temp_file); michael@0: if (!success) { michael@0: _wunlink(temp_filename); michael@0: return false; michael@0: } michael@0: michael@0: *temp_file_path = temp_filename; michael@0: michael@0: return writer.GetModuleInfo(pdb_info); michael@0: } michael@0: michael@0: void printUsageAndExit() { michael@0: wprintf(L"Usage: symupload [--timeout NN] \n\n"); michael@0: wprintf(L"Timeout is in milliseconds, or can be 0 to be unlimited\n\n"); michael@0: wprintf(L"Example:\n\n\tsymupload.exe --timeout 0 chrome.dll http://no.free.symbol.server.for.you\n"); michael@0: exit(0); michael@0: } michael@0: int wmain(int argc, wchar_t *argv[]) { michael@0: if ((argc != 3) && michael@0: (argc != 5)) { michael@0: printUsageAndExit(); michael@0: } michael@0: michael@0: const wchar_t *module, *url; michael@0: int timeout = -1; michael@0: if (argc == 3) { michael@0: module = argv[1]; michael@0: url = argv[2]; michael@0: } else { michael@0: // check for timeout flag michael@0: if (!wcscmp(L"--timeout", argv[1])) { michael@0: timeout = _wtoi(argv[2]); michael@0: module = argv[3]; michael@0: url = argv[4]; michael@0: } else { michael@0: printUsageAndExit(); michael@0: } michael@0: } michael@0: michael@0: wstring symbol_file; michael@0: PDBModuleInfo pdb_info; michael@0: if (!DumpSymbolsToTempFile(module, &symbol_file, &pdb_info)) { michael@0: fwprintf(stderr, L"Could not get symbol data from %s\n", module); michael@0: return 1; michael@0: } michael@0: michael@0: wstring code_file = WindowsStringUtils::GetBaseName(wstring(module)); michael@0: michael@0: map parameters; michael@0: parameters[L"code_file"] = code_file; michael@0: parameters[L"debug_file"] = pdb_info.debug_file; michael@0: parameters[L"debug_identifier"] = pdb_info.debug_identifier; michael@0: parameters[L"os"] = L"windows"; // This version of symupload is Windows-only michael@0: parameters[L"cpu"] = pdb_info.cpu; michael@0: michael@0: // Don't make a missing version a hard error. Issue a warning, and let the michael@0: // server decide whether to reject files without versions. michael@0: wstring file_version; michael@0: if (GetFileVersionString(module, &file_version)) { michael@0: parameters[L"version"] = file_version; michael@0: } else { michael@0: fwprintf(stderr, L"Warning: Could not get file version for %s\n", module); michael@0: } michael@0: michael@0: bool success = HTTPUpload::SendRequest(url, parameters, michael@0: symbol_file, L"symbol_file", michael@0: timeout == -1 ? NULL : &timeout, michael@0: NULL, NULL); michael@0: _wunlink(symbol_file.c_str()); michael@0: michael@0: if (!success) { michael@0: fwprintf(stderr, L"Symbol file upload failed\n"); michael@0: return 1; michael@0: } michael@0: michael@0: wprintf(L"Uploaded symbols for windows-%s/%s/%s (%s %s)\n", michael@0: pdb_info.cpu.c_str(), pdb_info.debug_file.c_str(), michael@0: pdb_info.debug_identifier.c_str(), code_file.c_str(), michael@0: file_version.c_str()); michael@0: return 0; michael@0: }