1 /* Intel 386 target-dependent stuff.
2 Copyright (C) 1988, 1989, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #ifdef USE_PROC_FS /* Target dependent support for /proc */
26 #include <sys/procfs.h>
30 i386_get_frame_setup
PARAMS ((int));
33 i386_follow_jump
PARAMS ((void));
36 codestream_read
PARAMS ((unsigned char *, int));
39 codestream_seek
PARAMS ((int));
42 codestream_fill
PARAMS ((int));
44 /* helper functions for tm-i386.h */
46 /* Stdio style buffering was used to minimize calls to ptrace, but this
47 buffering did not take into account that the code section being accessed
48 may not be an even number of buffers long (even if the buffer is only
49 sizeof(int) long). In cases where the code section size happened to
50 be a non-integral number of buffers long, attempting to read the last
51 buffer would fail. Simply using target_read_memory and ignoring errors,
52 rather than read_memory, is not the correct solution, since legitimate
53 access errors would then be totally ignored. To properly handle this
54 situation and continue to use buffering would require that this code
55 be able to determine the minimum code section size granularity (not the
56 alignment of the section itself, since the actual failing case that
57 pointed out this problem had a section alignment of 4 but was not a
58 multiple of 4 bytes long), on a target by target basis, and then
59 adjust it's buffer size accordingly. This is messy, but potentially
60 feasible. It probably needs the bfd library's help and support. For
61 now, the buffer size is set to 1. (FIXME -fnf) */
63 #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
64 static CORE_ADDR codestream_next_addr
;
65 static CORE_ADDR codestream_addr
;
66 static unsigned char codestream_buf
[CODESTREAM_BUFSIZ
];
67 static int codestream_off
;
68 static int codestream_cnt
;
70 #define codestream_tell() (codestream_addr + codestream_off)
71 #define codestream_peek() (codestream_cnt == 0 ? \
72 codestream_fill(1): codestream_buf[codestream_off])
73 #define codestream_get() (codestream_cnt-- == 0 ? \
74 codestream_fill(0) : codestream_buf[codestream_off++])
77 codestream_fill (peek_flag
)
80 codestream_addr
= codestream_next_addr
;
81 codestream_next_addr
+= CODESTREAM_BUFSIZ
;
83 codestream_cnt
= CODESTREAM_BUFSIZ
;
84 read_memory (codestream_addr
,
85 (unsigned char *)codestream_buf
,
89 return (codestream_peek());
91 return (codestream_get());
95 codestream_seek (place
)
98 codestream_next_addr
= place
/ CODESTREAM_BUFSIZ
;
99 codestream_next_addr
*= CODESTREAM_BUFSIZ
;
102 while (codestream_tell() != place
)
107 codestream_read (buf
, count
)
114 for (i
= 0; i
< count
; i
++)
115 *p
++ = codestream_get ();
118 /* next instruction is a jump, move to target */
129 pos
= codestream_tell ();
132 if (codestream_peek () == 0x66)
138 switch (codestream_get ())
141 /* relative jump: if data16 == 0, disp32, else disp16 */
144 codestream_read ((unsigned char *)&short_delta
, 2);
146 /* include size of jmp inst (including the 0x66 prefix). */
147 pos
+= short_delta
+ 4;
151 codestream_read ((unsigned char *)&long_delta
, 4);
152 pos
+= long_delta
+ 5;
156 /* relative jump, disp8 (ignore data16) */
157 codestream_read ((unsigned char *)&byte_delta
, 1);
158 pos
+= byte_delta
+ 2;
161 codestream_seek (pos
);
165 * find & return amound a local space allocated, and advance codestream to
166 * first register push (if any)
168 * if entry sequence doesn't make sense, return -1, and leave
169 * codestream pointer random
173 i386_get_frame_setup (pc
)
178 codestream_seek (pc
);
182 op
= codestream_get ();
184 if (op
== 0x58) /* popl %eax */
187 * this function must start with
190 * xchgl %eax, (%esp) 0x87 0x04 0x24
191 * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
193 * (the system 5 compiler puts out the second xchg
194 * inst, and the assembler doesn't try to optimize it,
195 * so the 'sib' form gets generated)
197 * this sequence is used to get the address of the return
198 * buffer for a function that returns a structure
201 unsigned char buf
[4];
202 static unsigned char proto1
[3] = { 0x87,0x04,0x24 };
203 static unsigned char proto2
[4] = { 0x87,0x44,0x24,0x00 };
204 pos
= codestream_tell ();
205 codestream_read (buf
, 4);
206 if (bcmp (buf
, proto1
, 3) == 0)
208 else if (bcmp (buf
, proto2
, 4) == 0)
211 codestream_seek (pos
);
212 op
= codestream_get (); /* update next opcode */
215 if (op
== 0x55) /* pushl %ebp */
217 /* check for movl %esp, %ebp - can be written two ways */
218 switch (codestream_get ())
221 if (codestream_get () != 0xec)
225 if (codestream_get () != 0xe5)
231 /* check for stack adjustment
235 * note: you can't subtract a 16 bit immediate
236 * from a 32 bit reg, so we don't have to worry
237 * about a data16 prefix
239 op
= codestream_peek ();
242 /* subl with 8 bit immed */
244 if (codestream_get () != 0xec)
245 /* Some instruction starting with 0x83 other than subl. */
247 codestream_seek (codestream_tell () - 2);
250 /* subl with signed byte immediate
251 * (though it wouldn't make sense to be negative)
253 return (codestream_get());
257 /* subl with 32 bit immed */
260 if (codestream_get () != 0xec)
261 /* Some instruction starting with 0x81 other than subl. */
263 codestream_seek (codestream_tell () - 2);
266 /* subl with 32 bit immediate */
267 codestream_read ((unsigned char *)&locals
, 4);
268 SWAP_TARGET_AND_HOST (&locals
, 4);
278 /* enter instruction: arg is 16 bit unsigned immed */
279 unsigned short slocals
;
280 codestream_read ((unsigned char *)&slocals
, 2);
281 SWAP_TARGET_AND_HOST (&slocals
, 2);
282 codestream_get (); /* flush final byte of enter instruction */
288 /* Return number of args passed to a frame.
289 Can return -1, meaning no way to tell. */
291 /* on the 386, the instruction following the call could be:
292 * popl %ecx - one arg
293 * addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
294 * anything else - zero args
298 i386_frame_num_args (fi
)
299 struct frame_info
*fi
;
303 struct frame_info
*pfi
;
307 FRAMELESS_FUNCTION_INVOCATION (fi
, frameless
);
309 /* In the absence of a frame pointer, GDB doesn't get correct values
310 for nameless arguments. Return -1, so it doesn't print any
311 nameless arguments. */
314 pfi
= get_prev_frame_info (fi
);
317 /* Note: this can happen if we are looking at the frame for
318 main, because FRAME_CHAIN_VALID won't let us go into
319 start. If we have debugging symbols, that's not really
320 a big deal; it just means it will only show as many arguments
321 to main as are declared. */
327 op
= read_memory_integer (retpc
, 1);
333 op
= read_memory_integer (retpc
+1, 1);
335 /* addl $<signed imm 8 bits>, %esp */
336 return (read_memory_integer (retpc
+2,1)&0xff)/4;
341 { /* add with 32 bit immediate */
342 op
= read_memory_integer (retpc
+1, 1);
344 /* addl $<imm 32>, %esp */
345 return read_memory_integer (retpc
+2, 4) / 4;
357 * parse the first few instructions of the function to see
358 * what registers were stored.
360 * We handle these cases:
362 * The startup sequence can be at the start of the function,
363 * or the function can start with a branch to startup code at the end.
365 * %ebp can be set up with either the 'enter' instruction, or
366 * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
367 * but was once used in the sys5 compiler)
369 * Local space is allocated just below the saved %ebp by either the
370 * 'enter' instruction, or by 'subl $<size>, %esp'. 'enter' has
371 * a 16 bit unsigned argument for space to allocate, and the
372 * 'addl' instruction could have either a signed byte, or
375 * Next, the registers used by this function are pushed. In
376 * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
377 * (and sometimes a harmless bug causes it to also save but not restore %eax);
378 * however, the code below is willing to see the pushes in any order,
379 * and will handle up to 8 of them.
381 * If the setup sequence is at the end of the function, then the
382 * next instruction will be a branch back to the start.
386 i386_frame_find_saved_regs (fip
, fsrp
)
387 struct frame_info
*fip
;
388 struct frame_saved_regs
*fsrp
;
393 CORE_ADDR dummy_bottom
;
397 bzero (fsrp
, sizeof *fsrp
);
399 /* if frame is the end of a dummy, compute where the
402 dummy_bottom
= fip
->frame
- 4 - REGISTER_BYTES
- CALL_DUMMY_LENGTH
;
404 /* check if the PC is in the stack, in a dummy frame */
405 if (dummy_bottom
<= fip
->pc
&& fip
->pc
<= fip
->frame
)
407 /* all regs were saved by push_call_dummy () */
409 for (i
= 0; i
< NUM_REGS
; i
++)
411 adr
-= REGISTER_RAW_SIZE (i
);
417 locals
= i386_get_frame_setup (get_pc_function_start (fip
->pc
));
421 adr
= fip
->frame
- 4 - locals
;
422 for (i
= 0; i
< 8; i
++)
424 op
= codestream_get ();
425 if (op
< 0x50 || op
> 0x57)
427 fsrp
->regs
[op
- 0x50] = adr
;
432 fsrp
->regs
[PC_REGNUM
] = fip
->frame
+ 4;
433 fsrp
->regs
[FP_REGNUM
] = fip
->frame
;
436 /* return pc of first real instruction */
439 i386_skip_prologue (pc
)
445 if (i386_get_frame_setup (pc
) < 0)
448 /* found valid frame setup - codestream now points to
449 * start of push instructions for saving registers
452 /* skip over register saves */
453 for (i
= 0; i
< 8; i
++)
455 op
= codestream_peek ();
456 /* break if not pushl inst */
457 if (op
< 0x50 || op
> 0x57)
464 return (codestream_tell ());
468 i386_push_dummy_frame ()
470 CORE_ADDR sp
= read_register (SP_REGNUM
);
472 char regbuf
[MAX_REGISTER_RAW_SIZE
];
474 sp
= push_word (sp
, read_register (PC_REGNUM
));
475 sp
= push_word (sp
, read_register (FP_REGNUM
));
476 write_register (FP_REGNUM
, sp
);
477 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
479 read_register_gen (regnum
, regbuf
);
480 sp
= push_bytes (sp
, regbuf
, REGISTER_RAW_SIZE (regnum
));
482 write_register (SP_REGNUM
, sp
);
488 FRAME frame
= get_current_frame ();
491 struct frame_saved_regs fsr
;
492 struct frame_info
*fi
;
493 char regbuf
[MAX_REGISTER_RAW_SIZE
];
495 fi
= get_frame_info (frame
);
497 get_frame_saved_regs (fi
, &fsr
);
498 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
501 adr
= fsr
.regs
[regnum
];
504 read_memory (adr
, regbuf
, REGISTER_RAW_SIZE (regnum
));
505 write_register_bytes (REGISTER_BYTE (regnum
), regbuf
,
506 REGISTER_RAW_SIZE (regnum
));
509 write_register (FP_REGNUM
, read_memory_integer (fp
, 4));
510 write_register (PC_REGNUM
, read_memory_integer (fp
+ 4, 4));
511 write_register (SP_REGNUM
, fp
+ 8);
512 flush_cached_frames ();
513 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),
517 #ifdef USE_PROC_FS /* Target dependent support for /proc */
519 /* The /proc interface divides the target machine's register set up into
520 two different sets, the general register set (gregset) and the floating
521 point register set (fpregset). For each set, there is an ioctl to get
522 the current register set and another ioctl to set the current values.
524 The actual structure passed through the ioctl interface is, of course,
525 naturally machine dependent, and is different for each set of registers.
526 For the i386 for example, the general register set is typically defined
529 typedef int gregset_t[19]; (in <sys/regset.h>)
531 #define GS 0 (in <sys/reg.h>)
537 and the floating point set by:
539 typedef struct fpregset
543 struct fpchip_state // fp extension state //
545 int state[27]; // 287/387 saved state //
546 int status; // status word saved at exception //
548 struct fp_emul_space // for emulators //
553 int f_fpregs[62]; // union of the above //
555 long f_wregs[33]; // saved weitek state //
558 These routines provide the packing and unpacking of gregset_t and
559 fpregset_t formatted data.
563 /* This is a duplicate of the table in i386-xdep.c. */
565 static int regmap
[] =
574 /* Given a pointer to a general register set in /proc format (gregset_t *),
575 unpack the register contents and supply them as gdb's idea of the current
579 supply_gregset (gregsetp
)
583 register greg_t
*regp
= (greg_t
*) gregsetp
;
586 for (regno
= 0 ; regno
< NUM_REGS
; regno
++)
588 supply_register (regno
, (char *) (regp
+ regmap
[regno
]));
593 fill_gregset (gregsetp
, regno
)
598 register greg_t
*regp
= (greg_t
*) gregsetp
;
599 extern char registers
[];
602 for (regi
= 0 ; regi
< NUM_REGS
; regi
++)
604 if ((regno
== -1) || (regno
== regi
))
606 *(regp
+ regmap
[regno
]) = *(int *) ®isters
[REGISTER_BYTE (regi
)];
611 #if defined (FP0_REGNUM)
613 /* Given a pointer to a floating point register set in /proc format
614 (fpregset_t *), unpack the register contents and supply them as gdb's
615 idea of the current floating point register values. */
618 supply_fpregset (fpregsetp
)
619 fpregset_t
*fpregsetp
;
623 /* FIXME: see m68k-tdep.c for an example, for the m68k. */
626 /* Given a pointer to a floating point register set in /proc format
627 (fpregset_t *), update the register specified by REGNO from gdb's idea
628 of the current floating point register set. If REGNO is -1, update
632 fill_fpregset (fpregsetp
, regno
)
633 fpregset_t
*fpregsetp
;
639 extern char registers
[];
641 /* FIXME: see m68k-tdep.c for an example, for the m68k. */
644 #endif /* defined (FP0_REGNUM) */
646 #endif /* USE_PROC_FS */