14 #include <mach/mach_time.h>
19 #include <objc/objc-runtime.h>
23 #ifndef __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES
24 #define __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES 0
27 #include <CoreFoundation/CoreFoundation.h>
29 #include <AvailabilityMacros.h>
30 #if (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_7)
31 #include <ApplicationServices/ApplicationServices.h>
33 #include <CoreGraphics/CoreGraphics.h>
34 #include <CoreText/CoreText.h>
50 if (
auto ep = std::current_exception())
53 std::rethrow_exception(ep);
55 catch (std::exception
const &e)
58 const char *typeName =
typeid(e).name();
59 char *unmangled = abi::__cxa_demangle(typeName, 0, 0, &status);
63 VUserLog(
"Terminating due to uncaught %s: \"%s\"", typeName, e.what());
67 VUserLog(
"Terminating due to uncaught exception of unknown type");
72 VUserLog(
"Terminating because std::terminate was called (no exception data available)");
83 static mach_timebase_info_data_t info = {0,0};
86 kern_return_t ret = mach_timebase_info(&info);
87 if (ret != KERN_SUCCESS)
89 VUserLog(
"Failed to get mach timebase: %d", ret);
96 return (
double)mach_absolute_time() * info.numer / (info.denom * NSEC_PER_SEC);
115 } VuoLogProfileEntry;
116 typedef std::map<std::string, VuoLogProfileEntry> VuoLogProfileType;
117 static VuoLogProfileType *VuoLogProfile;
118 static dispatch_queue_t VuoLogProfileQueue;
126 static void __attribute__((constructor)) VuoLog_init(
void)
133 VuoLogProfileQueue = dispatch_queue_create(
"VuoLogProfile", NULL);
134 VuoLogProfile =
new VuoLogProfileType;
138 #if defined(VUO_PROFILE) || defined(DOXYGEN)
144 dispatch_sync(VuoLogProfileQueue, ^{
145 VuoLogProfileType::iterator i = VuoLogProfile->find(name);
146 if (i != VuoLogProfile->end())
148 i->second.total += time;
149 if (time < i->second.min)
150 i->second.min = time;
151 if (time > i->second.max)
152 i->second.max = time;
156 (*VuoLogProfile)[name] = (VuoLogProfileEntry){time, time, time, 1};
163 extern "C" void __attribute__((destructor)) VuoLog_dumpProfile(
void)
165 dispatch_sync(VuoLogProfileQueue, ^{
167 for (VuoLogProfileType::iterator i = VuoLogProfile->begin(); i != VuoLogProfile->end(); ++i)
168 fprintf(stderr,
"%30s %12.9f s (%7.4f%%) (min %12.9f, max %12.9f, avg %12.9f, count %lld)\n",
171 i->second.total * 100. / totalRuntime,
174 i->second.total / i->second.count,
189 #define VuoCrashReport_alignment __attribute__((aligned(8)))
206 VuoCrashReport_infoType VuoCrashReport __attribute__((section(
"__DATA,__crash_info"))) = { 4, NULL, NULL, NULL, NULL, NULL, NULL };
208 void VuoLog_statusF(
const char *file,
const unsigned int linenumber,
const char *
function,
const char *format, ...)
210 static dispatch_once_t statusInitialized = 0;
211 static dispatch_queue_t statusQueue;
212 dispatch_once(&statusInitialized, ^{
213 statusQueue = dispatch_queue_create(
"VuoLogStatus", NULL);
216 char *message =
nullptr;
220 va_start(args, format);
221 vasprintf(&message, format, args);
225 dispatch_sync(statusQueue, ^{
226 if (VuoCrashReport.message2)
227 free(VuoCrashReport.message2);
230 VuoCrashReport.message2 = message;
232 VuoCrashReport.message2 =
nullptr;
241 Class NSProcessInfoClass = objc_getClass(
"NSProcessInfo");
242 id processInfo = objc_msgSend((
id)NSProcessInfoClass, sel_getUid(
"processInfo"));
243 struct NSOperatingSystemVersion
249 typedef NSOperatingSystemVersion (*operatingSystemVersionType)(
id receiver,
SEL selector);
250 operatingSystemVersionType operatingSystemVersionFunc = (operatingSystemVersionType)objc_msgSend_stret;
251 NSOperatingSystemVersion operatingSystemVersion = operatingSystemVersionFunc(processInfo, sel_getUid(
"operatingSystemVersion"));
252 return operatingSystemVersion.minorVersion;
255 void VuoLog(
const char *file,
const unsigned int linenumber,
const char *
function,
const char *format, ...)
259 va_start(args, format);
260 int size = vsnprintf(NULL, 0, format, args);
263 char *formattedString = (
char *)malloc(size+1);
264 va_start(args, format);
265 vsnprintf(formattedString, size+1, format, args);
268 char *formattedFunction = NULL;
272 if (
function &&
function[0] ==
'_' &&
function[1] ==
'_')
274 int actualFunctionLength = atoi(
function + 2);
275 if (actualFunctionLength)
276 formattedFunction = strndup(
function + 3 + (
int)log10(actualFunctionLength), actualFunctionLength);
278 formattedFunction = strndup(
function + 2, strrchr(
function + 2,
'_') - (
function + 2));
283 if (strncmp(formattedFunction,
"_ZN", 3) == 0)
286 int len, priorLen = 0;
287 while ((len = atoi(formattedFunction + pos)))
289 pos += 1 + (int)log10(len) + len;
292 char *f2 = strndup(formattedFunction + pos - priorLen, priorLen);
293 free(formattedFunction);
294 formattedFunction = f2;
301 const char *f = formattedFunction ? formattedFunction :
function;
302 size_t fLen = strlen(f);
303 if (f[fLen - 1] !=
']')
305 size_t mallocSize = fLen + 2 + 1;
306 char *f2 = (
char *)malloc(mallocSize);
307 strlcpy(f2, f, mallocSize);
308 strlcat(f2,
"()", mallocSize);
309 if (formattedFunction)
310 free(formattedFunction);
311 formattedFunction = f2;
315 const char *formattedFile = file;
318 formattedFile =
"(unknown)";
321 if (
const char *lastSlash = strrchr(formattedFile,
'/'))
322 formattedFile = lastSlash + 1;
327 static double priorTime = 0;
328 const char *separator =
"";
329 if (priorTime > 0 && time - priorTime > 0.5)
335 fprintf(stderr,
"%s\033[38;5;%dm# pid=%5d t=%8.4fs %27.27s:%-4u %41.41s %s\033[0m\n", separator, getpid()%144+88, getpid(), time, formattedFile, linenumber, formattedFunction ? formattedFunction :
function, formattedString);
344 extern struct mach_header __dso_handle;
345 typedef void *(*vuoMacOsLogCreateType)(
const char *subsystem,
const char *category);
346 typedef void (*vuoMacOsLogInternalType)(
void *dso,
void *log, uint8_t type,
const char *message, ...);
347 typedef void (*vuoMacOsLogImplType)(
void *dso,
void *log, uint8_t type,
const char *format, uint8_t *buf, uint32_t size);
348 static vuoMacOsLogCreateType vuoMacOsLogCreate = NULL;
349 static vuoMacOsLogInternalType vuoMacOsLogInternal = NULL;
350 static vuoMacOsLogImplType vuoMacOsLogImpl = NULL;
351 static dispatch_once_t once = 0;
352 dispatch_once(&once, ^{
353 vuoMacOsLogCreate = (vuoMacOsLogCreateType)dlsym(RTLD_SELF,
"os_log_create");
355 vuoMacOsLogInternal = (vuoMacOsLogInternalType)dlsym(RTLD_SELF,
"_os_log_internal");
357 vuoMacOsLogImpl = (vuoMacOsLogImplType)dlsym(RTLD_SELF,
"_os_log_impl");
360 if (vuoMacOsLogCreate)
362 void *log = vuoMacOsLogCreate(
"org.vuo", formattedFile);
364 if (vuoMacOsLogInternal)
365 vuoMacOsLogInternal(&__dso_handle, log, 0 ,
"%{public}41s:%-4u %{public}s", formattedFunction ? formattedFunction :
function, linenumber, formattedString);
366 else if (vuoMacOsLogImpl)
368 char *formattedForOsLog;
369 asprintf(&formattedForOsLog,
"%41s:%-4u %s", formattedFunction ? formattedFunction :
function, linenumber, formattedString);
372 uint8_t logFormatDescriptor[12] = {
379 memcpy(logFormatDescriptor + 4, &formattedForOsLog, 8);
381 vuoMacOsLogImpl(&__dso_handle, log, 0 ,
"%{public}s", logFormatDescriptor,
sizeof(logFormatDescriptor));
383 free(formattedForOsLog);
391 aslmsg msg = asl_new(ASL_TYPE_MSG);
392 asl_set(msg, ASL_KEY_READ_UID,
"-1");
393 asl_log(NULL, msg, ASL_LEVEL_WARNING,
"%27s:%-4u %41s %s", formattedFile, linenumber, formattedFunction ? formattedFunction :
function, formattedString);
399 static dispatch_once_t historyInitialized = 0;
400 dispatch_once(&historyInitialized, ^{
409 char *formattedPrefixedString;
410 asprintf(&formattedPrefixedString,
"t=%8.4fs %27.27s:%-4u %41.41s %s", time, formattedFile, linenumber, formattedFunction ? formattedFunction :
function, formattedString);
411 free(formattedString);
412 if (formattedFunction)
413 free(formattedFunction);
435 if (VuoCrashReport.message)
436 free(VuoCrashReport.message);
444 char *message = (
char *)malloc(size);
449 strlcat(message,
"\n", size);
453 VuoCrashReport.message = message;
468 static dispatch_once_t checked = 0;
469 static bool debug =
false;
470 dispatch_once(&checked, ^{
471 debug = CFPreferencesGetAppBooleanValue(CFSTR(
"debug"), CFSTR(
"org.vuo.Editor"), NULL);
484 backtrace.erase(backtrace.begin());
487 for (
auto line : backtrace)
488 fprintf(stderr,
"%3d %s\n", i++, line.c_str());
498 size_t replacementLen = strlen(replacement);
500 regcomp(®ex, substringToRemove, REG_EXTENDED);
502 regmatch_t pmatch[nmatch];
503 while (!regexec(®ex, wholeString, nmatch, pmatch, 0))
505 strncpy(wholeString + pmatch[0].rm_so, replacement, replacementLen);
506 memmove(wholeString + pmatch[0].rm_so + replacementLen, wholeString + pmatch[0].rm_eo, strlen(wholeString + pmatch[0].rm_eo) + 1);
517 size_t size = backtrace(array, 100);
518 char **strings = backtrace_symbols(array, size);
519 vector<string> outputStrings;
522 for (
size_t i = 1; i < size; i++)
525 const int lineNumberLen = 4;
526 string trimmedLine = strings[i] + lineNumberLen;
528 const int libraryLen = 36;
529 const int addressLen = 19;
530 string symbol = strings[i] + lineNumberLen + libraryLen + addressLen;
531 string instructionOffset = symbol.substr(symbol.find(
' '));
532 symbol = symbol.substr(0, symbol.find(
' '));
535 char *unmangled = abi::__cxa_demangle(symbol.c_str(),
nullptr,
nullptr, &status);
540 VuoLog_replaceString(unmangled,
"basic_string<char, char_traits<char>, allocator<char> >",
"string");
545 outputStrings.push_back(trimmedLine.substr(0, libraryLen + addressLen) + unmangled + instructionOffset);
549 outputStrings.push_back(trimmedLine);
556 auto last = outputStrings.back();
557 if (last.substr(0, 4) ==
"??? "
558 && last.find(
" 0x0 + ") != string::npos)
559 outputStrings.pop_back();
561 last = outputStrings.back();
562 if (last.substr(0, 13) ==
"libdyld.dylib"
563 && last.find(
" start + ") != string::npos)
564 outputStrings.pop_back();
567 remove_if(outputStrings.begin(), outputStrings.end(),
568 [](
const string &s) {
569 return s.find(
"_dispatch_queue_override_invoke") != string::npos
570 || s.find(
"_dispatch_root_queue_drain") != string::npos
571 || s.find(
"_dispatch_worker_thread2") != string::npos
572 || s.find(
"_dispatch_lane_serial_drain") != string::npos
573 || s.find(
"_dispatch_lane_barrier_sync_invoke_and_complete") != string::npos
574 || s.find(
"_dispatch_lane_invoke") != string::npos
575 || s.find(
"_dispatch_workloop_worker_thread") != string::npos
576 || s.find(
"_dispatch_continuation_pop") != string::npos
577 || s.find(
"_dispatch_main_queue_callback_4CF") != string::npos
578 || s.find(
"__CFRunLoopDoObservers") != string::npos
579 || s.find(
"__CFRunLoopDoSource0") != string::npos
580 || s.find(
"__CFRunLoopDoSources0") != string::npos
581 || s.find(
"__CFRunLoopRun") != string::npos
582 || s.find(
"__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__") != string::npos
583 || s.find(
"__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__") != string::npos
584 || s.find(
"__CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__") != string::npos
585 || s.find(
"_CFXRegistrationPost") != string::npos
586 || s.find(
"_CFXNotification") != string::npos
587 || s.find(
"_NSWindowSendWindowDidMove") != string::npos
588 || s.find(
"_NSSendEventToObservers") != string::npos
589 || s.find(
"NSCarbonMenuImpl") != string::npos
590 || s.find(
"NSSLMMenuEventHandler") != string::npos
591 || s.find(
"CopyCarbonUIElementAttributeValue") != string::npos
592 || s.find(
"NSApplication(NSEvent) _") != string::npos
593 || s.find(
"NSApplication(NSAppleEventHandling) _") != string::npos
594 || s.find(
"withWindowOrderingObserverHeuristic") != string::npos
595 || s.find(
"aeProcessAppleEvent") != string::npos
596 || s.find(
"dispatchEventAndSendReply") != string::npos
597 || s.find(
"aeDispatchAppleEvent") != string::npos
598 || s.find(
"_NSAppleEventManagerGenericHandler") != string::npos
599 || s.find(
"NSWindow _") != string::npos
600 || s.find(
"HIServices") != string::npos
601 || s.find(
"HIToolbox") != string::npos
602 || s.find(
"RunCurrentEventLoopInMode") != string::npos
603 || s.find(
"ReceiveNextEventCommon") != string::npos
604 || s.find(
"_BlockUntilNextEventMatchingListInModeWithFilter") != string::npos
605 || s.find(
"_DPSNextEvent") != string::npos
606 || s.find(
"_pthread_wqthread") != string::npos
607 || s.find(
"qt_plugin_instance") != string::npos
608 || s.find(
"QWindowSystemInterface::") != string::npos
609 || s.find(
"QWidgetPrivate::") != string::npos
610 || s.find(
"QApplicationPrivate::") != string::npos
611 || s.find(
"QGuiApplicationPrivate::") != string::npos
612 || s.find(
"QCoreApplication::notifyInternal2") != string::npos
613 || s.find(
"QCoreApplicationPrivate::") != string::npos;
615 outputStrings.end());
619 return outputStrings;