OLD | NEW |
| (Empty) |
1 // Copyright 2006-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 #include "exception_utils.h" | |
17 | |
18 | |
19 // Capture a CONTEXT that represents machine state at callsite | |
20 // TODO(omaha): 64 bit platforms can pass through to | |
21 // RtlCaptureContext (or can they?) | |
22 __declspec(naked) PCONTEXT WINAPI CaptureContext(PCONTEXT runner) { | |
23 runner; // unreferenced formal parameter | |
24 __asm { | |
25 // set up a call frame | |
26 push ebp | |
27 mov ebp, esp | |
28 | |
29 // save ecx for later | |
30 push ecx | |
31 | |
32 // fetch the context record pointer argument into ecx | |
33 // which we use as pointer to context throughout the rest | |
34 // of this function | |
35 mov ecx, DWORD PTR [ebp + 8] | |
36 | |
37 // set flags | |
38 mov [ecx]CONTEXT.ContextFlags, CONTEXT_SEGMENTS | CONTEXT_INTEGER | \ | |
39 CONTEXT_CONTROL | CONTEXT_FLOATING_POINT | |
40 | |
41 // stash the integer registers away | |
42 mov [ecx]CONTEXT.Edi, edi | |
43 mov [ecx]CONTEXT.Ebx, ebx | |
44 mov [ecx]CONTEXT.Edx, edx | |
45 mov [ecx]CONTEXT.Eax, eax | |
46 mov [ecx]CONTEXT.Esi, esi | |
47 // get the saved ecx | |
48 pop eax | |
49 mov [ecx]CONTEXT.Ecx, eax | |
50 | |
51 // now control registers | |
52 pushfd | |
53 pop eax | |
54 mov [ecx]CONTEXT.EFlags, eax | |
55 | |
56 // get the old ebp, our FP points to it | |
57 mov eax, [ebp] | |
58 mov [ecx]CONTEXT.Ebp, eax | |
59 | |
60 // get return address and record as eip | |
61 mov eax, [ebp + 4] | |
62 mov [ecx]CONTEXT.Eip, eax | |
63 | |
64 // esp post-return is ... | |
65 lea eax, [ebp + 0xC] | |
66 mov [ecx]CONTEXT.Esp, eax | |
67 | |
68 // snarf segment registers | |
69 mov word ptr [ecx]CONTEXT.SegSs, ss | |
70 mov word ptr [ecx]CONTEXT.SegCs, cs | |
71 mov word ptr [ecx]CONTEXT.SegGs, gs | |
72 mov word ptr [ecx]CONTEXT.SegFs, fs | |
73 mov word ptr [ecx]CONTEXT.SegEs, es | |
74 mov word ptr [ecx]CONTEXT.SegDs, ds | |
75 | |
76 // and lastly grab floating point state | |
77 fnsave [ecx]CONTEXT.FloatSave | |
78 | |
79 // return the CONTEXT pointer | |
80 mov eax, ecx | |
81 | |
82 // and return | |
83 pop ebp | |
84 ret 4 | |
85 } | |
86 } | |
OLD | NEW |