xref: /aosp_15_r20/external/google-breakpad/src/common/mac/minidump_upload.m (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.m: Upload a minidump to a HTTP server.  The upload is sent as
30// a multipart/form-data POST request with the following parameters:
31//  prod: the product name
32//  ver: the product version
33//  symbol_file: the breakpad format symbol file
34
35#import <unistd.h>
36
37#import <Foundation/Foundation.h>
38
39#import "common/mac/HTTPMultipartUpload.h"
40
41typedef struct {
42  NSString* minidumpPath;
43  NSString* uploadURLStr;
44  NSString* product;
45  NSString* version;
46  BOOL success;
47} Options;
48
49//=============================================================================
50static void Start(Options* options) {
51  NSURL* url = [NSURL URLWithString:options->uploadURLStr];
52  HTTPMultipartUpload* ul = [[HTTPMultipartUpload alloc] initWithURL:url];
53  NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
54
55  // Add parameters
56  [parameters setObject:options->product forKey:@"prod"];
57  [parameters setObject:options->version forKey:@"ver"];
58  [ul setParameters:parameters];
59
60  // Add file
61  [ul addFileAtPath:options->minidumpPath name:@"upload_file_minidump"];
62
63  // Send it
64  NSError* error = nil;
65  NSData* data = [ul send:&error];
66  NSString* result = [[NSString alloc] initWithData:data
67                                           encoding:NSUTF8StringEncoding];
68
69  NSLog(@"Send: %@", error ? [error description] : @"No Error");
70  NSLog(@"Response: %ld", (long)[[ul response] statusCode]);
71  NSLog(@"Result: %lu bytes\n%@", (unsigned long)[data length], result);
72
73  [result release];
74  [ul release];
75  options->success = !error;
76}
77
78//=============================================================================
79static void Usage(int argc, const char* argv[]) {
80  fprintf(stderr, "Submit minidump information.\n");
81  fprintf(stderr,
82          "Usage: %s -p <product> -v <version> <minidump> "
83          "<upload-URL>\n",
84          argv[0]);
85  fprintf(stderr, "<minidump> should be a minidump.\n");
86  fprintf(stderr, "<upload-URL> is the destination for the upload\n");
87
88  fprintf(stderr, "\t-h: Usage\n");
89  fprintf(stderr, "\t-?: Usage\n");
90}
91
92//=============================================================================
93static void SetupOptions(int argc, const char* argv[], Options* options) {
94  extern int optind;
95  char ch;
96
97  while ((ch = getopt(argc, (char* const*)argv, "p:v:h?")) != -1) {
98    switch (ch) {
99      case 'p':
100        options->product = [NSString stringWithUTF8String:optarg];
101        break;
102      case 'v':
103        options->version = [NSString stringWithUTF8String:optarg];
104        break;
105
106      default:
107        Usage(argc, argv);
108        exit(0);
109        break;
110    }
111  }
112
113  if ((argc - optind) != 2) {
114    fprintf(stderr, "%s: Missing symbols file and/or upload-URL\n", argv[0]);
115    Usage(argc, argv);
116    exit(1);
117  }
118
119  options->minidumpPath = [NSString stringWithUTF8String:argv[optind]];
120  options->uploadURLStr = [NSString stringWithUTF8String:argv[optind + 1]];
121}
122
123//=============================================================================
124int main(int argc, const char* argv[]) {
125  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
126  Options options;
127
128  bzero(&options, sizeof(Options));
129  SetupOptions(argc, argv, &options);
130  Start(&options);
131
132  [pool release];
133  return options.success ? 0 : 1;
134}
135