toolkit/crashreporter/google-breakpad/src/tools/linux/symupload/sym_upload.cc

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 // symupload.cc: Upload a symbol file to a HTTP server. The upload is sent as
michael@0 31 // a multipart/form-data POST request with the following parameters:
michael@0 32 // code_file: the basename of the module, e.g. "app"
michael@0 33 // debug_file: the basename of the debugging file, e.g. "app"
michael@0 34 // debug_identifier: the debug file's identifier, usually consisting of
michael@0 35 // the guid and age embedded in the pdb, e.g.
michael@0 36 // "11111111BBBB3333DDDD555555555555F"
michael@0 37 // version: the file version of the module, e.g. "1.2.3.4"
michael@0 38 // os: the operating system that the module was built for
michael@0 39 // cpu: the CPU that the module was built for
michael@0 40 // symbol_file: the contents of the breakpad-format symbol file
michael@0 41
michael@0 42 #include <assert.h>
michael@0 43 #include <stdio.h>
michael@0 44 #include <stdlib.h>
michael@0 45 #include <unistd.h>
michael@0 46
michael@0 47 #include <functional>
michael@0 48 #include <iostream>
michael@0 49 #include <string>
michael@0 50 #include <vector>
michael@0 51
michael@0 52 #include "common/linux/http_upload.h"
michael@0 53
michael@0 54 using google_breakpad::HTTPUpload;
michael@0 55
michael@0 56 typedef struct {
michael@0 57 std::string symbolsPath;
michael@0 58 std::string uploadURLStr;
michael@0 59 std::string proxy;
michael@0 60 std::string proxy_user_pwd;
michael@0 61 std::string version;
michael@0 62 bool success;
michael@0 63 } Options;
michael@0 64
michael@0 65 static void TokenizeByChar(const std::string &source_string,
michael@0 66 int c, std::vector<std::string> *results) {
michael@0 67 assert(results);
michael@0 68 std::string::size_type cur_pos = 0, next_pos = 0;
michael@0 69 while ((next_pos = source_string.find(c, cur_pos)) != std::string::npos) {
michael@0 70 if (next_pos != cur_pos)
michael@0 71 results->push_back(source_string.substr(cur_pos, next_pos - cur_pos));
michael@0 72 cur_pos = next_pos + 1;
michael@0 73 }
michael@0 74 if (cur_pos < source_string.size() && next_pos != cur_pos)
michael@0 75 results->push_back(source_string.substr(cur_pos));
michael@0 76 }
michael@0 77
michael@0 78 //=============================================================================
michael@0 79 // Parse out the module line which have 5 parts.
michael@0 80 // MODULE <os> <cpu> <uuid> <module-name>
michael@0 81 static bool ModuleDataForSymbolFile(const std::string &file,
michael@0 82 std::vector<std::string> *module_parts) {
michael@0 83 assert(module_parts);
michael@0 84 const size_t kModulePartNumber = 5;
michael@0 85 FILE *fp = fopen(file.c_str(), "r");
michael@0 86 if (fp) {
michael@0 87 char buffer[1024];
michael@0 88 if (fgets(buffer, sizeof(buffer), fp)) {
michael@0 89 std::string line(buffer);
michael@0 90 std::string::size_type line_break_pos = line.find_first_of('\n');
michael@0 91 if (line_break_pos == std::string::npos) {
michael@0 92 assert(0 && "The file is invalid!");
michael@0 93 fclose(fp);
michael@0 94 return false;
michael@0 95 }
michael@0 96 line.resize(line_break_pos);
michael@0 97 const char kDelimiter = ' ';
michael@0 98 TokenizeByChar(line, kDelimiter, module_parts);
michael@0 99 if (module_parts->size() != kModulePartNumber)
michael@0 100 module_parts->clear();
michael@0 101 }
michael@0 102 fclose(fp);
michael@0 103 }
michael@0 104
michael@0 105 return module_parts->size() == kModulePartNumber;
michael@0 106 }
michael@0 107
michael@0 108 //=============================================================================
michael@0 109 static std::string CompactIdentifier(const std::string &uuid) {
michael@0 110 std::vector<std::string> components;
michael@0 111 TokenizeByChar(uuid, '-', &components);
michael@0 112 std::string result;
michael@0 113 for (size_t i = 0; i < components.size(); ++i)
michael@0 114 result += components[i];
michael@0 115 return result;
michael@0 116 }
michael@0 117
michael@0 118 //=============================================================================
michael@0 119 static void Start(Options *options) {
michael@0 120 std::map<std::string, std::string> parameters;
michael@0 121 options->success = false;
michael@0 122 std::vector<std::string> module_parts;
michael@0 123 if (!ModuleDataForSymbolFile(options->symbolsPath, &module_parts)) {
michael@0 124 fprintf(stderr, "Failed to parse symbol file!\n");
michael@0 125 return;
michael@0 126 }
michael@0 127
michael@0 128 std::string compacted_id = CompactIdentifier(module_parts[3]);
michael@0 129
michael@0 130 // Add parameters
michael@0 131 if (!options->version.empty())
michael@0 132 parameters["version"] = options->version;
michael@0 133
michael@0 134 // MODULE <os> <cpu> <uuid> <module-name>
michael@0 135 // 0 1 2 3 4
michael@0 136 parameters["os"] = module_parts[1];
michael@0 137 parameters["cpu"] = module_parts[2];
michael@0 138 parameters["debug_file"] = module_parts[4];
michael@0 139 parameters["code_file"] = module_parts[4];
michael@0 140 parameters["debug_identifier"] = compacted_id;
michael@0 141 std::string response, error;
michael@0 142 long response_code;
michael@0 143 bool success = HTTPUpload::SendRequest(options->uploadURLStr,
michael@0 144 parameters,
michael@0 145 options->symbolsPath,
michael@0 146 "symbol_file",
michael@0 147 options->proxy,
michael@0 148 options->proxy_user_pwd,
michael@0 149 "",
michael@0 150 &response,
michael@0 151 &response_code,
michael@0 152 &error);
michael@0 153
michael@0 154 if (!success) {
michael@0 155 printf("Failed to send symbol file: %s\n", error.c_str());
michael@0 156 printf("Response:\n");
michael@0 157 printf("%s\n", response.c_str());
michael@0 158 } else if (response_code == 0) {
michael@0 159 printf("Failed to send symbol file: No response code\n");
michael@0 160 } else if (response_code != 200) {
michael@0 161 printf("Failed to send symbol file: Response code %ld\n", response_code);
michael@0 162 printf("Response:\n");
michael@0 163 printf("%s\n", response.c_str());
michael@0 164 } else {
michael@0 165 printf("Successfully sent the symbol file.\n");
michael@0 166 }
michael@0 167 options->success = success;
michael@0 168 }
michael@0 169
michael@0 170 //=============================================================================
michael@0 171 static void
michael@0 172 Usage(int argc, const char *argv[]) {
michael@0 173 fprintf(stderr, "Submit symbol information.\n");
michael@0 174 fprintf(stderr, "Usage: %s [options...] <symbols> <upload-URL>\n", argv[0]);
michael@0 175 fprintf(stderr, "Options:\n");
michael@0 176 fprintf(stderr, "<symbols> should be created by using the dump_syms tool.\n");
michael@0 177 fprintf(stderr, "<upload-URL> is the destination for the upload\n");
michael@0 178 fprintf(stderr, "-v:\t Version information (e.g., 1.2.3.4)\n");
michael@0 179 fprintf(stderr, "-x:\t <host[:port]> Use HTTP proxy on given port\n");
michael@0 180 fprintf(stderr, "-u:\t <user[:password]> Set proxy user and password\n");
michael@0 181 fprintf(stderr, "-h:\t Usage\n");
michael@0 182 fprintf(stderr, "-?:\t Usage\n");
michael@0 183 }
michael@0 184
michael@0 185 //=============================================================================
michael@0 186 static void
michael@0 187 SetupOptions(int argc, const char *argv[], Options *options) {
michael@0 188 extern int optind;
michael@0 189 char ch;
michael@0 190
michael@0 191 while ((ch = getopt(argc, (char * const *)argv, "u:v:x:h?")) != -1) {
michael@0 192 switch (ch) {
michael@0 193 case 'u':
michael@0 194 options->proxy_user_pwd = optarg;
michael@0 195 break;
michael@0 196 case 'v':
michael@0 197 options->version = optarg;
michael@0 198 break;
michael@0 199 case 'x':
michael@0 200 options->proxy = optarg;
michael@0 201 break;
michael@0 202
michael@0 203 default:
michael@0 204 Usage(argc, argv);
michael@0 205 exit(0);
michael@0 206 break;
michael@0 207 }
michael@0 208 }
michael@0 209
michael@0 210 if ((argc - optind) != 2) {
michael@0 211 fprintf(stderr, "%s: Missing symbols file and/or upload-URL\n", argv[0]);
michael@0 212 Usage(argc, argv);
michael@0 213 exit(1);
michael@0 214 }
michael@0 215
michael@0 216 options->symbolsPath = argv[optind];
michael@0 217 options->uploadURLStr = argv[optind + 1];
michael@0 218 }
michael@0 219
michael@0 220 //=============================================================================
michael@0 221 int main (int argc, const char * argv[]) {
michael@0 222 Options options;
michael@0 223 SetupOptions(argc, argv, &options);
michael@0 224 Start(&options);
michael@0 225 return options.success ? 0 : 1;
michael@0 226 }

mercurial