summaryrefslogtreecommitdiffstats
path: root/chrome/app/breakpad_mac.mm
blob: 33078295369278e586da15207e3fa552d7d10873 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/app/breakpad_mac.h"

#import <Foundation/Foundation.h>

#include "base/base_switches.h"
#import "base/basictypes.h"
#include "base/command_line.h"
#import "base/logging.h"
#import "base/scoped_nsautorelease_pool.h"
#include "base/sys_string_conversions.h"
#import "breakpad/src/client/mac/Framework/Breakpad.h"
#include "chrome/common/child_process_logging.h"
#include "chrome/installer/util/google_update_settings.h"

extern "C" {

namespace {

BreakpadRef gBreakpadRef = NULL;

} // namespace

bool IsCrashReporterDisabled() {
  return gBreakpadRef == NULL;
}

void DestructCrashReporter() {
  if (gBreakpadRef) {
    BreakpadRelease(gBreakpadRef);
    gBreakpadRef = NULL;
  }
}

// Only called for a branded build of Chrome.app.
void InitCrashReporter() {
  DCHECK(gBreakpadRef == NULL);
  base::ScopedNSAutoreleasePool autorelease_pool;

  // Check for Send stats preference. If preference is not specifically turned
  // on then disable crash reporting.
  if (!GoogleUpdateSettings::GetCollectStatsConsent()) {
    LOG(WARNING) << "Breakpad disabled";
    return;
  }

  NSBundle* main_bundle = [NSBundle mainBundle];
  NSString* resource_path = [main_bundle resourcePath];

  NSDictionary* info_dictionary = [main_bundle infoDictionary];
  NSMutableDictionary *breakpad_config = [info_dictionary
                                             mutableCopy];

  // Tell Breakpad where inspector & crash_reporter are.
  NSString *inspector_location = [resource_path
      stringByAppendingPathComponent:@"crash_inspector"];
  NSString *reporter_bundle_location = [resource_path
      stringByAppendingPathComponent:@"crash_report_sender.app"];
  NSString *reporter_location = [[NSBundle
                                      bundleWithPath:reporter_bundle_location]
                                     executablePath];

  [breakpad_config setObject:inspector_location
                      forKey:@BREAKPAD_INSPECTOR_LOCATION];
  [breakpad_config setObject:reporter_location
                      forKey:@BREAKPAD_REPORTER_EXE_LOCATION];

  // Init breakpad
  BreakpadRef breakpad = NULL;
  breakpad = BreakpadCreate(breakpad_config);
  if (!breakpad) {
    LOG(ERROR) << "Breakpad init failed.";
    return;
  }

  // This needs to be set before calling SetCrashKeyValue().
  gBreakpadRef = breakpad;

  // Set breakpad MetaData values.
  // These values are added to the plist when building a branded Chrome.app.
  NSString* version_str = [info_dictionary objectForKey:@BREAKPAD_VERSION];
  SetCrashKeyValue(@"ver", version_str);
  NSString* prod_name_str = [info_dictionary objectForKey:@BREAKPAD_PRODUCT];
  SetCrashKeyValue(@"prod", prod_name_str);
  SetCrashKeyValue(@"plat", @"OS X");

  // Enable child process crashes to include the page url.
  child_process_logging::SetCrashKeyFunctions(
      SetCrashKeyValue, ClearCrashKeyValue);
}

void InitCrashProcessInfo() {
  if (gBreakpadRef == NULL) {
    return;
  }

  // Determine the process type.
  NSString *process_type = @"browser";
  const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
  std::wstring process_type_switch =
      parsed_command_line.GetSwitchValue(switches::kProcessType);
  if (!process_type_switch.empty()) {
    process_type = base::SysWideToNSString(process_type_switch);
  }

  // Store process type in crash dump.
  SetCrashKeyValue(@"ptype", process_type);
}

void SetCrashKeyValue(NSString* key, NSString* value) {
  // Comment repeated from header to prevent confusion:
  // IMPORTANT: On OS X, the key/value pairs are sent to the crash server
  // out of bounds and not recorded on disk in the minidump, this means
  // that if you look at the minidump file locally you won't see them!
  if (gBreakpadRef == NULL) {
    return;
  }

  BreakpadAddUploadParameter(gBreakpadRef, key, value);
}

void ClearCrashKeyValue(NSString* key) {
  if (gBreakpadRef == NULL) {
    return;
  }

  BreakpadRemoveUploadParameter(gBreakpadRef, key);
}

}