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 if (VuoCrashReport.message2)
210 free(VuoCrashReport.message2);
215 va_start(args, format);
216 vasprintf(&VuoCrashReport.message2, format, args);
220 VuoCrashReport.message2 =
nullptr;
228 Class NSProcessInfoClass = objc_getClass(
"NSProcessInfo");
229 id processInfo = objc_msgSend((
id)NSProcessInfoClass, sel_getUid(
"processInfo"));
230 struct NSOperatingSystemVersion
236 typedef NSOperatingSystemVersion (*operatingSystemVersionType)(
id receiver,
SEL selector);
237 operatingSystemVersionType operatingSystemVersionFunc = (operatingSystemVersionType)objc_msgSend_stret;
238 NSOperatingSystemVersion operatingSystemVersion = operatingSystemVersionFunc(processInfo, sel_getUid(
"operatingSystemVersion"));
239 return operatingSystemVersion.minorVersion;
242 void VuoLog(
const char *file,
const unsigned int linenumber,
const char *
function,
const char *format, ...)
246 va_start(args, format);
247 int size = vsnprintf(NULL, 0, format, args);
250 char *formattedString = (
char *)malloc(size+1);
251 va_start(args, format);
252 vsnprintf(formattedString, size+1, format, args);
255 char *formattedFunction = NULL;
259 if (
function &&
function[0] ==
'_' &&
function[1] ==
'_')
261 int actualFunctionLength = atoi(
function + 2);
262 if (actualFunctionLength)
263 formattedFunction = strndup(
function + 3 + (
int)log10(actualFunctionLength), actualFunctionLength);
265 formattedFunction = strndup(
function + 2, strrchr(
function + 2,
'_') - (
function + 2));
270 if (strncmp(formattedFunction,
"_ZN", 3) == 0)
273 int len, priorLen = 0;
274 while ((len = atoi(formattedFunction + pos)))
276 pos += 1 + (int)log10(len) + len;
279 char *f2 = strndup(formattedFunction + pos - priorLen, priorLen);
280 free(formattedFunction);
281 formattedFunction = f2;
288 const char *f = formattedFunction ? formattedFunction :
function;
289 size_t fLen = strlen(f);
290 if (f[fLen - 1] !=
']')
292 size_t mallocSize = fLen + 2 + 1;
293 char *f2 = (
char *)malloc(mallocSize);
294 strlcpy(f2, f, mallocSize);
295 strlcat(f2,
"()", mallocSize);
296 if (formattedFunction)
297 free(formattedFunction);
298 formattedFunction = f2;
302 const char *formattedFile = file;
305 if (
const char *lastSlash = strrchr(file,
'/'))
306 formattedFile = lastSlash + 1;
312 fprintf(stderr,
"\033[38;5;%dm# pid=%5d t=%8.4fs %27.27s:%-4u %41.41s %s\033[0m\n", getpid()%144+88, getpid(), time, formattedFile, linenumber, formattedFunction ? formattedFunction :
function, formattedString);
321 extern struct mach_header __dso_handle;
322 typedef void *(*vuoMacOsLogCreateType)(
const char *subsystem,
const char *category);
323 typedef void (*vuoMacOsLogInternalType)(
void *dso,
void *log, uint8_t type,
const char *message, ...);
324 typedef void (*vuoMacOsLogImplType)(
void *dso,
void *log, uint8_t type,
const char *format, uint8_t *buf, uint32_t size);
325 static vuoMacOsLogCreateType vuoMacOsLogCreate = NULL;
326 static vuoMacOsLogInternalType vuoMacOsLogInternal = NULL;
327 static vuoMacOsLogImplType vuoMacOsLogImpl = NULL;
328 static dispatch_once_t once = 0;
329 dispatch_once(&once, ^{
330 vuoMacOsLogCreate = (vuoMacOsLogCreateType)dlsym(RTLD_SELF,
"os_log_create");
332 vuoMacOsLogInternal = (vuoMacOsLogInternalType)dlsym(RTLD_SELF,
"_os_log_internal");
334 vuoMacOsLogImpl = (vuoMacOsLogImplType)dlsym(RTLD_SELF,
"_os_log_impl");
337 if (vuoMacOsLogCreate)
339 void *log = vuoMacOsLogCreate(
"org.vuo", formattedFile);
341 if (vuoMacOsLogInternal)
342 vuoMacOsLogInternal(&__dso_handle, log, 0 ,
"%{public}41s:%-4u %{public}s", formattedFunction ? formattedFunction :
function, linenumber, formattedString);
343 else if (vuoMacOsLogImpl)
345 char *formattedForOsLog;
346 asprintf(&formattedForOsLog,
"%41s:%-4u %s", formattedFunction ? formattedFunction :
function, linenumber, formattedString);
349 uint8_t logFormatDescriptor[12] = {
356 memcpy(logFormatDescriptor + 4, &formattedForOsLog, 8);
358 vuoMacOsLogImpl(&__dso_handle, log, 0 ,
"%{public}s", logFormatDescriptor,
sizeof(logFormatDescriptor));
360 free(formattedForOsLog);
368 aslmsg msg = asl_new(ASL_TYPE_MSG);
369 asl_set(msg, ASL_KEY_READ_UID,
"-1");
370 asl_log(NULL, msg, ASL_LEVEL_WARNING,
"%27s:%-4u %41s %s", formattedFile, linenumber, formattedFunction ? formattedFunction :
function, formattedString);
376 static dispatch_once_t historyInitialized = 0;
377 dispatch_once(&historyInitialized, ^{
386 char *formattedPrefixedString;
387 asprintf(&formattedPrefixedString,
"t=%8.4fs %27.27s:%-4u %41.41s %s", time, formattedFile, linenumber, formattedFunction ? formattedFunction :
function, formattedString);
388 free(formattedString);
389 if (formattedFunction)
390 free(formattedFunction);
412 if (VuoCrashReport.message)
413 free(VuoCrashReport.message);
421 char *message = (
char *)malloc(size);
426 strlcat(message,
"\n", size);
430 VuoCrashReport.message = message;
445 static dispatch_once_t checked = 0;
446 static bool debug =
false;
447 dispatch_once(&checked, ^{
448 debug = CFPreferencesGetAppBooleanValue(CFSTR(
"debug"), CFSTR(
"org.vuo.Editor"), NULL);
461 return strdup(
"(null)");
463 CFStringRef d = CFCopyDescription(variable);
464 CFIndex len = CFStringGetLength(d)+1;
465 char *z = (
char *)malloc(len);
466 CFStringGetCString(d, z, len, kCFStringEncodingUTF8);
479 backtrace.erase(backtrace.begin());
482 for (
auto line : backtrace)
483 fprintf(stderr,
"%3d %s\n", i++, line.c_str());
493 size_t replacementLen = strlen(replacement);
495 regcomp(®ex, substringToRemove, REG_EXTENDED);
497 regmatch_t pmatch[nmatch];
498 while (!regexec(®ex, wholeString, nmatch, pmatch, 0))
500 strncpy(wholeString + pmatch[0].rm_so, replacement, replacementLen);
501 memmove(wholeString + pmatch[0].rm_so + replacementLen, wholeString + pmatch[0].rm_eo, strlen(wholeString + pmatch[0].rm_eo) + 1);
512 size_t size = backtrace(array, 100);
513 char **strings = backtrace_symbols(array, size);
514 vector<string> outputStrings;
517 for (
size_t i = 1; i < size; i++)
520 const int lineNumberLen = 4;
521 string trimmedLine = strings[i] + lineNumberLen;
523 const int libraryLen = 36;
524 const int addressLen = 19;
525 string symbol = strings[i] + lineNumberLen + libraryLen + addressLen;
526 string instructionOffset = symbol.substr(symbol.find(
' '));
527 symbol = symbol.substr(0, symbol.find(
' '));
530 char *unmangled = abi::__cxa_demangle(symbol.c_str(),
nullptr,
nullptr, &status);
535 VuoLog_replaceString(unmangled,
"basic_string<char, char_traits<char>, allocator<char> >",
"string");
540 outputStrings.push_back(trimmedLine.substr(0, libraryLen + addressLen) + unmangled + instructionOffset);
544 outputStrings.push_back(trimmedLine);
551 auto last = outputStrings.back();
552 if (last.substr(0, 4) ==
"??? "
553 && last.find(
" 0x0 + ") != string::npos)
554 outputStrings.pop_back();
556 last = outputStrings.back();
557 if (last.substr(0, 13) ==
"libdyld.dylib"
558 && last.find(
" start + ") != string::npos)
559 outputStrings.pop_back();
562 remove_if(outputStrings.begin(), outputStrings.end(),
563 [](
const string &s) {
564 return s.find(
"_dispatch_queue_override_invoke") != string::npos
565 || s.find(
"_dispatch_root_queue_drain") != string::npos
566 || s.find(
"_dispatch_worker_thread2") != string::npos
567 || s.find(
"_dispatch_lane_serial_drain") != string::npos
568 || s.find(
"_dispatch_lane_barrier_sync_invoke_and_complete") != string::npos
569 || s.find(
"_dispatch_lane_invoke") != string::npos
570 || s.find(
"_dispatch_workloop_worker_thread") != string::npos
571 || s.find(
"_dispatch_continuation_pop") != string::npos
572 || s.find(
"_dispatch_main_queue_callback_4CF") != string::npos
573 || s.find(
"__CFRunLoopDoObservers") != string::npos
574 || s.find(
"__CFRunLoopDoSource0") != string::npos
575 || s.find(
"__CFRunLoopDoSources0") != string::npos
576 || s.find(
"__CFRunLoopRun") != string::npos
577 || s.find(
"__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__") != string::npos
578 || s.find(
"__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__") != string::npos
579 || s.find(
"__CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__") != string::npos
580 || s.find(
"_CFXRegistrationPost") != string::npos
581 || s.find(
"_CFXNotification") != string::npos
582 || s.find(
"_NSWindowSendWindowDidMove") != string::npos
583 || s.find(
"_NSSendEventToObservers") != string::npos
584 || s.find(
"NSCarbonMenuImpl") != string::npos
585 || s.find(
"NSSLMMenuEventHandler") != string::npos
586 || s.find(
"CopyCarbonUIElementAttributeValue") != string::npos
587 || s.find(
"NSApplication(NSEvent) _") != string::npos
588 || s.find(
"NSApplication(NSAppleEventHandling) _") != string::npos
589 || s.find(
"withWindowOrderingObserverHeuristic") != string::npos
590 || s.find(
"aeProcessAppleEvent") != string::npos
591 || s.find(
"dispatchEventAndSendReply") != string::npos
592 || s.find(
"aeDispatchAppleEvent") != string::npos
593 || s.find(
"_NSAppleEventManagerGenericHandler") != string::npos
594 || s.find(
"NSWindow _") != string::npos
595 || s.find(
"HIServices") != string::npos
596 || s.find(
"HIToolbox") != string::npos
597 || s.find(
"RunCurrentEventLoopInMode") != string::npos
598 || s.find(
"ReceiveNextEventCommon") != string::npos
599 || s.find(
"_BlockUntilNextEventMatchingListInModeWithFilter") != string::npos
600 || s.find(
"_DPSNextEvent") != string::npos
601 || s.find(
"_pthread_wqthread") != string::npos
602 || s.find(
"qt_plugin_instance") != string::npos
603 || s.find(
"QWindowSystemInterface::") != string::npos
604 || s.find(
"QWidgetPrivate::") != string::npos
605 || s.find(
"QApplicationPrivate::") != string::npos
606 || s.find(
"QGuiApplicationPrivate::") != string::npos
607 || s.find(
"QCoreApplication::notifyInternal2") != string::npos
608 || s.find(
"QCoreApplicationPrivate::") != string::npos;
610 outputStrings.end());
614 return outputStrings;