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
|
/** @file
Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <Uefi.h>
#include "Edb.h"
EFI_DEBUGGER_PRIVATE_DATA mDebuggerPrivate = {
EFI_DEBUGGER_SIGNATURE, // Signature
IsaEbc, // Isa
(EBC_DEBUGGER_MAJOR_VERSION << 16) |
EBC_DEBUGGER_MINOR_VERSION, // EfiDebuggerRevision
(VM_MAJOR_VERSION << 16) |
VM_MINOR_VERSION, // EbcVmRevision
{
EFI_DEBUGGER_CONFIGURATION_VERSION,
&mDebuggerPrivate,
}, // DebuggerConfiguration
NULL, // DebugImageInfoTableHeader
NULL, // Vol
NULL, // PciRootBridgeIo
mDebuggerCommandSet, // DebuggerCommandSet
{0}, // DebuggerSymbolContext
0, // DebuggerBreakpointCount
{{0}}, // DebuggerBreakpointContext
0, // CallStackEntryCount
{{0}}, // CallStackEntry
0, // TraceEntryCount
{{0}}, // TraceEntry
{0}, // StepContext
{0}, // GoTilContext
0, // InstructionScope
EFI_DEBUG_DEFAULT_INSTRUCTION_NUMBER, // InstructionNumber
EFI_DEBUG_FLAG_EBC_BOE | EFI_DEBUG_FLAG_EBC_BOT, // FeatureFlags
0, // StatusFlags
FALSE, // EnablePageBreak
NULL // BreakEvent
};
CHAR16 *mExceptionStr[] = {
L"EXCEPT_EBC_UNDEFINED",
L"EXCEPT_EBC_DIVIDE_ERROR",
L"EXCEPT_EBC_DEBUG",
L"EXCEPT_EBC_BREAKPOINT",
L"EXCEPT_EBC_OVERFLOW",
L"EXCEPT_EBC_INVALID_OPCODE",
L"EXCEPT_EBC_STACK_FAULT",
L"EXCEPT_EBC_ALIGNMENT_CHECK",
L"EXCEPT_EBC_INSTRUCTION_ENCODING",
L"EXCEPT_EBC_BAD_BREAK",
L"EXCEPT_EBC_SINGLE_STEP",
};
/**
Clear all the breakpoint.
@param DebuggerPrivate EBC Debugger private data structure
@param NeedRemove Whether need to remove all the breakpoint
**/
VOID
EdbClearAllBreakpoint (
IN EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate,
IN BOOLEAN NeedRemove
)
{
UINTN Index;
//
// Patch all the breakpoint
//
for (Index = 0; (Index < DebuggerPrivate->DebuggerBreakpointCount) && (Index < EFI_DEBUGGER_BREAKPOINT_MAX); Index++) {
if (DebuggerPrivate->DebuggerBreakpointContext[Index].State) {
CopyMem (
(VOID *)(UINTN)DebuggerPrivate->DebuggerBreakpointContext[Index].BreakpointAddress,
&DebuggerPrivate->DebuggerBreakpointContext[Index].OldInstruction,
sizeof(UINT16)
);
}
}
//
// Zero Breakpoint context, if need to remove all breakpoint
//
if (NeedRemove) {
DebuggerPrivate->DebuggerBreakpointCount = 0;
ZeroMem (DebuggerPrivate->DebuggerBreakpointContext, sizeof(DebuggerPrivate->DebuggerBreakpointContext));
}
//
// Done
//
return ;
}
/**
Set all the breakpoint.
@param DebuggerPrivate EBC Debugger private data structure
**/
VOID
EdbSetAllBreakpoint (
IN EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate
)
{
UINTN Index;
UINT16 Data16;
//
// Set all the breakpoint (BREAK(3) : 0x0300)
//
Data16 = 0x0300;
for (Index = 0; (Index < DebuggerPrivate->DebuggerBreakpointCount) && (Index < EFI_DEBUGGER_BREAKPOINT_MAX); Index++) {
if (DebuggerPrivate->DebuggerBreakpointContext[Index].State) {
CopyMem (
(VOID *)(UINTN)DebuggerPrivate->DebuggerBreakpointContext[Index].BreakpointAddress,
&Data16,
sizeof(UINT16)
);
}
}
//
// Check if current break is caused by breakpoint set.
// If so, we need to patch memory back to let user see the real memory.
//
if (DebuggerPrivate->DebuggerBreakpointContext[EFI_DEBUGGER_BREAKPOINT_MAX].BreakpointAddress != 0) {
CopyMem (
(VOID *)(UINTN)DebuggerPrivate->DebuggerBreakpointContext[EFI_DEBUGGER_BREAKPOINT_MAX].BreakpointAddress,
&DebuggerPrivate->DebuggerBreakpointContext[EFI_DEBUGGER_BREAKPOINT_MAX].OldInstruction,
sizeof(UINT16)
);
DebuggerPrivate->StatusFlags &= ~EFI_DEBUG_FLAG_EBC_B_BP;
}
//
// Done
//
return ;
}
/**
Check all the breakpoint, if match, then set status flag, and record current breakpoint.
Then clear all breakpoint to let user see a clean memory
@param DebuggerPrivate EBC Debugger private data structure
@param SystemContext EBC system context.
**/
VOID
EdbCheckBreakpoint (
IN EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate,
IN EFI_SYSTEM_CONTEXT SystemContext
)
{
UINT64 Address;
UINTN Index;
BOOLEAN IsHitBreakpoint;
//
// Roll back IP for breakpoint instruction (BREAK(3) : 0x0300)
//
Address = SystemContext.SystemContextEbc->Ip - sizeof(UINT16);
//
// Check if the breakpoint is hit
//
IsHitBreakpoint = FALSE;
for (Index = 0; (Index < DebuggerPrivate->DebuggerBreakpointCount) && (Index < EFI_DEBUGGER_BREAKPOINT_MAX); Index++) {
if ((DebuggerPrivate->DebuggerBreakpointContext[Index].BreakpointAddress == Address) &&
(DebuggerPrivate->DebuggerBreakpointContext[Index].State)) {
IsHitBreakpoint = TRUE;
break;
}
}
if (IsHitBreakpoint) {
//
// If hit, record current breakpoint
//
DebuggerPrivate->DebuggerBreakpointContext[EFI_DEBUGGER_BREAKPOINT_MAX] = DebuggerPrivate->DebuggerBreakpointContext[Index];
DebuggerPrivate->DebuggerBreakpointContext[EFI_DEBUGGER_BREAKPOINT_MAX].State = TRUE;
//
// Update: IP and Instruction (NOTE: Since we not allow set breakpoint to BREAK 3, this update is safe)
//
SystemContext.SystemContextEbc->Ip = Address;
//
// Set Flags
//
DebuggerPrivate->StatusFlags |= EFI_DEBUG_FLAG_EBC_BP;
} else {
//
// If not hit, check whether current IP is in breakpoint list,
// because STEP will be triggered before execute the instruction.
// We should not patch it in de-init.
//
Address = SystemContext.SystemContextEbc->Ip;
//
// Check if the breakpoint is hit
//
IsHitBreakpoint = FALSE;
for (Index = 0; (Index < DebuggerPrivate->DebuggerBreakpointCount) && (Index < EFI_DEBUGGER_BREAKPOINT_MAX); Index++) {
if ((DebuggerPrivate->DebuggerBreakpointContext[Index].BreakpointAddress == Address) &&
(DebuggerPrivate->DebuggerBreakpointContext[Index].State)) {
IsHitBreakpoint = TRUE;
break;
}
}
if (IsHitBreakpoint) {
//
// If hit, record current breakpoint
//
DebuggerPrivate->DebuggerBreakpointContext[EFI_DEBUGGER_BREAKPOINT_MAX] = DebuggerPrivate->DebuggerBreakpointContext[Index];
DebuggerPrivate->DebuggerBreakpointContext[EFI_DEBUGGER_BREAKPOINT_MAX].State = TRUE;
//
// Do not set Breakpoint flag. We record the address here just let it not patch breakpoint address when de-init.
//
} else {
//
// Zero current breakpoint
//
ZeroMem (
&DebuggerPrivate->DebuggerBreakpointContext[EFI_DEBUGGER_BREAKPOINT_MAX],
sizeof(DebuggerPrivate->DebuggerBreakpointContext[EFI_DEBUGGER_BREAKPOINT_MAX])
);
}
}
//
// Done
//
return ;
}
/**
clear all the symbol.
@param DebuggerPrivate EBC Debugger private data structure
**/
VOID
EdbClearSymbol (
IN EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate
)
{
EFI_DEBUGGER_SYMBOL_CONTEXT *DebuggerSymbolContext;
EFI_DEBUGGER_SYMBOL_OBJECT *Object;
UINTN ObjectIndex;
UINTN Index;
//
// Go throuth each object
//
DebuggerSymbolContext = &DebuggerPrivate->DebuggerSymbolContext;
for (ObjectIndex = 0; ObjectIndex < DebuggerSymbolContext->ObjectCount; ObjectIndex++) {
Object = &DebuggerSymbolContext->Object[ObjectIndex];
//
// Go throuth each entry
//
for (Index = 0; Index < Object->EntryCount; Index++) {
ZeroMem (&Object->Entry[Index], sizeof(Object->Entry[Index]));
}
ZeroMem (Object->Name, sizeof(Object->Name));
Object->EntryCount = 0;
Object->BaseAddress = 0;
Object->StartEntrypointRVA = 0;
Object->MainEntrypointRVA = 0;
//
// Free source buffer
//
for (Index = 0; Object->SourceBuffer[Index] != NULL; Index++) {
gBS->FreePool (Object->SourceBuffer[Index]);
Object->SourceBuffer[Index] = NULL;
}
}
DebuggerSymbolContext->ObjectCount = 0;
return ;
}
/**
Initialize Debugger private data structure
@param DebuggerPrivate EBC Debugger private data structure
@param ExceptionType Exception type.
@param SystemContext EBC system context.
@param Initialized Whether the DebuggerPrivate data is initialized.
**/
EFI_STATUS
InitDebuggerPrivateData (
IN EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate,
IN EFI_EXCEPTION_TYPE ExceptionType,
IN EFI_SYSTEM_CONTEXT SystemContext,
IN BOOLEAN Initialized
)
{
//
// clear STEP flag in any condition.
//
if (SystemContext.SystemContextEbc->Flags & ((UINT64) VMFLAGS_STEP)) {
SystemContext.SystemContextEbc->Flags &= ~((UINT64) VMFLAGS_STEP);
}
if (!Initialized) {
//
// Initialize everything
//
DebuggerPrivate->InstructionNumber = EFI_DEBUG_DEFAULT_INSTRUCTION_NUMBER;
DebuggerPrivate->DebuggerBreakpointCount = 0;
ZeroMem (DebuggerPrivate->DebuggerBreakpointContext, sizeof(DebuggerPrivate->DebuggerBreakpointContext));
// DebuggerPrivate->StatusFlags = 0;
DebuggerPrivate->DebuggerSymbolContext.DisplaySymbol = TRUE;
DebuggerPrivate->DebuggerSymbolContext.DisplayCodeOnly = FALSE;
DebuggerPrivate->DebuggerSymbolContext.ObjectCount = 0;
} else {
//
// Already initialized, just check Breakpoint here.
//
if (ExceptionType == EXCEPT_EBC_BREAKPOINT) {
EdbCheckBreakpoint (DebuggerPrivate, SystemContext);
}
//
// Clear all breakpoint
//
EdbClearAllBreakpoint (DebuggerPrivate, FALSE);
}
//
// Set Scope to currentl IP. (Note: Check Breakpoint may change Ip)
//
DebuggerPrivate->InstructionScope = SystemContext.SystemContextEbc->Ip;
//
// Done
//
return EFI_SUCCESS;
}
/**
De-initialize Debugger private data structure.
@param DebuggerPrivate EBC Debugger private data structure
@param ExceptionType Exception type.
@param SystemContext EBC system context.
@param Initialized Whether the DebuggerPrivate data is initialized.
**/
EFI_STATUS
DeinitDebuggerPrivateData (
IN EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate,
IN EFI_EXCEPTION_TYPE ExceptionType,
IN EFI_SYSTEM_CONTEXT SystemContext,
IN BOOLEAN Initialized
)
{
if (!Initialized) {
//
// If it does not want initialized state, de-init everything
//
DebuggerPrivate->FeatureFlags = EFI_DEBUG_FLAG_EBC_BOE | EFI_DEBUG_FLAG_EBC_BOT;
DebuggerPrivate->CallStackEntryCount = 0;
DebuggerPrivate->TraceEntryCount = 0;
ZeroMem (DebuggerPrivate->CallStackEntry, sizeof(DebuggerPrivate->CallStackEntry));
ZeroMem (DebuggerPrivate->TraceEntry, sizeof(DebuggerPrivate->TraceEntry));
//
// Clear all breakpoint
//
EdbClearAllBreakpoint (DebuggerPrivate, TRUE);
//
// Clear symbol
//
EdbClearSymbol (DebuggerPrivate);
} else {
//
// If it wants to keep initialized state, just set breakpoint.
//
EdbSetAllBreakpoint (DebuggerPrivate);
}
//
// Clear Step context
//
ZeroMem (&mDebuggerPrivate.StepContext, sizeof(mDebuggerPrivate.StepContext));
DebuggerPrivate->StatusFlags = 0;
//
// Done
//
return EFI_SUCCESS;
}
/**
Print the reason of current break to EbcDebugger.
@param DebuggerPrivate EBC Debugger private data structure
@param ExceptionType Exception type.
@param SystemContext EBC system context.
@param Initialized Whether the DebuggerPrivate data is initialized.
**/
VOID
PrintExceptionReason (
IN EFI_DEBUGGER_PRIVATE_DATA *DebuggerPrivate,
IN EFI_EXCEPTION_TYPE ExceptionType,
IN EFI_SYSTEM_CONTEXT SystemContext,
IN BOOLEAN Initialized
)
{
//
// Print break status
//
if ((DebuggerPrivate->StatusFlags & EFI_DEBUG_FLAG_EBC_GT) == EFI_DEBUG_FLAG_EBC_GT) {
EDBPrint (L"Break on GoTil\n");
} else if ((DebuggerPrivate->StatusFlags & EFI_DEBUG_FLAG_EBC_BOC) == EFI_DEBUG_FLAG_EBC_BOC) {
EDBPrint (L"Break on CALL\n");
} else if ((DebuggerPrivate->StatusFlags & EFI_DEBUG_FLAG_EBC_BOCX) == EFI_DEBUG_FLAG_EBC_BOCX) {
EDBPrint (L"Break on CALLEX\n");
} else if ((DebuggerPrivate->StatusFlags & EFI_DEBUG_FLAG_EBC_BOR) == EFI_DEBUG_FLAG_EBC_BOR) {
EDBPrint (L"Break on RET\n");
} else if ((DebuggerPrivate->StatusFlags & EFI_DEBUG_FLAG_EBC_BOE) == EFI_DEBUG_FLAG_EBC_BOE) {
EDBPrint (L"Break on Entrypoint\n");
} else if ((DebuggerPrivate->StatusFlags & EFI_DEBUG_FLAG_EBC_BOT) == EFI_DEBUG_FLAG_EBC_BOT) {
EDBPrint (L"Break on Thunk\n");
} else if ((DebuggerPrivate->StatusFlags & EFI_DEBUG_FLAG_EBC_STEPOVER) == EFI_DEBUG_FLAG_EBC_STEPOVER) {
EDBPrint (L"Break on StepOver\n");
} else if ((DebuggerPrivate->StatusFlags & EFI_DEBUG_FLAG_EBC_STEPOUT) == EFI_DEBUG_FLAG_EBC_STEPOUT) {
EDBPrint (L"Break on StepOut\n");
} else if ((DebuggerPrivate->StatusFlags & EFI_DEBUG_FLAG_EBC_BP) == EFI_DEBUG_FLAG_EBC_BP) {
EDBPrint (L"Break on Breakpoint\n");
} else if ((DebuggerPrivate->StatusFlags & EFI_DEBUG_FLAG_EBC_BOK) == EFI_DEBUG_FLAG_EBC_BOK) {
EDBPrint (L"Break on Key\n");
} else {
EDBPrint (L"Exception Type - %x", (UINTN)ExceptionType);
if ((ExceptionType >= EXCEPT_EBC_UNDEFINED) && (ExceptionType <= EXCEPT_EBC_STEP)) {
EDBPrint (L" (%s)\n", mExceptionStr[ExceptionType]);
} else {
EDBPrint (L"\n");
}
}
return ;
}
/**
The default Exception Callback for the VM interpreter.
In this function, we report status code, and print debug information
about EBC_CONTEXT, then dead loop.
@param ExceptionType Exception type.
@param SystemContext EBC system context.
**/
VOID
EFIAPI
EdbExceptionHandler (
IN EFI_EXCEPTION_TYPE ExceptionType,
IN OUT EFI_SYSTEM_CONTEXT SystemContext
)
{
CHAR16 InputBuffer[EFI_DEBUG_INPUS_BUFFER_SIZE];
CHAR16 *CommandArg;
EFI_DEBUGGER_COMMAND DebuggerCommand;
EFI_DEBUG_STATUS DebugStatus;
STATIC BOOLEAN mInitialized;
mInitialized = FALSE;
DEBUG ((DEBUG_ERROR, "Hello EBC Debugger!\n"));
if (!mInitialized) {
//
// Print version
//
EDBPrint (
L"EBC Interpreter Version - %d.%d\n",
(UINTN)VM_MAJOR_VERSION,
(UINTN)VM_MINOR_VERSION
);
EDBPrint (
L"EBC Debugger Version - %d.%d\n",
(UINTN)EBC_DEBUGGER_MAJOR_VERSION,
(UINTN)EBC_DEBUGGER_MINOR_VERSION
);
}
//
// Init Private Data
//
InitDebuggerPrivateData (&mDebuggerPrivate, ExceptionType, SystemContext, mInitialized);
//
// EDBPrint basic info
//
PrintExceptionReason (&mDebuggerPrivate, ExceptionType, SystemContext, mInitialized);
EdbShowDisasm (&mDebuggerPrivate, SystemContext);
// EFI_BREAKPOINT ();
if (!mInitialized) {
//
// Interactive with user
//
EDBPrint (L"\nPlease enter command now, \'h\' for help.\n");
EDBPrint (L"(Using <Command> -b <...> to enable page break.)\n");
}
mInitialized = TRUE;
//
// Dispatch each command
//
while (TRUE) {
//
// Get user input
//
Input (L"\n\r" EFI_DEBUG_PROMPT_STRING, InputBuffer, EFI_DEBUG_INPUS_BUFFER_SIZE);
EDBPrint (L"\n");
//
// Get command
//
DebuggerCommand = MatchDebuggerCommand (InputBuffer, &CommandArg);
if (DebuggerCommand == NULL) {
EDBPrint (L"ERROR: Command not found!\n");
continue;
}
//
// Check PageBreak;
//
if (CommandArg != NULL) {
if (StriCmp (CommandArg, L"-b") == 0) {
CommandArg = StrGetNextTokenLine (L" ");
mDebuggerPrivate.EnablePageBreak = TRUE;
}
}
//
// Dispatch command
//
DebugStatus = DebuggerCommand (CommandArg, &mDebuggerPrivate, ExceptionType, SystemContext);
mDebuggerPrivate.EnablePageBreak = FALSE;
//
// Check command return status
//
if (DebugStatus == EFI_DEBUG_RETURN) {
mInitialized = FALSE;
break;
} else if (DebugStatus == EFI_DEBUG_BREAK) {
break;
} else if (DebugStatus == EFI_DEBUG_CONTINUE) {
continue;
} else {
ASSERT (FALSE);
}
}
//
// Deinit Private Data
//
DeinitDebuggerPrivateData (&mDebuggerPrivate, ExceptionType, SystemContext, mInitialized);
DEBUG ((DEBUG_ERROR, "Goodbye EBC Debugger!\n"));
return;
}
|