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