Last active
September 13, 2015 17:14
-
-
Save swillits/f1fdb1d013008a30889d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// OSXCrashReporterInfo.h | |
// OSXCrashReporterInfo | |
// | |
// Created by Seth Willits on 10/26/14. | |
// Copyright (c) 2014 Araelium Group. All rights reserved. | |
// | |
#import <CoreFoundation/CoreFoundation.h> | |
void OSXCrashReporterInfoInitialize(void); | |
void __attribute__((overloadable)) OSXCrashReporterInfoSet(const char * string, size_t length); | |
void __attribute__((overloadable)) OSXCrashReporterInfoSet(const char * string); | |
void OSXCrashReporterInfoClear(void); | |
// | |
// OSXCrashReporterInfo.m | |
// OSXCrashReporterInfo | |
// | |
// Created by Seth Willits on 10/26/14. | |
// Copyright (c) 2014 Araelium Group. All rights reserved. | |
// | |
#import "OSXCrashReporterInfo.h" | |
// We declare a symbol with a standard/convention name | |
static char *__crashreporter_info__ = nil; | |
// Mark the __crashreporter_info__ field as "referenced dynamically" | |
// This means it won't get stripped and will be included in the resulting | |
// binary so the crash reporter can see it. | |
asm(".desc ___crashreporter_info__, 0x10"); | |
typedef struct { | |
dispatch_queue_t queue; | |
size_t messageCapacity; | |
} OSXCrashReporterInfo; | |
static OSXCrashReporterInfo sInfo; | |
__attribute__((constructor)) | |
void OSXCrashReporterInfoInitialize(void) | |
{ | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
sInfo.queue = dispatch_queue_create("AGCrashReporterInfo", DISPATCH_QUEUE_SERIAL); | |
sInfo.messageCapacity = 0; | |
__crashreporter_info__ = nil; | |
}); | |
} | |
void __attribute__((overloadable)) OSXCrashReporterInfoSet(const char * string, size_t length) | |
{ | |
dispatch_sync(sInfo.queue, ^{ | |
if (length > 0) { | |
if (!__crashreporter_info__) { | |
sInfo.messageCapacity = length + 1; | |
__crashreporter_info__ = malloc(sInfo.messageCapacity); | |
} | |
if (length > sInfo.messageCapacity) { | |
__crashreporter_info__ = realloc(__crashreporter_info__, length + 1); | |
if (__crashreporter_info__) { | |
sInfo.messageCapacity = length + 1; | |
} else { | |
sInfo.messageCapacity = 0; | |
} | |
} | |
if (__crashreporter_info__) { | |
memcpy(__crashreporter_info__, string, length); | |
__crashreporter_info__[length] = 0; | |
} | |
} else { | |
if (__crashreporter_info__) { | |
__crashreporter_info__[0] = 0; | |
} | |
} | |
}); | |
} | |
void __attribute__((overloadable)) OSXCrashReporterInfoSet(const char * string) | |
{ | |
if (string) { | |
OSXCrashReporterInfoSet(string, strlen(string)); | |
} else { | |
OSXCrashReporterInfoSet(NULL, 0); | |
} | |
} | |
void OSXCrashReporterInfoClear() | |
{ | |
OSXCrashReporterInfoSet(NULL, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment