xref: /aosp_15_r20/external/google-breakpad/src/tools/linux/symupload/minidump_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 // minidump_upload.cc: Upload a minidump to a HTTP server.
30 // The upload is sent as a multipart/form-data POST request with
31 // the following parameters:
32 //  prod: the product name
33 //  ver: the product version
34 //  symbol_file: the breakpad format symbol file
35 
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>  // Must come first
38 #endif
39 
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 
44 #include <string>
45 
46 #include "common/linux/http_upload.h"
47 #include "common/path_helper.h"
48 #include "common/using_std_string.h"
49 
50 using google_breakpad::HTTPUpload;
51 
52 struct Options {
53   string minidumpPath;
54   string uploadURLStr;
55   string product;
56   string version;
57   string proxy;
58   string proxy_user_pwd;
59   bool success;
60 };
61 
62 //=============================================================================
Start(Options * options)63 static void Start(Options *options) {
64   std::map<string, string> parameters;
65   // Add parameters
66   parameters["prod"] = options->product;
67   parameters["ver"] = options->version;
68 
69   std::map<string, string> files;
70   files["upload_file_minidump"] = options->minidumpPath;
71 
72   // Send it
73   string response, error;
74   bool success = HTTPUpload::SendRequest(options->uploadURLStr,
75                                          parameters,
76                                          files,
77                                          options->proxy,
78                                          options->proxy_user_pwd,
79                                          "",
80                                          &response,
81                                          NULL,
82                                          &error);
83 
84   if (success) {
85     printf("Successfully sent the minidump file.\n");
86   } else {
87     printf("Failed to send minidump: %s\n", error.c_str());
88   }
89   printf("Response:\n");
90   printf("%s\n", response.c_str());
91   options->success = success;
92 }
93 
94 //=============================================================================
95 static void
Usage(int argc,const char * argv[])96 Usage(int argc, const char *argv[]) {
97   fprintf(stderr, "Submit minidump information.\n");
98   fprintf(stderr,
99           "Usage: %s [options...] -p <product> -v <version> <minidump> "
100           "<upload-URL>\n",
101           google_breakpad::BaseName(argv[0]).c_str());
102   fprintf(stderr, "Options:\n");
103   fprintf(stderr, "<minidump> should be a minidump.\n");
104   fprintf(stderr, "<upload-URL> is the destination for the upload\n");
105 
106   fprintf(stderr, "-p:\t <product> Product name\n");
107   fprintf(stderr, "-v:\t <version> Product version\n");
108   fprintf(stderr, "-x:\t <host[:port]> Use HTTP proxy on given port\n");
109   fprintf(stderr, "-u:\t <user[:password]> Set proxy user and password\n");
110   fprintf(stderr, "-h:\t Usage\n");
111   fprintf(stderr, "-?:\t Usage\n");
112 }
113 
114 //=============================================================================
115 static void
SetupOptions(int argc,const char * argv[],Options * options)116 SetupOptions(int argc, const char *argv[], Options *options) {
117   extern int optind;
118   int ch;
119 
120   while ((ch = getopt(argc, (char * const*)argv, "p:u:v:x:h?")) != -1) {
121     switch (ch) {
122       case 'p':
123         options->product = optarg;
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 
135       default:
136         fprintf(stderr, "Invalid option '%c'\n", ch);
137         Usage(argc, argv);
138         exit(1);
139         break;
140     }
141   }
142 
143   if ((argc - optind) != 2) {
144     fprintf(stderr, "%s: Missing symbols file and/or upload-URL\n", argv[0]);
145     Usage(argc, argv);
146     exit(1);
147   }
148 
149   options->minidumpPath = argv[optind];
150   options->uploadURLStr = argv[optind + 1];
151 }
152 
153 //=============================================================================
main(int argc,const char * argv[])154 int main(int argc, const char* argv[]) {
155   Options options;
156   SetupOptions(argc, argv, &options);
157   Start(&options);
158   return options.success ? 0 : 1;
159 }
160