1 // Copyright (c) 2011 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.
29 
30 // symbol_upload.cc: implemented google_breakpad::sym_upload::Start, a helper
31 // function for linux symbol upload tool.
32 
33 #include "common/linux/http_upload.h"
34 #include "common/linux/symbol_upload.h"
35 
36 #include <assert.h>
37 #include <stdio.h>
38 
39 #include <functional>
40 #include <vector>
41 
42 namespace google_breakpad {
43 namespace sym_upload {
44 
TokenizeByChar(const string & source_string,int c,std::vector<string> * results)45 void TokenizeByChar(const string &source_string, int c,
46                     std::vector<string> *results) {
47   assert(results);
48   string::size_type cur_pos = 0, next_pos = 0;
49   while ((next_pos = source_string.find(c, cur_pos)) != string::npos) {
50     if (next_pos != cur_pos)
51       results->push_back(source_string.substr(cur_pos, next_pos - cur_pos));
52     cur_pos = next_pos + 1;
53   }
54   if (cur_pos < source_string.size() && next_pos != cur_pos)
55     results->push_back(source_string.substr(cur_pos));
56 }
57 
58 //=============================================================================
59 // Parse out the module line which have 5 parts.
60 // MODULE <os> <cpu> <uuid> <module-name>
ModuleDataForSymbolFile(const string & file,std::vector<string> * module_parts)61 bool ModuleDataForSymbolFile(const string &file,
62                              std::vector<string> *module_parts) {
63   assert(module_parts);
64   const size_t kModulePartNumber = 5;
65   FILE* fp = fopen(file.c_str(), "r");
66   if (fp) {
67     char buffer[1024];
68     if (fgets(buffer, sizeof(buffer), fp)) {
69       string line(buffer);
70       string::size_type line_break_pos = line.find_first_of('\n');
71       if (line_break_pos == string::npos) {
72         assert(0 && "The file is invalid!");
73         fclose(fp);
74         return false;
75       }
76       line.resize(line_break_pos);
77       const char kDelimiter = ' ';
78       TokenizeByChar(line, kDelimiter, module_parts);
79       if (module_parts->size() != kModulePartNumber)
80         module_parts->clear();
81     }
82     fclose(fp);
83   }
84 
85   return module_parts->size() == kModulePartNumber;
86 }
87 
88 //=============================================================================
CompactIdentifier(const string & uuid)89 string CompactIdentifier(const string &uuid) {
90   std::vector<string> components;
91   TokenizeByChar(uuid, '-', &components);
92   string result;
93   for (size_t i = 0; i < components.size(); ++i)
94     result += components[i];
95   return result;
96 }
97 
98 //=============================================================================
Start(Options * options)99 void Start(Options *options) {
100   std::map<string, string> parameters;
101   options->success = false;
102   std::vector<string> module_parts;
103   if (!ModuleDataForSymbolFile(options->symbolsPath, &module_parts)) {
104     fprintf(stderr, "Failed to parse symbol file!\n");
105     return;
106   }
107 
108   string compacted_id = CompactIdentifier(module_parts[3]);
109 
110   // Add parameters
111   if (!options->version.empty())
112     parameters["version"] = options->version;
113 
114   // MODULE <os> <cpu> <uuid> <module-name>
115   // 0      1    2     3      4
116   parameters["os"] = module_parts[1];
117   parameters["cpu"] = module_parts[2];
118   parameters["debug_file"] = module_parts[4];
119   parameters["code_file"] = module_parts[4];
120   parameters["debug_identifier"] = compacted_id;
121 
122   std::map<string, string> files;
123   files["symbol_file"] = options->symbolsPath;
124 
125   string response, error;
126   long response_code;
127   bool success = HTTPUpload::SendRequest(options->uploadURLStr,
128                                          parameters,
129                                          files,
130                                          options->proxy,
131                                          options->proxy_user_pwd,
132                                          "",
133                                          &response,
134                                          &response_code,
135                                          &error);
136 
137   if (!success) {
138     printf("Failed to send symbol file: %s\n", error.c_str());
139     printf("Response code: %ld\n", response_code);
140     printf("Response:\n");
141     printf("%s\n", response.c_str());
142   } else if (response_code == 0) {
143     printf("Failed to send symbol file: No response code\n");
144   } else if (response_code != 200) {
145     printf("Failed to send symbol file: Response code %ld\n", response_code);
146     printf("Response:\n");
147     printf("%s\n", response.c_str());
148   } else {
149     printf("Successfully sent the symbol file.\n");
150   }
151   options->success = success;
152 }
153 
154 }  // namespace sym_upload
155 }  // namespace google_breakpad
156