summaryrefslogtreecommitdiff
path: root/src/vm/eventreporter.cpp
blob: 6ef767c424b7e327f60cee639ae861a89ec374d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//

//
//*****************************************************************************
// EventReporter.cpp
//
// A utility to log an entry in event log.
//
//*****************************************************************************


#include "common.h"
#include "utilcode.h"
#include "eventreporter.h"
#include "typestring.h"
#include "debugdebugger.h"

#include "../dlls/mscorrc/resource.h"

#if defined(FEATURE_CORECLR)
#include "getproductversionnumber.h"
#endif // FEATURE_CORECLR

//---------------------------------------------------------------------------------------
//
// A constructor for EventReporter.  The header of the log is generated here.
//
// Arguments:
//    type - Event report type
//
// Assumptions:
//    The argument type must be valid.
//
EventReporter::EventReporter(EventReporterType type)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

    m_eventType = type;

    HMODULE hModule = WszGetModuleHandle(NULL);
    PathString appPath;
    DWORD ret = WszGetModuleFileName(hModule, appPath);

    fBufferFull = FALSE;

    InlineSString<256> ssMessage;

    if(!ssMessage.LoadResource(CCompRC::Optional, IDS_ER_APPLICATION))
        m_Description.Append(W("Application: "));
    else
    {
        m_Description.Append(ssMessage);
    }

    // If we were able to get an app name.
    if (ret != 0)
    {
        // If app name has a '\', consider the part after that; otherwise consider whole name.
        LPCWSTR appName =  wcsrchr(appPath, W('\\'));
        appName = appName ? appName+1 : appPath;
        m_Description.Append(appName);
        m_Description.Append(W("\n"));
    }
    else
    {
        ssMessage.Clear();
        if(!ssMessage.LoadResource(CCompRC::Optional, IDS_ER_UNKNOWN))
            m_Description.Append(W("unknown\n"));
        else
        {
            m_Description.Append(ssMessage);
            m_Description.Append(W("\n"));
        }
    }

    ssMessage.Clear();
    if(!ssMessage.LoadResource(CCompRC::Optional, IDS_ER_FRAMEWORK_VERSION))
#ifndef FEATURE_CORECLR
        m_Description.Append(W("Framework Version: "));
#else // FEATURE_CORECLR
        m_Description.Append(W("CoreCLR Version: "));
#endif // !FEATURE_CORECLR
    else
    {
        m_Description.Append(ssMessage);
    }

    BOOL fHasVersion = FALSE;
#ifndef FEATURE_CORECLR
    if (GetCLRModule() != NULL)
    {
        WCHAR buffer[80];
        DWORD length;
        if (SUCCEEDED(GetCORVersionInternal(buffer, 80, &length)))
        {
            m_Description.Append(buffer);
            m_Description.Append(W("\n"));
            fHasVersion = TRUE;
        }
    }
#else // FEATURE_CORECLR
    DWORD dwMajorVersion = 0;
    DWORD dwMinorVersion = 0;
    DWORD dwBuild = 0;
    DWORD dwRevision = 0;
    EventReporter::GetCoreCLRInstanceProductVersion(&dwMajorVersion, &dwMinorVersion, &dwBuild, &dwRevision);
    m_Description.AppendPrintf(W("%lu.%lu.%lu.%lu\n"),dwMajorVersion, dwMinorVersion, dwBuild, dwRevision);
    fHasVersion = TRUE;
#endif // !FEATURE_CORECLR

    if (!fHasVersion)
    {
        ssMessage.Clear();
        if(!ssMessage.LoadResource(CCompRC::Optional, IDS_ER_UNKNOWN))
            m_Description.Append(W("unknown\n"));
        else
        {
            m_Description.Append(ssMessage);
            m_Description.Append(W("\n"));
        }        
    }

    ssMessage.Clear();

    switch(m_eventType) {
    case ERT_UnhandledException:
        if(!ssMessage.LoadResource(CCompRC::Optional, IDS_ER_UNHANDLEDEXCEPTION))
            m_Description.Append(W("Description: The process was terminated due to an unhandled exception."));
        else
        {
            m_Description.Append(ssMessage);
        }
        m_Description.Append(W("\n"));
        break;

    case ERT_ManagedFailFast:
        if(!ssMessage.LoadResource(CCompRC::Optional, IDS_ER_MANAGEDFAILFAST))
            m_Description.Append(W("Description: The application requested process termination through System.Environment.FailFast(string message)."));
        else
        {
            m_Description.Append(ssMessage);
        }
        m_Description.Append(W("\n"));
        break;

    case ERT_UnmanagedFailFast:
        if(!ssMessage.LoadResource(CCompRC::Optional, IDS_ER_UNMANAGEDFAILFAST))
            m_Description.Append(W("Description: The process was terminated due to an internal error in the .NET Runtime "));
        else
        {
            m_Description.Append(ssMessage);
        }
        break;

    case ERT_StackOverflow:
        // Fetch the localized Stack Overflow Error text or fall back on a hardcoded variant if things get dire.
        if(!ssMessage.LoadResource(CCompRC::Optional, IDS_ER_STACK_OVERFLOW))
            m_Description.Append(W("Description: The process was terminated due to stack overflow."));
        else
        {
            m_Description.Append(ssMessage);
        }
        m_Description.Append(W("\n"));
        break;

    case ERT_CodeContractFailed:
        if(!ssMessage.LoadResource(CCompRC::Optional, IDS_ER_CODECONTRACT_FAILED))
            m_Description.Append(W("Description: The application encountered a bug.  A managed code contract (precondition, postcondition, object invariant, or assert) failed."));
        else
        {
            m_Description.Append(ssMessage);
        }
        m_Description.Append(W("\n"));
        break;

    default:
        _ASSERTE(!"Unknown EventReporterType.");
        break;
    }
}


//---------------------------------------------------------------------------------------
//
// Add extra description to the EventLog report.
//
// Arguments:
//    pString - The extra description to append to log
//
// Return Value:
//    None.
//
void EventReporter::AddDescription(__in WCHAR *pString)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        SO_INTOLERANT;
        MODE_ANY;
    }
    CONTRACTL_END;
    StackSString s(pString);
    AddDescription(s);
}

//---------------------------------------------------------------------------------------
//
// Add extra description to the EventLog report.
//
// Arguments:
//    pString - The extra description to append to log
//
// Return Value:
//    None.
//
void EventReporter::AddDescription(SString& s)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        SO_INTOLERANT;
        MODE_ANY;
    }
    CONTRACTL_END;
    _ASSERTE (m_eventType == ERT_UnhandledException || m_eventType == ERT_ManagedFailFast ||
              m_eventType == ERT_UnmanagedFailFast || m_eventType == ERT_CodeContractFailed);
    if (m_eventType == ERT_ManagedFailFast)
    {
        SmallStackSString ssMessage;
        if(!ssMessage.LoadResource(CCompRC::Optional, IDS_ER_MANAGEDFAILFASTMSG))
            m_Description.Append(W("Message: "));
        else
        {
            m_Description.Append(ssMessage);
        }
    }
    else if (m_eventType == ERT_UnhandledException)
    {
        SmallStackSString ssMessage;
        if (!ssMessage.LoadResource(CCompRC::Optional, IDS_ER_UNHANDLEDEXCEPTIONMSG))
        {
            m_Description.Append(W("Exception Info: "));
        }
        else
        {
            m_Description.Append(ssMessage);
        }
    }
    else if (m_eventType == ERT_CodeContractFailed)
    {
        SmallStackSString ssMessage;
        if (!ssMessage.LoadResource(CCompRC::Optional, IDS_ER_CODECONTRACT_DETAILMSG))
            m_Description.Append(W("Contract details: "));
        else
            m_Description.Append(ssMessage);
    }
    m_Description.Append(s);
    m_Description.Append(W("\n"));
}

//---------------------------------------------------------------------------------------
//
// Add a marker for stack trace section in the EventLog entry
//
// Arguments:
//    None.
//
// Return Value:
//    None.
//
void EventReporter::BeginStackTrace()
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        SO_INTOLERANT;
        MODE_ANY;
    }
    CONTRACTL_END;
    _ASSERTE (m_eventType == ERT_UnhandledException || m_eventType == ERT_ManagedFailFast || m_eventType == ERT_CodeContractFailed);
    InlineSString<80> ssMessage;
    if(!ssMessage.LoadResource(CCompRC::Optional, IDS_ER_STACK))
        m_Description.Append(W("Stack:\n"));
    else
    {
        m_Description.Append(ssMessage);
        m_Description.Append(W("\n"));
    }
}

//---------------------------------------------------------------------------------------
//
// Add the signature of one managed stack frame into EventLog entry.
//
// Arguments:
//    s       - The signature of managed function, including argument type
//
// Return Value:
//    None.
//
void EventReporter::AddStackTrace(SString& s)
{
    WRAPPER_NO_CONTRACT;
    _ASSERTE (m_eventType == ERT_UnhandledException || m_eventType == ERT_ManagedFailFast || m_eventType == ERT_CodeContractFailed);

    // Continue to append to the buffer until we are full
    if (fBufferFull == FALSE)
    {
        m_Description.Append(s);
        m_Description.Append(W("\n"));

        COUNT_T curSize = m_Description.GetCount();

        // Truncate the buffer if we have exceeded the limit based upon the OS we are on
        DWORD dwMaxSizeLimit = MAX_SIZE_EVENTLOG_ENTRY_STRING_WINVISTA;
        if (curSize >= dwMaxSizeLimit)
        {
            // Load the truncation message
            StackSString truncate;
            if (!truncate.LoadResource(CCompRC::Optional, IDS_ER_MESSAGE_TRUNCATE))
            {
                truncate.Set(W("The remainder of the message was truncated."));
            }
            truncate.Insert(truncate.Begin(), W("\n"));
            truncate.Insert(truncate.End(), W("\n"));

            SString::Iterator ext;
            COUNT_T truncCount = truncate.GetCount();

            // Go back "truncCount" characters from the end of the string.
            // The "-1" in end is to accomodate null termination.
            ext = m_Description.Begin() + dwMaxSizeLimit - truncCount - 1;

            // Now look for a "\n" from the last position we got
            BOOL fFoundMarker = m_Description.FindBack(ext, W("\n"));
            if (ext != m_Description.Begin())
            {
                // Move to the next character if we found the "\n"
                if (fFoundMarker)
                   ext++;
            }

            // Truncate the string till our current position and append
            // the truncation message
            m_Description.Truncate(ext);
            m_Description.Append(truncate);

            // Set the flag that we are full - no point appending more stack details
            fBufferFull = TRUE;
        }
    }
}

//---------------------------------------------------------------------------------------
//
// Generate an entry in EventLog.
//
// Arguments:
//    None.
//
// Return Value:
//    None.
//
void EventReporter::Report()
{
    CONTRACTL
    {
        NOTHROW; 
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    DWORD eventID;
    switch (m_eventType)
    {
    case ERT_UnhandledException:
        eventID = 1026;
        break;
    case ERT_ManagedFailFast:
        eventID = 1025;
        break;
    case ERT_UnmanagedFailFast:
        eventID = 1023;
        break;
    case ERT_StackOverflow:
        eventID = 1027;
        break;
    case ERT_CodeContractFailed:
        eventID = 1028;
        break;
    default:
        _ASSERTE(!"Invalid event type");
        eventID = 1023;
        break;
    }

    CONTRACT_VIOLATION(ThrowsViolation);
    
    COUNT_T ctSize = m_Description.GetCount();
    LOG((LF_EH, LL_INFO100, "EventReporter::Report - Writing %d bytes to event log.\n", ctSize));

    if (ctSize > 0)
    {
        DWORD dwRetVal = ClrReportEvent(W(".NET Runtime"),
                       EVENTLOG_ERROR_TYPE,
                       0,
                       eventID,
                       NULL,
                       m_Description.GetUnicode()
                       );

        if (dwRetVal != ERROR_SUCCESS)
        {
            LOG((LF_EH, LL_INFO100, "EventReporter::Report - Error (win32 code %d) while writing to event log.\n", dwRetVal));

            // We were unable to log the error to event log - now check why.
            if ((dwRetVal != ERROR_EVENTLOG_FILE_CORRUPT) && (dwRetVal != ERROR_LOG_FILE_FULL) && 
                (dwRetVal != ERROR_NOT_ENOUGH_MEMORY)) // Writing to the log can fail under OOM (observed on Vista)
            {
                // If the event log file was neither corrupt nor full, then assert,
                // since something is wrong!
#ifndef _TARGET_ARM_
                //ARMTODO: Event reporting is currently non-functional on winpe.
                _ASSERTE(!"EventReporter::Report - Unable to log details to event log!");
#endif
            }
            else
            {
                // Since the event log file was either corrupt or full, simply
                // write this status to our log. We cannot fix a corrupt file
                // and we cannot clear the log since we dont administer the machine.
                STRESS_LOG0(LF_CORDB, LL_INFO10000, "EventReporter::Report: Event log is full, corrupt or not enough memory to process.\n");
            }
        }
    }
}


//---------------------------------------------------------------------------------------
//
// Check if we should generate an EventLog entry.
//
// Arguments:
//    None
//
// Return Value:
//    TRUE  - We should generate one entry
//    FALSE - We should not generate one entry
//
BOOL ShouldLogInEventLog()
{
    CONTRACTL
    {
        NOTHROW; 
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    // no event log on Apollo
    return FALSE;
}

//---------------------------------------------------------------------------------------
//
// A callback function for stack walker to save signature of one managed frame.
//
// Arguments:
//    pCF      - The frame info passed by stack walker.
//    pData    - The data to pass info between stack walker and its caller
//
// Return Value:
//    SWA_CONTINUE  - Continue search for the next frame.
//
struct LogCallstackData
{
    EventReporter *pReporter;
    SmallStackSString *pWordAt;
};

StackWalkAction LogCallstackForEventReporterCallback(
    CrawlFrame       *pCF,      //
    VOID*             pData     // Caller's private data
)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        SO_INTOLERANT;
        MODE_ANY;
    }
    CONTRACTL_END;

    EventReporter* pReporter = ((LogCallstackData*)pData)->pReporter;
    SmallStackSString *pWordAt = ((LogCallstackData*)pData)->pWordAt;

    MethodDesc *pMD = pCF->GetFunction();
    _ASSERTE(pMD != NULL);

    StackSString str;
    str = *pWordAt;

    TypeString::AppendMethodInternal(str, pMD, TypeString::FormatNamespace|TypeString::FormatFullInst|TypeString::FormatSignature); 
    pReporter->AddStackTrace(str);

    return SWA_CONTINUE;
}

//---------------------------------------------------------------------------------------
//
// A woker to save managed stack trace.
//
// Arguments:
//    reporter - EventReporter object for EventLog
//
// Return Value:
//    None
//
void LogCallstackForEventReporterWorker(EventReporter& reporter)
{
    Thread* pThread = GetThread();
    _ASSERTE (pThread);

    SmallStackSString WordAt;

    if (!WordAt.LoadResource(CCompRC::Optional, IDS_ER_WORDAT))
    {
        WordAt.Set(W("   at"));
    }
    else
    {
        WordAt.Insert(WordAt.Begin(), W("   "));
    }
    WordAt += W(" ");

    LogCallstackData data = {
        &reporter, &WordAt
    };

    pThread->StackWalkFrames(&LogCallstackForEventReporterCallback, &data, QUICKUNWIND | FUNCTIONSONLY);
}

//---------------------------------------------------------------------------------------
//
// Generate stack trace info for those managed frames on the stack currently.
//
// Arguments:
//    reporter - EventReporter object for EventLog
//
// Return Value:
//    None
//
void LogCallstackForEventReporter(EventReporter& reporter)
{
    WRAPPER_NO_CONTRACT;

    reporter.BeginStackTrace();

    LogCallstackForEventReporterWorker(reporter);
}

void ReportExceptionStackHelper(OBJECTREF exObj, EventReporter& reporter, SmallStackSString& wordAt, int recursionLimit)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    if (exObj == NULL || recursionLimit == 0)
    {
        return;
    }

    struct
    {
        OBJECTREF exObj;
        EXCEPTIONREF ex;
        STRINGREF remoteStackTraceString;
    } gc;
    ZeroMemory(&gc, sizeof(gc));
    gc.exObj = exObj;
    gc.ex = (EXCEPTIONREF)exObj;

    GCPROTECT_BEGIN(gc);

    ReportExceptionStackHelper((gc.ex)->GetInnerException(), reporter, wordAt, recursionLimit - 1);

    StackSString exTypeStr;
    TypeString::AppendType(exTypeStr, TypeHandle((gc.ex)->GetMethodTable()), TypeString::FormatNamespace | TypeString::FormatFullInst);
    reporter.AddDescription(exTypeStr);

    gc.remoteStackTraceString = (gc.ex)->GetRemoteStackTraceString();
    if (gc.remoteStackTraceString != NULL && gc.remoteStackTraceString->GetStringLength())
    {
        SString remoteStackTrace;
        gc.remoteStackTraceString->GetSString(remoteStackTrace);

        // If source info is contained, trim it
        StripFileInfoFromStackTrace(remoteStackTrace);

        reporter.AddStackTrace(remoteStackTrace);
    }

    DebugStackTrace::GetStackFramesData stackFramesData;
    stackFramesData.pDomain = NULL;
    stackFramesData.skip = 0;
    stackFramesData.NumFramesRequested = 0;

    DebugStackTrace::GetStackFramesFromException(&(gc.exObj), &stackFramesData);

    for (int j = 0; j < stackFramesData.cElements; j++)
    {
        StackSString str;
        str = wordAt;
        TypeString::AppendMethodInternal(str, stackFramesData.pElements[j].pFunc, TypeString::FormatNamespace | TypeString::FormatFullInst | TypeString::FormatSignature);
        reporter.AddStackTrace(str);
    }

    StackSString separator(L""); // This will result in blank line
    reporter.AddStackTrace(separator);

    GCPROTECT_END();
}


//---------------------------------------------------------------------------------------
//
// Generate an EventLog entry for unhandled exception.
//
// Arguments:
//    pExceptionInfo - Exception information
//
// Return Value:
//    None
//
void DoReportForUnhandledException(PEXCEPTION_POINTERS pExceptionInfo)
{
    WRAPPER_NO_CONTRACT;
    
    if (ShouldLogInEventLog())
    {
        Thread *pThread = GetThread();
        EventReporter reporter(EventReporter::ERT_UnhandledException);
        EX_TRY
        {
            StackSString s;
            if (pThread && pThread->HasException() != NULL)
            {
                GCX_COOP();
                struct
                {
                    OBJECTREF throwable;
                    STRINGREF originalExceptionMessage;
                } gc;
                ZeroMemory(&gc, sizeof(gc));
                
                GCPROTECT_BEGIN(gc);

                gc.throwable = pThread->GetThrowable();
                _ASSERTE(gc.throwable != NULL);

#ifdef FEATURE_CORECLR
                // On CoreCLR, managed code execution happens in non-default AppDomains and all threads have an AD transition
                // at their base from DefaultDomain to the target Domain before they start executing managed code. Thus, when 
                // an exception goes unhandled in a non-default AppDomain on a reverse pinvoke thread, the original exception details are copied 
                // to the Message property of System.CrossAppDomainMarshaledException instance at the AD transition boundary,
                // and the exception is then thrown in the calling AppDomain. This is done since CoreCLR does not support marshaling of 
                // objects across AppDomains.
                //
                // On SL, exceptions dont go unhandled to the OS. But in WLC, they can. Thus, when the scenario above happens for WLC,
                // the OS will invoke CoreCLR's registered UEF and reach here to write the stacktrace from the 
                // exception object (which will be a CrossAppDomainMarshaledException instance) to the event log. At this point,
                // we shall be in DefaultDomain. 
                //
                // However, the original exception details are in the Message property of CrossAppDomainMarshaledException. So, we should
                // look that up and if it is not empty, add those details to the EventReporter so that they get written to the
                // event log as well.
                //
                // We can also be here when in non-DefaultDomain an exception goes unhandled on a pure managed thread. In such a case,
                // we wont have CrossAppDomainMarshaledException instance but the original exception object that will be used to extract
                //  the stack trace from.
                if (pThread->GetDomain()->IsDefaultDomain())
                {
                    if (IsExceptionOfType(kCrossAppDomainMarshaledException, &(gc.throwable)))
                    {
                        // This is a CrossAppDomainMarshaledException instance - check if it has
                        // something for us in the Message property.
                        gc.originalExceptionMessage = ((EXCEPTIONREF)gc.throwable)->GetMessage();
                        if (gc.originalExceptionMessage != NULL)
                        {
                            // Ok - so, we have details about the original exception. Add them to the
                            // EventReporter object so that they get written to the event log.
                            reporter.AddDescription(gc.originalExceptionMessage->GetBuffer());

                            LOG((LF_EH, LL_INFO100, "DoReportForUnhandledException - Added original exception details to EventReporter from CrossAppDomainMarshaledException object.\n"));
                        }
                        else
                        {
                            LOG((LF_EH, LL_INFO100, "DoReportForUnhandledException - Original exception details not present in CrossAppDomainMarshaledException object.\n"));
                        }
                    }
                }
                else
#endif // FEATURE_CORECLR
                {
                    if (IsException(gc.throwable->GetMethodTable()))
                    {
                        SmallStackSString wordAt;
                        if (!wordAt.LoadResource(CCompRC::Optional, IDS_ER_WORDAT))
                        {
                            wordAt.Set(W("   at"));
                        }
                        else
                        {
                            wordAt.Insert(wordAt.Begin(), W("   "));
                        }
                        wordAt += W(" ");

                        ReportExceptionStackHelper(gc.throwable, reporter, wordAt, /* recursionLimit = */10);
                    }
                    else
                    {
                        TypeString::AppendType(s, TypeHandle(gc.throwable->GetMethodTable()), TypeString::FormatNamespace | TypeString::FormatFullInst);
                        reporter.AddDescription(s);
                        reporter.BeginStackTrace();
                        LogCallstackForEventReporterWorker(reporter);
                    }
                }

                GCPROTECT_END();
            }
            else
            {
                InlineSString<80> ssErrorFormat;
                if(!ssErrorFormat.LoadResource(CCompRC::Optional, IDS_ER_UNHANDLEDEXCEPTIONINFO))
                    ssErrorFormat.Set(W("exception code %1, exception address %2"));
                SmallStackSString exceptionCodeString;
                exceptionCodeString.Printf(W("%x"), pExceptionInfo->ExceptionRecord->ExceptionCode);
                SmallStackSString addressString;
                addressString.Printf(W("%p"), (UINT_PTR)pExceptionInfo->ExceptionRecord->ExceptionAddress);
                s.FormatMessage(FORMAT_MESSAGE_FROM_STRING, (LPCWSTR)ssErrorFormat, 0, 0, exceptionCodeString, addressString);
                reporter.AddDescription(s);
                if (pThread)
                {
                    LogCallstackForEventReporter(reporter);
                }
            }
        }
        EX_CATCH
        {
            // We are reporting an exception.  If we throw while working on this, it is not fatal.
        }
        EX_END_CATCH(SwallowAllExceptions);

        reporter.Report();
    }
}

#if defined(FEATURE_CORECLR)
// This function will return the product version of CoreCLR
// instance we are executing in.
void EventReporter::GetCoreCLRInstanceProductVersion(DWORD * pdwMajor, DWORD * pdwMinor, DWORD * pdwBuild, DWORD * pdwRevision)
{
    STATIC_CONTRACT_THROWS;

    // Get the instance of the runtime
    HMODULE hModRuntime = GetCLRModule();
    _ASSERTE(hModRuntime != NULL);

    // Get the path to the runtime
    PathString runtimePath;
    DWORD ret = WszGetModuleFileName(hModRuntime, runtimePath);
    if (ret != 0)
    {
        // Got the path - get the file version from the path
        SString path;
        path.Clear();
        path.Append(runtimePath);
        DWORD dwVersionMS = 0;
        DWORD dwVersionLS = 0;
        GetProductVersionNumber(path, &dwVersionMS, &dwVersionLS);

        // Get the Major.Minor.Build.Revision details from the returned values
        *pdwMajor = HIWORD(dwVersionMS);
        *pdwMinor = LOWORD(dwVersionMS);
        *pdwBuild = HIWORD(dwVersionLS);
        *pdwRevision = LOWORD(dwVersionLS);
        LOG((LF_CORDB, LL_INFO100, "GetCoreCLRInstanceVersion: Got CoreCLR version: %lu.%lu.%lu.%lu\n",
            *pdwMajor, *pdwMinor, *pdwBuild, *pdwRevision));
    }
    else
    {
        // Failed to get the path
        LOG((LF_CORDB, LL_INFO100, "GetCoreCLRInstanceVersion: Unable to get CoreCLR version.\n"));
    }
}
#endif // FEATURE_CORECLR