• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
29 
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
41 
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include <locale>
48 
49 #include "common/linux/symbol_upload.h"
50 
51 using google_breakpad::sym_upload::UploadProtocol;
52 using google_breakpad::sym_upload::Options;
53 
StrToUpper(std::string * str)54 static void StrToUpper(std::string* str) {
55   if (str == nullptr) {
56     fprintf(stderr, "nullptr passed to StrToUpper.\n");
57     exit(1);
58   }
59   for (size_t i = 0; i < str->length(); i++) {
60     (*str)[i] = std::toupper((*str)[i], std::locale::classic());
61   }
62 }
63 
64 //=============================================================================
65 static void
Usage(int argc,const char * argv[])66 Usage(int argc, const char *argv[]) {
67   fprintf(stderr, "Submit symbol information.\n");
68   fprintf(stderr, "Usage: %s [options...] <symbol-file> <upload-URL>\n",
69       argv[0]);
70   fprintf(stderr, "Options:\n");
71   fprintf(stderr, "<symbol-file> should be created by using the dump_syms"
72       "tool.\n");
73   fprintf(stderr, "<upload-URL> is the destination for the upload\n");
74   fprintf(stderr, "-p:\t <protocol> One of ['sym-upload-v1',"
75     " 'sym-upload-v2'], defaults to 'sym-upload-v1'.\n");
76   fprintf(stderr, "-v:\t Version information (e.g., 1.2.3.4)\n");
77   fprintf(stderr, "-x:\t <host[:port]> Use HTTP proxy on given port\n");
78   fprintf(stderr, "-u:\t <user[:password]> Set proxy user and password\n");
79   fprintf(stderr, "-h:\t Usage\n");
80   fprintf(stderr, "-?:\t Usage\n");
81   fprintf(stderr, "\n");
82   fprintf(stderr, "These options only work with 'sym-upload-v2' protocol:\n");
83   fprintf(stderr, "-k:\t <API-key> A secret used to authenticate with the"
84       " API.\n");
85   fprintf(stderr, "-f:\t Force symbol upload if already exists.\n");
86   fprintf(stderr, "-t:\t <symbol-type> Explicitly set symbol upload type ("
87       "default is 'breakpad').\n"
88       "\t One of ['breakpad', 'elf', 'pe', 'macho', 'debug_only', 'dwp', "
89       "'dsym', 'pdb'].\n"
90       "\t Note: When this flag is set to anything other than 'breakpad', then "
91       "the '-c' and '-i' flags must also be set.\n");
92   fprintf(stderr, "-c:\t <code-file> Explicitly set 'code_file' for symbol "
93       "upload (basename of executable).\n");
94   fprintf(stderr, "-i:\t <debug-id> Explicitly set 'debug_id' for symbol "
95       "upload (typically build ID of executable).\n");
96   fprintf(stderr, "\n");
97   fprintf(stderr, "Examples:\n");
98   fprintf(stderr, "  With 'sym-upload-v1':\n");
99   fprintf(stderr, "    %s path/to/symbol_file http://myuploadserver\n",
100       argv[0]);
101   fprintf(stderr, "  With 'sym-upload-v2':\n");
102   fprintf(stderr, "    [Defaulting to symbol type 'BREAKPAD']\n");
103   fprintf(stderr, "    %s -p sym-upload-v2 -k mysecret123! "
104       "path/to/symbol_file http://myuploadserver\n", argv[0]);
105   fprintf(stderr, "    [Explicitly set symbol type to 'elf']\n");
106   fprintf(stderr, "    %s -p sym-upload-v2 -k mysecret123! -t elf "
107       "-c app -i 11111111BBBB3333DDDD555555555555F "
108       "path/to/symbol_file http://myuploadserver\n", argv[0]);
109 }
110 
111 //=============================================================================
112 static void
SetupOptions(int argc,const char * argv[],Options * options)113 SetupOptions(int argc, const char *argv[], Options *options) {
114   extern int optind;
115   int ch;
116   constexpr char flag_pattern[] = "u:v:x:p:k:t:c:i:hf?";
117 
118   while ((ch = getopt(argc, (char * const *)argv, flag_pattern)) != -1) {
119     switch (ch) {
120       case 'h':
121       case '?':
122         Usage(argc, argv);
123         exit(0);
124         break;
125       case 'u':
126         options->proxy_user_pwd = optarg;
127         break;
128       case 'v':
129         options->version = optarg;
130         break;
131       case 'x':
132         options->proxy = optarg;
133         break;
134       case 'p':
135         if (strcmp(optarg, "sym-upload-v2") == 0) {
136           options->upload_protocol = UploadProtocol::SYM_UPLOAD_V2;
137         } else if (strcmp(optarg, "sym-upload-v1") == 0) {
138           options->upload_protocol = UploadProtocol::SYM_UPLOAD_V1;
139         } else {
140           fprintf(stderr, "Invalid protocol '%s'\n", optarg);
141           Usage(argc, argv);
142           exit(1);
143         }
144         break;
145       case 'k':
146         options->api_key = optarg;
147         break;
148       case 't': {
149         // This is really an enum, so treat as upper-case for consistency with
150         // enum naming convention on server-side.
151         options->type = optarg;
152         StrToUpper(&(options->type));
153         break;
154       }
155       case 'c':
156         options->code_file = optarg;
157         break;
158       case 'i':
159         options->debug_id = optarg;
160         break;
161       case 'f':
162         options->force = true;
163         break;
164 
165       default:
166         fprintf(stderr, "Invalid option '%c'\n", ch);
167         Usage(argc, argv);
168         exit(1);
169         break;
170     }
171   }
172 
173   if ((argc - optind) != 2) {
174     fprintf(stderr, "%s: Missing symbols file and/or upload-URL\n", argv[0]);
175     Usage(argc, argv);
176     exit(1);
177   }
178 
179   bool is_breakpad_upload = options->type.empty() ||
180       options->type == google_breakpad::sym_upload::kBreakpadSymbolType;
181   bool has_code_file = !options->code_file.empty();
182   bool has_debug_id = !options->debug_id.empty();
183   if (is_breakpad_upload && (has_code_file || has_debug_id)) {
184     fprintf(stderr, "\n");
185     fprintf(stderr, "%s: -c and -i should only be specified for non-breakpad "
186         "symbol upload types.\n", argv[0]);
187     fprintf(stderr, "\n");
188     Usage(argc, argv);
189     exit(1);
190   }
191   if (!is_breakpad_upload && (!has_code_file || !has_debug_id)) {
192     fprintf(stderr, "\n");
193     fprintf(stderr, "%s: -c and -i must be specified for non-breakpad "
194         "symbol upload types.\n", argv[0]);
195     fprintf(stderr, "\n");
196     Usage(argc, argv);
197     exit(1);
198   }
199 
200   options->symbolsPath = argv[optind];
201   options->uploadURLStr = argv[optind + 1];
202 }
203 
204 //=============================================================================
main(int argc,const char * argv[])205 int main(int argc, const char* argv[]) {
206   Options options;
207   SetupOptions(argc, argv, &options);
208   google_breakpad::sym_upload::Start(&options);
209   return options.success ? 0 : 1;
210 }
211