3 * Support for interop with the Microsoft Error Reporting tool
6 * Alexander Kyte (alkyte@microsoft.com)
8 * (C) 2018 Microsoft, Inc.
15 #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
16 #include "mono-merp.h"
21 // OSX OS stuff now, for merpGUI interop
22 #include <mach/mach.h>
23 #include <mach/task_info.h>
24 #include <mach/mach_types.h>
25 #include <mach/mach_traps.h>
26 #include <servers/bootstrap.h>
28 #include <metadata/locales.h>
31 #if defined(HAVE_SYS_UTSNAME_H)
32 #include <sys/utsname.h>
35 // To get the apple machine model
36 #include <sys/param.h>
37 #include <sys/sysctl.h>
40 #include <mono/utils/json.h>
41 #include <mono/utils/mono-state.h>
42 #include <utils/mono-threads-debug.h>
45 kernel_version_string (void)
47 #ifdef HAVE_SYS_UTSNAME_H
48 static struct utsname name
;
49 static const char *version_string
;
51 if (!version_string
) {
52 // WSL does not always nul terminate. WSL was fixed February 2018.
53 // Restore memset if variable made non-static.
54 //memset (&name, 0, sizeof (name));
56 if (uname (&name
) >= 0)
57 version_string
= name
.release
;
62 return version_string
;
68 starts_with (const char *pre
, size_t pre_sz
, const char *str
)
70 return strncmp (pre
, str
, pre_sz
) == 0;
74 macos_version_string (void)
76 /* Can't allocate in here, could be called from a signal handler in a
79 static const char *version_string
;
81 static const size_t buf_size
= sizeof (buf
);
84 return version_string
;
86 /* macOS 10.13.6 or later */
87 if (!version_string
) {
89 if (sysctlbyname ("kern.osproductversion", NULL
, &size
, NULL
, 0) < 0 || size
>= buf_size
) {
90 /* if we couldn't get the size or if it needs more space that we have in buf, leave it empty */
92 return version_string
;
95 if (sysctlbyname ("kern.osproductversion", (char*)buf
, &size
, NULL
, 0) >= 0)
96 version_string
= &buf
[0];
98 /* macOS 10.13.5 or older */
99 if (!version_string
) {
100 const char *kv_string
= kernel_version_string ();
101 if (starts_with (G_STRING_CONSTANT_AND_LENGTH ("17"), kv_string
))
102 version_string
= "10.13"; // High Sierra
103 else if (starts_with (G_STRING_CONSTANT_AND_LENGTH ("16"), kv_string
))
104 version_string
= "10.12"; // Sierra
105 else if (starts_with (G_STRING_CONSTANT_AND_LENGTH ("15"), kv_string
))
106 version_string
= "10.11"; // El Capitan
107 else if (starts_with (G_STRING_CONSTANT_AND_LENGTH ("14"), kv_string
))
108 version_string
= "10.10"; // Yosemite
109 else if (starts_with (G_STRING_CONSTANT_AND_LENGTH ("13"), kv_string
))
110 version_string
= "10.9"; // Mavericks
111 else if (starts_with (G_STRING_CONSTANT_AND_LENGTH ("12"), kv_string
))
112 version_string
= "10.8"; // Mountain Lion
113 else if (starts_with (G_STRING_CONSTANT_AND_LENGTH ("11"), kv_string
))
114 version_string
= "10.7"; // Lion
119 return version_string
;
122 // To get the path of the running process
138 MERP_EXC_FORCE_QUIT
= 1,
139 MERP_EXC_SIGSEGV
= 2,
140 MERP_EXC_SIGABRT
= 3,
144 MERP_EXC_SIGFPE
= 7 ,
145 MERP_EXC_SIGTRAP
= 8,
146 MERP_EXC_SIGKILL
= 9,
151 const char *merpFilePath
;
152 const char *crashLogPath
;
153 const char *werXmlPath
;
155 const char *bundleIDArg
; // App Bundle ID (required for bucketization)
156 const char *versionArg
; // App Version (required for bucketization)
158 MerpArch archArg
; // Arch, MacOS only, bails out if not found also required for bucketization. (required)
159 MERPExcType exceptionArg
; // Exception type (refer to merpcommon.h and mach/exception_types.h for more info (optional)
161 const char *serviceNameArg
; // This is the Bootstrap service name that MERP GUI will create to receive mach_task_self on a port created. Bails out if MERP GUI fails to receive mach_task_self from the crashed app. (Required for crash log generation)
162 const char *servicePathArg
; // The path to the executable, used to relaunch the crashed app.
164 const char *moduleName
;
165 const char *moduleVersion
;
168 const char *osVersion
;
169 int uiLidArg
; // MONO_LOCALE_INVARIANT 0x007F
171 char systemModel
[100];
172 const char *systemManufacturer
;
174 const char *eventType
; /* Must be MONO_MERP_EVENT_TYPE_STR */
176 MonoStackHash hashes
;
180 /* The event type determines the format of the fields that are reported. It
181 * must be MonoAppCrash for the rest of our report to make sense.
183 #define MONO_MERP_EVENT_TYPE_STR "MonoAppCrash"
187 gboolean enable_merp
;
189 const char *appBundleID
;
191 const char *appSignature
;
192 const char *appVersion
;
193 const char *merpGUIPath
;
194 const char *eventType
;
195 const char *merpFilePath
;
196 const char *crashLogPath
;
197 const char *werXmlPath
;
198 const char *moduleVersion
;
204 static MerpOptions config
;
209 } MonoMerpAnnotationEntry
;
212 get_merp_bitness (MerpArch arch
)
220 g_assert_not_reached ();
229 #elif defined(TARGET_AMD64)
230 return MerpArchx86_64
;
231 #elif defined(TARGET_POWERPC)
233 #elif defined(TARGET_POWERPC64)
234 return MerpArchPPC64
;
236 g_assert_not_reached ();
241 get_merp_exctype (MERPExcType exc
)
244 case MERP_EXC_FORCE_QUIT
:
246 case MERP_EXC_SIGSEGV
:
248 case MERP_EXC_SIGABRT
:
250 case MERP_EXC_SIGSYS
:
252 case MERP_EXC_SIGILL
:
254 case MERP_EXC_SIGBUS
:
256 case MERP_EXC_SIGFPE
:
258 case MERP_EXC_SIGTRAP
:
260 case MERP_EXC_SIGKILL
:
265 // Exception type documented as optional, not optional
266 g_assert_not_reached ();
268 g_assert_not_reached ();
273 parse_exception_type (const char *signal
)
275 if (!strcmp (signal
, "SIGSEGV"))
276 return MERP_EXC_SIGSEGV
;
278 if (!strcmp (signal
, "SIGFPE"))
279 return MERP_EXC_SIGFPE
;
281 if (!strcmp (signal
, "SIGILL"))
282 return MERP_EXC_SIGILL
;
284 if (!strcmp (signal
, "SIGABRT"))
285 return MERP_EXC_SIGABRT
;
287 // Force quit == hang?
288 // We need a default for this
289 if (!strcmp (signal
, "SIGTERM"))
290 return MERP_EXC_HANG
;
292 // FIXME: There are no other such signal
293 // strings passed to mono_handle_native_crash at the
294 // time of writing this
295 g_error ("Merp doesn't know how to handle %s\n", signal
);
298 static int merp_file_permissions
= S_IWUSR
| S_IRUSR
| S_IRGRP
| S_IROTH
;
301 mono_merp_write_params (MERPStruct
*merp
)
303 int handle
= g_open (merp
->merpFilePath
, O_TRUNC
| O_WRONLY
| O_CREAT
, merp_file_permissions
);
304 g_assertf (handle
!= -1, "Could not open MERP file at %s", merp
->merpFilePath
);
306 g_async_safe_fprintf(handle
, "ApplicationBundleId: %s\n", merp
->bundleIDArg
);
307 g_async_safe_fprintf(handle
, "ApplicationVersion: %s\n", merp
->versionArg
);
308 g_async_safe_fprintf(handle
, "ApplicationBitness: %s\n", get_merp_bitness (merp
->archArg
));
310 g_async_safe_fprintf(handle
, "ApplicationName: %s\n", merp
->serviceNameArg
);
311 g_async_safe_fprintf(handle
, "ApplicationPath: %s\n", merp
->servicePathArg
);
312 g_async_safe_fprintf(handle
, "BlameModuleName: %s\n", merp
->moduleName
);
313 g_async_safe_fprintf(handle
, "BlameModuleVersion: %s\n", merp
->moduleVersion
);
314 g_async_safe_fprintf(handle
, "BlameModuleOffset: 0x%llx\n", (unsigned long long)merp
->moduleOffset
);
315 g_async_safe_fprintf(handle
, "ExceptionType: %s\n", get_merp_exctype (merp
->exceptionArg
));
316 g_async_safe_fprintf(handle
, "StackChecksum: 0x%llx\n", merp
->hashes
.offset_free_hash
);
317 g_async_safe_fprintf(handle
, "StackHash: 0x%llx\n", merp
->hashes
.offset_rich_hash
);
320 g_async_safe_fprintf(handle
, "OSVersion: %s\n", merp
->osVersion
);
321 g_async_safe_fprintf(handle
, "LanguageID: 0x%x\n", merp
->uiLidArg
);
322 g_async_safe_fprintf(handle
, "SystemManufacturer: %s\n", merp
->systemManufacturer
);
323 g_async_safe_fprintf(handle
, "SystemModel: %s\n", merp
->systemModel
);
324 g_async_safe_fprintf(handle
, "EventType: %s\n", merp
->eventType
);
331 mono_merp_send (MERPStruct
*merp
)
333 gboolean invoke_success
= FALSE
;
335 #if defined(HAVE_EXECV) && defined(HAVE_FORK)
336 pid_t pid
= (pid_t
) fork ();
338 // Only one we define on OSX
340 const char *open_path
= "/usr/bin/open";
341 const char *argvOpen
[] = {open_path
, "-a", config
.merpGUIPath
, NULL
};
342 execv (open_path
, (char**)argvOpen
);
346 int exit_status
= FALSE
;
349 if (waitpid(pid
, &status
, WUNTRACED
| WCONTINUED
) == -1)
352 if (WIFEXITED(status
)) {
353 exit_status
= WEXITSTATUS(status
);
354 invoke_success
= (exit_status
== 0);
356 } else if (WIFSIGNALED(status
)) {
362 // // Create process to launch merp gui application
365 return invoke_success
;
369 get_apple_model (char *buffer
, size_t max_length
)
373 // Get the number of bytes to copy
374 sysctlbyname("hw.model", NULL
, &sz
, NULL
, 0);
376 if (sz
> max_length
) {
381 sysctlbyname("hw.model", buffer
, &sz
, NULL
, 0);
385 mono_init_merp (const intptr_t crashed_pid
, const char *signal
, MonoStackHash
*hashes
, MERPStruct
*merp
)
387 mono_memory_barrier ();
388 g_assert (mono_merp_enabled ());
390 merp
->merpFilePath
= config
.merpFilePath
;
391 merp
->crashLogPath
= config
.crashLogPath
;
392 merp
->werXmlPath
= config
.werXmlPath
;
394 // If these aren't set, icall wasn't made
395 // don't do merp? / don't set the variable to use merp;
396 g_assert (config
.appBundleID
);
397 g_assert (config
.appVersion
);
398 merp
->bundleIDArg
= config
.appSignature
;
399 merp
->versionArg
= config
.appVersion
;
401 merp
->archArg
= get_merp_arch ();
402 merp
->exceptionArg
= parse_exception_type (signal
);
404 merp
->serviceNameArg
= config
.appBundleID
;
405 merp
->servicePathArg
= config
.appPath
;
407 merp
->moduleName
= "Mono Exception";
408 merp
->moduleVersion
= config
.moduleVersion
;
410 merp
->moduleOffset
= 0;
412 merp
->uiLidArg
= MONO_LOCALE_INVARIANT
;
413 #if defined (TARGET_OSX)
414 merp
->osVersion
= macos_version_string ();
416 merp
->osVersion
= kernel_version_string ();
419 // FIXME: THis is apple-only for now
420 merp
->systemManufacturer
= "apple";
421 get_apple_model ((char *) merp
->systemModel
, sizeof (merp
->systemModel
));
423 merp
->eventType
= MONO_MERP_EVENT_TYPE_STR
;
425 merp
->hashes
= *hashes
;
427 merp
->annotations
= config
.annotations
;
431 mono_merp_write_fingerprint_payload (const char *non_param_data
, const MERPStruct
*merp
)
433 int handle
= g_open (merp
->crashLogPath
, O_TRUNC
| O_WRONLY
| O_CREAT
, merp_file_permissions
);
434 g_assertf (handle
!= -1, "Could not open crash log file at %s", merp
->crashLogPath
);
436 g_async_safe_fprintf(handle
, "{\n");
437 g_async_safe_fprintf(handle
, "\t\"payload\" : \n");
438 g_write (handle
, non_param_data
, (guint32
)strlen (non_param_data
));
439 g_async_safe_fprintf(handle
, ",\n");
441 g_async_safe_fprintf(handle
, "\t\"parameters\" : \n{\n");
442 g_async_safe_fprintf(handle
, "\t\t\"ApplicationBundleId\" : \"%s\",\n", merp
->bundleIDArg
);
443 g_async_safe_fprintf(handle
, "\t\t\"ApplicationVersion\" : \"%s\",\n", merp
->versionArg
);
444 g_async_safe_fprintf(handle
, "\t\t\"ApplicationBitness\" : \"%s\",\n", get_merp_bitness (merp
->archArg
));
445 g_async_safe_fprintf(handle
, "\t\t\"ApplicationName\" : \"%s\",\n", merp
->serviceNameArg
);
446 g_async_safe_fprintf(handle
, "\t\t\"BlameModuleName\" : \"%s\",\n", merp
->moduleName
);
447 g_async_safe_fprintf(handle
, "\t\t\"BlameModuleVersion\" : \"%s\",\n", merp
->moduleVersion
);
448 g_async_safe_fprintf(handle
, "\t\t\"BlameModuleOffset\" : \"0x%lx\",\n", merp
->moduleOffset
);
449 g_async_safe_fprintf(handle
, "\t\t\"ExceptionType\" : \"%s\",\n", get_merp_exctype (merp
->exceptionArg
));
450 g_async_safe_fprintf(handle
, "\t\t\"StackChecksum\" : \"0x%llx\",\n", merp
->hashes
.offset_free_hash
);
451 g_async_safe_fprintf(handle
, "\t\t\"StackHash\" : \"0x%llx\",\n", merp
->hashes
.offset_rich_hash
);
452 g_async_safe_fprintf(handle
, "\t\t\"Extra\" : \n\t\t{\n");
454 for (GSList
*cursor
= merp
->annotations
; cursor
; cursor
= cursor
->next
) {
455 MonoMerpAnnotationEntry
*iter
= (MonoMerpAnnotationEntry
*) cursor
->data
;
456 g_async_safe_fprintf(handle
, "\t\t\t\"%s\" : \"%s\"\n", iter
->key
, iter
->value
);
459 g_async_safe_fprintf(handle
, "\t\t},\n");
462 g_async_safe_fprintf(handle
, "\t\t\"OSVersion\" : \"%s\",\n", merp
->osVersion
);
463 g_async_safe_fprintf(handle
, "\t\t\"LanguageID\" : \"0x%x\",\n", merp
->uiLidArg
);
464 g_async_safe_fprintf(handle
, "\t\t\"SystemManufacturer\" : \"%s\",\n", merp
->systemManufacturer
);
465 g_async_safe_fprintf(handle
, "\t\t\"SystemModel\" : \"%s\",\n", merp
->systemModel
);
466 g_async_safe_fprintf(handle
, "\t\t\"EventType\" : \"%s\"\n", merp
->eventType
);
469 g_async_safe_fprintf(handle
, "\t}\n");
470 g_async_safe_fprintf(handle
, "}\n");
479 mono_write_wer_template (MERPStruct
*merp
)
481 // Note about missing ProcessInformation block: we have no PID that makes sense
482 // and when mono is embedded and used to run functions without an entry point,
483 // there is no image that would make any semantic sense to send either.
484 // It's a nuanced problem, each way we can run mono would need a separate fix.
486 int handle
= g_open (merp
->werXmlPath
, O_WRONLY
| O_CREAT
| O_TRUNC
, merp_file_permissions
);
487 g_assertf (handle
!= -1, "Could not open WER XML file at %s", merp
->werXmlPath
);
490 g_async_safe_fprintf(handle
, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
491 g_async_safe_fprintf(handle
, "<WERReportMetadata>\n");
492 g_async_safe_fprintf(handle
, "<ProblemSignatures>\n");
493 g_async_safe_fprintf(handle
, "<EventType>%s</EventType>\n", merp
->eventType
);
496 g_async_safe_fprintf(handle
, "<Parameter%d>%s</Parameter%d>\n", i
, merp
->bundleIDArg
, i
);
498 g_async_safe_fprintf(handle
, "<Parameter%d>%s</Parameter%d>\n", i
, merp
->versionArg
, i
);
500 g_async_safe_fprintf(handle
, "<Parameter%d>%s</Parameter%d>\n", i
, get_merp_bitness (merp
->archArg
), i
);
502 g_async_safe_fprintf(handle
, "<Parameter%d>%s</Parameter%d>\n", i
, merp
->serviceNameArg
, i
);
504 g_async_safe_fprintf(handle
, "<Parameter%d>%s</Parameter%d>\n", i
, merp
->moduleName
, i
);
506 g_async_safe_fprintf(handle
, "<Parameter%d>%s</Parameter%d>\n", i
, merp
->moduleVersion
, i
);
508 g_async_safe_fprintf(handle
, "<Parameter%d>0x%zx</Parameter%d>\n", i
, merp
->moduleOffset
, i
);
510 g_async_safe_fprintf(handle
, "<Parameter%d>%s</Parameter%d>\n", i
, get_merp_exctype (merp
->exceptionArg
), i
);
512 g_async_safe_fprintf(handle
, "<Parameter%d>0x%llx</Parameter%d>\n", i
, merp
->hashes
.offset_free_hash
, i
);
514 g_async_safe_fprintf(handle
, "<Parameter%d>0x%llx</Parameter%d>\n", i
, merp
->hashes
.offset_rich_hash
, i
);
516 g_async_safe_fprintf(handle
, "<Parameter%d>%s</Parameter%d>\n", i
, merp
->osVersion
, i
);
518 g_async_safe_fprintf(handle
, "<Parameter%d>0x%x</Parameter%d>\n", i
, merp
->uiLidArg
, i
);
520 g_async_safe_fprintf(handle
, "<Parameter%d>%s</Parameter%d>\n", i
, merp
->systemManufacturer
, i
);
522 g_async_safe_fprintf(handle
, "<Parameter%d>%s</Parameter%d>\n", i
, merp
->systemModel
, i
);
525 g_async_safe_fprintf(handle
, "</ProblemSignatures>\n");
526 g_async_safe_fprintf(handle
, "</WERReportMetadata>\n");
535 mono_merp_invoke (const intptr_t crashed_pid
, const char *signal
, const char *non_param_data
, MonoStackHash
*hashes
)
538 int merp_tmp_file_tag
= 2;
539 gboolean alloc_success
= mono_state_alloc_mem (&mem
, merp_tmp_file_tag
, sizeof (MERPStruct
));
543 MERPStruct
*merp
= (MERPStruct
*) mem
.mem
;
544 memset (merp
, 0, sizeof (*merp
));
546 mono_summarize_timeline_phase_log (MonoSummaryMerpWriter
);
548 mono_init_merp (crashed_pid
, signal
, hashes
, merp
);
550 if (!mono_merp_write_params (merp
))
553 if (!mono_merp_write_fingerprint_payload (non_param_data
, merp
))
556 if (!mono_write_wer_template (merp
))
560 mono_summarize_timeline_phase_log (MonoSummaryMerpInvoke
);
561 gboolean success
= mono_merp_send (merp
);
564 mono_summarize_timeline_phase_log (MonoSummaryCleanup
);
566 mono_state_free_mem (&mem
);
572 mono_merp_add_annotation (const char *key
, const char *value
)
574 MonoMerpAnnotationEntry
*entry
= g_new0 (MonoMerpAnnotationEntry
, 1);
575 entry
->key
= g_strdup (key
);
576 entry
->value
= g_strdup (value
);
577 config
.annotations
= g_slist_prepend (config
.annotations
, entry
);
581 mono_merp_disable (void)
583 mono_memory_barrier ();
585 if (!config
.enable_merp
)
588 g_free ((char*)config
.appBundleID
); // cast away const
589 g_free ((char*)config
.appSignature
);
590 g_free ((char*)config
.appVersion
);
591 g_free ((char*)config
.merpGUIPath
);
592 g_free ((char*)config
.appPath
);
593 g_free ((char*)config
.moduleVersion
);
594 g_slist_free (config
.annotations
);
595 memset (&config
, 0, sizeof (config
));
597 mono_memory_barrier ();
601 mono_merp_enable (const char *appBundleID
, const char *appSignature
, const char *appVersion
, const char *merpGUIPath
, const char *appPath
, const char *configDir
)
603 mono_memory_barrier ();
605 g_assert (!config
.enable_merp
);
610 const char *home
= g_get_home_dir ();
611 prefix
= g_strdup_printf ("%s/Library/Group Containers/UBF8T346G9.ms/", home
);
613 prefix
= g_strdup (configDir
);
615 config
.merpFilePath
= g_strdup_printf ("%s%s", prefix
, "MERP.uploadparams.txt");
616 config
.crashLogPath
= g_strdup_printf ("%s%s", prefix
, "lastcrashlog.txt");
617 config
.werXmlPath
= g_strdup_printf ("%s%s", prefix
, "CustomLogsMetadata.xml");
620 config
.moduleVersion
= mono_get_runtime_callbacks ()->get_runtime_build_info ();
622 config
.appBundleID
= g_strdup (appBundleID
);
623 config
.appSignature
= g_strdup (appSignature
);
624 config
.appVersion
= g_strdup (appVersion
);
625 config
.merpGUIPath
= g_strdup (merpGUIPath
);
626 config
.appPath
= g_strdup (appPath
);
628 config
.log
= g_getenv ("MONO_MERP_VERBOSE") != NULL
;
630 config
.enable_merp
= TRUE
;
632 mono_memory_barrier ();
636 mono_merp_enabled (void)
638 return config
.enable_merp
;
643 #include <mono/utils/mono-compiler.h>
645 MONO_EMPTY_SOURCE_FILE (mono_merp
);