-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathDebuggedThread.cs
More file actions
548 lines (499 loc) · 19.5 KB
/
DebuggedThread.cs
File metadata and controls
548 lines (499 loc) · 19.5 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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using MICore;
using Microsoft.DebugEngineHost;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace Microsoft.MIDebugEngine
{
public class DebuggedThread
{
public DebuggedThread(int id, MIDebugEngine.AD7Engine engine)
{
Id = id;
Name = "";
TargetId = (uint)id;
AD7Thread ad7Thread = new MIDebugEngine.AD7Thread(engine, this);
Client = ad7Thread;
ChildThread = false;
}
public int Id { get; private set; }
public uint TargetId { get; set; }
public Object Client { get; private set; } // really AD7Thread
public bool Alive { get; set; }
public bool Default { get; set; }
public string Name { get; set; }
public bool ChildThread { get; set; } // transient child thread, don't inform UI of this thread
}
internal class ThreadCache
{
private List<DebuggedThread> _threadList;
private Dictionary<int, List<ThreadContext>> _stackFrames;
private Dictionary<int, ThreadContext> _topContext; // can retrieve the top frame without walking the stack
private bool _stateChange; // indicates that a thread has been created/destroyed since last thread-info
private bool _full; // indicates whether the cache has already been filled via -thread-info
private ISampleEngineCallback _callback;
private DebuggedProcess _debugger;
private List<DebuggedThread> _deadThreads;
private List<DebuggedThread> _newThreads;
private Dictionary<string, List<int>> _threadGroups;
private static uint s_targetId = 0; // Thread ids should be start at 0 in the default scenario
private const string c_defaultGroupId = "i1"; // gdb's default group id, also used for any process without group ids
private List<DebuggedThread> DeadThreads
{
get
{
if (_deadThreads == null)
{
_deadThreads = new List<DebuggedThread>();
}
return _deadThreads;
}
}
private List<DebuggedThread> NewThreads
{
get
{
if (_newThreads == null)
{
_newThreads = new List<DebuggedThread>();
}
return _newThreads;
}
}
internal ThreadCache(ISampleEngineCallback callback, DebuggedProcess debugger)
{
_threadList = new List<DebuggedThread>();
_stackFrames = new Dictionary<int, List<ThreadContext>>();
_topContext = new Dictionary<int, ThreadContext>();
_threadGroups = new Dictionary<string, List<int>>();
_threadGroups[c_defaultGroupId] = new List<int>(); // initialize the processes thread group
_stateChange = true;
_callback = callback;
_debugger = debugger;
_full = false;
debugger.RunModeEvent += SendThreadEvents;
}
internal async Task<DebuggedThread[]> GetThreads()
{
bool stateChange = false;
lock (_threadList)
{
stateChange = _stateChange;
}
if (stateChange)
{
await CollectThreadsInfo(0);
}
lock (_threadList)
{
return _threadList.ToArray();
}
}
internal async Task<DebuggedThread> GetThread(int id)
{
DebuggedThread[] threads = await GetThreads();
foreach (var t in threads)
{
if (t.Id == id)
{
return t;
}
}
return null;
}
internal async Task<List<ThreadContext>> StackFrames(DebuggedThread thread)
{
lock (_threadList)
{
if (!_threadList.Contains(thread))
{
return null; // thread must be dead
}
if (_stackFrames.ContainsKey(thread.Id))
{
return _stackFrames[thread.Id];
}
}
List<ThreadContext> stack = null;
try
{
stack = await WalkStack(thread);
}
catch (UnexpectedMIResultException)
{
_debugger.Logger.WriteLine(LogLevel.Error, "Stack walk failed on thread: " + thread.TargetId);
_stateChange = true; // thread may have been deleted. Force a resync
}
lock (_threadList)
{
_stackFrames[thread.Id] = stack;
_topContext[thread.Id] = (stack != null && stack.Count > 0) ? stack[0] : null;
return _stackFrames[thread.Id];
}
}
internal async Task<ThreadContext> GetThreadContext(DebuggedThread thread)
{
lock (_threadList)
{
if (_topContext.ContainsKey(thread.Id))
{
return _topContext[thread.Id];
}
if (_full)
{
return null; // no context available for this thread
}
}
return await CollectThreadsInfo(thread.Id);
}
internal void MarkDirty()
{
lock (_threadList)
{
_topContext.Clear();
_stackFrames.Clear();
_full = false;
}
}
internal async Task ThreadCreatedEvent(int id, string groupId)
{
// Mark that the threads have changed
lock (_threadList)
{
{
var thread = _threadList.Find(t => t.Id == id);
if (thread == null)
{
_stateChange = true;
}
}
// This must go before getting the thread-info for the thread since that method call is async.
// The threadId must be added to the thread-group before the new thread is created or else it will
// be marked as a child thread and then thread-created and thread-exited won't be sent to the UI
if (string.IsNullOrEmpty(groupId))
{
groupId = c_defaultGroupId;
}
if (!_threadGroups.ContainsKey(groupId))
{
_threadGroups[groupId] = new List<int>();
}
_threadGroups[groupId].Add(id);
}
// Run Thread-info now to get the target-id
ResultValue resVal = null;
if (id >= 0)
{
uint? tid = null;
tid = (uint)id;
Results results = await _debugger.MICommandFactory.ThreadInfo(tid);
if (results.ResultClass != ResultClass.done)
{
// This can happen on some versions of gdb where thread-info is not supported while running, so only assert if we're also not running.
if (this._debugger.ProcessState != ProcessState.Running)
{
Debug.Fail("Thread info not successful");
}
}
else
{
var tlist = results.Find<ValueListValue>("threads");
// tlist.Content.Length could be 0 when the thread exits between it getting created and we request thread-info
Debug.Assert(tlist.Content.Length <= 1, "Expected at most 1 thread, received more than one thread.");
resVal = tlist.Content.FirstOrDefault(item => item.FindInt("id") == id);
}
}
if (resVal != null)
{
lock (_threadList)
{
bool bNew = false;
var thread = SetThreadInfoFromResultValue(resVal, out bNew);
Debug.Assert(thread.Id == id, "thread.Id and id should match");
if (bNew)
{
NewThreads.Add(thread);
SendThreadEvents(null, null);
}
}
}
}
internal void ThreadExitedEvent(int id)
{
DebuggedThread thread = null;
lock (_threadList)
{
thread = _threadList.Find(t => t.Id == id);
if (thread != null)
{
DeadThreads.Add(thread);
_threadList.Remove(thread);
_stateChange = true;
}
foreach (var g in _threadGroups)
{
if (g.Value.Contains(id))
{
g.Value.Remove(id);
break;
}
}
}
if (thread != null)
{
SendThreadEvents(null, null);
}
}
internal void ThreadGroupExitedEvent(string groupId)
{
lock (_threadList)
{
_threadGroups.Remove(groupId);
}
}
private bool IsInParent(int tid)
{
// only those threads in the s_defaultGroupId threadgroup are in the debuggee, others are transient while attaching to a child process
return _threadGroups.ContainsKey(c_defaultGroupId) && _threadGroups[c_defaultGroupId].Contains(tid);
}
private async Task<List<ThreadContext>> WalkStack(DebuggedThread thread)
{
List<ThreadContext> stack = null;
TupleValue[] frameinfo = await _debugger.MICommandFactory.StackListFrames(thread.Id, 0, 1000);
if (frameinfo == null)
{
_debugger.Logger.WriteLine(LogLevel.Error, "Failed to get frame info");
}
else
{
stack = new List<ThreadContext>();
foreach (var frame in frameinfo)
{
stack.Add(CreateContext(frame));
}
}
return stack;
}
private ThreadContext CreateContext(TupleValue frame)
{
ulong? pc = frame.TryFindAddr("addr");
// don't report source line info for modules marked as IgnoreSource
bool ignoreSource = false;
if (pc != null)
{
var module = _debugger.FindModule(pc.Value);
if (module != null && module.IgnoreSource)
{
ignoreSource = true;
}
}
MITextPosition textPosition = !ignoreSource ? MITextPosition.TryParse(this._debugger, frame) : null;
string func = frame.TryFindString("func");
uint level = frame.FindUint("level");
string from = frame.TryFindString("from");
return new ThreadContext(pc, textPosition, func, level, from);
}
private bool TryGetTidFromTargetId(string targetId, out uint tid)
{
tid = 0;
if (System.UInt32.TryParse(targetId, out tid) && tid != 0)
{
return true;
}
else if (targetId.StartsWith("Thread ", StringComparison.OrdinalIgnoreCase) &&
System.UInt32.TryParse(targetId.Substring("Thread ".Length), out tid) &&
tid != 0
)
{
return true;
}
else if (targetId.StartsWith("Process ", StringComparison.OrdinalIgnoreCase) &&
System.UInt32.TryParse(targetId.Substring("Process ".Length), out tid) &&
tid != 0
)
{ // First thread in a linux process has tid == pid
return true;
}
else if (targetId.StartsWith("Thread ", StringComparison.OrdinalIgnoreCase))
{
// In processes with pthreads the thread name is in form: "Thread <0x123456789abc> (LWP <thread-id>)"
int lwp_pos = targetId.IndexOf("(LWP ", StringComparison.Ordinal);
int paren_pos = targetId.LastIndexOf(')');
int len = paren_pos - (lwp_pos + 5);
if (len > 0 && System.UInt32.TryParse(targetId.Substring(lwp_pos + 5, len), out tid) && tid != 0)
{
return true;
}
}
else if (targetId.StartsWith("LWP ", StringComparison.OrdinalIgnoreCase) &&
System.UInt32.TryParse(targetId.Substring("LWP ".Length), out tid) &&
tid != 0
)
{
// In gdb coredumps the thread name is in the form:" LWP <thread-id>"
return true;
}
else if (targetId.StartsWith("pid ", StringComparison.OrdinalIgnoreCase))
{
// In QNX gdb coredumps the thread name is in the form:"pid <pid> tid <pid>", eg "pid 4194373 tid 308"
int tid_pos = targetId.IndexOf("tid ", StringComparison.Ordinal);
int len = targetId.Length - (tid_pos + 4);
if (len > 0 && System.UInt32.TryParse(targetId.Substring(tid_pos + 4, len), out tid) && tid != 0)
{
return true;
}
}
else
{
tid = ++s_targetId;
return true;
}
return false;
}
private DebuggedThread SetThreadInfoFromResultValue(ResultValue resVal, out bool isNewThread)
{
isNewThread = false;
int threadId = resVal.FindInt("id");
string targetId = resVal.TryFindString("target-id");
DebuggedThread thread = FindThread(threadId, out isNewThread);
thread.Alive = true;
// Only update targetId if it is a new thread.
if (isNewThread && !String.IsNullOrEmpty(targetId))
{
uint tid = 0;
if (TryGetTidFromTargetId(targetId, out tid))
{
thread.TargetId = tid;
}
}
if (resVal.Contains("name"))
{
thread.Name = resVal.FindString("name");
}
return thread;
}
private async Task<ThreadContext> CollectThreadsInfo(int cxtThreadId)
{
ThreadContext ret = null;
// set of threads has changed or thread locations have been asked for
Results threadsinfo = await _debugger.MICommandFactory.ThreadInfo();
if (threadsinfo.ResultClass != ResultClass.done)
{
Debug.Fail("Failed to get thread info");
}
else
{
var tlist = threadsinfo.Find<ValueListValue>("threads");
// update our thread list
lock (_threadList)
{
foreach (var thread in _threadList)
{
thread.Alive = false;
}
foreach (var t in tlist.Content)
{
bool bNew = false;
var thread = SetThreadInfoFromResultValue(t, out bNew);
int threadId = thread.Id;
if (bNew)
{
NewThreads.Add(thread);
}
TupleValue[] frames = ((TupleValue)t).FindAll<TupleValue>("frame");
if (frames.Any())
{
List<ThreadContext> stack = new List<ThreadContext>();
stack.AddRange(frames.Select(frame => CreateContext(frame)));
_topContext[threadId] = stack[0];
if (threadId == cxtThreadId)
{
ret = _topContext[threadId];
}
if (stack.Count > 1)
{
_stackFrames[threadId] = stack;
}
}
}
foreach (var thread in _threadList.ToList())
{
if (!thread.Alive)
{
DeadThreads.Add(thread);
_threadList.Remove(thread);
}
}
_stateChange = false;
_full = true;
}
}
return ret;
}
internal void SendThreadEvents(object sender, EventArgs e)
{
if (_debugger.Engine.ProgramCreateEventSent)
{
List<DebuggedThread> deadThreads;
List<DebuggedThread> newThreads;
lock (_threadList)
{
deadThreads = _deadThreads;
_deadThreads = null;
newThreads = _newThreads;
_newThreads = null;
}
if (newThreads != null)
{
if (newThreads.Count == _threadList.Count)
{
// These are the first threads. Send a processInfoUpdateEvent too.
AD7ProcessInfoUpdatedEvent.Send(_debugger.Engine, _debugger.LaunchOptions.ExePath, (uint)_debugger.PidByInferior("i1"));
}
foreach (var newt in newThreads)
{
// If we are child process debugging, check and see if its a child thread
if (!(_debugger.IsChildProcessDebugging && newt.ChildThread))
{
_callback.OnThreadStart(newt);
}
}
}
if (deadThreads != null)
{
foreach (var dead in deadThreads)
{
// If we are child process debugging, check and see if its a child thread
if (!(_debugger.IsChildProcessDebugging && dead.ChildThread))
{
// Send the destroy event outside the lock
_callback.OnThreadExit(dead, 0);
}
}
}
}
}
private DebuggedThread FindThread(int id, out bool bNew)
{
DebuggedThread newthread;
bNew = false;
var thread = _threadList.Find(t => t.Id == id);
if (thread != null)
return thread;
// thread not found, so create it, and return it
newthread = new DebuggedThread(id, _debugger.Engine);
if (!IsInParent(id))
{
newthread.ChildThread = true;
}
newthread.Default = false;
_threadList.Add(newthread);
bNew = true;
return newthread;
}
}
}