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