1
// Copyright (c) 2006, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
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
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.
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.
30
// symupload.m: Upload a symbol file to a HTTP server. The upload is sent as
31
// a multipart/form-data POST request with the following parameters:
32
// code_file: the basename of the module, e.g. "app"
33
// debug_file: the basename of the debugging file, e.g. "app"
34
// debug_identifier: the debug file's identifier, usually consisting of
35
// the guid and age embedded in the pdb, e.g.
36
// "11111111BBBB3333DDDD555555555555F"
37
// os: the operating system that the module was built for
38
// cpu: the CPU that the module was built for (x86 or ppc)
39
// symbol_file: the contents of the breakpad-format symbol file
43
#include <Foundation/Foundation.h>
44
#include "HTTPMultipartUpload.h"
47
NSString *symbolsPath;
48
NSString *uploadURLStr;
52
//=============================================================================
53
static NSArray *ModuleDataForSymbolFile(NSString *file) {
54
NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:file];
55
NSData *data = [fh readDataOfLength:1024];
56
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
57
NSScanner *scanner = [NSScanner scannerWithString:str];
59
NSMutableArray *parts = nil;
60
const int MODULE_ID_INDEX = 3;
62
if ([scanner scanUpToString:@"\n" intoString:&line]) {
63
parts = [[NSMutableArray alloc] init];
64
NSScanner *moduleInfoScanner = [NSScanner scannerWithString:line];
66
// Get everything BEFORE the module name. None of these properties
68
for (int i = 0; i <= MODULE_ID_INDEX; i++) {
69
[moduleInfoScanner scanUpToString:@" " intoString:&moduleInfo];
70
[parts addObject:moduleInfo];
73
// Now get the module name. This can have a space so we scan to
74
// the end of the line.
75
[moduleInfoScanner scanUpToString:@"\n" intoString:&moduleInfo];
76
[parts addObject:moduleInfo];
84
//=============================================================================
85
static NSString *CompactIdentifier(NSString *uuid) {
86
NSMutableString *str = [NSMutableString stringWithString:uuid];
87
[str replaceOccurrencesOfString:@"-" withString:@"" options:0
88
range:NSMakeRange(0, [str length])];
93
//=============================================================================
94
static void Start(Options *options) {
95
NSURL *url = [NSURL URLWithString:options->uploadURLStr];
96
HTTPMultipartUpload *ul = [[HTTPMultipartUpload alloc] initWithURL:url];
97
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
98
NSArray *moduleParts = ModuleDataForSymbolFile(options->symbolsPath);
99
NSMutableString *compactedID =
100
[NSMutableString stringWithString:[moduleParts objectAtIndex:3]];
101
[compactedID replaceOccurrencesOfString:@"-" withString:@"" options:0
102
range:NSMakeRange(0, [compactedID length])];
105
[parameters setObject:compactedID forKey:@"debug_identifier"];
107
// MODULE <os> <cpu> <uuid> <module-name>
109
[parameters setObject:[moduleParts objectAtIndex:1] forKey:@"os"];
110
[parameters setObject:[moduleParts objectAtIndex:2] forKey:@"cpu"];
111
[parameters setObject:[moduleParts objectAtIndex:4] forKey:@"debug_file"];
112
[parameters setObject:[moduleParts objectAtIndex:4] forKey:@"code_file"];
113
[ul setParameters:parameters];
115
NSArray *keys = [parameters allKeys];
116
int count = [keys count];
117
for (int i = 0; i < count; ++i) {
118
NSString *key = [keys objectAtIndex:i];
119
NSString *value = [parameters objectForKey:key];
120
fprintf(stdout, "'%s' = '%s'\n", [key UTF8String],
125
[ul addFileAtPath:options->symbolsPath name:@"symbol_file"];
128
NSError *error = nil;
129
NSData *data = [ul send:&error];
130
NSString *result = [[NSString alloc] initWithData:data
131
encoding:NSUTF8StringEncoding];
133
fprintf(stdout, "Send: %s\n", error ? [[error description] UTF8String] :
135
fprintf(stdout, "Response: %d\n", [[ul response] statusCode]);
136
fprintf(stdout, "Result: %d bytes\n%s\n", [data length], [result UTF8String]);
140
options->success = !error;
143
//=============================================================================
145
Usage(int argc, const char *argv[]) {
146
fprintf(stderr, "Submit symbol information.\n");
147
fprintf(stderr, "Usage: %s <symbols> <upload-URL>\n", argv[0]);
148
fprintf(stderr, "<symbols> should be created by using the dump_syms tool.\n");
149
fprintf(stderr, "<upload-URL> is the destination for the upload\n");
150
fprintf(stderr, "\t-h: Usage\n");
151
fprintf(stderr, "\t-?: Usage\n");
154
//=============================================================================
156
SetupOptions(int argc, const char *argv[], Options *options) {
160
while ((ch = getopt(argc, (char * const *)argv, "h?")) != -1) {
169
if ((argc - optind) != 2) {
170
fprintf(stderr, "%s: Missing symbols file and/or upload-URL\n", argv[0]);
175
options->symbolsPath = [NSString stringWithCString:argv[optind]];
176
options->uploadURLStr = [NSString stringWithCString:argv[optind + 1]];
179
//=============================================================================
180
int main (int argc, const char * argv[]) {
181
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
184
bzero(&options, sizeof(Options));
185
SetupOptions(argc, argv, &options);
189
return options.success ? 0 : 1;