1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/client/mac/crash_generation/ConfigFile.mm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,190 @@ 1.4 +// Copyright (c) 2011, 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 +// Utility class that can persist a SimpleStringDictionary to disk. 1.34 + 1.35 +#import "client/mac/crash_generation/ConfigFile.h" 1.36 + 1.37 +#import <Foundation/Foundation.h> 1.38 +#include <stdio.h> 1.39 +#include <sys/time.h> 1.40 + 1.41 +#import "client/apple/Framework/BreakpadDefines.h" 1.42 +#import "common/mac/SimpleStringDictionary.h" 1.43 +#import "GTMDefines.h" 1.44 + 1.45 +#define VERBOSE 0 1.46 + 1.47 +#if VERBOSE 1.48 + bool gDebugLog = true; 1.49 +#else 1.50 + bool gDebugLog = false; 1.51 +#endif 1.52 + 1.53 +#define DEBUGLOG if (gDebugLog) fprintf 1.54 + 1.55 +namespace google_breakpad { 1.56 + 1.57 +//============================================================================= 1.58 +BOOL EnsureDirectoryPathExists(NSString *dirPath) { 1.59 + NSFileManager *mgr = [NSFileManager defaultManager]; 1.60 + 1.61 + NSDictionary *attrs = 1.62 + [NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:0750] 1.63 + forKey:NSFilePosixPermissions]; 1.64 + 1.65 + return [mgr createDirectoryAtPath:dirPath 1.66 + withIntermediateDirectories:YES 1.67 + attributes:attrs 1.68 + error:nil]; 1.69 +} 1.70 + 1.71 +//============================================================================= 1.72 +BOOL ConfigFile::WriteData(const void *data, size_t length) { 1.73 + size_t result = write(config_file_, data, length); 1.74 + 1.75 + return result == length; 1.76 +} 1.77 + 1.78 +//============================================================================= 1.79 +BOOL ConfigFile::AppendConfigData(const char *key, 1.80 + const void *data, size_t length) { 1.81 + assert(config_file_ != -1); 1.82 + 1.83 + if (!key) { 1.84 + DEBUGLOG(stderr, "Breakpad: Missing Key\n"); 1.85 + return NO; 1.86 + } 1.87 + 1.88 + if (!data) { 1.89 + DEBUGLOG(stderr, "Breakpad: Missing data for key: %s\n", key ? key : 1.90 + "<Unknown Key>"); 1.91 + return NO; 1.92 + } 1.93 + 1.94 + // Write the key, \n, length of data (ascii integer), \n, data 1.95 + char buffer[16]; 1.96 + char nl = '\n'; 1.97 + BOOL result = WriteData(key, strlen(key)); 1.98 + 1.99 + snprintf(buffer, sizeof(buffer) - 1, "\n%lu\n", length); 1.100 + result &= WriteData(buffer, strlen(buffer)); 1.101 + result &= WriteData(data, length); 1.102 + result &= WriteData(&nl, 1); 1.103 + return result; 1.104 +} 1.105 + 1.106 +//============================================================================= 1.107 +BOOL ConfigFile::AppendConfigString(const char *key, 1.108 + const char *value) { 1.109 + return AppendConfigData(key, value, strlen(value)); 1.110 +} 1.111 + 1.112 +//============================================================================= 1.113 +BOOL ConfigFile::AppendCrashTimeParameters(const char *processStartTimeString) { 1.114 + // Set process uptime parameter 1.115 + struct timeval tv; 1.116 + gettimeofday(&tv, NULL); 1.117 + 1.118 + char processUptimeString[32], processCrashtimeString[32]; 1.119 + // Set up time if we've received the start time. 1.120 + if (processStartTimeString) { 1.121 + time_t processStartTime = strtol(processStartTimeString, NULL, 10); 1.122 + time_t processUptime = tv.tv_sec - processStartTime; 1.123 + // Store the uptime in milliseconds. 1.124 + sprintf(processUptimeString, "%llu", 1.125 + static_cast<unsigned long long int>(processUptime) * 1000); 1.126 + if (!AppendConfigString(BREAKPAD_PROCESS_UP_TIME, processUptimeString)) 1.127 + return false; 1.128 + } 1.129 + 1.130 + sprintf(processCrashtimeString, "%zd", tv.tv_sec); 1.131 + return AppendConfigString(BREAKPAD_PROCESS_CRASH_TIME, 1.132 + processCrashtimeString); 1.133 +} 1.134 + 1.135 +//============================================================================= 1.136 +void ConfigFile::WriteFile(const char* directory, 1.137 + const SimpleStringDictionary *configurationParameters, 1.138 + const char *dump_dir, 1.139 + const char *minidump_id) { 1.140 + 1.141 + assert(config_file_ == -1); 1.142 + 1.143 + // Open and write out configuration file preamble 1.144 + if (directory) { 1.145 + snprintf(config_file_path_, sizeof(config_file_path_), "%s/Config-XXXXXX", 1.146 + directory); 1.147 + } else { 1.148 + strlcpy(config_file_path_, "/tmp/Config-XXXXXX", 1.149 + sizeof(config_file_path_)); 1.150 + } 1.151 + config_file_ = mkstemp(config_file_path_); 1.152 + 1.153 + if (config_file_ == -1) { 1.154 + DEBUGLOG(stderr, 1.155 + "mkstemp(config_file_path_) == -1 (%s)\n", 1.156 + strerror(errno)); 1.157 + return; 1.158 + } 1.159 + else { 1.160 + DEBUGLOG(stderr, "Writing config file to (%s)\n", config_file_path_); 1.161 + } 1.162 + 1.163 + has_created_file_ = true; 1.164 + 1.165 + // Add the minidump dir 1.166 + AppendConfigString(kReporterMinidumpDirectoryKey, dump_dir); 1.167 + AppendConfigString(kReporterMinidumpIDKey, minidump_id); 1.168 + 1.169 + // Write out the configuration parameters 1.170 + BOOL result = YES; 1.171 + const SimpleStringDictionary &dictionary = *configurationParameters; 1.172 + 1.173 + const KeyValueEntry *entry = NULL; 1.174 + SimpleStringDictionaryIterator iter(dictionary); 1.175 + 1.176 + while ((entry = iter.Next())) { 1.177 + DEBUGLOG(stderr, 1.178 + "config: (%s) -> (%s)\n", 1.179 + entry->GetKey(), 1.180 + entry->GetValue()); 1.181 + result = AppendConfigString(entry->GetKey(), entry->GetValue()); 1.182 + 1.183 + if (!result) 1.184 + break; 1.185 + } 1.186 + AppendCrashTimeParameters( 1.187 + configurationParameters->GetValueForKey(BREAKPAD_PROCESS_START_TIME)); 1.188 + 1.189 + close(config_file_); 1.190 + config_file_ = -1; 1.191 +} 1.192 + 1.193 +} // namespace google_breakpad