forked from microsoft/vs-threading
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.cpp
More file actions
764 lines (667 loc) · 20.6 KB
/
Copy pathHelpers.cpp
File metadata and controls
764 lines (667 loc) · 20.6 KB
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
#include "stdafx.h"
#include "Helpers.h"
#include "outputcallbacks.h"
bool TryFindSOS(
_In_ IDebugClient* pDebugClient,
_Out_ std::string &strSOS)
{
strSOS.clear();
std::string strOutput;
if (SUCCEEDED(Execute(pDebugClient, ".extmatch /e *\\sos*.dll Threads", strOutput))
&& !strOutput.empty())
{
size_t pos = strOutput.find_first_of("\r\n");
if (pos != std::string::npos)
{
strOutput.erase(pos);
}
size_t dot = strOutput.rfind('.');
if (dot != std::string::npos)
{
strSOS = strOutput.substr(1, dot - 1);
}
}
return !strSOS.empty();
}
bool EnsureLoadSOS(
_In_ IDebugClient* pDebugClient,
_Out_ std::string &strSOS,
_Out_ std::string &strOutput)
{
strSOS.clear();
strOutput.clear();
if (TryFindSOS(pDebugClient, strSOS))
{
return true;
}
if (SUCCEEDED(Execute(pDebugClient, ".loadby sos.dll clr", strOutput)))
{
return TryFindSOS(pDebugClient, strSOS);
}
return false;
}
std::string GetFullCommand(
_In_ const std::string &strSOS,
_In_ const std::string &strCommand)
{
return "!" + strSOS + "." + strCommand;
}
HRESULT Execute(
_In_ IDebugClient* pDebugClient,
_In_ const std::string &strCommand,
_Out_ std::string &strOutput)
{
HRESULT hr = E_FAIL;
strOutput.clear();
CComPtr<IDebugClient> srpNewClient;
if (SUCCEEDED(pDebugClient->CreateClient(&srpNewClient)))
{
COutputCallbacks *pOutputCallbacks = new COutputCallbacks();
if (SUCCEEDED(srpNewClient->SetOutputMask(pOutputCallbacks->SupportedMask()))
&& SUCCEEDED(srpNewClient->SetOutputCallbacks(pOutputCallbacks)))
{
CComQIPtr<IDebugControl> srpNewControl(srpNewClient);
if (srpNewControl)
{
hr = srpNewControl->Execute(DEBUG_OUTCTL_THIS_CLIENT | DEBUG_OUTCTL_NOT_LOGGED, strCommand.c_str(), DEBUG_EXECUTE_NOT_LOGGED | DEBUG_EXECUTE_NO_REPEAT);
if (pOutputCallbacks->BufferError())
{
strOutput = pOutputCallbacks->BufferError();
if (SUCCEEDED(hr))
{
hr = E_FAIL; // Ensure returning failure when there is error message.
}
}
else if (pOutputCallbacks->BufferNormal())
{
strOutput = pOutputCallbacks->BufferNormal();
}
// Print the command outputs if enable verbose mode.
{
CComQIPtr<IDebugControl> srpControl(pDebugClient);
srpControl->Output(DEBUG_OUTPUT_VERBOSE, "Execute command: %s\n%s\n", strCommand.c_str(), strOutput.c_str());
}
}
srpNewClient->SetOutputCallbacks(nullptr);
}
pOutputCallbacks->Release();
}
return hr;
}
void SplitString(
_In_ const std::string &strText,
_In_ const std::string &strDelim,
_Out_ std::vector<std::string> &tokens)
{
tokens.clear();
std::string strCopyText(strText);
char *pszContext = nullptr;
char *pszToken = strtok_s(const_cast<char *>(strCopyText.c_str()), strDelim.c_str(), &pszContext);
while (pszToken != nullptr)
{
tokens.push_back(pszToken);
pszToken = strtok_s(nullptr, strDelim.c_str(), &pszContext);
}
}
static std::regex g_addressPattern("[0-9a-fA-F]{8}");
void ExtractExceptionInfosFromThreadsOutput(
_In_ const std::string &strOutput,
_Out_ std::vector<ExceptionInfo> &exceptionInfos)
{
static std::string strDelim = " ()";
exceptionInfos.clear();
std::istringstream iss(strOutput);
std::string strLine;
std::vector<std::string> tokens;
while (std::getline(iss, strLine))
{
SplitString(strLine, strDelim, tokens);
for (auto i = tokens.size() - 1; i >= 10; --i)
{
if (std::regex_match(tokens[i], g_addressPattern))
{
exceptionInfos.push_back(ExceptionInfo{tokens[0], tokens[i]});
break;
}
}
}
}
bool TryParsePrintExceptionOutput(
_In_ const std::string &strOutput,
_Out_ ExceptionDetail &detail)
{
detail = ExceptionDetail{};
std::istringstream iss(strOutput);
std::string strLine;
// Exception object
{
if (!std::getline(iss, strLine))
{
return false;
}
std::smatch match;
if (!std::regex_search(strLine, match, g_addressPattern))
{
return false;
}
detail.m_strExceptionObject = match.str();
}
// Exception type
{
if (!std::getline(iss, strLine))
{
return false;
}
size_t pos = strLine.find_last_of(" :");
if (pos == std::string::npos)
{
return false;
}
detail.m_strExceptionType = strLine.substr(pos + 1);
}
// Skip Message
if (!std::getline(iss, strLine))
{
return false;
}
// InnerException [optional]
{
if (!std::getline(iss, strLine))
{
return false;
}
std::smatch match;
if (std::regex_search(strLine, match, g_addressPattern))
{
detail.m_strInnerExceptionObject = match.str();
}
}
// StackTrace (generated)
{
if (!std::getline(iss, strLine))
{
return false;
}
// SP IP Function
if (!std::getline(iss, strLine))
{
return false;
}
size_t pos = strLine.rfind("Function");
if (pos == std::string::npos)
{
return false;
}
while (std::getline(iss, strLine) && !strLine.empty())
{
detail.m_stackTraces.push_back(strLine.substr(pos));
}
if (detail.m_stackTraces.empty())
{
return false;
}
}
// Skip StackTraceString
if (!std::getline(iss, strLine))
{
return false;
}
// HResult
{
if (!std::getline(iss, strLine))
{
return false;
}
size_t pos = strLine.find_last_of(" :");
if (pos == std::string::npos)
{
return false;
}
detail.m_strHResult = strLine.substr(pos + 1);
}
detail.m_strRawText = strOutput;
return true;
}
void CollectExceptionDetails(
_In_ IDebugClient* pDebugClient,
_In_ const std::string &strSOS,
_In_ const std::string &strExceptionObject,
_Out_ std::vector<ExceptionDetail> &exceptionDetailsIncludingInnerExceptions)
{
std::string strPrintExceptionCommand = GetFullCommand(strSOS, "PrintException");
std::string strException = strExceptionObject;
while (!strException.empty())
{
std::string strCommand = strPrintExceptionCommand + " " + strException;
strException.clear();
std::string strOutput;
if (SUCCEEDED(Execute(pDebugClient, strCommand, strOutput)))
{
ExceptionDetail detail;
if (TryParsePrintExceptionOutput(strOutput, detail))
{
exceptionDetailsIncludingInnerExceptions.push_back(detail);
strException = detail.m_strInnerExceptionObject;
}
}
}
}
static std::vector<std::string> g_immunizedSymbolsStartWithIgnoreCase =
{
"mscorlib",
"System",
"Microsoft_VisualStudio_Validation",
"Microsoft_VisualStudio_Threading",
"Microsoft_Collections_Immutable",
"Microsoft.VisualStudio.Validation",
"Microsoft.VisualStudio.Threading",
"Microsoft.Collections.Immutable",
"Microsoft.VisualStudio.Shell.VsTaskLibraryHelper",
"Microsoft.VisualStudio.Services.VsTask",
"Microsoft.VisualStudio.ProjectSystem.ThreadHandlingMultithreaded",
"Microsoft.VisualStudio.ProjectSystem.VS.HResult",
"Microsoft.VisualStudio.Project.VisualC.VCProjectEngine.ApartmentMarshaler.Invoke",
};
static std::vector<std::string> g_immunizedSymbolsContain =
{
"ErrorUtilities.",
"ErrorHandler.",
"ThrowOnFailure",
"ThrowIfNotOnUIThread",
"HrInvoke",
};
bool IsImmunized(_In_ const std::string &strFrame)
{
for (const std::string &strImmunizedSymbolStartWithIgnoreCase : g_immunizedSymbolsStartWithIgnoreCase)
{
if (strFrame.length() > strImmunizedSymbolStartWithIgnoreCase.length()
&& 0 == _strnicmp(strFrame.c_str(), strImmunizedSymbolStartWithIgnoreCase.c_str(), strImmunizedSymbolStartWithIgnoreCase.length()))
{
return true;
}
}
for (const std::string &strImmunizedSymbolsContain : g_immunizedSymbolsContain)
{
if (strFrame.find(strImmunizedSymbolsContain) != std::string::npos)
{
return true;
}
}
return false;
}
std::string GetSymbolFromFrame(_In_ const std::string &strFrame)
{
size_t pos = strFrame.find('(');
std::string strSymbol = strFrame.substr(0, pos);
// Normalize NGENed image name.
pos = strSymbol.find("_ni!");
if (pos != std::string::npos)
{
strSymbol.erase(pos, 3);
}
return strSymbol;
}
std::string GuessBlamedSymbol(
_In_ const ExceptionDetail &detail)
{
for (const std::string &strFrame : detail.m_stackTraces)
{
if (!IsImmunized(strFrame))
{
return GetSymbolFromFrame(strFrame);
}
}
// Otherwise, fallback to the last frame.
return GetSymbolFromFrame(detail.m_stackTraces.back());
}
bool IsDueToHangDetected(
_In_ const ExceptionDetail &detail)
{
for (const std::string &stack : detail.m_stackTraces)
{
if (stack.find("OnHangDetected") != std::string::npos)
{
return true;
}
}
return false;
}
static std::regex g_framePattern("^[0-9a-f]{8} [0-9a-f]{8} ([a-z].+)", std::regex_constants::icase);
bool ParseClrStackFrame(
_In_ const std::string &strFrame,
_Out_ std::string &strFunc)
{
std::smatch matches;
if (std::regex_match(strFrame, matches, g_framePattern))
{
strFunc = matches[1].str();
return true;
}
return false;
}
bool ParseKStackFrame(
_In_ const std::string &strFrame,
_Out_ std::string &strFunc)
{
static std::regex s_funcPattern("[0-9a-fA-F-]{8} [0-9a-fA-F-]{8} (.+!.+\\+0x[0-9a-fA-F]+)");
std::smatch match;
if (std::regex_search(strFrame, match, s_funcPattern))
{
strFunc = match[1].str();
return true;
}
return false;
}
/* !threadpool
CPU utilization: 3%
Worker Thread: Total: 129 Running: 110 Idle: 0 MaxLimit: 2047 MinLimit: 12
Work Request in Queue: 0
--------------------------------------
Number of Timers: 1
--------------------------------------
Completion Port Thread:Total: 4 Free: 4 MaxFree: 24 CurrentLimit: 4 MaxLimit: 1000 MinLimit: 12
*/
bool GetThreadPoolStatus(
_In_ const std::string &strThreadPoolOutput,
_Out_ ThreadPoolStatus &status)
{
static std::regex s_workerThreadPattern("^Worker Thread: Total: (\\d+) Running: (\\d+)", std::regex::icase);
std::stringstream ss(strThreadPoolOutput);
std::string strLine;
while (std::getline(ss, strLine))
{
std::smatch match;
if (std::regex_search(strLine, match, s_workerThreadPattern))
{
status.m_iRunningWorkers = std::atoi(match[2].str().c_str());
return true;
}
}
return false;
}
int GetNumberOfThreads(
_In_ PDEBUG_CLIENT pDebugClient)
{
static std::regex s_threadIdPattern("^\\s*(\\d+)\\s*Id:", std::regex::icase);
std::string strOutput;
if (SUCCEEDED(Execute(pDebugClient, "~*", strOutput)))
{
std::stringstream ss(strOutput);
std::string strLine;
int threadId = -1;
while (std::getline(ss, strLine))
{
std::smatch match;
if (std::regex_search(strLine, match, s_threadIdPattern))
{
threadId = std::atoi(match[1].str().c_str());
}
}
return threadId + 1;
}
return 0;
}
struct FieldHeaderInfo
{
std::string m_strName;
size_t m_iStart;
size_t m_iLength;
};
bool ParseFieldHeaders(
_In_ const std::string &strHeaders,
_Out_ std::vector<FieldHeaderInfo> &headers)
{
static std::regex s_fieldHeaderPattern("^\\s+(\\S+)");
headers.clear();
std::smatch match;
std::string::const_iterator begin = strHeaders.begin();
while (std::regex_search(begin, strHeaders.end(), match, s_fieldHeaderPattern))
{
FieldHeaderInfo header{};
header.m_strName.assign(match[1].first, match[1].second);
header.m_iStart = begin - strHeaders.begin();
header.m_iLength = match.length();
headers.push_back(std::move(header));
begin += match.length();
}
if (!headers.empty())
{
headers.back().m_iLength = std::string::npos;
}
return headers.size() == 8;
}
void Trim(_Inout_ std::string &strText)
{
size_t start = strText.find_first_not_of(' ');
if (start == std::string::npos)
{
strText.clear();
return;
}
size_t end = strText.find_last_not_of(' ');
if (end - start + 1 < strText.length())
{
strText.erase(end + 1);
strText.erase(0, start);
}
}
void SetFieldValue(
_Inout_ FieldInfo &field,
_In_ const std::string &strName,
_Inout_ std::string &&strValue)
{
Trim(strValue);
if (strName == "MT")
{
field.m_strMethodTable = strValue;
}
else if (strName == "Offset")
{
field.m_iOffset = atoi(strValue.c_str());
}
else if (strName == "VT")
{
field.m_fIsValueType = atoi(strValue.c_str()) != 0;
}
else if (strName == "Value")
{
field.m_strValue = strValue;
}
else if (strName == "Name")
{
field.m_strName = strValue;
}
}
/* !do 032f2398
Name: Microsoft.VisualStudio.Services.Settings.SettingsPackage+<ShellInitPlusFiveSeconds>d__1
MethodTable: 6f6a03fc
EEClass: 6f58da24
Size: 32(0x20) bytes
File: C:\windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.VisualStudio.Shell.UI.Internal\v4.0_14.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Shell.UI.Internal.dll
Fields:
MT Field Offset Type VT Attr Value Name
6cc19158 4001249 8 System.Int32 1 instance 1 <>1__state
6cc223fc 400124a c ...TaskMethodBuilder 1 instance 032f23a4 <>t__builder
6cbe4fd0 400124b 18 ...olean, mscorlib]] 1 instance 032f23b0 <>u__1
6cc17670 400124c 4 System.Object 0 instance 0a4862f4 <>u__2
*/
bool ParseDumpObjectOutput(
_In_ const std::string &strDumpObject,
_Out_ ObjectInfo &objectInfo)
{
objectInfo.m_strType.clear();
objectInfo.m_fields.clear();
std::stringstream ss(strDumpObject);
std::string strLine;
bool fFoundFields = false;
while (std::getline(ss, strLine))
{
size_t sep = strLine.find(':');
if (sep != std::string::npos)
{
if (strncmp(strLine.c_str(), "Name", sep) == 0)
{
size_t start = strLine.find_first_not_of(' ', sep + 1);
if (start != std::string::npos)
{
objectInfo.m_strType.assign(strLine, start);
}
}
if (strncmp(strLine.c_str(), "String", sep) == 0)
{
size_t start = strLine.find_first_not_of(' ', sep + 1);
if (start != std::string::npos)
{
objectInfo.m_string.assign(strLine, start);
}
}
else if (strncmp(strLine.c_str(), "Fields", sep) == 0)
{
fFoundFields = true;
break;
}
}
}
if (fFoundFields && !objectInfo.m_strType.empty())
{
if (std::getline(ss, strLine))
{
std::vector<FieldHeaderInfo> headers;
if (ParseFieldHeaders(strLine, headers))
{
while (std::getline(ss, strLine))
{
if (strLine.empty() || isspace(strLine[0]) || (int)strLine.length() <= headers.back().m_iStart )
{
continue;
}
int start = 0;
int end = 0;
int fieldIndex = 0;
FieldInfo fieldInfo{};
for (const FieldHeaderInfo &headerInfo : headers)
{
// SOS doesn't align all fields with head correctly, causing the code reads wrong values and names.
if (fieldIndex < 6)
{
std::string strValue(strLine, headerInfo.m_iStart, headerInfo.m_iLength);
SetFieldValue(fieldInfo, headerInfo.m_strName, std::move(strValue));
start = headerInfo.m_iStart + headerInfo.m_iLength;
}
else
{
while (start < (int)strLine.length() && isspace(strLine[start]))
{
start++;
}
end = start;
while (end < (int)strLine.length() && !isspace(strLine[end]))
{
end++;
}
std::string strValue(strLine, start, end - start);
SetFieldValue(fieldInfo, headerInfo.m_strName, std::move(strValue));
start = end;
}
fieldIndex++;
}
objectInfo.m_fields.push_back(std::move(fieldInfo));
}
}
}
}
return !objectInfo.m_strType.empty() && !objectInfo.m_fields.empty();
}
/* !name2ee mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner
Module: 6c7f1000
Assembly: mscorlib.dll
Token: 020008be
MethodTable: 6cbe4dbc
EEClass: 6c855780
Name: System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner
*/
bool ParseName2EEOutput(
_In_ const std::string &strName2EE,
_Out_ std::string &strMethodTable)
{
std::stringstream ss(strName2EE);
std::string strLine;
while (std::getline(ss, strLine))
{
size_t sep = strLine.find(':');
if (sep != std::string::npos)
{
size_t start = strLine.find_first_not_of(' ', sep + 1);
if (start != std::string::npos)
{
if (strncmp(strLine.c_str(), "MethodTable", sep) == 0)
{
strMethodTable.assign(strLine, start);
return true;
}
}
}
}
return false;
}
bool GetFieldInfo(
_In_ const ObjectInfo &objectInfo,
_In_ const std::string &strFieldName,
_Out_ FieldInfo &fieldInfo)
{
for (const FieldInfo &fi: objectInfo.m_fields)
{
if (fi.m_strName == strFieldName)
{
fieldInfo = fi;
return true;
}
}
return false;
}
bool FindFieldAndGetObjectInfo(
_In_ PDEBUG_CLIENT pDebugClient,
_In_ const std::string &strSOS,
_In_ const ObjectInfo &objectInfo,
_In_ const std::string &strFieldName,
_Out_ ObjectInfo &fieldObjectInfo)
{
FieldInfo fieldInfo{};
if (!GetFieldInfo(objectInfo, strFieldName, fieldInfo) || fieldInfo.m_strValue == "00000000")
{
return false;
}
std::string strCommand;
if (fieldInfo.m_fIsValueType)
{
if (fieldInfo.m_strMethodTable == "00000000")
{
return false;
}
strCommand.append("dumpvc ")
.append(fieldInfo.m_strMethodTable)
.append(" ")
.append(fieldInfo.m_strValue);
}
else
{
strCommand.append("do ")
.append(fieldInfo.m_strValue);
}
std::string strOutput;
HRESULT hr = Execute(pDebugClient, GetFullCommand(strSOS, strCommand), strOutput);
if (FAILED(hr))
{
CComQIPtr<IDebugControl> srpControl(pDebugClient);
srpControl->Output(DEBUG_OUTPUT_ERROR, "Failed to run !%s: %s\n", strCommand.c_str(), strOutput.c_str());
return false;
}
if (!ParseDumpObjectOutput(strOutput, fieldObjectInfo))
{
CComQIPtr<IDebugControl> srpControl(pDebugClient);
srpControl->Output(DEBUG_OUTPUT_ERROR, "Failed to parse the output of !%s: %s\n", strCommand.c_str(), strOutput.c_str());
return false;
}
fieldObjectInfo.m_strAddress = fieldInfo.m_strValue;
return true;
}