2 Copyright (C) 1988, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
6 GDB 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 1, or (at your option)
11 GDB 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 GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
28 #include <sys/types.h>
31 #include <sys/param.h>
35 #include <sys/ioctl.h>
40 #define N_SET_MAGIC(exec, val) ((exec).magic = (val))
42 #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
49 /* I don't know whether this is right for cross-debugging even if you
50 do somehow manage to get the right include file. */
51 #if defined (USE_MACHINE_REG_H)
52 #include <machine/reg.h>
57 /* helper functions for m-i386.h */
59 /* stdio style buffering to minimize calls to ptrace */
60 static CORE_ADDR codestream_next_addr
;
61 static CORE_ADDR codestream_addr
;
62 static unsigned char codestream_buf
[sizeof (int)];
63 static int codestream_off
;
64 static int codestream_cnt
;
66 #define codestream_tell() (codestream_addr + codestream_off)
67 #define codestream_peek() (codestream_cnt == 0 ? \
68 codestream_fill(1): codestream_buf[codestream_off])
69 #define codestream_get() (codestream_cnt-- == 0 ? \
70 codestream_fill(0) : codestream_buf[codestream_off++])
73 codestream_fill (peek_flag
)
75 codestream_addr
= codestream_next_addr
;
76 codestream_next_addr
+= sizeof (int);
78 codestream_cnt
= sizeof (int);
79 read_memory (codestream_addr
,
80 (unsigned char *)codestream_buf
,
84 return (codestream_peek());
86 return (codestream_get());
90 codestream_seek (place
)
92 codestream_next_addr
= place
& -sizeof (int);
95 while (codestream_tell() != place
)
100 codestream_read (buf
, count
)
106 for (i
= 0; i
< count
; i
++)
107 *p
++ = codestream_get ();
110 /* next instruction is a jump, move to target */
120 pos
= codestream_tell ();
123 if (codestream_peek () == 0x66)
129 switch (codestream_get ())
132 /* relative jump: if data16 == 0, disp32, else disp16 */
135 codestream_read ((unsigned char *)&short_delta
, 2);
136 pos
+= short_delta
+ 3; /* include size of jmp inst */
140 codestream_read ((unsigned char *)&long_delta
, 4);
141 pos
+= long_delta
+ 5;
145 /* relative jump, disp8 (ignore data16) */
146 codestream_read ((unsigned char *)&byte_delta
, 1);
147 pos
+= byte_delta
+ 2;
150 codestream_seek (pos
+ data16
);
154 * find & return amound a local space allocated, and advance codestream to
155 * first register push (if any)
157 * if entry sequence doesn't make sense, return -1, and leave
158 * codestream pointer random
161 i386_get_frame_setup (pc
)
165 codestream_seek (pc
);
169 op
= codestream_get ();
171 if (op
== 0x58) /* popl %eax */
174 * this function must start with
177 * xchgl %eax, (%esp) 0x87 0x04 0x24
178 * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
180 * (the system 5 compiler puts out the second xchg
181 * inst, and the assembler doesn't try to optimize it,
182 * so the 'sib' form gets generated)
184 * this sequence is used to get the address of the return
185 * buffer for a function that returns a structure
188 unsigned char buf
[4];
189 static unsigned char proto1
[3] = { 0x87,0x04,0x24 };
190 static unsigned char proto2
[4] = { 0x87,0x44,0x24,0x00 };
191 pos
= codestream_tell ();
192 codestream_read (buf
, 4);
193 if (bcmp (buf
, proto1
, 3) == 0)
195 else if (bcmp (buf
, proto2
, 4) == 0)
198 codestream_seek (pos
);
199 op
= codestream_get (); /* update next opcode */
202 if (op
== 0x55) /* pushl %ebp */
204 /* check for movl %esp, %ebp - can be written two ways */
205 switch (codestream_get ())
208 if (codestream_get () != 0xec)
212 if (codestream_get () != 0xe5)
218 /* check for stack adjustment
222 * note: you can't subtract a 16 bit immediate
223 * from a 32 bit reg, so we don't have to worry
224 * about a data16 prefix
226 op
= codestream_peek ();
229 /* subl with 8 bit immed */
231 if (codestream_get () != 0xec)
232 /* Some instruction starting with 0x83 other than subl. */
234 codestream_seek (codestream_tell () - 2);
237 /* subl with signed byte immediate
238 * (though it wouldn't make sense to be negative)
240 return (codestream_get());
244 /* subl with 32 bit immed */
247 if (codestream_get () != 0xec)
248 /* Some instruction starting with 0x81 other than subl. */
250 codestream_seek (codestream_tell () - 2);
253 /* subl with 32 bit immediate */
254 codestream_read ((unsigned char *)&locals
, 4);
264 /* enter instruction: arg is 16 bit unsigned immed */
265 unsigned short slocals
;
266 codestream_read ((unsigned char *)&slocals
, 2);
267 codestream_get (); /* flush final byte of enter instruction */
273 /* Return number of args passed to a frame.
274 Can return -1, meaning no way to tell. */
276 /* on the 386, the instruction following the call could be:
277 * popl %ecx - one arg
278 * addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
279 * anything else - zero args
283 i386_frame_num_args (fi
)
284 struct frame_info fi
;
288 struct frame_info
*pfi
;
292 FRAMELESS_FUNCTION_INVOCATION (fi
, frameless
);
294 /* In the absence of a frame pointer, GDB doesn't get correct values
295 for nameless arguments. Return -1, so it doesn't print any
296 nameless arguments. */
299 pfi
= get_prev_frame_info ((fi
));
302 /* Note: this can happen if we are looking at the frame for
303 main, because FRAME_CHAIN_VALID won't let us go into
304 start. If we have debugging symbols, that's not really
305 a big deal; it just means it will only show as many arguments
306 to main as are declared. */
312 op
= read_memory_integer (retpc
, 1);
318 op
= read_memory_integer (retpc
+1, 1);
320 /* addl $<signed imm 8 bits>, %esp */
321 return (read_memory_integer (retpc
+2,1)&0xff)/4;
326 { /* add with 32 bit immediate */
327 op
= read_memory_integer (retpc
+1, 1);
329 /* addl $<imm 32>, %esp */
330 return read_memory_integer (retpc
+2, 4) / 4;
342 * parse the first few instructions of the function to see
343 * what registers were stored.
345 * We handle these cases:
347 * The startup sequence can be at the start of the function,
348 * or the function can start with a branch to startup code at the end.
350 * %ebp can be set up with either the 'enter' instruction, or
351 * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
352 * but was once used in the sys5 compiler)
354 * Local space is allocated just below the saved %ebp by either the
355 * 'enter' instruction, or by 'subl $<size>, %esp'. 'enter' has
356 * a 16 bit unsigned argument for space to allocate, and the
357 * 'addl' instruction could have either a signed byte, or
360 * Next, the registers used by this function are pushed. In
361 * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
362 * (and sometimes a harmless bug causes it to also save but not restore %eax);
363 * however, the code below is willing to see the pushes in any order,
364 * and will handle up to 8 of them.
366 * If the setup sequence is at the end of the function, then the
367 * next instruction will be a branch back to the start.
370 i386_frame_find_saved_regs (fip
, fsrp
)
371 struct frame_info
*fip
;
372 struct frame_saved_regs
*fsrp
;
377 CORE_ADDR dummy_bottom
;
381 bzero (fsrp
, sizeof *fsrp
);
383 /* if frame is the end of a dummy, compute where the
386 dummy_bottom
= fip
->frame
- 4 - REGISTER_BYTES
- CALL_DUMMY_LENGTH
;
388 /* check if the PC is in the stack, in a dummy frame */
389 if (dummy_bottom
<= fip
->pc
&& fip
->pc
<= fip
->frame
)
391 /* all regs were saved by push_call_dummy () */
393 for (i
= 0; i
< NUM_REGS
; i
++)
395 adr
-= REGISTER_RAW_SIZE (i
);
401 locals
= i386_get_frame_setup (get_pc_function_start (fip
->pc
));
405 adr
= fip
->frame
- 4 - locals
;
406 for (i
= 0; i
< 8; i
++)
408 op
= codestream_get ();
409 if (op
< 0x50 || op
> 0x57)
411 fsrp
->regs
[op
- 0x50] = adr
;
416 fsrp
->regs
[PC_REGNUM
] = fip
->frame
+ 4;
417 fsrp
->regs
[FP_REGNUM
] = fip
->frame
;
420 /* return pc of first real instruction */
421 i386_skip_prologue (pc
)
426 if (i386_get_frame_setup (pc
) < 0)
429 /* found valid frame setup - codestream now points to
430 * start of push instructions for saving registers
433 /* skip over register saves */
434 for (i
= 0; i
< 8; i
++)
436 op
= codestream_peek ();
437 /* break if not pushl inst */
438 if (op
< 0x50 || op
> 0x57)
445 return (codestream_tell ());
448 i386_push_dummy_frame ()
450 CORE_ADDR sp
= read_register (SP_REGNUM
);
452 char regbuf
[MAX_REGISTER_RAW_SIZE
];
454 sp
= push_word (sp
, read_register (PC_REGNUM
));
455 sp
= push_word (sp
, read_register (FP_REGNUM
));
456 write_register (FP_REGNUM
, sp
);
457 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
459 read_register_gen (regnum
, regbuf
);
460 sp
= push_bytes (sp
, regbuf
, REGISTER_RAW_SIZE (regnum
));
462 write_register (SP_REGNUM
, sp
);
467 FRAME frame
= get_current_frame ();
470 struct frame_saved_regs fsr
;
471 struct frame_info
*fi
;
472 char regbuf
[MAX_REGISTER_RAW_SIZE
];
474 fi
= get_frame_info (frame
);
476 get_frame_saved_regs (fi
, &fsr
);
477 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
480 adr
= fsr
.regs
[regnum
];
483 read_memory (adr
, regbuf
, REGISTER_RAW_SIZE (regnum
));
484 write_register_bytes (REGISTER_BYTE (regnum
), regbuf
,
485 REGISTER_RAW_SIZE (regnum
));
488 write_register (FP_REGNUM
, read_memory_integer (fp
, 4));
489 write_register (PC_REGNUM
, read_memory_integer (fp
+ 4, 4));
490 write_register (SP_REGNUM
, fp
+ 8);
491 flush_cached_frames ();
492 set_current_frame ( create_new_frame (read_register (FP_REGNUM
),