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>
49 if (
auto ep = std::current_exception())
52 std::rethrow_exception(ep);
54 catch (std::exception
const &e)
57 const char *typeName =
typeid(e).name();
58 char *unmangled = abi::__cxa_demangle(typeName, 0, 0, &status);
62 VUserLog(
"Terminating due to uncaught %s: \"%s\"", typeName, e.what());
66 VUserLog(
"Terminating due to uncaught exception of unknown type");
71 VUserLog(
"Terminating because std::terminate was called (no exception data available)");
82 static mach_timebase_info_data_t info = {0,0};
85 kern_return_t ret = mach_timebase_info(&info);
86 if (ret != KERN_SUCCESS)
88 VUserLog(
"Failed to get mach timebase: %d", ret);
95 return (
double)mach_absolute_time() * info.numer / (info.denom * NSEC_PER_SEC);
114 } VuoLogProfileEntry;
115 typedef std::map<std::string, VuoLogProfileEntry> VuoLogProfileType;
116 static VuoLogProfileType *VuoLogProfile;
117 static dispatch_queue_t VuoLogProfileQueue;
132 VuoLogProfileQueue = dispatch_queue_create(
"VuoLogProfile", NULL);
133 VuoLogProfile =
new VuoLogProfileType;
137 #if defined(VUO_PROFILE) || defined(DOXYGEN)
143 dispatch_sync(VuoLogProfileQueue, ^{
144 VuoLogProfileType::iterator i = VuoLogProfile->find(name);
145 if (i != VuoLogProfile->end())
147 i->second.total += time;
148 if (time < i->second.min)
149 i->second.min = time;
150 if (time > i->second.max)
151 i->second.max = time;
155 (*VuoLogProfile)[name] = (VuoLogProfileEntry){time, time, time, 1};
164 dispatch_sync(VuoLogProfileQueue, ^{
166 for (VuoLogProfileType::iterator i = VuoLogProfile->begin(); i != VuoLogProfile->end(); ++i)
167 fprintf(stderr,
"%30s %12.9f s (%7.4f%%) (min %12.9f, max %12.9f, avg %12.9f, count %lld)\n",
170 i->second.total * 100. / totalRuntime,
173 i->second.total / i->second.count,
188 #define VuoCrashReport_alignment __attribute__((aligned(8)))
207 void VuoLog_statusF(
const char *file,
const unsigned int linenumber,
const char *
function,
const char *format, ...)
209 static dispatch_once_t statusInitialized = 0;
210 static dispatch_queue_t statusQueue;
211 dispatch_once(&statusInitialized, ^{
212 statusQueue = dispatch_queue_create(
"VuoLogStatus", NULL);
215 char *message =
nullptr;
219 va_start(args, format);
220 vasprintf(&message, format, args);
224 dispatch_sync(statusQueue, ^{
225 if (VuoCrashReport.message2)
226 free(VuoCrashReport.message2);
229 VuoCrashReport.message2 = message;
231 VuoCrashReport.message2 =
nullptr;
240 Class NSProcessInfoClass = objc_getClass(
"NSProcessInfo");
241 id processInfo = objc_msgSend((
id)NSProcessInfoClass, sel_getUid(
"processInfo"));
242 struct NSOperatingSystemVersion
248 typedef NSOperatingSystemVersion (*operatingSystemVersionType)(
id receiver,
SEL selector);
249 operatingSystemVersionType operatingSystemVersionFunc = (operatingSystemVersionType)objc_msgSend_stret;
250 NSOperatingSystemVersion operatingSystemVersion = operatingSystemVersionFunc(processInfo, sel_getUid(
"operatingSystemVersion"));
251 return operatingSystemVersion.minorVersion;
254 void VuoLog(
const char *file,
const unsigned int linenumber,
const char *
function,
const char *format, ...)
258 va_start(args, format);
259 int size = vsnprintf(NULL, 0, format, args);
262 char *formattedString = (
char *)malloc(size+1);
263 va_start(args, format);
264 vsnprintf(formattedString, size+1, format, args);
267 char *formattedFunction = NULL;
271 if (
function &&
function[0] ==
'_' &&
function[1] ==
'_')
273 int actualFunctionLength = atoi(
function + 2);
274 if (actualFunctionLength)
275 formattedFunction = strndup(
function + 3 + (
int)log10(actualFunctionLength), actualFunctionLength);
277 formattedFunction = strndup(
function + 2, strrchr(
function + 2,
'_') - (
function + 2));
282 if (strncmp(formattedFunction,
"_ZN", 3) == 0)
285 int len, priorLen = 0;
286 while ((len = atoi(formattedFunction + pos)))
288 pos += 1 + (int)log10(len) + len;
291 char *f2 = strndup(formattedFunction + pos - priorLen, priorLen);
292 free(formattedFunction);
293 formattedFunction = f2;
300 const char *f = formattedFunction ? formattedFunction :
function;
301 size_t fLen = strlen(f);
302 if (f[fLen - 1] !=
']')
304 size_t mallocSize = fLen + 2 + 1;
305 char *f2 = (
char *)malloc(mallocSize);
306 strlcpy(f2, f, mallocSize);
307 strlcat(f2,
"()", mallocSize);
308 if (formattedFunction)
309 free(formattedFunction);
310 formattedFunction = f2;
314 const char *formattedFile = file;
317 if (
const char *lastSlash = strrchr(file,
'/'))
318 formattedFile = lastSlash + 1;
323 static double priorTime = 0;
324 const char *separator =
"";
325 if (priorTime > 0 && time - priorTime > 0.5)
331 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);
340 extern struct mach_header __dso_handle;
341 typedef void *(*vuoMacOsLogCreateType)(
const char *subsystem,
const char *category);
342 typedef void (*vuoMacOsLogInternalType)(
void *dso,
void *log, uint8_t type,
const char *message, ...);
343 typedef void (*vuoMacOsLogImplType)(
void *dso,
void *log, uint8_t type,
const char *format, uint8_t *buf, uint32_t size);
344 static vuoMacOsLogCreateType vuoMacOsLogCreate = NULL;
345 static vuoMacOsLogInternalType vuoMacOsLogInternal = NULL;
346 static vuoMacOsLogImplType vuoMacOsLogImpl = NULL;
347 static dispatch_once_t once = 0;
348 dispatch_once(&once, ^{
349 vuoMacOsLogCreate = (vuoMacOsLogCreateType)dlsym(RTLD_SELF,
"os_log_create");
351 vuoMacOsLogInternal = (vuoMacOsLogInternalType)dlsym(RTLD_SELF,
"_os_log_internal");
353 vuoMacOsLogImpl = (vuoMacOsLogImplType)dlsym(RTLD_SELF,
"_os_log_impl");
356 if (vuoMacOsLogCreate)
358 void *log = vuoMacOsLogCreate(
"org.vuo", formattedFile);
360 if (vuoMacOsLogInternal)
361 vuoMacOsLogInternal(&__dso_handle, log, 0 ,
"%{public}41s:%-4u %{public}s", formattedFunction ? formattedFunction :
function, linenumber, formattedString);
362 else if (vuoMacOsLogImpl)
364 char *formattedForOsLog;
365 asprintf(&formattedForOsLog,
"%41s:%-4u %s", formattedFunction ? formattedFunction :
function, linenumber, formattedString);
368 uint8_t logFormatDescriptor[12] = {
375 memcpy(logFormatDescriptor + 4, &formattedForOsLog, 8);
377 vuoMacOsLogImpl(&__dso_handle, log, 0 ,
"%{public}s", logFormatDescriptor,
sizeof(logFormatDescriptor));
379 free(formattedForOsLog);
387 aslmsg msg = asl_new(ASL_TYPE_MSG);
388 asl_set(msg, ASL_KEY_READ_UID,
"-1");
389 asl_log(NULL, msg, ASL_LEVEL_WARNING,
"%27s:%-4u %41s %s", formattedFile, linenumber, formattedFunction ? formattedFunction :
function, formattedString);
395 static dispatch_once_t historyInitialized = 0;
396 dispatch_once(&historyInitialized, ^{
405 char *formattedPrefixedString;
406 asprintf(&formattedPrefixedString,
"t=%8.4fs %27.27s:%-4u %41.41s %s", time, formattedFile, linenumber, formattedFunction ? formattedFunction :
function, formattedString);
407 free(formattedString);
408 if (formattedFunction)
409 free(formattedFunction);
431 if (VuoCrashReport.message)
432 free(VuoCrashReport.message);
440 char *message = (
char *)malloc(size);
445 strlcat(message,
"\n", size);
449 VuoCrashReport.message = message;
464 static dispatch_once_t checked = 0;
465 static bool debug =
false;
466 dispatch_once(&checked, ^{
467 debug = CFPreferencesGetAppBooleanValue(CFSTR(
"debug"), CFSTR(
"org.vuo.Editor"), NULL);
480 return strdup(
"(null)");
482 CFStringRef d = CFCopyDescription(variable);
483 CFIndex len = CFStringGetLength(d)+1;
484 char *z = (
char *)malloc(len);
485 CFStringGetCString(d, z, len, kCFStringEncodingUTF8);
498 backtrace.erase(backtrace.begin());
501 for (
auto line : backtrace)
502 fprintf(stderr,
"%3d %s\n", i++, line.c_str());
512 size_t replacementLen = strlen(replacement);
514 regcomp(®ex, substringToRemove, REG_EXTENDED);
516 regmatch_t pmatch[nmatch];
517 while (!regexec(®ex, wholeString, nmatch, pmatch, 0))
519 strncpy(wholeString + pmatch[0].rm_so, replacement, replacementLen);
520 memmove(wholeString + pmatch[0].rm_so + replacementLen, wholeString + pmatch[0].rm_eo, strlen(wholeString + pmatch[0].rm_eo) + 1);
531 size_t size = backtrace(array, 100);
532 char **strings = backtrace_symbols(array, size);
533 vector<string> outputStrings;
536 for (
size_t i = 1; i < size; i++)
539 const int lineNumberLen = 4;
540 string trimmedLine = strings[i] + lineNumberLen;
542 const int libraryLen = 36;
543 const int addressLen = 19;
544 string symbol = strings[i] + lineNumberLen + libraryLen + addressLen;
545 string instructionOffset = symbol.substr(symbol.find(
' '));
546 symbol = symbol.substr(0, symbol.find(
' '));
549 char *unmangled = abi::__cxa_demangle(symbol.c_str(),
nullptr,
nullptr, &status);
554 VuoLog_replaceString(unmangled,
"basic_string<char, char_traits<char>, allocator<char> >",
"string");
559 outputStrings.push_back(trimmedLine.substr(0, libraryLen + addressLen) + unmangled + instructionOffset);
563 outputStrings.push_back(trimmedLine);
570 auto last = outputStrings.back();
571 if (last.substr(0, 4) ==
"??? "
572 && last.find(
" 0x0 + ") != string::npos)
573 outputStrings.pop_back();
575 last = outputStrings.back();
576 if (last.substr(0, 13) ==
"libdyld.dylib"
577 && last.find(
" start + ") != string::npos)
578 outputStrings.pop_back();
581 remove_if(outputStrings.begin(), outputStrings.end(),
582 [](
const string &s) {
583 return s.find(
"_dispatch_queue_override_invoke") != string::npos
584 || s.find(
"_dispatch_root_queue_drain") != string::npos
585 || s.find(
"_dispatch_worker_thread2") != string::npos
586 || s.find(
"_dispatch_lane_serial_drain") != string::npos
587 || s.find(
"_dispatch_lane_barrier_sync_invoke_and_complete") != string::npos
588 || s.find(
"_dispatch_lane_invoke") != string::npos
589 || s.find(
"_dispatch_workloop_worker_thread") != string::npos
590 || s.find(
"_dispatch_continuation_pop") != string::npos
591 || s.find(
"_dispatch_main_queue_callback_4CF") != string::npos
592 || s.find(
"__CFRunLoopDoObservers") != string::npos
593 || s.find(
"__CFRunLoopDoSource0") != string::npos
594 || s.find(
"__CFRunLoopDoSources0") != string::npos
595 || s.find(
"__CFRunLoopRun") != string::npos
596 || s.find(
"__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__") != string::npos
597 || s.find(
"__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__") != string::npos
598 || s.find(
"__CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__") != string::npos
599 || s.find(
"_CFXRegistrationPost") != string::npos
600 || s.find(
"_CFXNotification") != string::npos
601 || s.find(
"_NSWindowSendWindowDidMove") != string::npos
602 || s.find(
"_NSSendEventToObservers") != string::npos
603 || s.find(
"NSCarbonMenuImpl") != string::npos
604 || s.find(
"NSSLMMenuEventHandler") != string::npos
605 || s.find(
"CopyCarbonUIElementAttributeValue") != string::npos
606 || s.find(
"NSApplication(NSEvent) _") != string::npos
607 || s.find(
"NSApplication(NSAppleEventHandling) _") != string::npos
608 || s.find(
"withWindowOrderingObserverHeuristic") != string::npos
609 || s.find(
"aeProcessAppleEvent") != string::npos
610 || s.find(
"dispatchEventAndSendReply") != string::npos
611 || s.find(
"aeDispatchAppleEvent") != string::npos
612 || s.find(
"_NSAppleEventManagerGenericHandler") != string::npos
613 || s.find(
"NSWindow _") != string::npos
614 || s.find(
"HIServices") != string::npos
615 || s.find(
"HIToolbox") != string::npos
616 || s.find(
"RunCurrentEventLoopInMode") != string::npos
617 || s.find(
"ReceiveNextEventCommon") != string::npos
618 || s.find(
"_BlockUntilNextEventMatchingListInModeWithFilter") != string::npos
619 || s.find(
"_DPSNextEvent") != string::npos
620 || s.find(
"_pthread_wqthread") != string::npos
621 || s.find(
"qt_plugin_instance") != string::npos
622 || s.find(
"QWindowSystemInterface::") != string::npos
623 || s.find(
"QWidgetPrivate::") != string::npos
624 || s.find(
"QApplicationPrivate::") != string::npos
625 || s.find(
"QGuiApplicationPrivate::") != string::npos
626 || s.find(
"QCoreApplication::notifyInternal2") != string::npos
627 || s.find(
"QCoreApplicationPrivate::") != string::npos;
629 outputStrings.end());
633 return outputStrings;