xref: /aosp_15_r20/external/google-breakpad/src/tools/linux/symupload/sym_upload.cc (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1 // Copyright 2006 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //     * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //     * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 // symupload.cc: Upload a symbol file to a HTTP server.  The upload is sent as
30 // a multipart/form-data POST request with the following parameters:
31 //  code_file: the basename of the module, e.g. "app"
32 //  debug_file: the basename of the debugging file, e.g. "app"
33 //  debug_identifier: the debug file's identifier, usually consisting of
34 //                    the guid and age embedded in the pdb, e.g.
35 //                    "11111111BBBB3333DDDD555555555555F"
36 //  version: the file version of the module, e.g. "1.2.3.4"
37 //  os: the operating system that the module was built for
38 //  cpu: the CPU that the module was built for
39 //  symbol_file: the contents of the breakpad-format symbol file
40 
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>  // Must come first
43 #endif
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 #include <locale>
51 
52 #include "common/linux/symbol_upload.h"
53 #include "common/path_helper.h"
54 
55 using google_breakpad::sym_upload::UploadProtocol;
56 using google_breakpad::sym_upload::Options;
57 
StrToUpper(std::string * str)58 static void StrToUpper(std::string* str) {
59   if (str == nullptr) {
60     fprintf(stderr, "nullptr passed to StrToUpper.\n");
61     exit(1);
62   }
63   for (size_t i = 0; i < str->length(); i++) {
64     (*str)[i] = std::toupper((*str)[i], std::locale::classic());
65   }
66 }
67 
68 //=============================================================================
69 static void
Usage(int argc,const char * argv[])70 Usage(int argc, const char *argv[]) {
71   fprintf(stderr, "Submit symbol information.\n");
72   fprintf(stderr, "Usage: %s [options...] <symbol-file> <upload-URL>\n",
73           google_breakpad::BaseName(argv[0]).c_str());
74   fprintf(stderr, "Options:\n");
75   fprintf(stderr,
76           "<symbol-file> should be created by using the dump_syms "
77           "tool.\n");
78   fprintf(stderr, "<upload-URL> is the destination for the upload\n");
79   fprintf(stderr, "-p:\t <protocol> One of ['sym-upload-v1',"
80     " 'sym-upload-v2'], defaults to 'sym-upload-v1'.\n");
81   fprintf(stderr, "-v:\t Version information (e.g., 1.2.3.4)\n");
82   fprintf(stderr, "-x:\t <host[:port]> Use HTTP proxy on given port\n");
83   fprintf(stderr, "-u:\t <user[:password]> Set proxy user and password\n");
84   fprintf(stderr, "-h:\t Usage\n");
85   fprintf(stderr, "-?:\t Usage\n");
86   fprintf(stderr, "\n");
87   fprintf(stderr, "These options only work with 'sym-upload-v2' protocol:\n");
88   fprintf(stderr, "-k:\t <API-key> A secret used to authenticate with the"
89       " API.\n");
90   fprintf(stderr, "-f:\t Force symbol upload if already exists.\n");
91   fprintf(stderr, "-t:\t <symbol-type> Explicitly set symbol upload type ("
92       "default is 'breakpad').\n"
93       "\t One of ['breakpad', 'elf', 'pe', 'macho', 'debug_only', 'dwp', "
94       "'dsym', 'pdb'].\n"
95       "\t Note: When this flag is set to anything other than 'breakpad', then "
96       "the '-c' and '-i' flags must also be set.\n");
97   fprintf(stderr, "-c:\t <code-file> Explicitly set 'code_file' for symbol "
98       "upload (basename of executable).\n");
99   fprintf(stderr, "-i:\t <debug-id> Explicitly set 'debug_id' for symbol "
100       "upload (typically build ID of executable).\n");
101   fprintf(stderr, "\n");
102   fprintf(stderr, "Examples:\n");
103   fprintf(stderr, "  With 'sym-upload-v1':\n");
104   fprintf(stderr, "    %s path/to/symbol_file http://myuploadserver\n",
105       argv[0]);
106   fprintf(stderr, "  With 'sym-upload-v2':\n");
107   fprintf(stderr, "    [Defaulting to symbol type 'BREAKPAD']\n");
108   fprintf(stderr, "    %s -p sym-upload-v2 -k mysecret123! "
109       "path/to/symbol_file http://myuploadserver\n", argv[0]);
110   fprintf(stderr, "    [Explicitly set symbol type to 'elf']\n");
111   fprintf(stderr, "    %s -p sym-upload-v2 -k mysecret123! -t elf "
112       "-c app -i 11111111BBBB3333DDDD555555555555F "
113       "path/to/symbol_file http://myuploadserver\n", argv[0]);
114 }
115 
116 //=============================================================================
117 static void
SetupOptions(int argc,const char * argv[],Options * options)118 SetupOptions(int argc, const char *argv[], Options *options) {
119   extern int optind, optopt;
120   int ch;
121   constexpr char flag_pattern[] = "u:v:x:p:k:t:c:i:hf?";
122 
123   while ((ch = getopt(argc, (char * const*)argv, flag_pattern)) != -1) {
124     switch (ch) {
125       case 'h':
126       case '?':
127         Usage(argc, argv);
128         // ch might be '?' because getopt found an error while parsing args (as
129         // opposed to finding "-?" as an arg), in which case optopt is set to
130         // the bad arg value, so return an error code if optopt is set,
131         // otherwise exit cleanly.
132         exit(optopt == 0 ? 0 : 1);
133       case 'u':
134         options->proxy_user_pwd = optarg;
135         break;
136       case 'v':
137         options->version = optarg;
138         break;
139       case 'x':
140         options->proxy = optarg;
141         break;
142       case 'p':
143         if (strcmp(optarg, "sym-upload-v2") == 0) {
144           options->upload_protocol = UploadProtocol::SYM_UPLOAD_V2;
145         } else if (strcmp(optarg, "sym-upload-v1") == 0) {
146           options->upload_protocol = UploadProtocol::SYM_UPLOAD_V1;
147         } else {
148           fprintf(stderr, "Invalid protocol '%s'\n", optarg);
149           Usage(argc, argv);
150           exit(1);
151         }
152         break;
153       case 'k':
154         options->api_key = optarg;
155         break;
156       case 't': {
157         // This is really an enum, so treat as upper-case for consistency with
158         // enum naming convention on server-side.
159         options->type = optarg;
160         StrToUpper(&(options->type));
161         break;
162       }
163       case 'c':
164         options->code_file = optarg;
165         break;
166       case 'i':
167         options->debug_id = optarg;
168         break;
169       case 'f':
170         options->force = true;
171         break;
172 
173       default:
174         fprintf(stderr, "Invalid option '%c'\n", ch);
175         Usage(argc, argv);
176         exit(1);
177     }
178   }
179 
180   if ((argc - optind) != 2) {
181     fprintf(stderr, "%s: Missing symbols file and/or upload-URL\n", argv[0]);
182     Usage(argc, argv);
183     exit(1);
184   }
185 
186   bool is_breakpad_upload = options->type.empty() ||
187       options->type == google_breakpad::sym_upload::kBreakpadSymbolType;
188   bool has_code_file = !options->code_file.empty();
189   bool has_debug_id = !options->debug_id.empty();
190   if (is_breakpad_upload && (has_code_file || has_debug_id)) {
191     fprintf(stderr, "\n");
192     fprintf(stderr, "%s: -c and -i should only be specified for non-breakpad "
193         "symbol upload types.\n", argv[0]);
194     fprintf(stderr, "\n");
195     Usage(argc, argv);
196     exit(1);
197   }
198   if (!is_breakpad_upload && (!has_code_file || !has_debug_id)) {
199     fprintf(stderr, "\n");
200     fprintf(stderr, "%s: -c and -i must be specified for non-breakpad "
201         "symbol upload types.\n", argv[0]);
202     fprintf(stderr, "\n");
203     Usage(argc, argv);
204     exit(1);
205   }
206 
207   options->symbolsPath = argv[optind];
208   options->uploadURLStr = argv[optind + 1];
209 }
210 
211 //=============================================================================
main(int argc,const char * argv[])212 int main(int argc, const char* argv[]) {
213   Options options;
214   SetupOptions(argc, argv, &options);
215   google_breakpad::sym_upload::Start(&options);
216   return options.success ? 0 : 1;
217 }
218