1 /* Native-dependent code for Lynx running on i386's, for GDB.
2 Copyright 1988, 1989, 1991, 1992, 1993
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
27 #include <sys/ptrace.h>
28 #include "/usr/include/sys/wait.h"
30 /* these values indicate the offset of the named register in the econtext
48 /* Currently these are not being used. So set them to 0 */
53 static struct target_ops lynx_core_ops
;
55 /* this table must line up with REGISTER_NAMES in m-i386.h */
56 static unsigned int regmap
[] =
64 /* Return the address in the core dump or inferior of register REGNO.
65 BLOCKEND is the address of the econtext structure */
68 register_addr (regno
, blockend
)
71 if (regno
< 0 || regno
>= NUM_REGS
)
72 error ("Invalid register number %d.", regno
);
74 return (blockend
+ regmap
[regno
] * sizeof (long));
77 /* Fetch one register. */
80 fetch_register (regno
, offset
, bpid
)
85 char buf
[MAX_REGISTER_RAW_SIZE
];
86 char mess
[128]; /* For messages */
89 regaddr
= register_addr (regno
, offset
);
90 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (int))
93 *(int *) &buf
[i
] = ptrace (PTRACE_PEEKTHREAD
, bpid
,
94 (PTRACE_ARG3_TYPE
) regaddr
, 0);
95 regaddr
+= sizeof (int);
98 sprintf (mess
, "reading register %s (#%d)", reg_names
[regno
], regno
);
99 perror_with_name (mess
);
102 supply_register (regno
, buf
);
105 /* Store our register values back into the inferior.
106 If REGNO is -1, do this for all registers.
107 Otherwise, REGNO specifies which register (so we can save time). */
110 store_register (regno
, offset
, bpid
)
114 unsigned int regaddr
;
116 extern char registers
[];
119 regaddr
= register_addr (regno
, offset
);
120 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof(int))
123 ptrace (PTRACE_POKEUSER
, bpid
, (PTRACE_ARG3_TYPE
) regaddr
,
124 *(int *) ®isters
[REGISTER_BYTE (regno
) + i
]);
127 sprintf (mess
, "writing register number %d(%d)", regno
, i
);
128 perror_with_name (mess
);
130 regaddr
+= sizeof(int);
134 /* return an offset for use with register_addr() */
141 unsigned int specpage_off
, offset
= (char *) &s
.ecp
- (char *) &s
;
144 specpage_off
= ptrace (PTRACE_THREADUSER
, pid
, (PTRACE_ARG3_TYPE
) 0, 0);
146 perror_with_name ("ptrace");
148 offset
= ptrace (PTRACE_PEEKTHREAD
, pid
, (PTRACE_ARG3_TYPE
) offset
, 0)
151 perror_with_name ("ptrace");
155 /* Fetch all registers, or just one, from the child process. */
158 fetch_inferior_registers (regno
)
161 unsigned int offset
= fetch_offset (inferior_pid
);
165 for (regno
= 0; regno
< NUM_REGS
; regno
++)
166 fetch_register (regno
, offset
, inferior_pid
);
169 fetch_register (regno
, offset
, inferior_pid
);
172 /* Store all registers, or just one, to the child process. */
175 store_inferior_registers (regno
)
178 unsigned int offset
= fetch_offset (inferior_pid
);
182 for (regno
= 0; regno
< NUM_REGS
; regno
++)
183 store_register (regno
, offset
, inferior_pid
);
186 store_register (regno
, offset
, inferior_pid
);
189 /* Wait for child to do something. Return pid of child, or -1 in case
190 of error; store status through argument pointer STATUS. */
205 set_sigint_trap(); /* Causes SIGINT to be passed on to the
215 if (save_errno
== EINTR
)
217 fprintf (stderr
, "Child process unexpectedly missing: %s.\n",
218 safe_strerror (save_errno
));
219 *status
= 42; /* Claim it exited with signal 42 */
223 if (pid
!= PIDGET (inferior_pid
)) /* Some other process?!? */
226 /* thread = WIFTID (*status);*/
227 thread
= *status
>> 16;
229 /* Initial thread value can only be acquired via wait, so we have to
230 resort to this hack. */
232 if (TIDGET (inferior_pid
) == 0)
234 inferior_pid
= BUILDPID (inferior_pid
, thread
);
235 add_thread (inferior_pid
);
238 pid
= BUILDPID (pid
, thread
);
244 /* Convert a Lynx process ID to a string. Returns the string in a static
248 i386lynx_pid_to_str (pid
)
253 sprintf (buf
, "process %d thread %d", PIDGET (pid
), TIDGET (pid
));
258 /* Extract the register values out of the core file and store
259 them where `read_register' will find them.
261 CORE_REG_SECT points to the register values themselves, read into memory.
262 CORE_REG_SIZE is the size of that area.
263 WHICH says which set of registers we are handling (0 = int, 2 = float
264 on machines where they are discontiguous).
265 REG_ADDR is the offset from u.u_ar0 to the register values relative to
266 core_reg_sect. This is used with old-fashioned core files to
267 locate the registers in a large upage-plus-stack ".reg" section.
268 Original upage address X is at location core_reg_sect+x+reg_addr.
272 fetch_core_registers (core_reg_sect
, core_reg_size
, which
, reg_addr
)
274 unsigned core_reg_size
;
279 unsigned int regno
, addr
;
281 for (regno
= 0; regno
< NUM_REGS
; regno
++)
283 addr
= register_addr (regno
, (char *) &s
.ec
- (char *) &s
);
284 supply_register (regno
, core_reg_sect
+ addr
);
290 /* Discard all vestiges of any previous core file
291 and mark data and stack spaces as empty. */
295 lynx_core_close (quitting
)
300 free (bfd_get_filename (core_bfd
));
301 bfd_close (core_bfd
);
304 if (core_ops
.to_sections
)
306 free ((PTR
)lynx_core_ops
.to_sections
);
307 lynx_core_ops
.to_sections
= NULL
;
308 lynx_core_ops
.to_sections_end
= NULL
;
313 /* This routine opens and sets up the core file bfd */
316 lynx_core_open (filename
, from_tty
)
322 struct cleanup
*old_chain
;
328 target_preopen (from_tty
);
332 "No core file specified. (Use `detach' to stop debugging a core file.)"
333 : "No core file specified.");
336 filename
= tilde_expand (filename
);
337 if (filename
[0] != '/')
339 temp
= concat (current_directory
, "/", filename
, NULL
);
344 old_chain
= make_cleanup (free
, filename
);
346 scratch_chan
= open (filename
, write_files
? O_RDWR
: O_RDONLY
, 0);
347 if (scratch_chan
< 0)
348 perror_with_name (filename
);
350 temp_bfd
= bfd_fdopenr (filename
, NULL
, scratch_chan
);
351 if (temp_bfd
== NULL
)
352 perror_with_name (filename
);
354 if (!bfd_check_format (temp_bfd
, bfd_core
))
356 /* Do it after the err msg */
357 make_cleanup (bfd_close
, temp_bfd
);
358 error ("\"%s\" is not a core dump: %s", filename
, bfd_errmsg(bfd_error
));
361 /* Looks semi-reasonable. Toss the old core file and work on the new. */
363 discard_cleanups (old_chain
); /* Don't free filename any more */
364 unpush_target (&core_ops
);
366 old_chain
= make_cleanup (core_close
, core_bfd
);
370 /* Find the data section */
371 if (build_section_table (core_bfd
, &core_ops
.to_sections
,
372 &core_ops
.to_sections_end
))
373 error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd
),
374 bfd_errmsg (bfd_error
));
376 ontop
= !push_target (&core_ops
);
377 discard_cleanups (old_chain
);
379 p
= bfd_core_file_failing_command (core_bfd
);
381 printf_filtered ("Core was generated by `%s'.\n", p
);
383 siggy
= bfd_core_file_failing_signal (core_bfd
);
385 printf_filtered ("Program terminated with signal %d, %s.\n", siggy
,
386 safe_strsignal (siggy
));
388 /* Locate all of the thread register sections. They have names like .regxx,
389 where xx is the thread-id. */
391 bfd_map_over_sections (core_bfd
, grok_register_sections
,
395 /* Fetch all registers from core file */
396 target_fetch_registers (-1);
398 /* Now, set up the frame cache, and print the top of stack */
399 set_current_frame (create_new_frame (read_fp (),
401 select_frame (get_current_frame (), 0);
402 print_stack_frame (selected_frame
, selected_frame_level
, 1);
407 "you won't be able to access this core file until you terminate\n\
408 your %s; do ``info files''", current_target
->to_longname
);
413 lynx_core_detach (args
, from_tty
)
418 error ("Too many arguments");
419 unpush_target (&core_ops
);
421 printf_filtered ("No core file now.\n");
424 /* Get the registers out of a core file. This is the machine-
425 independent part. Fetch_core_registers is the machine-dependent
426 part, typically implemented in the xm-file for each architecture. */
428 /* We just get all the registers, so we don't use regno. */
431 get_core_registers (regno
)
439 sprintf (regsecname
, ".reg%d", TIDGET (inferior_pid
));
441 reg_sec
= bfd_get_section_by_name (core_bfd
, regsecname
);
444 size
= bfd_section_size (core_bfd
, reg_sec
);
445 the_regs
= alloca (size
);
446 if (bfd_get_section_contents (core_bfd
, reg_sec
, the_regs
, (file_ptr
)0,
449 fetch_core_registers (the_regs
, size
, 0,
450 (unsigned) bfd_section_vma (abfd
,reg_sec
));
455 fprintf_filtered (stderr
, "Couldn't fetch registers from core file: %s\n",
456 bfd_errmsg (bfd_error
));
464 struct target_ops
*t
;
466 print_section_info (t
, core_bfd
);
469 /* If mourn is being called in all the right places, this could be say
470 `gdb internal error' (since generic_mourn calls mark_breakpoints_out). */
473 ignore (addr
, contents
)
479 static struct target_ops
483 "Local core dump file",
484 "Use a core file as a target. Specify the filename of the core file.",
495 lynx_core_files_info
,
506 find_default_create_inferior
,
507 0, /* mourn_inferior */
509 0, /* notice_signals */
519 OPS_MAGIC
, /* Always the last thing */
523 _initialize_i386lynx_nat()
525 add_target (&lynx_core_ops
);