1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/tools/mac/symupload/symupload.m Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,192 @@ 1.4 +// Copyright (c) 2006, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 + 1.33 +// symupload.m: Upload a symbol file to a HTTP server. The upload is sent as 1.34 +// a multipart/form-data POST request with the following parameters: 1.35 +// code_file: the basename of the module, e.g. "app" 1.36 +// debug_file: the basename of the debugging file, e.g. "app" 1.37 +// debug_identifier: the debug file's identifier, usually consisting of 1.38 +// the guid and age embedded in the pdb, e.g. 1.39 +// "11111111BBBB3333DDDD555555555555F" 1.40 +// os: the operating system that the module was built for 1.41 +// cpu: the CPU that the module was built for (x86 or ppc) 1.42 +// symbol_file: the contents of the breakpad-format symbol file 1.43 + 1.44 +#include <unistd.h> 1.45 + 1.46 +#include <Foundation/Foundation.h> 1.47 +#include "HTTPMultipartUpload.h" 1.48 + 1.49 +typedef struct { 1.50 + NSString *symbolsPath; 1.51 + NSString *uploadURLStr; 1.52 + BOOL success; 1.53 +} Options; 1.54 + 1.55 +//============================================================================= 1.56 +static NSArray *ModuleDataForSymbolFile(NSString *file) { 1.57 + NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:file]; 1.58 + NSData *data = [fh readDataOfLength:1024]; 1.59 + NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 1.60 + NSScanner *scanner = [NSScanner scannerWithString:str]; 1.61 + NSString *line; 1.62 + NSMutableArray *parts = nil; 1.63 + const int MODULE_ID_INDEX = 3; 1.64 + 1.65 + if ([scanner scanUpToString:@"\n" intoString:&line]) { 1.66 + parts = [[NSMutableArray alloc] init]; 1.67 + NSScanner *moduleInfoScanner = [NSScanner scannerWithString:line]; 1.68 + NSString *moduleInfo; 1.69 + // Get everything BEFORE the module name. None of these properties 1.70 + // can have spaces. 1.71 + for (int i = 0; i <= MODULE_ID_INDEX; i++) { 1.72 + [moduleInfoScanner scanUpToString:@" " intoString:&moduleInfo]; 1.73 + [parts addObject:moduleInfo]; 1.74 + } 1.75 + 1.76 + // Now get the module name. This can have a space so we scan to 1.77 + // the end of the line. 1.78 + [moduleInfoScanner scanUpToString:@"\n" intoString:&moduleInfo]; 1.79 + [parts addObject:moduleInfo]; 1.80 + } 1.81 + 1.82 + [str release]; 1.83 + 1.84 + return parts; 1.85 +} 1.86 + 1.87 +//============================================================================= 1.88 +static NSString *CompactIdentifier(NSString *uuid) { 1.89 + NSMutableString *str = [NSMutableString stringWithString:uuid]; 1.90 + [str replaceOccurrencesOfString:@"-" withString:@"" options:0 1.91 + range:NSMakeRange(0, [str length])]; 1.92 + 1.93 + return str; 1.94 +} 1.95 + 1.96 +//============================================================================= 1.97 +static void Start(Options *options) { 1.98 + NSURL *url = [NSURL URLWithString:options->uploadURLStr]; 1.99 + HTTPMultipartUpload *ul = [[HTTPMultipartUpload alloc] initWithURL:url]; 1.100 + NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; 1.101 + NSArray *moduleParts = ModuleDataForSymbolFile(options->symbolsPath); 1.102 + NSMutableString *compactedID = 1.103 + [NSMutableString stringWithString:[moduleParts objectAtIndex:3]]; 1.104 + [compactedID replaceOccurrencesOfString:@"-" withString:@"" options:0 1.105 + range:NSMakeRange(0, [compactedID length])]; 1.106 + 1.107 + // Add parameters 1.108 + [parameters setObject:compactedID forKey:@"debug_identifier"]; 1.109 + 1.110 + // MODULE <os> <cpu> <uuid> <module-name> 1.111 + // 0 1 2 3 4 1.112 + [parameters setObject:[moduleParts objectAtIndex:1] forKey:@"os"]; 1.113 + [parameters setObject:[moduleParts objectAtIndex:2] forKey:@"cpu"]; 1.114 + [parameters setObject:[moduleParts objectAtIndex:4] forKey:@"debug_file"]; 1.115 + [parameters setObject:[moduleParts objectAtIndex:4] forKey:@"code_file"]; 1.116 + [ul setParameters:parameters]; 1.117 + 1.118 + NSArray *keys = [parameters allKeys]; 1.119 + int count = [keys count]; 1.120 + for (int i = 0; i < count; ++i) { 1.121 + NSString *key = [keys objectAtIndex:i]; 1.122 + NSString *value = [parameters objectForKey:key]; 1.123 + fprintf(stdout, "'%s' = '%s'\n", [key UTF8String], 1.124 + [value UTF8String]); 1.125 + } 1.126 + 1.127 + // Add file 1.128 + [ul addFileAtPath:options->symbolsPath name:@"symbol_file"]; 1.129 + 1.130 + // Send it 1.131 + NSError *error = nil; 1.132 + NSData *data = [ul send:&error]; 1.133 + NSString *result = [[NSString alloc] initWithData:data 1.134 + encoding:NSUTF8StringEncoding]; 1.135 + int status = [[ul response] statusCode]; 1.136 + 1.137 + fprintf(stdout, "Send: %s\n", error ? [[error description] UTF8String] : 1.138 + "No Error"); 1.139 + fprintf(stdout, "Response: %d\n", status); 1.140 + fprintf(stdout, "Result: %lu bytes\n%s\n", 1.141 + (unsigned long)[data length], [result UTF8String]); 1.142 + 1.143 + [result release]; 1.144 + [ul release]; 1.145 + options->success = !error && status==200; 1.146 +} 1.147 + 1.148 +//============================================================================= 1.149 +static void 1.150 +Usage(int argc, const char *argv[]) { 1.151 + fprintf(stderr, "Submit symbol information.\n"); 1.152 + fprintf(stderr, "Usage: %s <symbols> <upload-URL>\n", argv[0]); 1.153 + fprintf(stderr, "<symbols> should be created by using the dump_syms tool.\n"); 1.154 + fprintf(stderr, "<upload-URL> is the destination for the upload\n"); 1.155 + fprintf(stderr, "\t-h: Usage\n"); 1.156 + fprintf(stderr, "\t-?: Usage\n"); 1.157 +} 1.158 + 1.159 +//============================================================================= 1.160 +static void 1.161 +SetupOptions(int argc, const char *argv[], Options *options) { 1.162 + extern int optind; 1.163 + char ch; 1.164 + 1.165 + while ((ch = getopt(argc, (char * const *)argv, "h?")) != -1) { 1.166 + switch (ch) { 1.167 + default: 1.168 + Usage(argc, argv); 1.169 + exit(0); 1.170 + break; 1.171 + } 1.172 + } 1.173 + 1.174 + if ((argc - optind) != 2) { 1.175 + fprintf(stderr, "%s: Missing symbols file and/or upload-URL\n", argv[0]); 1.176 + Usage(argc, argv); 1.177 + exit(1); 1.178 + } 1.179 + 1.180 + options->symbolsPath = [NSString stringWithUTF8String:argv[optind]]; 1.181 + options->uploadURLStr = [NSString stringWithUTF8String:argv[optind + 1]]; 1.182 +} 1.183 + 1.184 +//============================================================================= 1.185 +int main (int argc, const char * argv[]) { 1.186 + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 1.187 + Options options; 1.188 + 1.189 + bzero(&options, sizeof(Options)); 1.190 + SetupOptions(argc, argv, &options); 1.191 + Start(&options); 1.192 + 1.193 + [pool release]; 1.194 + return options.success ? 0 : 1; 1.195 +}