summaryrefslogtreecommitdiffstats
path: root/MdeModulePkg/Universal/EbcDxe/EbcDebugger/EdbHook.c
blob: 4a04e902fbb96be609c15599f83e6eb331ffc9bf (plain)
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
/** @file

Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent


**/

#include "Edb.h"

/**

  Check the Hook flag, and trigger exception if match.

  @param  VmPtr        - EbcDebuggerCheckHookFlag
  @param  Flag         - Feature flag

**/
VOID
EbcDebuggerCheckHookFlag (
  IN VM_CONTEXT  *VmPtr,
  IN UINT32      Flag
  )
{
  if ((mDebuggerPrivate.FeatureFlags & Flag) == Flag) {
    mDebuggerPrivate.StatusFlags = Flag;
    EbcDebugSignalException (
      EXCEPT_EBC_BREAKPOINT,
      EXCEPTION_FLAG_NONE,
      VmPtr
      );
  }

  return;
}

/**

  It will record soruce address for Callstack entry.

  @param  SourceEntry  - Source address
  @param  Type         - Branch type

**/
VOID
EbcDebuggerPushCallstackSource (
  IN UINT64                    SourceEntry,
  IN EFI_DEBUGGER_BRANCH_TYPE  Type
  )
{
  if (mDebuggerPrivate.CallStackEntryCount > EFI_DEBUGGER_CALLSTACK_MAX) {
    ASSERT (FALSE);
    mDebuggerPrivate.CallStackEntryCount = EFI_DEBUGGER_CALLSTACK_MAX;
  }

  //
  // Record the new callstack entry
  //
  mDebuggerPrivate.CallStackEntry[mDebuggerPrivate.CallStackEntryCount].SourceAddress = SourceEntry;
  mDebuggerPrivate.CallStackEntry[mDebuggerPrivate.CallStackEntryCount].Type          = Type;

  //
  // Do not change CallStackEntryCount
  //

  return;
}

/**

  It will record parameter for Callstack entry.

  @param  ParameterAddress - The address for the parameter
  @param  Type             - Branch type

**/
VOID
EbcDebuggerPushCallstackParameter (
  IN UINT64                    ParameterAddress,
  IN EFI_DEBUGGER_BRANCH_TYPE  Type
  )
{
  if (mDebuggerPrivate.CallStackEntryCount > EFI_DEBUGGER_CALLSTACK_MAX) {
    ASSERT (FALSE);
    mDebuggerPrivate.CallStackEntryCount = EFI_DEBUGGER_CALLSTACK_MAX;
  }

  //
  // Record the new callstack parameter
  //
  mDebuggerPrivate.CallStackEntry[mDebuggerPrivate.CallStackEntryCount].ParameterAddr = (UINTN)ParameterAddress;
  CopyMem (
    mDebuggerPrivate.CallStackEntry[mDebuggerPrivate.CallStackEntryCount].Parameter,
    (VOID *)(UINTN)ParameterAddress,
    sizeof (mDebuggerPrivate.CallStackEntry[mDebuggerPrivate.CallStackEntryCount].Parameter)
    );

  //
  // Do not change CallStackEntryCount
  //

  return;
}

/**

  It will record source address for callstack entry.

  @param  DestEntry    - Source address
  @param  Type         - Branch type

**/
VOID
EbcDebuggerPushCallstackDest (
  IN UINT64                    DestEntry,
  IN EFI_DEBUGGER_BRANCH_TYPE  Type
  )
{
  UINTN  Index;

  if (mDebuggerPrivate.CallStackEntryCount < EFI_DEBUGGER_CALLSTACK_MAX) {
    //
    // If there is empty entry for callstack, add it
    //
    ASSERT (mDebuggerPrivate.CallStackEntry[mDebuggerPrivate.CallStackEntryCount].Type == Type);
    mDebuggerPrivate.CallStackEntry[mDebuggerPrivate.CallStackEntryCount].DestAddress = DestEntry;
    mDebuggerPrivate.CallStackEntryCount++;
  } else {
    //
    // If there is no empty entry for callstack, throw the oldest one
    //
    ASSERT (mDebuggerPrivate.CallStackEntry[EFI_DEBUGGER_TRACE_MAX].Type == Type);
    for (Index = 0; Index < EFI_DEBUGGER_CALLSTACK_MAX; Index++) {
      CopyMem (
        &mDebuggerPrivate.CallStackEntry[Index],
        &mDebuggerPrivate.CallStackEntry[Index + 1],
        sizeof (mDebuggerPrivate.CallStackEntry[Index])
        );
    }

    mDebuggerPrivate.CallStackEntry[EFI_DEBUGGER_CALLSTACK_MAX - 1].DestAddress = DestEntry;
    mDebuggerPrivate.CallStackEntryCount                                        = EFI_DEBUGGER_CALLSTACK_MAX;
  }

  return;
}

/**

  It will throw the newest Callstack entry.

**/
VOID
EbcDebuggerPopCallstack (
  VOID
  )
{
  if ((mDebuggerPrivate.CallStackEntryCount > 0) &&
      (mDebuggerPrivate.CallStackEntryCount <= EFI_DEBUGGER_CALLSTACK_MAX))
  {
    //
    // Throw the newest one
    //
    mDebuggerPrivate.CallStackEntryCount--;
    mDebuggerPrivate.CallStackEntry[mDebuggerPrivate.CallStackEntryCount].SourceAddress = 0;
    mDebuggerPrivate.CallStackEntry[mDebuggerPrivate.CallStackEntryCount].DestAddress   = 0;
  } else if (mDebuggerPrivate.CallStackEntryCount == 0) {
    //
    // NOT assert here because it is reasonable, because when we start to build
    // callstack, we do not know how many function already called.
    //
  } else {
    ASSERT (FALSE);
  }

  return;
}

/**

  It will record source address for trace entry.

  @param  SourceEntry  - Source address
  @param  Type         - Branch type

**/
VOID
EbcDebuggerPushTraceSourceEntry (
  IN UINT64                    SourceEntry,
  IN EFI_DEBUGGER_BRANCH_TYPE  Type
  )
{
  if (mDebuggerPrivate.TraceEntryCount > EFI_DEBUGGER_TRACE_MAX) {
    ASSERT (FALSE);
    mDebuggerPrivate.TraceEntryCount = EFI_DEBUGGER_TRACE_MAX;
  }

  //
  // Record the new trace entry
  //
  mDebuggerPrivate.TraceEntry[mDebuggerPrivate.TraceEntryCount].SourceAddress = SourceEntry;
  mDebuggerPrivate.TraceEntry[mDebuggerPrivate.TraceEntryCount].Type          = Type;

  //
  // Do not change TraceEntryCount
  //

  return;
}

/**

  It will record destination address for trace entry.

  @param  DestEntry    - Destination address
  @param  Type         - Branch type

**/
VOID
EbcDebuggerPushTraceDestEntry (
  IN UINT64                    DestEntry,
  IN EFI_DEBUGGER_BRANCH_TYPE  Type
  )
{
  UINTN  Index;

  if (mDebuggerPrivate.TraceEntryCount < EFI_DEBUGGER_TRACE_MAX) {
    //
    // If there is empty entry for trace, add it
    //
    ASSERT (mDebuggerPrivate.TraceEntry[mDebuggerPrivate.TraceEntryCount].Type == Type);
    mDebuggerPrivate.TraceEntry[mDebuggerPrivate.TraceEntryCount].DestAddress = DestEntry;
    mDebuggerPrivate.TraceEntryCount++;
  } else {
    //
    // If there is no empty entry for trace, throw the oldest one
    //
    ASSERT (mDebuggerPrivate.TraceEntry[EFI_DEBUGGER_TRACE_MAX].Type == Type);
    for (Index = 0; Index < EFI_DEBUGGER_TRACE_MAX; Index++) {
      CopyMem (
        &mDebuggerPrivate.TraceEntry[Index],
        &mDebuggerPrivate.TraceEntry[Index + 1],
        sizeof (mDebuggerPrivate.TraceEntry[Index])
        );
    }

    mDebuggerPrivate.TraceEntry[EFI_DEBUGGER_CALLSTACK_MAX - 1].DestAddress = DestEntry;
    mDebuggerPrivate.TraceEntryCount                                        = EFI_DEBUGGER_TRACE_MAX;
  }

  return;
}

/**

  It will record address for StepEntry, if STEPOVER or STEPOUT is enabled.

  @param  Entry    - Break Address
  @param  FramePtr - Break Frame pointer
  @param  Flag     - for STEPOVER or STEPOUT

**/
VOID
EbcDebuggerPushStepEntry (
  IN UINT64  Entry,
  IN UINT64  FramePtr,
  IN UINT32  Flag
  )
{
  //
  // Check StepOver
  //
  if ((Flag == EFI_DEBUG_FLAG_EBC_STEPOVER) &&
      ((mDebuggerPrivate.FeatureFlags & EFI_DEBUG_FLAG_EBC_STEPOVER) == EFI_DEBUG_FLAG_EBC_STEPOVER))
  {
    mDebuggerPrivate.StepContext.BreakAddress = Entry;
    mDebuggerPrivate.StepContext.FramePointer = FramePtr;
    mDebuggerPrivate.FeatureFlags            &= ~EFI_DEBUG_FLAG_EBC_B_STEPOVER;
  }

  //
  // Check StepOut
  //
  if ((Flag == EFI_DEBUG_FLAG_EBC_STEPOUT) &&
      ((mDebuggerPrivate.FeatureFlags & EFI_DEBUG_FLAG_EBC_STEPOUT) == EFI_DEBUG_FLAG_EBC_STEPOUT))
  {
    mDebuggerPrivate.StepContext.BreakAddress = Entry;
    mDebuggerPrivate.StepContext.FramePointer = FramePtr;
    mDebuggerPrivate.FeatureFlags            &= ~EFI_DEBUG_FLAG_EBC_B_STEPOUT;
  }
}

/**
  Notify the callback function when an event is triggered.

  @param  Event                    Indicates the event that invoke this function.
  @param  Context                  Indicates the calling context.

**/
VOID
EFIAPI
EbcDebuggerBreakEventFunc (
  IN EFI_EVENT  Event,
  IN VOID       *Context
  )
{
  EFI_STATUS  Status;

  if ((mDebuggerPrivate.FeatureFlags & EFI_DEBUG_FLAG_EBC_BOK) != EFI_DEBUG_FLAG_EBC_BOK) {
    return;
  }

  Status = gBS->CheckEvent (gST->ConIn->WaitForKey);
  if (Status == EFI_SUCCESS) {
    mDebuggerPrivate.StatusFlags = EFI_DEBUG_FLAG_EBC_BOK;
  }
}

/**

  The hook in InitializeEbcDriver.
  It will init the EbcDebuggerPrivate data structure.

  @param Handle           - The EbcDebugProtocol handle.
  @param EbcDebugProtocol - The EbcDebugProtocol interface.

**/
VOID
EbcDebuggerHookInit (
  IN EFI_HANDLE                  Handle,
  IN EFI_DEBUG_SUPPORT_PROTOCOL  *EbcDebugProtocol
  )
{
  EFI_STATUS                  Status;
  UINTN                       Index;
  EFI_DEBUGGER_SYMBOL_OBJECT  *Object;
  EFI_DEBUGGER_SYMBOL_ENTRY   *Entry;

  //
  // Register all exception handler
  //
  for (Index = EXCEPT_EBC_UNDEFINED; Index <= EXCEPT_EBC_STEP; Index++) {
    EbcDebugProtocol->RegisterExceptionCallback (
                        EbcDebugProtocol,
                        0,
                        NULL,
                        Index
                        );
    EbcDebugProtocol->RegisterExceptionCallback (
                        EbcDebugProtocol,
                        0,
                        EdbExceptionHandler,
                        Index
                        );
  }

  //
  // Init Symbol
  //
  Object = AllocateZeroPool (sizeof (EFI_DEBUGGER_SYMBOL_OBJECT) * EFI_DEBUGGER_SYMBOL_OBJECT_MAX);
  ASSERT (Object != NULL);
  mDebuggerPrivate.DebuggerSymbolContext.Object         = Object;
  mDebuggerPrivate.DebuggerSymbolContext.ObjectCount    = 0;
  mDebuggerPrivate.DebuggerSymbolContext.MaxObjectCount = EFI_DEBUGGER_SYMBOL_OBJECT_MAX;
  for (Index = 0; Index < EFI_DEBUGGER_SYMBOL_OBJECT_MAX; Index++) {
    Entry = AllocateZeroPool (sizeof (EFI_DEBUGGER_SYMBOL_ENTRY) * EFI_DEBUGGER_SYMBOL_ENTRY_MAX);
    ASSERT (Entry != NULL);
    Object[Index].Entry         = Entry;
    Object[Index].MaxEntryCount = EFI_DEBUGGER_SYMBOL_ENTRY_MAX;
    Object[Index].SourceBuffer  = AllocateZeroPool (sizeof (VOID *) * (EFI_DEBUGGER_SYMBOL_ENTRY_MAX + 1));
    ASSERT (Object[Index].SourceBuffer != NULL);
  }

  //
  // locate PciRootBridgeIo
  //
  Status = gBS->LocateProtocol (
                  &gEfiPciRootBridgeIoProtocolGuid,
                  NULL,
                  (VOID **)&mDebuggerPrivate.PciRootBridgeIo
                  );

  //
  // locate DebugImageInfoTable
  //
  Status = EfiGetSystemConfigurationTable (
             &gEfiDebugImageInfoTableGuid,
             (VOID **)&mDebuggerPrivate.DebugImageInfoTableHeader
             );

  //
  // Register Debugger Configuration Protocol, for config in shell
  //
  Status = gBS->InstallProtocolInterface (
                  &Handle,
                  &gEfiDebuggerConfigurationProtocolGuid,
                  EFI_NATIVE_INTERFACE,
                  &mDebuggerPrivate.DebuggerConfiguration
                  );

  //
  //
  // Create break event
  //
  Status = gBS->CreateEvent (
                  EVT_TIMER | EVT_NOTIFY_SIGNAL,
                  TPL_CALLBACK,
                  EbcDebuggerBreakEventFunc,
                  NULL,
                  &mDebuggerPrivate.BreakEvent
                  );
  if (!EFI_ERROR (Status)) {
    Status = gBS->SetTimer (
                    mDebuggerPrivate.BreakEvent,
                    TimerPeriodic,
                    EFI_DEBUG_BREAK_TIMER_INTERVAL
                    );
  }

  return;
}

/**

  The hook in UnloadImage for EBC Interpreter.
  It clean up the environment.

**/
VOID
EbcDebuggerHookUnload (
  VOID
  )
{
  UINTN                       Index;
  UINTN                       SubIndex;
  EFI_DEBUGGER_SYMBOL_OBJECT  *Object;

  //
  // Close the break event
  //
  if (mDebuggerPrivate.BreakEvent != NULL) {
    gBS->CloseEvent (mDebuggerPrivate.BreakEvent);
  }

  //
  // Clean up the symbol
  //
  Object = mDebuggerPrivate.DebuggerSymbolContext.Object;
  for (Index = 0; Index < EFI_DEBUGGER_SYMBOL_OBJECT_MAX; Index++) {
    //
    // Clean up Entry
    //
    gBS->FreePool (Object[Index].Entry);
    Object[Index].Entry      = NULL;
    Object[Index].EntryCount = 0;
    //
    // Clean up source buffer
    //
    for (SubIndex = 0; Object[Index].SourceBuffer[SubIndex] != NULL; SubIndex++) {
      gBS->FreePool (Object[Index].SourceBuffer[SubIndex]);
      Object[Index].SourceBuffer[SubIndex] = NULL;
    }

    gBS->FreePool (Object[Index].SourceBuffer);
    Object[Index].SourceBuffer = NULL;
  }

  //
  // Clean up Object
  //
  gBS->FreePool (Object);
  mDebuggerPrivate.DebuggerSymbolContext.Object      = NULL;
  mDebuggerPrivate.DebuggerSymbolContext.ObjectCount = 0;

  //
  // Done
  //
  return;
}

/**

  The hook in EbcUnloadImage.
  Currently do nothing here.

  @param  Handle           - The EbcImage handle.

**/
VOID
EbcDebuggerHookEbcUnloadImage (
  IN EFI_HANDLE  Handle
  )
{
  return;
}

/**

  The hook in ExecuteEbcImageEntryPoint.
  It will record the call-stack entry. (-1 means EbcImageEntryPoint call)
  and trigger Exception if BOE enabled.


  @param  VmPtr - pointer to VM context.

**/
VOID
EbcDebuggerHookExecuteEbcImageEntryPoint (
  IN VM_CONTEXT  *VmPtr
  )
{
  EbcDebuggerPushCallstackSource ((UINT64)(UINTN)-1, EfiDebuggerBranchTypeEbcCall);
  EbcDebuggerPushCallstackParameter ((UINT64)(UINTN)VmPtr->Gpr[0], EfiDebuggerBranchTypeEbcCall);
  EbcDebuggerPushCallstackDest ((UINT64)(UINTN)VmPtr->Ip, EfiDebuggerBranchTypeEbcCall);
  EbcDebuggerCheckHookFlag (VmPtr, EFI_DEBUG_FLAG_EBC_BOE);
  return;
}

/**

  The hook in ExecuteEbcImageEntryPoint.
  It will record the call-stack entry. (-2 means EbcInterpret call)
  and trigger Exception if BOT enabled.

  @param  VmPtr - pointer to VM context.

**/
VOID
EbcDebuggerHookEbcInterpret (
  IN VM_CONTEXT  *VmPtr
  )
{
  EbcDebuggerPushCallstackSource ((UINT64)(UINTN)-2, EfiDebuggerBranchTypeEbcCall);
  EbcDebuggerPushCallstackParameter ((UINT64)(UINTN)VmPtr->Gpr[0], EfiDebuggerBranchTypeEbcCall);
  EbcDebuggerPushCallstackDest ((UINT64)(UINTN)VmPtr->Ip, EfiDebuggerBranchTypeEbcCall);
  EbcDebuggerCheckHookFlag (VmPtr, EFI_DEBUG_FLAG_EBC_BOT);
  return;
}

/**

  The hook in EbcExecute, before ExecuteFunction.
  It will trigger Exception if GoTil, StepOver, or StepOut hit.

  @param  VmPtr - pointer to VM context.

**/
VOID
EbcDebuggerHookExecuteStart (
  IN VM_CONTEXT  *VmPtr
  )
{
  EFI_TPL  CurrentTpl;

  //
  // Check Ip for GoTil
  //
  if (mDebuggerPrivate.GoTilContext.BreakAddress == (UINT64)(UINTN)VmPtr->Ip) {
    mDebuggerPrivate.StatusFlags               = EFI_DEBUG_FLAG_EBC_GT;
    mDebuggerPrivate.GoTilContext.BreakAddress = 0;
    EbcDebugSignalException (
      EXCEPT_EBC_BREAKPOINT,
      EXCEPTION_FLAG_NONE,
      VmPtr
      );
    mDebuggerPrivate.StatusFlags &= ~EFI_DEBUG_FLAG_EBC_B_GT;
    return;
  }

  //
  // Check ReturnAddress for StepOver
  //
  if ((mDebuggerPrivate.StepContext.BreakAddress == (UINT64)(UINTN)VmPtr->Ip) &&
      (mDebuggerPrivate.StepContext.FramePointer == (UINT64)(UINTN)VmPtr->FramePtr))
  {
    mDebuggerPrivate.StatusFlags              = EFI_DEBUG_FLAG_EBC_STEPOVER;
    mDebuggerPrivate.StepContext.BreakAddress = 0;
    mDebuggerPrivate.StepContext.FramePointer = 0;
    EbcDebugSignalException (
      EXCEPT_EBC_BREAKPOINT,
      EXCEPTION_FLAG_NONE,
      VmPtr
      );
    mDebuggerPrivate.StatusFlags &= ~EFI_DEBUG_FLAG_EBC_B_STEPOVER;
  }

  //
  // Check FramePtr for StepOut
  //
  if (mDebuggerPrivate.StepContext.BreakAddress == (UINT64)(UINTN)VmPtr->FramePtr) {
    mDebuggerPrivate.StatusFlags              = EFI_DEBUG_FLAG_EBC_STEPOUT;
    mDebuggerPrivate.StepContext.BreakAddress = 0;
    mDebuggerPrivate.StepContext.FramePointer = 0;
    EbcDebugSignalException (
      EXCEPT_EBC_BREAKPOINT,
      EXCEPTION_FLAG_NONE,
      VmPtr
      );
    mDebuggerPrivate.StatusFlags &= ~EFI_DEBUG_FLAG_EBC_B_STEPOUT;
  }

  //
  // Check Flags for BreakOnKey
  //
  if (mDebuggerPrivate.StatusFlags == EFI_DEBUG_FLAG_EBC_BOK) {
    //
    // Only break when the current TPL <= TPL_APPLICATION
    //
    CurrentTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
    gBS->RestoreTPL (CurrentTpl);
    if (CurrentTpl <= TPL_APPLICATION) {
      EbcDebugSignalException (
        EXCEPT_EBC_BREAKPOINT,
        EXCEPTION_FLAG_NONE,
        VmPtr
        );
      mDebuggerPrivate.StatusFlags &= ~EFI_DEBUG_FLAG_EBC_B_BOK;
    }
  }

  return;
}

/**

  The hook in EbcExecute, after ExecuteFunction.
  It will record StepOut Entry if need.

  @param  VmPtr - pointer to VM context.

**/
VOID
EbcDebuggerHookExecuteEnd (
  IN VM_CONTEXT  *VmPtr
  )
{
  UINTN  Address;

  //
  // Use FramePtr as checkpoint for StepOut
  //
  CopyMem (&Address, (VOID *)((UINTN)VmPtr->FramePtr), sizeof (Address));
  EbcDebuggerPushStepEntry (Address, (UINT64)(UINTN)VmPtr->FramePtr, EFI_DEBUG_FLAG_EBC_STEPOUT);

  return;
}

/**

  The hook in ExecuteCALL, before move IP.
  It will trigger Exception if BOC enabled,
  and record Callstack, and trace information.

  @param  VmPtr - pointer to VM context.

**/
VOID
EbcDebuggerHookCALLStart (
  IN VM_CONTEXT  *VmPtr
  )
{
  EbcDebuggerCheckHookFlag (VmPtr, EFI_DEBUG_FLAG_EBC_BOC);
  EbcDebuggerPushCallstackSource ((UINT64)(UINTN)VmPtr->Ip, EfiDebuggerBranchTypeEbcCall);
  EbcDebuggerPushCallstackParameter ((UINT64)(UINTN)VmPtr->Gpr[0], EfiDebuggerBranchTypeEbcCall);
  EbcDebuggerPushTraceSourceEntry ((UINT64)(UINTN)VmPtr->Ip, EfiDebuggerBranchTypeEbcCall);
  return;
}

/**

  The hook in ExecuteCALL, after move IP.
  It will record Callstack, trace information
  and record StepOver/StepOut Entry if need.

  @param  VmPtr - pointer to VM context.

**/
VOID
EbcDebuggerHookCALLEnd (
  IN VM_CONTEXT  *VmPtr
  )
{
  UINT64  Address;
  UINTN   FramePtr;

  EbcDebuggerPushCallstackDest ((UINT64)(UINTN)VmPtr->Ip, EfiDebuggerBranchTypeEbcCall);
  EbcDebuggerPushTraceDestEntry ((UINT64)(UINTN)VmPtr->Ip, EfiDebuggerBranchTypeEbcCall);

  //
  // Get Old FramePtr
  //
  CopyMem (&FramePtr, (VOID *)((UINTN)VmPtr->FramePtr), sizeof (FramePtr));

  //
  // Use ReturnAddress as checkpoint for StepOver
  //
  CopyMem (&Address, (VOID *)(UINTN)VmPtr->Gpr[0], sizeof (Address));
  EbcDebuggerPushStepEntry (Address, FramePtr, EFI_DEBUG_FLAG_EBC_STEPOVER);

  //
  // Use FramePtr as checkpoint for StepOut
  //
  Address = 0;
  CopyMem (&Address, (VOID *)(FramePtr), sizeof (UINTN));
  EbcDebuggerPushStepEntry (Address, FramePtr, EFI_DEBUG_FLAG_EBC_STEPOUT);

  return;
}

/**

  The hook in ExecuteCALL, before call EbcLLCALLEX.
  It will trigger Exception if BOCX enabled,
  and record Callstack information.

  @param  VmPtr - pointer to VM context.

**/
VOID
EbcDebuggerHookCALLEXStart (
  IN VM_CONTEXT  *VmPtr
  )
{
  EbcDebuggerCheckHookFlag (VmPtr, EFI_DEBUG_FLAG_EBC_BOCX);
  //  EbcDebuggerPushCallstackSource ((UINT64)(UINTN)VmPtr->Ip, EfiDebuggerBranchTypeEbcCallEx);
  //  EbcDebuggerPushCallstackParameter ((UINT64)(UINTN)VmPtr->R[0], EfiDebuggerBranchTypeEbcCallEx);
  EbcDebuggerPushTraceSourceEntry ((UINT64)(UINTN)VmPtr->Ip, EfiDebuggerBranchTypeEbcCallEx);
  return;
}

/**

  The hook in ExecuteCALL, after call EbcLLCALLEX.
  It will record trace information.

  @param  VmPtr - pointer to VM context.

**/
VOID
EbcDebuggerHookCALLEXEnd (
  IN VM_CONTEXT  *VmPtr
  )
{
  //  EbcDebuggerPushCallstackDest ((UINT64)(UINTN)VmPtr->Ip, EfiDebuggerBranchTypeEbcCallEx);
  EbcDebuggerPushTraceDestEntry ((UINT64)(UINTN)VmPtr->Ip, EfiDebuggerBranchTypeEbcCallEx);
  return;
}

/**

  The hook in ExecuteRET, before move IP.
  It will trigger Exception if BOR enabled,
  and record Callstack, and trace information.

  @param  VmPtr - pointer to VM context.

**/
VOID
EbcDebuggerHookRETStart (
  IN VM_CONTEXT  *VmPtr
  )
{
  EbcDebuggerCheckHookFlag (VmPtr, EFI_DEBUG_FLAG_EBC_BOR);
  EbcDebuggerPopCallstack ();
  EbcDebuggerPushTraceSourceEntry ((UINT64)(UINTN)VmPtr->Ip, EfiDebuggerBranchTypeEbcRet);
  return;
}

/**

  The hook in ExecuteRET, after move IP.
  It will record trace information.

  @param  VmPtr - pointer to VM context.

**/
VOID
EbcDebuggerHookRETEnd (
  IN VM_CONTEXT  *VmPtr
  )
{
  EbcDebuggerPushTraceDestEntry ((UINT64)(UINTN)VmPtr->Ip, EfiDebuggerBranchTypeEbcRet);
  return;
}

/**

  The hook in ExecuteJMP, before move IP.
  It will record trace information.

  @param  VmPtr - pointer to VM context.

**/
VOID
EbcDebuggerHookJMPStart (
  IN VM_CONTEXT  *VmPtr
  )
{
  EbcDebuggerPushTraceSourceEntry ((UINT64)(UINTN)VmPtr->Ip, EfiDebuggerBranchTypeEbcJmp);
  return;
}

/**

  The hook in ExecuteJMP, after move IP.
  It will record trace information.

  @param  VmPtr - pointer to VM context.

**/
VOID
EbcDebuggerHookJMPEnd (
  IN VM_CONTEXT  *VmPtr
  )
{
  EbcDebuggerPushTraceDestEntry ((UINT64)(UINTN)VmPtr->Ip, EfiDebuggerBranchTypeEbcJmp);
  return;
}

/**

  The hook in ExecuteJMP8, before move IP.
  It will record trace information.

  @param  VmPtr - pointer to VM context.

**/
VOID
EbcDebuggerHookJMP8Start (
  IN VM_CONTEXT  *VmPtr
  )
{
  EbcDebuggerPushTraceSourceEntry ((UINT64)(UINTN)VmPtr->Ip, EfiDebuggerBranchTypeEbcJmp8);
  return;
}

/**

  The hook in ExecuteJMP8, after move IP.
  It will record trace information.

  @param  VmPtr - pointer to VM context.

**/
VOID
EbcDebuggerHookJMP8End (
  IN VM_CONTEXT  *VmPtr
  )
{
  EbcDebuggerPushTraceDestEntry ((UINT64)(UINTN)VmPtr->Ip, EfiDebuggerBranchTypeEbcJmp8);
  return;
}