Google Breakpad之六,mac平台crash

E文-》http://code.google.com/p/google-breakpad/wiki/MacBreakpadStarterGuide

 有时候英文的意会,比中文翻译更能帮助理解。

如何把breakpad加入你的mac程序

  • How To Add Breakpad To Your Mac Client Application
    • Preparing a binary build of Breakpad for use in your tree
    • Building the requisite targets
    • Adding Breakpad.framework
    • Copy Breakpad into your Application Package
    • Add a New Run Script build phase
    • Adjust the Project Settings
    • Write Code!
    • Configure Breakpad
    • Build Your Application
    • Verify
    • Upload the symbol file
    • Final Notes

这是一篇按部就班来编译breakpad,在mac程序中使用的文档

准备一个用于你的源码树的Breakpad的库

你可以编译二进制库来用或者在工程中直接用源码,推荐使用前者,因为使用breakpad源码容易出现问题(matching up configuration names),而且breakpad库需要改动的时候很少。

编译必须的东西

breakpad签出的所有目录都和src是相关的。

  • 编译client/mac/Breakpad.xcodeproj的Release版所有目标。
  • 执行cp -R client/mac/build/Release/Breakpad.framework <你的源码源码树的位置>
  • 在 tools/mac/dump_syms 下,编译dump_syms.xcodeproj,拷贝tools/mac/dump_syms/build/Release/dump_syms 到一个安全的在编译时可以运行的目录。

添加 Breakpad.framework

在你程序的framework下,添加Breakpad.Framework到你的项目的framework设置中。当您选择它从文件选择器,它会让你选择一个目标,将它添加到;前进和检查相关的应用程序。

拷贝breakpad到你的程序包

拷贝breakpad到你的程序包,使它运行时可被找到。

去目标的章节Xcode项目窗口。单击披露三角形,揭示了您的应用程序的构建阶段。使用上下文菜单(控制点击)添加一个新的拷贝文件。在新phase的新‘Get Info’的通用面板上,设置目的地址指向‘Frameworks’,关闭‘Info’面板,使用上下文菜单来重命名你的新phase‘Copy Frameworks’,拖动Breakpad进这个复制的Frameworks phase。拖动的复制框架现在又拖到这个副本Breakpad框架阶段。拖动它不管它出现在项目文件树哪里。

添加新的运行脚本编译 phase

在最后的build phases,添加一个新的build phase。在你的工程里,Xcode调用 /usr/bin/strip前会先运行它。这是个你可以调用 dump_sym 输出symbols的地方。对我而言,相关的如下:

#!/bin/sh
$TOOL_DIR=<location of dump_syms from step 3 above>

"$TOOL_DIR/dump_syms" -a ppc "$PROD" > "$TARGET_NAME ppc.breakpad"

"$TOOL_DIR/dump_syms" -a i386 "$PROD" > "$TARGET_NAME i386.breakpad"

调整工程设置

  • 开启Separate Strip,
  • 设置Strip 的风格成: Non-Global Symbols.

写代码!

你需要一个像NSApplication一样的对象。 在它的头文件中,你需要增加:

  1. 为Breakpad添加一个 ivar。
  2. 一个applicationShouldTerminate:(NSApplication* sender) 消息的声明。
#import <Breakpad/Breakpad.h>

@interface BreakpadTest : NSObject {
   .
   .
   .
   BreakpadRef breakpad;
   .
   .
   .
}
.
.
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
.
.
@end

进入你的对象的实现文件中,

  1. 增加如下的InitBreakpad方法
  2. 修改你的awakeFromNib 方法像下面一样,
  3. 修改/增加你的程序的委派方法像下面一样
static BreakpadRef InitBreakpad(void) {
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  BreakpadRef breakpad = 0;
  NSDictionary *plist = [[NSBundle mainBundle] infoDictionary];
  if (plist) {
    // Note: version 1.0.0.4 of the framework changed the type of the argument 
    // from CFDictionaryRef to NSDictionary * on the next line:
    breakpad = BreakpadCreate(plist);
  }
  [pool release];
  return breakpad;
}

- (void)awakeFromNib {
  breakpad = InitBreakpad();
}

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
  BreakpadRelease(breakpad);
  return NSTerminateNow;
}

配置Breakpad

为程序配置Breakpad 。

  1. 看看Breakpad.h中的 Breakpad.framework如下的关键点,传给BreakpadCreate()的默认值、以及相关描述。
  2. 增加/编辑Breakpad  传给BreakpadCreate()在字典的特定入口 -- 就像你程序的info plist.

  Notifier Info.plist的示例:

  <key>BreakpadProduct</key><string>Google_Notifier_Mac</string>
  <key>BreakpadProductDisplay</key><string>${PRODUCT_NAME}</string>

编译你的程序

基本上搞定!

核实

复核:

你的程序有如下的包内容:myApp.app/Contents/Frameworks/Breakpad.framework.

符号文件有合理的内容 (用文本编辑器查看)

再看一下项目中复制的Frameworks phase, 泄露.h文件? 选中并删除它们。 (如果你拖拽了一串文件进你的工程, Xcode 经常复制你的.h文件进build,泄露谷歌的隐私,需警惕!)

upload符号文件

配置你的编译路径,使minidump processor可访问的位置来存储符号.。tools/mac/symupload工具能HTTP的POST方式发送符号文件。测试

配置breakpad发送,在Info.plist加入URL

<key>BreakpadURL</key>
<string>upload URL</string>
<key>BreakpadReportInterval</key>
<string>30</string>

终了

Breakpad检查是否运行在debugger方式,是的话啥都不干。你可以设置shell的 BREAKPAD_IGNORE_DEBUGGER为非0值来强制它干活。 写代码使用#if DEBUG 来控制编译时代码被编译。查看//depot/googlemac/GoogleNotifier/main.m示例。示例,当你的程序forks()(就是开子进程)时, exception handlers会为子进程而重置。所以必须重新初始化 Breakpad,否则异常会被苹果的 Crash Reporter 处理。

你可能感兴趣的:(Google Breakpad之六,mac平台crash)