00491 :
00492
00493 This function virtually unwinds
the specfified function by executing its
00494 prologue code backwards.
00495
00496 If
the function
is a leaf function, then
the address where
control left
00497
the previous frame
is obtained from
the context record. If
the function
00498
is a nested function, but not an exception or interrupt frame, then
the
00499 prologue code
is executed backwards and
the address where
control left
00500
the previous frame
is obtained from
the updated context record.
00501
00502 If
the function
is register save millicode,
it is treated as a leaf
00503 function. If
the function
is register restore millicode,
the remaining
00504 body
is executed forwards and
the address where
control left
the
00505 previous frame
is obtained from
the final blr instruction.
00506
00507 If
the function was called via glue code and
is not that glue code,
00508
the prologe of
the glue code
is executed backwards in addition to
the
00509 above actions.
00510
00511 Otherwise, an exception or interrupt entry to
the system
is being
00512 unwound and a specially coded prologue restores
the return address
00513 twice. Once from
the fault instruction address and once from
the saved
00514
return address
register. The first restore
is returned as
the function
00515 value and
the second restore
is place in
the updated context record.
00516
00517 If a context pointers record
is specified, then
the address where each
00518 nonvolatile registers
is restored from
is recorded in
the appropriate
00519 element of
the context pointers record.
00520
00521 Arguments:
00522
00523 ControlPc - Supplies
the address where
control left
the specified
00524 function.
00525
00526 FunctionEntry - Supplies
the address of
the function table entry
for the
00527 specified function or
NULL if the function
is a leaf function.
00528
00529 ContextRecord - Supplies
the address of a context record.
00530
00531 InFunction - Supplies a pointer to a variable that receives whether
the
00532
control PC
is within
the current function.
00533
00534 EstablisherFrame - Supplies a pointer to a variable that receives
the
00535
the establisher frame pointer value.
00536
00537 ContextPointers - Supplies an optional pointer to a context pointers
00538 record.
00539
00540 LowStackLimit, HighStackLimit - Range of valid values
for the stack
00541 pointer. This indicates whether
it is valid to examine NextPc.
00542
00543 Return Value:
00544
00545 The address where
control left
the previous frame
is returned as
the
00546 function value.
00547
00548 --*/
00549
00550 {
00551
ITERATOR Iterator[8];
00552
PITERATOR Piterator;
00553 ULONG Address;
00554
PDOUBLE FloatingRegister;
00555 PPC_INSTRUCTION I;
00556 PULONG IntegerRegister;
00557 ULONG NextPc, Pc;
00558 BOOLEAN RestoredLr =
FALSE;
00559 BOOLEAN RestoredSp =
FALSE;
00560 BOOLEAN ComputedSp =
FALSE;
00561 ULONG Rt;
00562
MILLICODE_INFO Info;
00563
INSTR_CLASS InstrClass;
00564
#ifdef _IMAGEHLP_SOURCE_
00565
DWORD ImagehlpCb = 0;
00566 RUNTIME_FUNCTION SavedFunctionEntry;
00567
#else
00568
ULONG EstablisherFrameValue;
00569
#endif
00570
00571
00572
00573
00574
00575 FloatingRegister = &ContextRecord->Fpr0;
00576 IntegerRegister = &ContextRecord->Gpr0;
00577
00578
00579
00580
00581
00582
if (FunctionEntry ==
NULL) {
00583
00584
00585
00586 NextPc = ContextRecord->Lr - 4;
00587
00588
00589
00590
00591
00592
if (NextPc == ControlPc)
00593
return NextPc;
00594
00595
goto CheckForGlue;
00596 }
00597
#ifdef _IMAGEHLP_SOURCE_
00598
else {
00599 SavedFunctionEntry = *FunctionEntry;
00600 FunctionEntry = &SavedFunctionEntry;
00601 }
00602
#endif
00603
00604
00605
00606
00607
00608
NOT_IMAGEHLP (*EstablisherFrame =
00609 EstablisherFrameValue = ContextRecord->Gpr1);
00610
00611
READ_ULONG (ControlPc, I.Long);
00612
if (I.Long == RETURN_INSTR) {
00613
00614
00615
00616
00617
00618
00619
NOT_IMAGEHLP(*InFunction = FALSE);
00620 NextPc = ContextRecord->Lr;
00621
goto CheckForGlue;
00622 }
00623 InstrClass =
ClassifyInstruction(&I, UnwindReverse,
00624 #ifdef _IMAGEHLP_SOURCE_
00625 hProcess, ReadMemory, FunctionTableAccess,
00626 #endif
00627 ControlPc, &Info);
00628
if (InstrClass ==
InstrRestoreCode) {
00629
00630
00631
00632
00633
00634
00635
00636 Iterator[0].
BeginPc = Info.
TargetPc;
00637 Iterator[0].
EndPc = Info.
FunctionEntry->EndAddress;
00638 Iterator[0].
Increment = 4;
00639 Iterator[0].
Intent =
UnwindForward;
00640
NOT_IMAGEHLP(*InFunction = FALSE);
00641
00642 }
else if (
00643
#ifdef FUNCTION_ENTRY_IS_IMAGE_STYLE
00644
(FunctionEntry->BeginAddress -
00645 FunctionEntry->PrologEndAddress) == 2
00646
#else
00647
FunctionEntry->ExceptionHandler == 0 &&
00648 (ULONG)FunctionEntry->HandlerData == 2
00649
#endif
00650
) {
00651
00652
00653
00654
00655
00656 Iterator[0].
BeginPc = ControlPc;
00657 Iterator[0].
EndPc = FunctionEntry->EndAddress;
00658 Iterator[0].
Increment = 4;
00659 Iterator[0].
Intent =
UnwindForward;
00660
NOT_IMAGEHLP(*InFunction = FALSE);
00661
00662 }
else {
00663
00664
00665
00666
00667
00668
00669
if (InstrClass ==
InstrTOCRestore) {
00670 PPC_INSTRUCTION Iprev;
00671
READ_ULONG (ControlPc - 4, Iprev.Long);
00672
if (
ClassifyInstruction (&Iprev, UnwindReverse,
00673 #ifdef _IMAGEHLP_SOURCE_
00674 hProcess, ReadMemory, FunctionTableAccess,
00675 #endif
00676 ControlPc - 4, &Info) ==
InstrGlue) {
00677
00678
00679
00680
00681
00682
00683 ControlPc += 4;
00684 Address = IntegerRegister[I.Dform_RA] + I.Dform_D;
00685 Rt = I.Dform_RT;
00686
READ_ULONG (Address, IntegerRegister[Rt]);
00687
if (ARGUMENT_PRESENT (ContextPointers))
00688 ContextPointers->IntegerContext[Rt] = (PULONG) Address;
00689 }
00690 }
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700 Iterator[0].
EndPc = FunctionEntry->BeginAddress - 4;
00701 Iterator[0].
Increment = -4;
00702 Iterator[0].
Intent =
UnwindReverse;
00703
if ((ControlPc < FunctionEntry->BeginAddress) ||
00704 (ControlPc >= (FunctionEntry->PrologEndAddress & ~3))) {
00705
NOT_IMAGEHLP(*InFunction = TRUE);
00706 Iterator[0].
BeginPc = ((FunctionEntry->PrologEndAddress & ~3) - 4);
00707 }
else {
00708
NOT_IMAGEHLP(*InFunction = FALSE);
00709 Iterator[0].
BeginPc = ControlPc - 4;
00710 }
00711 }
00712
00713
00714
00715
00716
00717 NextPc = ContextRecord->Lr - 4;
00718 UnwindGlue:
00719
for (Piterator = Iterator; Piterator >= Iterator; Piterator--) {
00720
for (Pc = Piterator->
BeginPc;
00721 Pc != Piterator->
EndPc;
00722 Pc += Piterator->
Increment) {
00723
00724
READ_ULONG (Pc, I.Long);
00725 Address = IntegerRegister[I.Dform_RA] + I.Dform_D;
00726 Rt = I.Dform_RT;
00727
switch (
ClassifyInstruction (&I, Piterator->
Intent,
00728 #ifdef _IMAGEHLP_SOURCE_
00729 hProcess, ReadMemory, FunctionTableAccess,
00730 #endif
00731 Pc, &Info)) {
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
case InstrMFLR:
00751 ContextRecord->Lr = IntegerRegister[Rt];
00752
if ( RestoredLr ==
FALSE ) {
00753 NextPc = ContextRecord->Lr - 4;
00754 RestoredLr =
TRUE;
00755 }
else {
00756 NextPc += 4;
00757 }
00758
continue;
00759
00760
00761
00762
00763
case InstrBLR:
00764 NextPc = ContextRecord->Lr - 4;
00765
break;
00766
00767
00768
00769
00770
case InstrMFCR:
00771 ContextRecord->Cr = IntegerRegister[Rt];
00772
continue;
00773
00774
00775
00776
00777
case InstrSTW:
00778
00779
00780
00781
00782
00783
00784
READ_ULONG (Address, IntegerRegister[Rt]);
00785
if (ARGUMENT_PRESENT (ContextPointers))
00786 ContextPointers->IntegerContext[Rt] = (PULONG) Address;
00787
continue;
00788
00789
00790
00791
00792
00793
00794
case InstrSTWU:
00795 Address = IntegerRegister[
GPR1];
00796
READ_ULONG(Address,IntegerRegister[GPR1]);
00797
if (RestoredSp ==
FALSE) {
00798
NOT_IMAGEHLP (*EstablisherFrame =
00799 EstablisherFrameValue = ContextRecord->Gpr1);
00800 RestoredSp =
TRUE;
00801 }
00802
if (ARGUMENT_PRESENT (ContextPointers))
00803 ContextPointers->IntegerContext[Rt] = (PULONG) Address;
00804
continue;
00805
00806
00807
00808
00809
case InstrSTFD:
00810
READ_DOUBLE (Address, FloatingRegister[Rt]);
00811
if (ARGUMENT_PRESENT (ContextPointers))
00812 ContextPointers->FloatingContext[Rt] = (
PDOUBLE) Address;
00813
continue;
00814
00815
00816
00817
00818
case InstrMR:
00819 IntegerRegister[I.Xform_RA] = IntegerRegister[Rt];
00820
continue;
00821
case InstrMRfwd:
00822 IntegerRegister[Rt] = IntegerRegister[I.Xform_RA];
00823
continue;
00824
case InstrMRr12:
00825 IntegerRegister[Rt] = IntegerRegister[I.Xform_RA];
00826
break;
00827
00828
00829
00830
00831
case InstrADDIfwd:
00832 IntegerRegister[Rt] = Address;
00833
continue;
00834
case InstrADDIr12:
00835
if (!ComputedSp) {
00836
00837
00838 IntegerRegister[Rt] = IntegerRegister[
GPR1];
00839 }
00840 IntegerRegister[Rt] += I.Dform_SI;
00841
break;
00842
00843
00844
00845
00846
case InstrSTWUr12:
00847 ComputedSp =
TRUE;
00848 Address = IntegerRegister[
GPR1];
00849
READ_ULONG(Address,IntegerRegister[GPR12]);
00850
continue;
00851
00852
00853
00854
00855
case InstrSaveCode:
00856
00857
00858
00859
00860 Piterator++;
00861 Piterator->
BeginPc = Info.
FunctionEntry->EndAddress - 4;
00862 Piterator->
EndPc = Info.
TargetPc - 4;
00863 Piterator->
Increment = -4;
00864 Piterator->
Intent =
UnwindReverseR12;
00865
00866
00867
00868 Piterator++;
00869 Piterator->
BeginPc = Pc - 4;
00870 Piterator->
EndPc = Piterator[-2].
EndPc;
00871 Piterator->
Increment = -4;
00872 Piterator->
Intent =
UnwindR12;
00873 ComputedSp =
FALSE;
00874
00875
00876
00877
00878 Piterator[-2].
BeginPc = Pc - 4;
00879 Piterator++;
00880
break;
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
case InstrGlue:
00892
00893
00894
00895
00896
00897
00898
00899
00900
if (Piterator - Iterator + 4
00901 >
sizeof (Iterator) /
sizeof (Iterator[0]))
00902
return 0;
00903
00904
00905
00906
00907
00908
00909 Piterator++;
00910 Piterator->
BeginPc
00911 = (Info.
FunctionEntry->PrologEndAddress & ~3) - 4;
00912 Piterator->
EndPc = Info.
FunctionEntry->BeginAddress - 4;
00913 Piterator->
Increment = -4;
00914 Piterator->
Intent =
UnwindReverse;
00915
00916
00917
00918
00919 Piterator[-1].
BeginPc = Pc - 4;
00920 Piterator++;
00921
break;
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
case InstrSetEstablisher:
00932
NOT_IMAGEHLP (*EstablisherFrame =
00933 EstablisherFrameValue = ContextRecord->Gpr1);
00934
continue;
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
case InstrIgnore:
00945
default:
00946
continue;
00947 }
00948
break;
00949 }
00950 }
00951
00952 CheckForGlue:
00953
00954
00955
00956
00957
00958
00959
if (NextPc == 0 || NextPc + 4 == 0
00960
#ifdef _IMAGEHLP_SOURCE_
00961
|| NextPc == 1
00962
#else
00963
|| EstablisherFrameValue < LowStackLimit
00964 || EstablisherFrameValue > HighStackLimit
00965 || (EstablisherFrameValue & 0x7) != 0
00966
#endif
00967
)
00968
return NextPc;
00969
00970
00971
00972
00973
#ifdef _IMAGEHLP_SOURCE_
00974
READ_ULONG (NextPc, I.Long);
00975
#else
00976
if ( !
NT_SUCCESS(
TryReadUlong(NextPc, &I.Long)) ) {
00977
return NextPc;
00978 }
00979
#endif
00980
if (I.Primary_Op != B_OP)
00981
return NextPc;
00982
00983
00984
00985
00986
00987 Pc = ((LONG)(I.Iform_LI) << 2) + (I.Iform_AA ? 0 : NextPc);
00988
00989
00990
00991
00992
00993
00994
if (FunctionEntry !=
NULL
00995 && Pc >= FunctionEntry->BeginAddress
00996 && Pc < FunctionEntry->EndAddress)
00997
return NextPc;
00998
00999
01000
01001
01002
01003
01004
if ((FunctionEntry = (PRUNTIME_FUNCTION)
RtlLookupFunctionEntry(Pc)) !=
NULL) {
01005
01006
01007
01008
READ_ULONG (Pc, I.Long);
01009
if (I.Primary_Op == LWZ_OP && I.Dform_RA ==
GPR2) {
01010
01011
01012
01013
READ_ULONG (Pc + 4, I.Long);
01014
if (I.Primary_Op == B_OP && I.Iform_LK) {
01015
01016
01017
01018
01019 Pc = ((LONG)(I.Iform_LI) << 2) + (I.Iform_AA ? 0 : Pc + 4);
01020 FunctionEntry = (PRUNTIME_FUNCTION)
RtlLookupFunctionEntry(Pc);
01021 }
01022 }
01023 }
01024
01025
01026
01027
01028
if (!(FunctionEntry !=
NULL
01029
#ifndef FUNCTION_ENTRY_IS_IMAGE_STYLE
01030
&& FunctionEntry->ExceptionHandler == 0
01031 && (ULONG)FunctionEntry->HandlerData == 3
01032
#else
01033
&& (FunctionEntry->BeginAddress <
01034 FunctionEntry->PrologEndAddress)
01035 && (FunctionEntry->PrologEndAddress & 3) == 1
01036
#endif
01037
))
01038
return NextPc;
01039
01040
01041
01042
01043
01044
01045
#ifdef _IMAGEHLP_SOURCE_
01046
SavedFunctionEntry = *FunctionEntry;
01047 FunctionEntry = &SavedFunctionEntry;
01048
#endif
01049
Iterator[0].EndPc = FunctionEntry->BeginAddress - 4;
01050 Iterator[0].Increment = -4;
01051 Iterator[0].Intent =
UnwindReverse;
01052 Iterator[0].BeginPc = ((FunctionEntry->PrologEndAddress & ~3) - 4);
01053
goto UnwindGlue;
01054 }