Commit | Line | Data |
---|---|---|
53e95fcf JS |
1 | /* Target-dependent code for Linux running on x86-64, for GDB. |
2 | Copyright 2001 | |
3 | Free Software Foundation, Inc. | |
4 | Contributed by Jiri Smid, SuSE Labs. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
22 | ||
23 | #include "defs.h" | |
24 | #include "inferior.h" | |
25 | #include "gdbcore.h" | |
26 | #include "regcache.h" | |
27 | #include "x86-64-tdep.h" | |
28 | #include "dwarf2cfi.h" | |
29 | ||
30 | #define STRUCT_OFFSET(struct_type, member) \ | |
31 | ((long) ((char*) &((struct_type*) 0)->member)) | |
32 | ||
33 | #define LINUX_SIGTRAMP_INSN0 (0x48) /* mov $NNNNNNNN,%rax */ | |
34 | #define LINUX_SIGTRAMP_OFFSET0 (0) | |
35 | #define LINUX_SIGTRAMP_INSN1 (0x0f) /* syscall */ | |
36 | #define LINUX_SIGTRAMP_OFFSET1 (7) | |
37 | ||
38 | static const unsigned char linux_sigtramp_code[] = { | |
39 | LINUX_SIGTRAMP_INSN0, 0xc7, 0xc0, 0x89, 0x00, 0x00, 0x00, /* mov $0x89,%rax */ | |
40 | LINUX_SIGTRAMP_INSN1, 0x05 /* syscall */ | |
41 | }; | |
42 | ||
43 | #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code) | |
44 | ||
45 | /* If PC is in a sigtramp routine, return the address of the start of | |
46 | the routine. Otherwise, return 0. */ | |
47 | ||
48 | static CORE_ADDR | |
49 | x86_64_linux_sigtramp_start (CORE_ADDR pc) | |
50 | { | |
51 | unsigned char buf[LINUX_SIGTRAMP_LEN]; | |
52 | if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0) | |
53 | return 0; | |
54 | ||
55 | if (buf[0] != LINUX_SIGTRAMP_INSN0) | |
56 | { | |
57 | if (buf[0] != LINUX_SIGTRAMP_INSN1) | |
58 | return 0; | |
59 | ||
60 | pc -= LINUX_SIGTRAMP_OFFSET1; | |
61 | ||
62 | if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0) | |
63 | return 0; | |
64 | } | |
65 | ||
66 | if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0) | |
67 | return 0; | |
68 | ||
69 | return pc; | |
70 | } | |
71 | ||
72 | /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */ | |
73 | #define LINUX_UCONTEXT_SIGCONTEXT_OFFSET (36) | |
74 | ||
75 | /* Assuming FRAME is for a Linux sigtramp routine, return the address | |
76 | of the associated sigcontext structure. */ | |
77 | CORE_ADDR | |
78 | x86_64_linux_sigcontext_addr (struct frame_info *frame) | |
79 | { | |
80 | CORE_ADDR pc; | |
81 | ||
82 | pc = x86_64_linux_sigtramp_start (frame->pc); | |
83 | if (pc) | |
84 | { | |
85 | if (frame->next) | |
86 | /* If this isn't the top frame, the next frame must be for the | |
87 | signal handler itself. The sigcontext structure is part of | |
88 | the user context. */ | |
89 | return frame->next->frame + sizeof (struct siginfo) + | |
90 | LINUX_UCONTEXT_SIGCONTEXT_OFFSET; | |
91 | ||
92 | ||
93 | /* This is the top frame. */ | |
94 | return read_register (SP_REGNUM) + sizeof (struct siginfo) + | |
95 | LINUX_UCONTEXT_SIGCONTEXT_OFFSET; | |
96 | ||
97 | } | |
98 | ||
99 | error ("Couldn't recognize signal trampoline."); | |
100 | return 0; | |
101 | } | |
102 | ||
103 | /* Offset to saved PC in sigcontext, from <asm/sigcontext.h>. */ | |
104 | #define LINUX_SIGCONTEXT_PC_OFFSET (136) | |
105 | ||
106 | /* Assuming FRAME is for a Linux sigtramp routine, return the saved | |
107 | program counter. */ | |
108 | ||
109 | CORE_ADDR | |
110 | x86_64_linux_sigtramp_saved_pc (struct frame_info *frame) | |
111 | { | |
112 | CORE_ADDR addr; | |
113 | ||
114 | addr = x86_64_linux_sigcontext_addr (frame); | |
115 | return read_memory_integer (addr + LINUX_SIGCONTEXT_PC_OFFSET, 8); | |
116 | } | |
117 | ||
118 | /* Immediately after a function call, return the saved pc. */ | |
119 | ||
120 | CORE_ADDR | |
121 | x86_64_linux_saved_pc_after_call (struct frame_info *frame) | |
122 | { | |
123 | if (frame->signal_handler_caller) | |
124 | return x86_64_linux_sigtramp_saved_pc (frame); | |
125 | ||
126 | return read_memory_integer (read_register (SP_REGNUM), 8); | |
127 | } | |
128 | ||
129 | /* Saved Pc. Get it from sigcontext if within sigtramp. */ | |
130 | CORE_ADDR | |
131 | x86_64_linux_frame_saved_pc (struct frame_info *frame) | |
132 | { | |
133 | if (frame->signal_handler_caller) | |
134 | return x86_64_linux_sigtramp_saved_pc (frame); | |
135 | return cfi_get_ra (frame); | |
136 | } |