-
Notifications
You must be signed in to change notification settings - Fork 2
/
DumpMemory.cs
627 lines (566 loc) · 20.5 KB
/
DumpMemory.cs
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
using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Debugger.Interop;
using Microsoft.VisualStudio.Shell.Interop;
using MsgBox = System.Windows.MessageBox;
namespace YifanLu.DumpMemory
{
class DumpMemory : INotifyPropertyChanged
{
/// <summary>
/// Simple ICommand implementation that just executes the command
/// </summary>
private class SimpleCommand : ICommand
{
/// <summary>
/// Action to execute
/// </summary>
private Action<object> _action;
/// <summary>
/// Create a SimpleCommand
/// </summary>
public SimpleCommand(Action<object> action)
{
_action = action;
}
/// <summary>
/// Not implemented
/// </summary>
public bool CanExecute(object parameter)
{
return true;
}
#pragma warning disable 67
/// <summary>
/// Not used
/// </summary>
public event EventHandler CanExecuteChanged;
#pragma warning restore 67
/// <summary>
/// Execute the action
/// </summary>
public void Execute(object parameter)
{
_action.Invoke(parameter);
}
}
#region Static members
/// <summary>
/// We need access to private field from VS to get current program debug context
/// </summary>
private static MethodInfo internalProgramMethod_;
/// <summary>
/// We need access to private field from VS to get current program stack frame
/// </summary>
private static MethodInfo internalStackFrameMethod_;
/// <summary>
/// Size of chunk to dump/write
/// </summary>
private const int BUFFER_SIZE = 1024;
#endregion
#region Events
/// <summary>
/// Multicast event for property change notifications.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Model
// Description in properties below
private string _path;
private string _addr_expr;
private uint _len;
private bool _isloading;
private bool _isdebugging;
private bool _isbusy;
private float _progress;
#endregion
#region Properties
/// <summary>
/// We need access to a private non-SDK exposed assembly to get debug context.
/// </summary>
private static Assembly InternalDebugAssembly
{
get
{
// Support VS 2010, 2011, 2012
foreach (string str in "10;11;12".Split(new char[] { ';' }))
{
try
{
return Assembly.Load(string.Format("Microsoft.VisualStudio.Debugger.Interop.Internal, Version={0}.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL", str));
}
catch (Exception)
{
}
}
return null;
}
}
/// <summary>
/// Action for when Browse button is clicked
/// </summary>
public ICommand BrowsePath
{
get
{
return new SimpleCommand((o) =>
{
FileDialog file;
if (IsLoading)
{
file = new OpenFileDialog();
file.Title = Resources.InputBrowseTitle;
}
else
{
file = new SaveFileDialog();
file.Title = Resources.OutputBrowseTitle;
}
if (file.ShowDialog() == DialogResult.OK)
{
Path = file.FileName;
}
});
}
}
/// <summary>
/// Action for when "Go" is clicked
/// </summary>
public ICommand Execute
{
get
{
return new SimpleCommand((o) =>
{
// Save program and frame because VS does not like these to be accessed in another thread
IDebugProgram2 program = (internalProgramMethod_.Invoke(VsDebugger, null) as IDebugProgram2);
IDebugStackFrame2 frame = (internalStackFrameMethod_.Invoke(VsDebugger, null) as IDebugStackFrame2);
// Run in another thread so we don't freeze the whole UI when dumping/writing
(new System.Threading.Thread(() =>
{
if (IsLoading)
{
DoLoadMemory(program, frame);
}
else
{
DoDumpMemory(program, frame);
}
})).Start();
});
}
}
/// <summary>
/// Action for when Load/Dump radio button is clicked
/// </summary>
public ICommand ResetOptions
{
get
{
return new SimpleCommand((o) =>
{
Path = string.Empty;
Length = "0";
});
}
}
/// <summary>
/// A file path to read from or write to
/// </summary>
public string Path
{
get
{
return _path;
}
set
{
if (value != _path)
{
_path = value;
OnPropertyChanged("Path");
}
}
}
/// <summary>
/// Address in debugee to dump from/write to. Can be any valid VS debug expression casted to void*.
/// </summary>
public string Address
{
get
{
return _addr_expr;
}
set
{
if (_addr_expr != value)
{
_addr_expr = value;
OnPropertyChanged("Address");
}
}
}
/// <summary>
/// Length to dump. Will be parsed in decimal unless it starts with "0x".
/// </summary>
public string Length
{
get
{
return string.Format("0x{0:X}", _len);
}
set
{
uint len;
if (value.StartsWith("0x"))
{
if (!UInt32.TryParse(value.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out len))
{
ShowError(Resources.InvalidLengthErrorText);
return;
}
}
else
{
if (!UInt32.TryParse(value, out len))
{
ShowError(Resources.InvalidLengthErrorText);
return;
}
}
if (_len != len)
{
_len = len;
OnPropertyChanged("Length");
}
}
}
/// <summary>
/// True if we want to load memory from file
/// </summary>
public bool IsLoading
{
get
{
return _isloading;
}
set
{
if (_isloading != value)
{
_isloading = value;
OnPropertyChanged("IsLoading");
OnPropertyChanged("IsDumping");
}
}
}
/// <summary>
/// True if we want to dump memory to file
/// </summary>
public bool IsDumping
{
get
{
return !IsLoading;
}
}
/// <summary>
/// True if control should be enabled
/// </summary>
public bool IsEnabled
{
get
{
return IsDebugging && !IsBusy;
}
}
/// <summary>
/// True if we are paused in execution debug state
/// </summary>
public bool IsDebugging
{
get
{
return _isdebugging;
}
set
{
if (_isdebugging != value)
{
_isdebugging = value;
OnPropertyChanged("IsEnabled");
}
}
}
/// <summary>
/// True if we are currently running a dump or write
/// </summary>
public bool IsBusy
{
get
{
return _isbusy;
}
set
{
if (_isbusy != value)
{
_isbusy = value;
OnPropertyChanged("IsEnabled");
OnPropertyChanged("IsBusy");
}
}
}
/// <summary>
/// Progress of current action
/// </summary>
public float Progress
{
get
{
return _progress;
}
set
{
if (_progress != value)
{
_progress = value;
OnPropertyChanged("Progress");
}
}
}
/// <summary>
/// VS debugger context. Assumed not null.
/// </summary>
public IVsDebugger VsDebugger;
#endregion
#region Constructors
/// <summary>
/// Initialize the methods for accessing VS private fields
/// </summary>
public DumpMemory()
{
try
{
internalProgramMethod_ = InternalDebugAssembly.GetType("Microsoft.VisualStudio.Debugger.Interop.Internal.IDebuggerInternal").GetProperty("CurrentProgram").GetGetMethod();
if ((internalProgramMethod_ == null) || !typeof(IDebugProgram2).IsAssignableFrom(internalProgramMethod_.ReturnType))
{
throw new ApplicationException();
}
internalStackFrameMethod_ = InternalDebugAssembly.GetType("Microsoft.VisualStudio.Debugger.Interop.Internal.IDebuggerInternal").GetProperty("CurrentStackFrame").GetGetMethod();
if ((internalStackFrameMethod_ == null) || !typeof(IDebugStackFrame2).IsAssignableFrom(internalStackFrameMethod_.ReturnType))
{
throw new ApplicationException();
}
}
catch (Exception)
{
ShowError(Resources.VSInternalMethodsNotFoundErrorText);
}
}
#endregion
#region INotifyPropertyChanged Members
/// <summary>
/// Checks if a property already matches a desired value. Sets the property and
/// notifies listeners only when necessary.
/// </summary>
/// <typeparam name="T">Type of the property.</typeparam>
/// <param name="storage">Reference to a property with both getter and setter.</param>
/// <param name="value">Desired value for the property.</param>
/// <param name="propertyName">Name of the property used to notify listeners. This
/// value is optional and can be provided automatically when invoked from compilers that
/// support CallerMemberName.</param>
/// <returns>True if the value was changed, false if the existing value matched the
/// desired value.</returns>
protected bool SetProperty<T>(ref T storage, T value, String propertyName = null)
{
if (object.Equals(storage, value)) return false;
storage = value;
this.OnPropertyChanged(propertyName);
return true;
}
/// <summary>
/// Notifies listeners that a property value has changed.
/// </summary>
/// <param name="propertyName">Name of the property used to notify listeners. This
/// value is optional and can be provided automatically when invoked from compilers
/// that support <see cref="CallerMemberNameAttribute"/>.</param>
protected void OnPropertyChanged(string propertyName = null)
{
var eventHandler = this.PropertyChanged;
if (eventHandler != null)
{
eventHandler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#region Methods
/// <summary>
/// Displays an error dialog
/// </summary>
/// <param name="msg">Text to show</param>
private void ShowError(string msg)
{
MsgBox.Show(msg, Resources.ErrorDialogTitle, MessageBoxButton.OK, MessageBoxImage.Error);
}
/// <summary>
/// Reads from or writes to the debug target at the evaluated expression.
/// </summary>
/// <param name="program">VS internal program</param>
/// <param name="frame">VS internal frame</param>
/// <param name="buffer">Buffer to read/write</param>
/// <param name="length">Length to read/write</param>
/// <param name="read">True to perform read, false to perform write</param>
/// <param name="expr">VS debugger expression. Will be casted to void*.</param>
/// <returns>True on success, false on failure</returns>
private bool MemoryAction(IDebugProgram2 program, IDebugStackFrame2 frame, ref byte[] buffer, uint length, bool read, string expr)
{
if (!IsDebugging)
{
ShowError(Resources.NotInDebugContextErrorText);
return false;
}
// Get the execution context (current frame) to evaluate the context
IDebugExpressionContext2 expressionContext;
if (frame.GetExpressionContext(out expressionContext) != VSConstants.S_OK || expressionContext == null)
{
ShowError(Resources.CannotGetExprContextErrorText);
return false;
}
// Try to parse the expression
IDebugExpression2 expression;
string error;
uint errorCharIndex;
if (expressionContext.ParseText(string.Format("(void *)({0})", expr), enum_PARSEFLAGS.PARSE_EXPRESSION, 10, out expression, out error, out errorCharIndex) != VSConstants.S_OK || errorCharIndex > 0)
{
ShowError(string.Format(Resources.ParseExpressionErrorText, expr, error));
return false;
}
// Try to evaluate the expression
IDebugProperty2 debugProperty;
if (expression.EvaluateSync(enum_EVALFLAGS.EVAL_NOSIDEEFFECTS, uint.MaxValue, null, out debugProperty) != VSConstants.S_OK || debugProperty == null)
{
ShowError(Resources.ExpressionEvalErrorText);
return false;
}
// Get memory context of our evaluated expression
IDebugMemoryContext2 memoryContext;
if (debugProperty.GetMemoryContext(out memoryContext) != VSConstants.S_OK || memoryContext == null)
{
ShowError(Resources.CannotGetFrameErrorText);
return false;
}
// Get memory accessor
// For some reason debugProperty.GetMemoryBytes does not work so we have to do it this way
IDebugMemoryBytes2 memoryBytes;
if (program.GetMemoryBytes(out memoryBytes) != VSConstants.S_OK || memoryBytes == null)
{
ShowError(Resources.CannotGetMemoryErrorText);
return false;
}
// Using the memory accessor and context, read or write to debugee memory
if (read)
{
uint numread = 0;
uint unreadable = 0;
memoryBytes.ReadAt(memoryContext, length, buffer, out numread, ref unreadable);
if (numread != length || unreadable > 0)
{
ShowError(string.Format(Resources.ChunkUnreadableErrorText, unreadable, length));
return false;
}
}
else
{
memoryBytes.WriteAt(memoryContext, length, buffer);
}
return true;
}
/// <summary>
/// Dump memory with options from window
/// </summary>
/// <param name="program">VS internal program</param>
/// <param name="frame">VS internal frame</param>
private void DoDumpMemory(IDebugProgram2 program, IDebugStackFrame2 frame)
{
IsBusy = true;
Progress = 0;
uint left = _len;
try
{
using (FileStream file = File.OpenWrite(Path))
{
byte[] buffer = new byte[BUFFER_SIZE];
while (left > 0)
{
uint read = left < BUFFER_SIZE ? left : BUFFER_SIZE;
if (!MemoryAction(program, frame, ref buffer, read, true, string.Format("{0}+{1}", _addr_expr, _len - left)))
{
break;
}
file.Write(buffer, 0, (int)read);
left -= read;
Progress = (float)(_len - left) / _len * 100;
}
if (left == 0)
{
MsgBox.Show(Resources.DumpSuccessText, Resources.SuccessDialogTitle, MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
catch (Exception ex)
{
ShowError(string.Format(Resources.DumpErrorText, ex.Message, left));
}
IsBusy = false;
Progress = 0;
}
/// <summary>
/// Load memory with options from window
/// </summary>
/// <param name="program">VS internal program</param>
/// <param name="frame">VS internal frame</param>
private void DoLoadMemory(IDebugProgram2 program, IDebugStackFrame2 frame)
{
IsBusy = true;
Progress = 0;
uint pos = 0;
try
{
using (FileStream file = File.OpenRead(Path))
{
byte[] buffer = new byte[BUFFER_SIZE];
long len = file.Length;
uint read;
while ((read = (uint)file.Read(buffer, 0, BUFFER_SIZE)) != 0)
{
if (!MemoryAction(program, frame, ref buffer, read, false, string.Format("{0}+{1}", _addr_expr, pos)))
{
break;
}
pos += read;
Progress = (float)pos / len * 100;
}
if (read == 0)
{
MsgBox.Show(Resources.LoadSuccessText, Resources.SuccessDialogTitle, MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
catch (Exception ex)
{
ShowError(string.Format(Resources.LoadErrorText, ex.Message, pos));
}
IsBusy = false;
Progress = 0;
}
#endregion
}
}