2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
6 // Type-safe helper wrapper to get an EXCEPTION_RECORD slot as a CORDB_ADDRESS
9 // pRecord - exception record
10 // idxSlot - slot to retrieve from.
13 // contents of slot as a CordbAddress.
14 CORDB_ADDRESS GetExceptionInfoAsAddress(const EXCEPTION_RECORD * pRecord, int idxSlot)
16 _ASSERTE((idxSlot >= 0) && (idxSlot < EXCEPTION_MAXIMUM_PARAMETERS));
18 // ExceptionInformation is an array of ULONG_PTR. CORDB_ADDRESS is a 0-extended ULONG64.
19 // So the implicit cast will work here on x86. On 64-bit, it's basically a nop.
20 return pRecord->ExceptionInformation[idxSlot];
24 // Determine if an exception event is a Debug event for this flavor of the CLR.
27 // pRecord - exception record
28 // pClrBaseAddress - clr Instance ID for which CLR in the target we're checking against.
31 // NULL if the exception is not a CLR managed debug event for the given Clr instance.
32 // Else, address in target process of managed debug event described by the exception (the payload).
35 // This decodes events raised by code:Debugger.SendRawEvent
36 // Anybody can spoof our exception, so this is not a reliably safe method.
37 // With multiple CLRs in the same process, it's essential to use the proper pClrBaseAddress.
38 CORDB_ADDRESS IsEventDebuggerNotification(
39 const EXCEPTION_RECORD * pRecord,
40 CORDB_ADDRESS pClrBaseAddress
43 _ASSERTE(pRecord != NULL);
45 // Must specify a CLR instance.
46 _ASSERTE(pClrBaseAddress != NULL);
48 // If it's not even our exception code, then it's not ours.
49 if (pRecord->ExceptionCode != CLRDBG_NOTIFICATION_EXCEPTION_CODE)
55 // Format of an ExceptionInformation parameter is:
56 // 0: cookie (CLRDBG_EXCEPTION_DATA_CHECKSUM)
57 // 1: Base address of mscorwks. This identifies the instance of the CLR.
58 // 2: Target Address of DebuggerIPCEvent, which contains the "real" event.
60 if (pRecord->NumberParameters != 3)
65 // 1st argument should always be the cookie.
66 // If cookie doesn't match, very likely it's a stray exception that happens to be using
68 DWORD cookie = (DWORD) pRecord->ExceptionInformation[0];
69 if (cookie != CLRDBG_EXCEPTION_DATA_CHECKSUM)
74 // TODO: We don't do this check in case of non-windows debugging now, because we don't support
75 // multi-instance debugging.
76 #if !defined(FEATURE_DBGIPC_TRANSPORT_VM) && !defined(FEATURE_DBGIPC_TRANSPORT_DI)
77 // If base-address doesn't match, then it's likely an event from another version of the CLR
79 // We need to be careful here. CORDB_ADDRESS is a ULONG64, whereas ExceptionInformation[1]
80 // is ULONG_PTR. So on 32-bit, their sizes don't match.
81 CORDB_ADDRESS pTargetBase = GetExceptionInfoAsAddress(pRecord, 1);
82 if (pTargetBase != pClrBaseAddress)
88 // It passes all the format checks. So now get the payload.
89 CORDB_ADDRESS ptrRemoteManagedEvent = GetExceptionInfoAsAddress(pRecord, 2);
91 return ptrRemoteManagedEvent;
94 #if defined(FEATURE_DBGIPC_TRANSPORT_VM) || defined(FEATURE_DBGIPC_TRANSPORT_DI)
95 void InitEventForDebuggerNotification(DEBUG_EVENT * pDebugEvent,
96 CORDB_ADDRESS pClrBaseAddress,
97 DebuggerIPCEvent * pIPCEvent)
99 pDebugEvent->dwDebugEventCode = EXCEPTION_DEBUG_EVENT;
101 pDebugEvent->u.Exception.dwFirstChance = TRUE;
102 pDebugEvent->u.Exception.ExceptionRecord.ExceptionCode = CLRDBG_NOTIFICATION_EXCEPTION_CODE;
103 pDebugEvent->u.Exception.ExceptionRecord.ExceptionFlags = 0;
104 pDebugEvent->u.Exception.ExceptionRecord.ExceptionRecord = NULL;
105 pDebugEvent->u.Exception.ExceptionRecord.ExceptionAddress = 0;
108 // Format of an ExceptionInformation parameter is:
109 // 0: cookie (CLRDBG_EXCEPTION_DATA_CHECKSUM)
110 // 1: Base address of mscorwks. This identifies the instance of the CLR.
111 // 2: Target Address of DebuggerIPCEvent, which contains the "real" event.
113 pDebugEvent->u.Exception.ExceptionRecord.NumberParameters = 3;
114 pDebugEvent->u.Exception.ExceptionRecord.ExceptionInformation[0] = CLRDBG_EXCEPTION_DATA_CHECKSUM;
115 pDebugEvent->u.Exception.ExceptionRecord.ExceptionInformation[1] = (ULONG_PTR)CORDB_ADDRESS_TO_PTR(pClrBaseAddress);
116 pDebugEvent->u.Exception.ExceptionRecord.ExceptionInformation[2] = (ULONG_PTR)pIPCEvent;
118 _ASSERTE(IsEventDebuggerNotification(&(pDebugEvent->u.Exception.ExceptionRecord), pClrBaseAddress) ==
119 PTR_TO_CORDB_ADDRESS(pIPCEvent));
121 #endif // defined(FEATURE_DBGIPC_TRANSPORT_VM) || defined(FEATURE_DBGIPC_TRANSPORT_DI)
123 //-----------------------------------------------------------------------------
124 // Helper to get the proper decorated name
125 // Caller ensures that pBufSize is large enough. We'll assert just to check,
126 // but no runtime failure.
127 // pBuf - the output buffer to write the decorated name in
128 // cBufSizeInChars - the size of the buffer in characters, including the null.
129 // pPrefx - The undecorated name of the event.
130 //-----------------------------------------------------------------------------
131 void GetPidDecoratedName(__out_z __out_ecount(cBufSizeInChars) WCHAR * pBuf, int cBufSizeInChars, const WCHAR * pPrefix, DWORD pid)
133 const WCHAR szGlobal[] = W("Global\\");
135 szGlobalLen = NumItems(szGlobal) - 1;
137 // Caller should always give us a big enough buffer.
138 _ASSERTE(cBufSizeInChars > (int) wcslen(pPrefix) + szGlobalLen);
140 // PERF: We are no longer calling GetSystemMetrics in an effort to prevent
141 // superfluous DLL loading on startup. Instead, we're prepending
142 // "Global\" to named kernel objects if we are on NT5 or above. The
143 // only bad thing that results from this is that you can't debug
144 // cross-session on NT4. Big bloody deal.
145 wcscpy_s(pBuf, cBufSizeInChars, szGlobal);
147 cBufSizeInChars -= szGlobalLen;
150 ret = _snwprintf_s(pBuf, cBufSizeInChars, _TRUNCATE, pPrefix, pid);
152 // Since this is all determined at compile time, we know we should have enough buffer.
153 _ASSERTE (ret != STRUNCATE);
156 //-----------------------------------------------------------------------------
157 // The 'internal' version of our IL to Native map (the DebuggerILToNativeMap struct)
158 // has an extra field - ICorDebugInfo::SourceTypes source. The 'external/user-visible'
159 // version (COR_DEBUG_IL_TO_NATIVE_MAP) lacks that field, so we need to translate our
160 // internal version to the external version.
161 // "Export" seemed more succinct than "CopyInternalToExternalILToNativeMap" :)
162 //-----------------------------------------------------------------------------
163 void ExportILToNativeMap(ULONG32 cMap, // [in] Min size of mapExt, mapInt
164 COR_DEBUG_IL_TO_NATIVE_MAP mapExt[], // [in] Filled in here
165 struct DebuggerILToNativeMap mapInt[],// [in] Source of info
166 SIZE_T sizeOfCode) // [in] Total size of method (bytes)
169 _ASSERTE(mapExt != NULL);
170 _ASSERTE(mapInt != NULL);
172 for(iMap=0; iMap < cMap; iMap++)
174 mapExt[iMap].ilOffset = mapInt[iMap].ilOffset ;
175 mapExt[iMap].nativeStartOffset = mapInt[iMap].nativeStartOffset ;
176 mapExt[iMap].nativeEndOffset = mapInt[iMap].nativeEndOffset ;
178 // An element that has an end offset of zero, means "till the end of
179 // the method". Pretty this up so that customers don't have to care about
181 if ((DWORD)mapInt[iMap].source & (DWORD)ICorDebugInfo::NATIVE_END_OFFSET_UNKNOWN)
183 mapExt[iMap].nativeEndOffset = (ULONG32)sizeOfCode;
188 // UnsafeGetConfigDWORD
189 SUPPRESS_ALLOCATION_ASSERTS_IN_THIS_SCOPE;
190 static int fReturnSourceTypeForTesting = -1;
191 if (fReturnSourceTypeForTesting == -1)
192 fReturnSourceTypeForTesting = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_ReturnSourceTypeForTesting);
194 if (fReturnSourceTypeForTesting)
196 // Steal the most significant four bits from the native end offset for the source type.
197 _ASSERTE( (mapExt[iMap].nativeEndOffset >> 28) == 0x0 );
198 _ASSERTE( (ULONG32)(mapInt[iMap].source) < 0xF );
199 mapExt[iMap].nativeEndOffset |= ((ULONG32)(mapInt[iMap].source) << 28);