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 // Send it
66 string response, error;
67 bool success = HTTPUpload::SendRequest(options->uploadURLStr,
68 parameters,
69 options->minidumpPath,
70 "upload_file_minidump",
71 options->proxy,
72 options->proxy_user_pwd,
73 "",
74 &response,
75 NULL,
76 &error);
77
78 if (success) {
79 printf("Successfully sent the minidump file.\n");
80 } else {
81 printf("Failed to send minidump: %s\n", error.c_str());
82 }
83 printf("Response:\n");
84 printf("%s\n", response.c_str());
85 options->success = success;
86 }
87
88 //=============================================================================
89 static void
Usage(int argc,const char * argv[])90 Usage(int argc, const char *argv[]) {
91 fprintf(stderr, "Submit minidump information.\n");
92 fprintf(stderr, "Usage: %s [options...] -p <product> -v <version> <minidump> "
93 "<upload-URL>\n", argv[0]);
94 fprintf(stderr, "Options:\n");
95 fprintf(stderr, "<minidump> should be a minidump.\n");
96 fprintf(stderr, "<upload-URL> is the destination for the upload\n");
97
98 fprintf(stderr, "-p:\t <product> Product name\n");
99 fprintf(stderr, "-v:\t <version> Product version\n");
100 fprintf(stderr, "-x:\t <host[:port]> Use HTTP proxy on given port\n");
101 fprintf(stderr, "-u:\t <user[:password]> Set proxy user and password\n");
102 fprintf(stderr, "-h:\t Usage\n");
103 fprintf(stderr, "-?:\t Usage\n");
104 }
105
106 //=============================================================================
107 static void
SetupOptions(int argc,const char * argv[],Options * options)108 SetupOptions(int argc, const char *argv[], Options *options) {
109 extern int optind;
110 int ch;
111
112 while ((ch = getopt(argc, (char * const *)argv, "p:u:v:x:h?")) != -1) {
113 switch (ch) {
114 case 'p':
115 options->product = optarg;
116 break;
117 case 'u':
118 options->proxy_user_pwd = optarg;
119 break;
120 case 'v':
121 options->version = optarg;
122 break;
123 case 'x':
124 options->proxy = optarg;
125 break;
126
127 default:
128 fprintf(stderr, "Invalid option '%c'\n", ch);
129 Usage(argc, argv);
130 exit(1);
131 break;
132 }
133 }
134
135 if ((argc - optind) != 2) {
136 fprintf(stderr, "%s: Missing symbols file and/or upload-URL\n", argv[0]);
137 Usage(argc, argv);
138 exit(1);
139 }
140
141 options->minidumpPath = argv[optind];
142 options->uploadURLStr = argv[optind + 1];
143 }
144
145 //=============================================================================
main(int argc,const char * argv[])146 int main(int argc, const char* argv[]) {
147 Options options;
148 SetupOptions(argc, argv, &options);
149 Start(&options);
150 return options.success ? 0 : 1;
151 }
152