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. */
26 #ifdef USE_PROC_FS /* Target dependent support for /proc */
27 #include <sys/procfs.h>
31 i386_get_frame_setup PARAMS ((int));
34 i386_follow_jump PARAMS ((void));
37 codestream_read PARAMS ((unsigned char *, int));
40 codestream_seek PARAMS ((int));
43 codestream_fill PARAMS ((int));
45 /* helper functions for tm-i386.h */
47 /* Stdio style buffering was used to minimize calls to ptrace, but this
48 buffering did not take into account that the code section being accessed
49 may not be an even number of buffers long (even if the buffer is only
50 sizeof(int) long). In cases where the code section size happened to
51 be a non-integral number of buffers long, attempting to read the last
52 buffer would fail. Simply using target_read_memory and ignoring errors,
53 rather than read_memory, is not the correct solution, since legitimate
54 access errors would then be totally ignored. To properly handle this
55 situation and continue to use buffering would require that this code
56 be able to determine the minimum code section size granularity (not the
57 alignment of the section itself, since the actual failing case that
58 pointed out this problem had a section alignment of 4 but was not a
59 multiple of 4 bytes long), on a target by target basis, and then
60 adjust it's buffer size accordingly. This is messy, but potentially
61 feasible. It probably needs the bfd library's help and support. For
62 now, the buffer size is set to 1. (FIXME -fnf) */
64 #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
65 static CORE_ADDR codestream_next_addr;
66 static CORE_ADDR codestream_addr;
67 static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
68 static int codestream_off;
69 static int codestream_cnt;
71 #define codestream_tell() (codestream_addr + codestream_off)
72 #define codestream_peek() (codestream_cnt == 0 ? \
73 codestream_fill(1): codestream_buf[codestream_off])
74 #define codestream_get() (codestream_cnt-- == 0 ? \
75 codestream_fill(0) : codestream_buf[codestream_off++])
78 codestream_fill (peek_flag)
81 codestream_addr = codestream_next_addr;
82 codestream_next_addr += CODESTREAM_BUFSIZ;
84 codestream_cnt = CODESTREAM_BUFSIZ;
85 read_memory (codestream_addr,
86 (unsigned char *)codestream_buf,
90 return (codestream_peek());
92 return (codestream_get());
96 codestream_seek (place)
99 codestream_next_addr = place / CODESTREAM_BUFSIZ;
100 codestream_next_addr *= CODESTREAM_BUFSIZ;
103 while (codestream_tell() != place)
108 codestream_read (buf, count)
115 for (i = 0; i < count; i++)
116 *p++ = codestream_get ();
119 /* next instruction is a jump, move to target */
130 pos = codestream_tell ();
133 if (codestream_peek () == 0x66)
139 switch (codestream_get ())
142 /* relative jump: if data16 == 0, disp32, else disp16 */
145 codestream_read ((unsigned char *)&short_delta, 2);
147 /* include size of jmp inst (including the 0x66 prefix). */
148 pos += short_delta + 4;
152 codestream_read ((unsigned char *)&long_delta, 4);
153 pos += long_delta + 5;
157 /* relative jump, disp8 (ignore data16) */
158 codestream_read ((unsigned char *)&byte_delta, 1);
159 pos += byte_delta + 2;
162 codestream_seek (pos);
166 * find & return amound a local space allocated, and advance codestream to
167 * first register push (if any)
169 * if entry sequence doesn't make sense, return -1, and leave
170 * codestream pointer random
174 i386_get_frame_setup (pc)
179 codestream_seek (pc);
183 op = codestream_get ();
185 if (op == 0x58) /* popl %eax */
188 * this function must start with
191 * xchgl %eax, (%esp) 0x87 0x04 0x24
192 * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
194 * (the system 5 compiler puts out the second xchg
195 * inst, and the assembler doesn't try to optimize it,
196 * so the 'sib' form gets generated)
198 * this sequence is used to get the address of the return
199 * buffer for a function that returns a structure
202 unsigned char buf[4];
203 static unsigned char proto1[3] = { 0x87,0x04,0x24 };
204 static unsigned char proto2[4] = { 0x87,0x44,0x24,0x00 };
205 pos = codestream_tell ();
206 codestream_read (buf, 4);
207 if (memcmp (buf, proto1, 3) == 0)
209 else if (memcmp (buf, proto2, 4) == 0)
212 codestream_seek (pos);
213 op = codestream_get (); /* update next opcode */
216 if (op == 0x55) /* pushl %ebp */
218 /* check for movl %esp, %ebp - can be written two ways */
219 switch (codestream_get ())
222 if (codestream_get () != 0xec)
226 if (codestream_get () != 0xe5)
232 /* check for stack adjustment
236 * note: you can't subtract a 16 bit immediate
237 * from a 32 bit reg, so we don't have to worry
238 * about a data16 prefix
240 op = codestream_peek ();
243 /* subl with 8 bit immed */
245 if (codestream_get () != 0xec)
246 /* Some instruction starting with 0x83 other than subl. */
248 codestream_seek (codestream_tell () - 2);
251 /* subl with signed byte immediate
252 * (though it wouldn't make sense to be negative)
254 return (codestream_get());
258 /* subl with 32 bit immed */
261 if (codestream_get () != 0xec)
262 /* Some instruction starting with 0x81 other than subl. */
264 codestream_seek (codestream_tell () - 2);
267 /* subl with 32 bit immediate */
268 codestream_read ((unsigned char *)&locals, 4);
269 SWAP_TARGET_AND_HOST (&locals, 4);
279 /* enter instruction: arg is 16 bit unsigned immed */
280 unsigned short slocals;
281 codestream_read ((unsigned char *)&slocals, 2);
282 SWAP_TARGET_AND_HOST (&slocals, 2);
283 codestream_get (); /* flush final byte of enter instruction */
289 /* Return number of args passed to a frame.
290 Can return -1, meaning no way to tell. */
292 /* on the 386, the instruction following the call could be:
293 * popl %ecx - one arg
294 * addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
295 * anything else - zero args
299 i386_frame_num_args (fi)
300 struct frame_info *fi;
304 struct frame_info *pfi;
308 FRAMELESS_FUNCTION_INVOCATION (fi, frameless);
310 /* In the absence of a frame pointer, GDB doesn't get correct values
311 for nameless arguments. Return -1, so it doesn't print any
312 nameless arguments. */
315 pfi = get_prev_frame_info (fi);
318 /* Note: this can happen if we are looking at the frame for
319 main, because FRAME_CHAIN_VALID won't let us go into
320 start. If we have debugging symbols, that's not really
321 a big deal; it just means it will only show as many arguments
322 to main as are declared. */
328 op = read_memory_integer (retpc, 1);
334 op = read_memory_integer (retpc+1, 1);
336 /* addl $<signed imm 8 bits>, %esp */
337 return (read_memory_integer (retpc+2,1)&0xff)/4;
342 { /* add with 32 bit immediate */
343 op = read_memory_integer (retpc+1, 1);
345 /* addl $<imm 32>, %esp */
346 return read_memory_integer (retpc+2, 4) / 4;
358 * parse the first few instructions of the function to see
359 * what registers were stored.
361 * We handle these cases:
363 * The startup sequence can be at the start of the function,
364 * or the function can start with a branch to startup code at the end.
366 * %ebp can be set up with either the 'enter' instruction, or
367 * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
368 * but was once used in the sys5 compiler)
370 * Local space is allocated just below the saved %ebp by either the
371 * 'enter' instruction, or by 'subl $<size>, %esp'. 'enter' has
372 * a 16 bit unsigned argument for space to allocate, and the
373 * 'addl' instruction could have either a signed byte, or
376 * Next, the registers used by this function are pushed. In
377 * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
378 * (and sometimes a harmless bug causes it to also save but not restore %eax);
379 * however, the code below is willing to see the pushes in any order,
380 * and will handle up to 8 of them.
382 * If the setup sequence is at the end of the function, then the
383 * next instruction will be a branch back to the start.
387 i386_frame_find_saved_regs (fip, fsrp)
388 struct frame_info *fip;
389 struct frame_saved_regs *fsrp;
393 CORE_ADDR dummy_bottom;
397 (void) memset (fsrp, 0, 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 */
648 #ifdef GET_LONGJMP_TARGET
650 /* Figure out where the longjmp will land. Slurp the args out of the stack.
651 We expect the first arg to be a pointer to the jmp_buf structure from which
652 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
653 This routine returns true on success. */
656 get_longjmp_target(pc)
659 CORE_ADDR sp, jb_addr;
661 sp = read_register(SP_REGNUM);
663 if (target_read_memory(sp + SP_ARG0, /* Offset of first arg on stack */
669 SWAP_TARGET_AND_HOST(&jb_addr, sizeof(CORE_ADDR));
671 if (target_read_memory(jb_addr + JB_PC * JB_ELEMENT_SIZE, (char *) pc,
675 SWAP_TARGET_AND_HOST(pc, sizeof(CORE_ADDR));
680 #endif /* GET_LONGJMP_TARGET */