Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // Copyright (c) 2006, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | |
michael@0 | 30 | // symupload.m: Upload a symbol file to a HTTP server. The upload is sent as |
michael@0 | 31 | // a multipart/form-data POST request with the following parameters: |
michael@0 | 32 | // code_file: the basename of the module, e.g. "app" |
michael@0 | 33 | // debug_file: the basename of the debugging file, e.g. "app" |
michael@0 | 34 | // debug_identifier: the debug file's identifier, usually consisting of |
michael@0 | 35 | // the guid and age embedded in the pdb, e.g. |
michael@0 | 36 | // "11111111BBBB3333DDDD555555555555F" |
michael@0 | 37 | // os: the operating system that the module was built for |
michael@0 | 38 | // cpu: the CPU that the module was built for (x86 or ppc) |
michael@0 | 39 | // symbol_file: the contents of the breakpad-format symbol file |
michael@0 | 40 | |
michael@0 | 41 | #include <unistd.h> |
michael@0 | 42 | |
michael@0 | 43 | #include <Foundation/Foundation.h> |
michael@0 | 44 | #include "HTTPMultipartUpload.h" |
michael@0 | 45 | |
michael@0 | 46 | typedef struct { |
michael@0 | 47 | NSString *symbolsPath; |
michael@0 | 48 | NSString *uploadURLStr; |
michael@0 | 49 | BOOL success; |
michael@0 | 50 | } Options; |
michael@0 | 51 | |
michael@0 | 52 | //============================================================================= |
michael@0 | 53 | static NSArray *ModuleDataForSymbolFile(NSString *file) { |
michael@0 | 54 | NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:file]; |
michael@0 | 55 | NSData *data = [fh readDataOfLength:1024]; |
michael@0 | 56 | NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; |
michael@0 | 57 | NSScanner *scanner = [NSScanner scannerWithString:str]; |
michael@0 | 58 | NSString *line; |
michael@0 | 59 | NSMutableArray *parts = nil; |
michael@0 | 60 | const int MODULE_ID_INDEX = 3; |
michael@0 | 61 | |
michael@0 | 62 | if ([scanner scanUpToString:@"\n" intoString:&line]) { |
michael@0 | 63 | parts = [[NSMutableArray alloc] init]; |
michael@0 | 64 | NSScanner *moduleInfoScanner = [NSScanner scannerWithString:line]; |
michael@0 | 65 | NSString *moduleInfo; |
michael@0 | 66 | // Get everything BEFORE the module name. None of these properties |
michael@0 | 67 | // can have spaces. |
michael@0 | 68 | for (int i = 0; i <= MODULE_ID_INDEX; i++) { |
michael@0 | 69 | [moduleInfoScanner scanUpToString:@" " intoString:&moduleInfo]; |
michael@0 | 70 | [parts addObject:moduleInfo]; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | // Now get the module name. This can have a space so we scan to |
michael@0 | 74 | // the end of the line. |
michael@0 | 75 | [moduleInfoScanner scanUpToString:@"\n" intoString:&moduleInfo]; |
michael@0 | 76 | [parts addObject:moduleInfo]; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | [str release]; |
michael@0 | 80 | |
michael@0 | 81 | return parts; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | //============================================================================= |
michael@0 | 85 | static NSString *CompactIdentifier(NSString *uuid) { |
michael@0 | 86 | NSMutableString *str = [NSMutableString stringWithString:uuid]; |
michael@0 | 87 | [str replaceOccurrencesOfString:@"-" withString:@"" options:0 |
michael@0 | 88 | range:NSMakeRange(0, [str length])]; |
michael@0 | 89 | |
michael@0 | 90 | return str; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | //============================================================================= |
michael@0 | 94 | static void Start(Options *options) { |
michael@0 | 95 | NSURL *url = [NSURL URLWithString:options->uploadURLStr]; |
michael@0 | 96 | HTTPMultipartUpload *ul = [[HTTPMultipartUpload alloc] initWithURL:url]; |
michael@0 | 97 | NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; |
michael@0 | 98 | NSArray *moduleParts = ModuleDataForSymbolFile(options->symbolsPath); |
michael@0 | 99 | NSMutableString *compactedID = |
michael@0 | 100 | [NSMutableString stringWithString:[moduleParts objectAtIndex:3]]; |
michael@0 | 101 | [compactedID replaceOccurrencesOfString:@"-" withString:@"" options:0 |
michael@0 | 102 | range:NSMakeRange(0, [compactedID length])]; |
michael@0 | 103 | |
michael@0 | 104 | // Add parameters |
michael@0 | 105 | [parameters setObject:compactedID forKey:@"debug_identifier"]; |
michael@0 | 106 | |
michael@0 | 107 | // MODULE <os> <cpu> <uuid> <module-name> |
michael@0 | 108 | // 0 1 2 3 4 |
michael@0 | 109 | [parameters setObject:[moduleParts objectAtIndex:1] forKey:@"os"]; |
michael@0 | 110 | [parameters setObject:[moduleParts objectAtIndex:2] forKey:@"cpu"]; |
michael@0 | 111 | [parameters setObject:[moduleParts objectAtIndex:4] forKey:@"debug_file"]; |
michael@0 | 112 | [parameters setObject:[moduleParts objectAtIndex:4] forKey:@"code_file"]; |
michael@0 | 113 | [ul setParameters:parameters]; |
michael@0 | 114 | |
michael@0 | 115 | NSArray *keys = [parameters allKeys]; |
michael@0 | 116 | int count = [keys count]; |
michael@0 | 117 | for (int i = 0; i < count; ++i) { |
michael@0 | 118 | NSString *key = [keys objectAtIndex:i]; |
michael@0 | 119 | NSString *value = [parameters objectForKey:key]; |
michael@0 | 120 | fprintf(stdout, "'%s' = '%s'\n", [key UTF8String], |
michael@0 | 121 | [value UTF8String]); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | // Add file |
michael@0 | 125 | [ul addFileAtPath:options->symbolsPath name:@"symbol_file"]; |
michael@0 | 126 | |
michael@0 | 127 | // Send it |
michael@0 | 128 | NSError *error = nil; |
michael@0 | 129 | NSData *data = [ul send:&error]; |
michael@0 | 130 | NSString *result = [[NSString alloc] initWithData:data |
michael@0 | 131 | encoding:NSUTF8StringEncoding]; |
michael@0 | 132 | int status = [[ul response] statusCode]; |
michael@0 | 133 | |
michael@0 | 134 | fprintf(stdout, "Send: %s\n", error ? [[error description] UTF8String] : |
michael@0 | 135 | "No Error"); |
michael@0 | 136 | fprintf(stdout, "Response: %d\n", status); |
michael@0 | 137 | fprintf(stdout, "Result: %lu bytes\n%s\n", |
michael@0 | 138 | (unsigned long)[data length], [result UTF8String]); |
michael@0 | 139 | |
michael@0 | 140 | [result release]; |
michael@0 | 141 | [ul release]; |
michael@0 | 142 | options->success = !error && status==200; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | //============================================================================= |
michael@0 | 146 | static void |
michael@0 | 147 | Usage(int argc, const char *argv[]) { |
michael@0 | 148 | fprintf(stderr, "Submit symbol information.\n"); |
michael@0 | 149 | fprintf(stderr, "Usage: %s <symbols> <upload-URL>\n", argv[0]); |
michael@0 | 150 | fprintf(stderr, "<symbols> should be created by using the dump_syms tool.\n"); |
michael@0 | 151 | fprintf(stderr, "<upload-URL> is the destination for the upload\n"); |
michael@0 | 152 | fprintf(stderr, "\t-h: Usage\n"); |
michael@0 | 153 | fprintf(stderr, "\t-?: Usage\n"); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | //============================================================================= |
michael@0 | 157 | static void |
michael@0 | 158 | SetupOptions(int argc, const char *argv[], Options *options) { |
michael@0 | 159 | extern int optind; |
michael@0 | 160 | char ch; |
michael@0 | 161 | |
michael@0 | 162 | while ((ch = getopt(argc, (char * const *)argv, "h?")) != -1) { |
michael@0 | 163 | switch (ch) { |
michael@0 | 164 | default: |
michael@0 | 165 | Usage(argc, argv); |
michael@0 | 166 | exit(0); |
michael@0 | 167 | break; |
michael@0 | 168 | } |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | if ((argc - optind) != 2) { |
michael@0 | 172 | fprintf(stderr, "%s: Missing symbols file and/or upload-URL\n", argv[0]); |
michael@0 | 173 | Usage(argc, argv); |
michael@0 | 174 | exit(1); |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | options->symbolsPath = [NSString stringWithUTF8String:argv[optind]]; |
michael@0 | 178 | options->uploadURLStr = [NSString stringWithUTF8String:argv[optind + 1]]; |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | //============================================================================= |
michael@0 | 182 | int main (int argc, const char * argv[]) { |
michael@0 | 183 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
michael@0 | 184 | Options options; |
michael@0 | 185 | |
michael@0 | 186 | bzero(&options, sizeof(Options)); |
michael@0 | 187 | SetupOptions(argc, argv, &options); |
michael@0 | 188 | Start(&options); |
michael@0 | 189 | |
michael@0 | 190 | [pool release]; |
michael@0 | 191 | return options.success ? 0 : 1; |
michael@0 | 192 | } |