]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/procfs.c
Fix formatting
[thirdparty/binutils-gdb.git] / gdb / procfs.c
CommitLineData
c906108c 1/* Machine independent support for SVR4 /proc (process file system) for GDB.
4b14d3e4 2 Copyright 1999-2000 Free Software Foundation, Inc.
c3f6f71d
JM
3 Written by Michael Snyder at Cygnus Solutions.
4 Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
c906108c 5
c3f6f71d 6This file is part of GDB.
c906108c 7
c3f6f71d
JM
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
c906108c 12
c3f6f71d
JM
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
c906108c 17
c3f6f71d
JM
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software Foundation,
20Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
c906108c 21
c3f6f71d
JM
22#include "defs.h"
23#include "inferior.h"
24#include "target.h"
25#include "gdbcore.h"
26#include "gdbcmd.h"
0fda6bd2 27#include "gdbthread.h"
c906108c 28
c3f6f71d
JM
29#if defined (NEW_PROC_API)
30#define _STRUCTURED_PROC 1 /* Should be done by configure script. */
31#endif
c906108c 32
c3f6f71d
JM
33#include <sys/procfs.h>
34#include <sys/fault.h>
35#include <sys/syscall.h>
36#include <sys/errno.h>
0fda6bd2
JM
37#include <sys/wait.h>
38#include <signal.h>
39#include <ctype.h>
40
c3f6f71d
JM
41/*
42 * PROCFS.C
43 *
44 * This module provides the interface between GDB and the
45 * /proc file system, which is used on many versions of Unix
46 * as a means for debuggers to control other processes.
47 * Examples of the systems that use this interface are:
48 * Irix
49 * Solaris
50 * OSF
51 * Unixware
52 *
53 * /proc works by immitating a file system: you open a simulated file
54 * that represents the process you wish to interact with, and
55 * perform operations on that "file" in order to examine or change
56 * the state of the other process.
57 *
58 * The most important thing to know about /proc and this module
59 * is that there are two very different interfaces to /proc:
60 * One that uses the ioctl system call, and
61 * another that uses read and write system calls.
62 * This module has to support both /proc interfaces. This means
63 * that there are two different ways of doing every basic operation.
64 *
65 * In order to keep most of the code simple and clean, I have
66 * defined an interface "layer" which hides all these system calls.
67 * An ifdef (NEW_PROC_API) determines which interface we are using,
68 * and most or all occurrances of this ifdef should be confined to
69 * this interface layer.
c906108c
SS
70 */
71
72
c3f6f71d
JM
73/* Determine which /proc API we are using:
74 The ioctl API defines PIOCSTATUS, while
75 the read/write (multiple fd) API never does. */
c906108c 76
c3f6f71d 77#ifdef NEW_PROC_API
c906108c 78#include <sys/types.h>
4b14d3e4 79#include "gdb_dirent.h" /* opendir/readdir, for listing the LWP's */
c3f6f71d 80#endif
c906108c 81
c3f6f71d
JM
82#include <fcntl.h> /* for O_RDONLY */
83#include <unistd.h> /* for "X_OK" */
84#include "gdb_stat.h" /* for struct stat */
c906108c 85
103b3ef5
MS
86/* Note: procfs-utils.h must be included after the above system header
87 files, because it redefines various system calls using macros.
88 This may be incompatible with the prototype declarations. */
89
103b3ef5
MS
90#include "proc-utils.h"
91
c60c0f5f
MS
92/* Prototypes for supply_gregset etc. */
93#include "gregset.h"
94
c3f6f71d 95/* =================== TARGET_OPS "MODULE" =================== */
c906108c 96
c3f6f71d
JM
97/*
98 * This module defines the GDB target vector and its methods.
99 */
c906108c 100
8ab86381 101static void procfs_open (char *, int);
a14ed312
KB
102static void procfs_attach (char *, int);
103static void procfs_detach (char *, int);
104static void procfs_resume (int, int, enum target_signal);
105static int procfs_can_run (void);
106static void procfs_stop (void);
107static void procfs_files_info (struct target_ops *);
108static void procfs_fetch_registers (int);
109static void procfs_store_registers (int);
110static void procfs_notice_signals (int);
111static void procfs_prepare_to_store (void);
112static void procfs_kill_inferior (void);
113static void procfs_mourn_inferior (void);
114static void procfs_create_inferior (char *, char *, char **);
115static int procfs_wait (int, struct target_waitstatus *);
116static int procfs_xfer_memory (CORE_ADDR,
117 char *, int, int, struct target_ops *);
118
119static int procfs_thread_alive (int);
120
121void procfs_find_new_threads (void);
122char *procfs_pid_to_str (int);
c3f6f71d
JM
123
124struct target_ops procfs_ops; /* the target vector */
c906108c 125
c3f6f71d
JM
126static void
127init_procfs_ops ()
128{
129 procfs_ops.to_shortname = "procfs";
130 procfs_ops.to_longname = "Unix /proc child process";
131 procfs_ops.to_doc =
132 "Unix /proc child process (started by the \"run\" command).";
133 procfs_ops.to_open = procfs_open;
134 procfs_ops.to_can_run = procfs_can_run;
135 procfs_ops.to_create_inferior = procfs_create_inferior;
136 procfs_ops.to_kill = procfs_kill_inferior;
137 procfs_ops.to_mourn_inferior = procfs_mourn_inferior;
138 procfs_ops.to_attach = procfs_attach;
139 procfs_ops.to_detach = procfs_detach;
140 procfs_ops.to_wait = procfs_wait;
141 procfs_ops.to_resume = procfs_resume;
142 procfs_ops.to_prepare_to_store = procfs_prepare_to_store;
143 procfs_ops.to_fetch_registers = procfs_fetch_registers;
144 procfs_ops.to_store_registers = procfs_store_registers;
145 procfs_ops.to_xfer_memory = procfs_xfer_memory;
146 procfs_ops.to_insert_breakpoint = memory_insert_breakpoint;
147 procfs_ops.to_remove_breakpoint = memory_remove_breakpoint;
148 procfs_ops.to_notice_signals = procfs_notice_signals;
149 procfs_ops.to_files_info = procfs_files_info;
150 procfs_ops.to_stop = procfs_stop;
151
152 procfs_ops.to_terminal_init = terminal_init_inferior;
153 procfs_ops.to_terminal_inferior = terminal_inferior;
154 procfs_ops.to_terminal_ours_for_output = terminal_ours_for_output;
155 procfs_ops.to_terminal_ours = terminal_ours;
156 procfs_ops.to_terminal_info = child_terminal_info;
157
158 procfs_ops.to_find_new_threads = procfs_find_new_threads;
159 procfs_ops.to_thread_alive = procfs_thread_alive;
160 procfs_ops.to_pid_to_str = procfs_pid_to_str;
161
103b3ef5
MS
162 procfs_ops.to_has_all_memory = 1;
163 procfs_ops.to_has_memory = 1;
c3f6f71d
JM
164 procfs_ops.to_has_execution = 1;
165 procfs_ops.to_has_stack = 1;
166 procfs_ops.to_has_registers = 1;
167 procfs_ops.to_stratum = process_stratum;
168 procfs_ops.to_has_thread_control = tc_schedlock;
169 procfs_ops.to_magic = OPS_MAGIC;
170}
c906108c 171
c3f6f71d
JM
172/* =================== END, TARGET_OPS "MODULE" =================== */
173
c3f6f71d
JM
174/*
175 * World Unification:
176 *
177 * Put any typedefs, defines etc. here that are required for
178 * the unification of code that handles different versions of /proc.
179 */
180
181#ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */
182#ifndef UNIXWARE
183 enum { READ_WATCHFLAG = WA_READ,
184 WRITE_WATCHFLAG = WA_WRITE,
185 EXEC_WATCHFLAG = WA_EXEC,
186 AFTER_WATCHFLAG = WA_TRAPAFTER
187 };
188#endif
189#else /* Irix method for watchpoints */
190 enum { READ_WATCHFLAG = MA_READ,
191 WRITE_WATCHFLAG = MA_WRITE,
192 EXEC_WATCHFLAG = MA_EXEC,
193 AFTER_WATCHFLAG = 0 /* trapafter not implemented */
194 };
195#endif
196
197
198
199
200/* =================== STRUCT PROCINFO "MODULE" =================== */
201
202 /* FIXME: this comment will soon be out of date W.R.T. threads. */
203
204/* The procinfo struct is a wrapper to hold all the state information
205 concerning a /proc process. There should be exactly one procinfo
206 for each process, and since GDB currently can debug only one
207 process at a time, that means there should be only one procinfo.
208 All of the LWP's of a process can be accessed indirectly thru the
209 single process procinfo.
210
211 However, against the day when GDB may debug more than one process,
212 this data structure is kept in a list (which for now will hold no
213 more than one member), and many functions will have a pointer to a
214 procinfo as an argument.
215
216 There will be a separate procinfo structure for use by the (not yet
217 implemented) "info proc" command, so that we can print useful
218 information about any random process without interfering with the
219 inferior's procinfo information. */
220
221#ifdef NEW_PROC_API
222/* format strings for /proc paths */
223# ifndef CTL_PROC_NAME_FMT
224# define MAIN_PROC_NAME_FMT "/proc/%d"
225# define CTL_PROC_NAME_FMT "/proc/%d/ctl"
226# define AS_PROC_NAME_FMT "/proc/%d/as"
227# define MAP_PROC_NAME_FMT "/proc/%d/map"
228# define STATUS_PROC_NAME_FMT "/proc/%d/status"
229# define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
230# endif
231/* the name of the proc status struct depends on the implementation */
232typedef pstatus_t gdb_prstatus_t;
233typedef lwpstatus_t gdb_lwpstatus_t;
234#else /* ! NEW_PROC_API */
235/* format strings for /proc paths */
236# ifndef CTL_PROC_NAME_FMT
237# define MAIN_PROC_NAME_FMT "/proc/%05d"
238# define CTL_PROC_NAME_FMT "/proc/%05d"
239# define AS_PROC_NAME_FMT "/proc/%05d"
240# define MAP_PROC_NAME_FMT "/proc/%05d"
241# define STATUS_PROC_NAME_FMT "/proc/%05d"
242# define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
243# endif
c906108c 244/* the name of the proc status struct depends on the implementation */
c5aa993b 245typedef prstatus_t gdb_prstatus_t;
c3f6f71d
JM
246typedef prstatus_t gdb_lwpstatus_t;
247#endif /* NEW_PROC_API */
c906108c 248
0d06e24b
JM
249/* Provide default composite pid manipulation macros for systems that
250 don't have threads. */
c906108c 251
c3f6f71d 252#ifndef PIDGET
0d06e24b
JM
253#define PIDGET(PID) (PID)
254#define TIDGET(PID) (PID)
eeefac92
AC
255#endif
256#ifndef MERGEPID
0d06e24b 257#define MERGEPID(PID, TID) (PID)
c906108c
SS
258#endif
259
c3f6f71d
JM
260typedef struct procinfo {
261 struct procinfo *next;
262 int pid; /* Process ID */
263 int tid; /* Thread/LWP id */
c906108c 264
c3f6f71d
JM
265 /* process state */
266 int was_stopped;
267 int ignore_next_sigstop;
c906108c 268
c3f6f71d
JM
269 /* The following four fd fields may be identical, or may contain
270 several different fd's, depending on the version of /proc
271 (old ioctl or new read/write). */
c906108c 272
c3f6f71d
JM
273 int ctl_fd; /* File descriptor for /proc control file */
274 /*
275 * The next three file descriptors are actually only needed in the
276 * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
277 * However, to avoid a bunch of #ifdefs in the code, we will use
278 * them uniformly by (in the case of the ioctl single-file-descriptor
279 * implementation) filling them with copies of the control fd.
280 */
281 int status_fd; /* File descriptor for /proc status file */
282 int as_fd; /* File descriptor for /proc as file */
c906108c 283
c3f6f71d 284 char pathname[MAX_PROC_NAME_SIZE]; /* Pathname to /proc entry */
c906108c 285
c3f6f71d
JM
286 fltset_t saved_fltset; /* Saved traced hardware fault set */
287 sigset_t saved_sigset; /* Saved traced signal set */
288 sigset_t saved_sighold; /* Saved held signal set */
289 sysset_t saved_exitset; /* Saved traced system call exit set */
290 sysset_t saved_entryset; /* Saved traced system call entry set */
c906108c 291
c3f6f71d 292 gdb_prstatus_t prstatus; /* Current process status info */
c906108c 293
c3f6f71d
JM
294#ifndef NEW_PROC_API
295 gdb_fpregset_t fpregset; /* Current floating point registers */
c5aa993b 296#endif
c3f6f71d
JM
297
298 struct procinfo *thread_list;
c906108c 299
c3f6f71d
JM
300 int status_valid : 1;
301 int gregs_valid : 1;
302 int fpregs_valid : 1;
303 int threads_valid: 1;
304} procinfo;
c906108c 305
c3f6f71d 306static char errmsg[128]; /* shared error msg buffer */
c906108c 307
c3f6f71d 308/* Function prototypes for procinfo module: */
c906108c 309
a14ed312
KB
310static procinfo *find_procinfo_or_die (int pid, int tid);
311static procinfo *find_procinfo (int pid, int tid);
312static procinfo *create_procinfo (int pid, int tid);
313static void destroy_procinfo (procinfo * p);
004527cb 314static void do_destroy_procinfo_cleanup (void *);
a14ed312
KB
315static void dead_procinfo (procinfo * p, char *msg, int killp);
316static int open_procinfo_files (procinfo * p, int which);
317static void close_procinfo_files (procinfo * p);
c906108c 318
c3f6f71d
JM
319/* The head of the procinfo list: */
320static procinfo * procinfo_list;
c906108c 321
c3f6f71d
JM
322/*
323 * Function: find_procinfo
324 *
325 * Search the procinfo list.
326 *
327 * Returns: pointer to procinfo, or NULL if not found.
328 */
c906108c 329
c3f6f71d
JM
330static procinfo *
331find_procinfo (pid, tid)
332 int pid;
333 int tid;
c5aa993b 334{
c3f6f71d 335 procinfo *pi;
c906108c 336
c3f6f71d
JM
337 for (pi = procinfo_list; pi; pi = pi->next)
338 if (pi->pid == pid)
339 break;
c906108c 340
c3f6f71d
JM
341 if (pi)
342 if (tid)
343 {
344 /* Don't check threads_valid. If we're updating the
345 thread_list, we want to find whatever threads are already
346 here. This means that in general it is the caller's
347 responsibility to check threads_valid and update before
348 calling find_procinfo, if the caller wants to find a new
349 thread. */
350
351 for (pi = pi->thread_list; pi; pi = pi->next)
352 if (pi->tid == tid)
353 break;
354 }
c906108c 355
c3f6f71d
JM
356 return pi;
357}
c906108c 358
c3f6f71d
JM
359/*
360 * Function: find_procinfo_or_die
361 *
362 * Calls find_procinfo, but errors on failure.
363 */
c906108c 364
c3f6f71d
JM
365static procinfo *
366find_procinfo_or_die (pid, tid)
367 int pid;
368 int tid;
369{
370 procinfo *pi = find_procinfo (pid, tid);
c906108c 371
c3f6f71d 372 if (pi == NULL)
0fda6bd2
JM
373 {
374 if (tid)
375 error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.",
376 pid, tid);
377 else
378 error ("procfs: couldn't find pid %d in procinfo list.", pid);
379 }
c3f6f71d
JM
380 return pi;
381}
c906108c 382
c3f6f71d
JM
383/*
384 * Function: open_procinfo_files
385 *
386 * Open the file descriptor for the process or LWP.
387 * ifdef NEW_PROC_API, we only open the control file descriptor;
388 * the others are opened lazily as needed.
389 * else (if not NEW_PROC_API), there is only one real
390 * file descriptor, but we keep multiple copies of it so that
391 * the code that uses them does not have to be #ifdef'd.
392 *
393 * Return: file descriptor, or zero for failure.
394 */
c906108c 395
c3f6f71d 396enum { FD_CTL, FD_STATUS, FD_AS };
c906108c 397
c3f6f71d
JM
398static int
399open_procinfo_files (pi, which)
400 procinfo *pi;
401 int which;
402{
0fda6bd2 403#ifdef NEW_PROC_API
c3f6f71d 404 char tmp[MAX_PROC_NAME_SIZE];
0fda6bd2 405#endif
c3f6f71d
JM
406 int fd;
407
408 /*
409 * This function is getting ALMOST long enough to break up into several.
410 * Here is some rationale:
411 *
412 * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
413 * There are several file descriptors that may need to be open
414 * for any given process or LWP. The ones we're intereted in are:
415 * - control (ctl) write-only change the state
416 * - status (status) read-only query the state
417 * - address space (as) read/write access memory
418 * - map (map) read-only virtual addr map
419 * Most of these are opened lazily as they are needed.
420 * The pathnames for the 'files' for an LWP look slightly
421 * different from those of a first-class process:
422 * Pathnames for a process (<proc-id>):
423 * /proc/<proc-id>/ctl
424 * /proc/<proc-id>/status
425 * /proc/<proc-id>/as
426 * /proc/<proc-id>/map
427 * Pathnames for an LWP (lwp-id):
428 * /proc/<proc-id>/lwp/<lwp-id>/lwpctl
429 * /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
430 * An LWP has no map or address space file descriptor, since
431 * the memory map and address space are shared by all LWPs.
432 *
433 * Everyone else (Solaris 2.5, Irix, OSF)
434 * There is only one file descriptor for each process or LWP.
435 * For convenience, we copy the same file descriptor into all
436 * three fields of the procinfo struct (ctl_fd, status_fd, and
437 * as_fd, see NEW_PROC_API above) so that code that uses them
438 * doesn't need any #ifdef's.
439 * Pathname for all:
440 * /proc/<proc-id>
441 *
442 * Solaris 2.5 LWP's:
443 * Each LWP has an independent file descriptor, but these
444 * are not obtained via the 'open' system call like the rest:
445 * instead, they're obtained thru an ioctl call (PIOCOPENLWP)
446 * to the file descriptor of the parent process.
447 *
448 * OSF threads:
449 * These do not even have their own independent file descriptor.
450 * All operations are carried out on the file descriptor of the
451 * parent process. Therefore we just call open again for each
452 * thread, getting a new handle for the same 'file'.
453 */
454
455#ifdef NEW_PROC_API
456 /*
457 * In this case, there are several different file descriptors that
458 * we might be asked to open. The control file descriptor will be
459 * opened early, but the others will be opened lazily as they are
460 * needed.
461 */
462
463 strcpy (tmp, pi->pathname);
464 switch (which) { /* which file descriptor to open? */
465 case FD_CTL:
466 if (pi->tid)
467 strcat (tmp, "/lwpctl");
468 else
469 strcat (tmp, "/ctl");
470 fd = open (tmp, O_WRONLY);
471 if (fd <= 0)
472 return 0; /* fail */
473 pi->ctl_fd = fd;
474 break;
475 case FD_AS:
476 if (pi->tid)
477 return 0; /* there is no 'as' file descriptor for an lwp */
478 strcat (tmp, "/as");
479 fd = open (tmp, O_RDWR);
480 if (fd <= 0)
481 return 0; /* fail */
482 pi->as_fd = fd;
483 break;
484 case FD_STATUS:
485 if (pi->tid)
486 strcat (tmp, "/lwpstatus");
487 else
488 strcat (tmp, "/status");
489 fd = open (tmp, O_RDONLY);
490 if (fd <= 0)
491 return 0; /* fail */
492 pi->status_fd = fd;
493 break;
494 default:
495 return 0; /* unknown file descriptor */
496 }
497#else /* not NEW_PROC_API */
498 /*
499 * In this case, there is only one file descriptor for each procinfo
500 * (ie. each process or LWP). In fact, only the file descriptor for
501 * the process can actually be opened by an 'open' system call.
502 * The ones for the LWPs have to be obtained thru an IOCTL call
503 * on the process's file descriptor.
504 *
505 * For convenience, we copy each procinfo's single file descriptor
506 * into all of the fields occupied by the several file descriptors
507 * of the NEW_PROC_API implementation. That way, the code that uses
508 * them can be written without ifdefs.
509 */
510
511
512#ifdef PIOCTSTATUS /* OSF */
513 if ((fd = open (pi->pathname, O_RDWR)) == 0) /* Only one FD; just open it. */
514 return 0;
515#else /* Sol 2.5, Irix, other? */
516 if (pi->tid == 0) /* Master procinfo for the process */
517 {
518 fd = open (pi->pathname, O_RDWR);
519 if (fd <= 0)
520 return 0; /* fail */
521 }
522 else /* LWP thread procinfo */
523 {
524#ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */
525 procinfo *process;
526 int lwpid = pi->tid;
527
528 /* Find the procinfo for the entire process. */
529 if ((process = find_procinfo (pi->pid, 0)) == NULL)
530 return 0; /* fail */
531
532 /* Now obtain the file descriptor for the LWP. */
533 if ((fd = ioctl (process->ctl_fd, PIOCOPENLWP, &lwpid)) <= 0)
534 return 0; /* fail */
535#else /* Irix, other? */
536 return 0; /* Don't know how to open threads */
537#endif /* Sol 2.5 PIOCOPENLWP */
538 }
539#endif /* OSF PIOCTSTATUS */
540 pi->ctl_fd = pi->as_fd = pi->status_fd = fd;
541#endif /* NEW_PROC_API */
c906108c 542
c3f6f71d
JM
543 return 1; /* success */
544}
c906108c 545
c3f6f71d
JM
546/*
547 * Function: create_procinfo
548 *
549 * Allocate a data structure and link it into the procinfo list.
02d5252f 550 * (First tries to find a pre-existing one (FIXME: why?)
c3f6f71d
JM
551 *
552 * Return: pointer to new procinfo struct.
553 */
c906108c 554
c3f6f71d
JM
555static procinfo *
556create_procinfo (pid, tid)
557 int pid;
558 int tid;
559{
560 procinfo *pi, *parent;
c906108c 561
0d06e24b 562 if ((pi = find_procinfo (pid, tid)))
c3f6f71d 563 return pi; /* Already exists, nothing to do. */
c906108c 564
c3f6f71d
JM
565 /* find parent before doing malloc, to save having to cleanup */
566 if (tid != 0)
567 parent = find_procinfo_or_die (pid, 0); /* FIXME: should I
568 create it if it
569 doesn't exist yet? */
c906108c 570
c3f6f71d
JM
571 pi = (procinfo *) xmalloc (sizeof (procinfo));
572 memset (pi, 0, sizeof (procinfo));
573 pi->pid = pid;
574 pi->tid = tid;
c906108c 575
c3f6f71d
JM
576 /* Chain into list. */
577 if (tid == 0)
578 {
579 sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
580 pi->next = procinfo_list;
581 procinfo_list = pi;
582 }
583 else
584 {
585#ifdef NEW_PROC_API
586 sprintf (pi->pathname, "/proc/%05d/lwp/%d", pid, tid);
587#else
588 sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
589#endif
590 pi->next = parent->thread_list;
591 parent->thread_list = pi;
592 }
593 return pi;
594}
c906108c 595
c3f6f71d
JM
596/*
597 * Function: close_procinfo_files
598 *
599 * Close all file descriptors associated with the procinfo
600 */
c906108c 601
c3f6f71d
JM
602static void
603close_procinfo_files (pi)
604 procinfo *pi;
605{
606 if (pi->ctl_fd > 0)
607 close (pi->ctl_fd);
608#ifdef NEW_PROC_API
609 if (pi->as_fd > 0)
610 close (pi->as_fd);
611 if (pi->status_fd > 0)
612 close (pi->status_fd);
613#endif
614 pi->ctl_fd = pi->as_fd = pi->status_fd = 0;
615}
c906108c 616
c3f6f71d
JM
617/*
618 * Function: destroy_procinfo
619 *
620 * Destructor function. Close, unlink and deallocate the object.
621 */
c906108c 622
c3f6f71d
JM
623static void
624destroy_one_procinfo (list, pi)
625 procinfo **list;
626 procinfo *pi;
627{
628 procinfo *ptr;
629
630 /* Step one: unlink the procinfo from its list */
631 if (pi == *list)
632 *list = pi->next;
633 else
634 for (ptr = *list; ptr; ptr = ptr->next)
635 if (ptr->next == pi)
636 {
637 ptr->next = pi->next;
638 break;
639 }
7a292a7a 640
c3f6f71d
JM
641 /* Step two: close any open file descriptors */
642 close_procinfo_files (pi);
7a292a7a 643
c3f6f71d
JM
644 /* Step three: free the memory. */
645 free (pi);
646}
c906108c 647
c3f6f71d
JM
648static void
649destroy_procinfo (pi)
650 procinfo *pi;
651{
652 procinfo *tmp;
c906108c 653
c3f6f71d
JM
654 if (pi->tid != 0) /* destroy a thread procinfo */
655 {
656 tmp = find_procinfo (pi->pid, 0); /* find the parent process */
657 destroy_one_procinfo (&tmp->thread_list, pi);
658 }
659 else /* destroy a process procinfo and all its threads */
660 {
661 /* First destroy the children, if any; */
662 while (pi->thread_list != NULL)
663 destroy_one_procinfo (&pi->thread_list, pi->thread_list);
664 /* Then destroy the parent. Genocide!!! */
665 destroy_one_procinfo (&procinfo_list, pi);
666 }
667}
c906108c 668
004527cb
AC
669static void
670do_destroy_procinfo_cleanup (void *pi)
671{
672 destroy_procinfo (pi);
673}
674
c3f6f71d 675enum { NOKILL, KILL };
c906108c 676
c3f6f71d
JM
677/*
678 * Function: dead_procinfo
679 *
680 * To be called on a non_recoverable error for a procinfo.
681 * Prints error messages, optionally sends a SIGKILL to the process,
682 * then destroys the data structure.
683 */
c906108c 684
c3f6f71d
JM
685static void
686dead_procinfo (pi, msg, kill_p)
687 procinfo *pi;
688 char *msg;
689 int kill_p;
690{
691 char procfile[80];
c906108c 692
c3f6f71d
JM
693 if (pi->pathname)
694 {
695 print_sys_errmsg (pi->pathname, errno);
696 }
697 else
698 {
699 sprintf (procfile, "process %d", pi->pid);
700 print_sys_errmsg (procfile, errno);
701 }
702 if (kill_p == KILL)
703 kill (pi->pid, SIGKILL);
c906108c 704
c3f6f71d
JM
705 destroy_procinfo (pi);
706 error (msg);
707}
c906108c 708
c3f6f71d 709/* =================== END, STRUCT PROCINFO "MODULE" =================== */
c906108c 710
c3f6f71d 711/* =================== /proc "MODULE" =================== */
c906108c 712
c3f6f71d
JM
713/*
714 * This "module" is the interface layer between the /proc system API
715 * and the gdb target vector functions. This layer consists of
716 * access functions that encapsulate each of the basic operations
717 * that we need to use from the /proc API.
718 *
719 * The main motivation for this layer is to hide the fact that
720 * there are two very different implementations of the /proc API.
721 * Rather than have a bunch of #ifdefs all thru the gdb target vector
722 * functions, we do our best to hide them all in here.
723 */
c906108c 724
a14ed312
KB
725int proc_get_status (procinfo * pi);
726long proc_flags (procinfo * pi);
727int proc_why (procinfo * pi);
728int proc_what (procinfo * pi);
729int proc_set_run_on_last_close (procinfo * pi);
730int proc_unset_run_on_last_close (procinfo * pi);
731int proc_set_inherit_on_fork (procinfo * pi);
732int proc_unset_inherit_on_fork (procinfo * pi);
733int proc_set_async (procinfo * pi);
734int proc_unset_async (procinfo * pi);
735int proc_stop_process (procinfo * pi);
736int proc_trace_signal (procinfo * pi, int signo);
737int proc_ignore_signal (procinfo * pi, int signo);
738int proc_clear_current_fault (procinfo * pi);
739int proc_set_current_signal (procinfo * pi, int signo);
740int proc_clear_current_signal (procinfo * pi);
741int proc_set_gregs (procinfo * pi);
742int proc_set_fpregs (procinfo * pi);
743int proc_wait_for_stop (procinfo * pi);
744int proc_run_process (procinfo * pi, int step, int signo);
745int proc_kill (procinfo * pi, int signo);
746int proc_parent_pid (procinfo * pi);
747int proc_get_nthreads (procinfo * pi);
748int proc_get_current_thread (procinfo * pi);
749int proc_set_held_signals (procinfo * pi, sigset_t * sighold);
750int proc_set_traced_sysexit (procinfo * pi, sysset_t * sysset);
751int proc_set_traced_sysentry (procinfo * pi, sysset_t * sysset);
752int proc_set_traced_faults (procinfo * pi, fltset_t * fltset);
753int proc_set_traced_signals (procinfo * pi, sigset_t * sigset);
754
755int proc_update_threads (procinfo * pi);
756int proc_iterate_over_threads (procinfo * pi,
8ab86381
KB
757 int (*func) (procinfo *, procinfo *, void *),
758 void *ptr);
a14ed312
KB
759
760gdb_gregset_t *proc_get_gregs (procinfo * pi);
761gdb_fpregset_t *proc_get_fpregs (procinfo * pi);
762sysset_t *proc_get_traced_sysexit (procinfo * pi, sysset_t * save);
763sysset_t *proc_get_traced_sysentry (procinfo * pi, sysset_t * save);
764fltset_t *proc_get_traced_faults (procinfo * pi, fltset_t * save);
765sigset_t *proc_get_traced_signals (procinfo * pi, sigset_t * save);
766sigset_t *proc_get_held_signals (procinfo * pi, sigset_t * save);
767sigset_t *proc_get_pending_signals (procinfo * pi, sigset_t * save);
768struct sigaction *proc_get_signal_actions (procinfo * pi,
769 struct sigaction *save);
770
771void proc_warn (procinfo * pi, char *func, int line);
772void proc_error (procinfo * pi, char *func, int line);
c906108c 773
c3f6f71d
JM
774void
775proc_warn (pi, func, line)
776 procinfo *pi;
777 char *func;
778 int line;
779{
780 sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
781 print_sys_errmsg (errmsg, errno);
782}
c906108c 783
c3f6f71d
JM
784void
785proc_error (pi, func, line)
786 procinfo *pi;
787 char *func;
788 int line;
789{
790 sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
791 perror_with_name (errmsg);
792}
c906108c 793
c3f6f71d
JM
794/*
795 * Function: proc_get_status
796 *
797 * Updates the status struct in the procinfo.
798 * There is a 'valid' flag, to let other functions know when
799 * this function needs to be called (so the status is only
800 * read when it is needed). The status file descriptor is
801 * also only opened when it is needed.
802 *
803 * Return: non-zero for success, zero for failure.
804 */
c906108c 805
c3f6f71d
JM
806int
807proc_get_status (pi)
808 procinfo *pi;
809{
810 /* Status file descriptor is opened "lazily" */
811 if (pi->status_fd == 0 &&
812 open_procinfo_files (pi, FD_STATUS) == 0)
813 {
814 pi->status_valid = 0;
815 return 0;
816 }
c906108c 817
c3f6f71d
JM
818#ifdef NEW_PROC_API
819 if (lseek (pi->status_fd, 0, SEEK_SET) < 0)
820 pi->status_valid = 0; /* fail */
821 else
822 {
823 /* Sigh... I have to read a different data structure,
824 depending on whether this is a main process or an LWP. */
825 if (pi->tid)
826 pi->status_valid = (read (pi->status_fd,
827 (char *) &pi->prstatus.pr_lwp,
828 sizeof (lwpstatus_t))
829 == sizeof (lwpstatus_t));
830 else
831 {
832 pi->status_valid = (read (pi->status_fd,
833 (char *) &pi->prstatus,
834 sizeof (gdb_prstatus_t))
835 == sizeof (gdb_prstatus_t));
836#if 0 /*def UNIXWARE*/
837 if (pi->status_valid &&
838 (pi->prstatus.pr_lwp.pr_flags & PR_ISTOP) &&
839 pi->prstatus.pr_lwp.pr_why == PR_REQUESTED)
840 /* Unixware peculiarity -- read the damn thing again! */
841 pi->status_valid = (read (pi->status_fd,
842 (char *) &pi->prstatus,
843 sizeof (gdb_prstatus_t))
844 == sizeof (gdb_prstatus_t));
845#endif /* UNIXWARE */
846 }
847 }
848#else /* ioctl method */
849#ifdef PIOCTSTATUS /* osf */
850 if (pi->tid == 0) /* main process */
851 {
852 /* Just read the danged status. Now isn't that simple? */
853 pi->status_valid =
854 (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
855 }
856 else
857 {
858 int win;
859 struct {
860 long pr_count;
861 tid_t pr_error_thread;
862 struct prstatus status;
863 } thread_status;
864
865 thread_status.pr_count = 1;
866 thread_status.status.pr_tid = pi->tid;
867 win = (ioctl (pi->status_fd, PIOCTSTATUS, &thread_status) >= 0);
868 if (win)
869 {
870 memcpy (&pi->prstatus, &thread_status.status,
871 sizeof (pi->prstatus));
872 pi->status_valid = 1;
873 }
874 }
875#else
876 /* Just read the danged status. Now isn't that simple? */
877 pi->status_valid = (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
878#endif
879#endif
c906108c 880
c3f6f71d
JM
881 if (pi->status_valid)
882 {
883 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
884 proc_why (pi),
885 proc_what (pi),
886 proc_get_current_thread (pi));
887 }
c906108c 888
c3f6f71d
JM
889 /* The status struct includes general regs, so mark them valid too */
890 pi->gregs_valid = pi->status_valid;
891#ifdef NEW_PROC_API
892 /* In the read/write multiple-fd model,
893 the status struct includes the fp regs too, so mark them valid too */
894 pi->fpregs_valid = pi->status_valid;
895#endif
896 return pi->status_valid; /* True if success, false if failure. */
897}
c906108c 898
c3f6f71d
JM
899/*
900 * Function: proc_flags
901 *
902 * returns the process flags (pr_flags field).
903 */
904
905long
906proc_flags (pi)
907 procinfo *pi;
908{
909 if (!pi->status_valid)
910 if (!proc_get_status (pi))
911 return 0; /* FIXME: not a good failure value (but what is?) */
c906108c 912
c3f6f71d 913#ifdef NEW_PROC_API
0d06e24b
JM
914# ifdef UNIXWARE
915 /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
916 pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
917 The two sets of flags don't overlap. */
918 return pi->prstatus.pr_flags | pi->prstatus.pr_lwp.pr_flags;
919# else
c3f6f71d 920 return pi->prstatus.pr_lwp.pr_flags;
0d06e24b 921# endif
c3f6f71d
JM
922#else
923 return pi->prstatus.pr_flags;
924#endif
925}
c906108c 926
c3f6f71d
JM
927/*
928 * Function: proc_why
929 *
930 * returns the pr_why field (why the process stopped).
931 */
c906108c 932
c3f6f71d
JM
933int
934proc_why (pi)
935 procinfo *pi;
936{
937 if (!pi->status_valid)
938 if (!proc_get_status (pi))
939 return 0; /* FIXME: not a good failure value (but what is?) */
c906108c 940
c3f6f71d
JM
941#ifdef NEW_PROC_API
942 return pi->prstatus.pr_lwp.pr_why;
943#else
944 return pi->prstatus.pr_why;
945#endif
946}
c906108c 947
c3f6f71d
JM
948/*
949 * Function: proc_what
950 *
951 * returns the pr_what field (details of why the process stopped).
952 */
c906108c 953
c3f6f71d
JM
954int
955proc_what (pi)
956 procinfo *pi;
957{
958 if (!pi->status_valid)
959 if (!proc_get_status (pi))
960 return 0; /* FIXME: not a good failure value (but what is?) */
c906108c 961
c3f6f71d
JM
962#ifdef NEW_PROC_API
963 return pi->prstatus.pr_lwp.pr_what;
964#else
965 return pi->prstatus.pr_what;
c906108c 966#endif
c3f6f71d 967}
c906108c 968
c3f6f71d
JM
969#ifndef PIOCSSPCACT /* The following is not supported on OSF. */
970/*
971 * Function: proc_nsysarg
972 *
973 * returns the pr_nsysarg field (number of args to the current syscall).
974 */
975
976int
977proc_nsysarg (pi)
978 procinfo *pi;
979{
980 if (!pi->status_valid)
981 if (!proc_get_status (pi))
982 return 0;
983
984#ifdef NEW_PROC_API
985 return pi->prstatus.pr_lwp.pr_nsysarg;
986#else
987 return pi->prstatus.pr_nsysarg;
c906108c 988#endif
c3f6f71d 989}
c906108c 990
c3f6f71d
JM
991/*
992 * Function: proc_sysargs
993 *
994 * returns the pr_sysarg field (pointer to the arguments of current syscall).
995 */
c906108c 996
c3f6f71d
JM
997long *
998proc_sysargs (pi)
999 procinfo *pi;
1000{
1001 if (!pi->status_valid)
1002 if (!proc_get_status (pi))
1003 return NULL;
1004
1005#ifdef NEW_PROC_API
1006 return (long *) &pi->prstatus.pr_lwp.pr_sysarg;
1007#else
1008 return (long *) &pi->prstatus.pr_sysarg;
1009#endif
1010}
c906108c 1011
c3f6f71d
JM
1012/*
1013 * Function: proc_syscall
1014 *
1015 * returns the pr_syscall field (id of current syscall if we are in one).
1016 */
c906108c 1017
c3f6f71d
JM
1018int
1019proc_syscall (pi)
1020 procinfo *pi;
1021{
1022 if (!pi->status_valid)
1023 if (!proc_get_status (pi))
1024 return 0;
1025
1026#ifdef NEW_PROC_API
1027 return pi->prstatus.pr_lwp.pr_syscall;
1028#else
1029 return pi->prstatus.pr_syscall;
1030#endif
1031}
1032#endif /* PIOCSSPCACT */
c906108c 1033
c3f6f71d
JM
1034/*
1035 * Function: proc_cursig:
1036 *
1037 * returns the pr_cursig field (current signal).
1038 */
c906108c 1039
c3f6f71d
JM
1040long
1041proc_cursig (struct procinfo *pi)
1042{
1043 if (!pi->status_valid)
1044 if (!proc_get_status (pi))
1045 return 0; /* FIXME: not a good failure value (but what is?) */
c906108c 1046
c3f6f71d
JM
1047#ifdef NEW_PROC_API
1048 return pi->prstatus.pr_lwp.pr_cursig;
1049#else
1050 return pi->prstatus.pr_cursig;
1051#endif
1052}
c906108c 1053
c3f6f71d 1054/*
0d06e24b 1055 * Function: proc_modify_flag
c3f6f71d
JM
1056 *
1057 * === I appologize for the messiness of this function.
1058 * === This is an area where the different versions of
1059 * === /proc are more inconsistent than usual. MVS
1060 *
1061 * Set or reset any of the following process flags:
1062 * PR_FORK -- forked child will inherit trace flags
1063 * PR_RLC -- traced process runs when last /proc file closed.
0d06e24b 1064 * PR_KLC -- traced process is killed when last /proc file closed.
c3f6f71d
JM
1065 * PR_ASYNC -- LWP's get to run/stop independently.
1066 *
1067 * There are three methods for doing this function:
1068 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1069 * [Sol6, Sol7, UW]
1070 * 2) Middle: PIOCSET/PIOCRESET
1071 * [Irix, Sol5]
1072 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1073 * [OSF, Sol5]
1074 *
1075 * Note: Irix does not define PR_ASYNC.
0d06e24b
JM
1076 * Note: OSF does not define PR_KLC.
1077 * Note: OSF is the only one that can ONLY use the oldest method.
c3f6f71d
JM
1078 *
1079 * Arguments:
1080 * pi -- the procinfo
1081 * flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1082 * mode -- 1 for set, 0 for reset.
1083 *
1084 * Returns non-zero for success, zero for failure.
1085 */
c906108c 1086
c3f6f71d 1087enum { FLAG_RESET, FLAG_SET };
c906108c 1088
c3f6f71d
JM
1089static int
1090proc_modify_flag (pi, flag, mode)
1091 procinfo *pi;
1092 long flag;
1093 long mode;
1094{
1095 long win = 0; /* default to fail */
1096
1097 /*
1098 * These operations affect the process as a whole, and applying
1099 * them to an individual LWP has the same meaning as applying them
1100 * to the main process. Therefore, if we're ever called with a
1101 * pointer to an LWP's procinfo, let's substitute the process's
1102 * procinfo and avoid opening the LWP's file descriptor
1103 * unnecessarily.
1104 */
1105
1106 if (pi->pid != 0)
1107 pi = find_procinfo_or_die (pi->pid, 0);
1108
1109#ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii */
1110 /* First normalize the PCUNSET/PCRESET command opcode
1111 (which for no obvious reason has a different definition
1112 from one operating system to the next...) */
1113#ifdef PCUNSET
1114#define GDBRESET PCUNSET
1115#endif
1116#ifdef PCRESET
1117#define GDBRESET PCRESET
c906108c 1118#endif
c3f6f71d
JM
1119 {
1120 long arg[2];
c906108c 1121
c3f6f71d
JM
1122 if (mode == FLAG_SET) /* Set the flag (RLC, FORK, or ASYNC) */
1123 arg[0] = PCSET;
1124 else /* Reset the flag */
1125 arg[0] = GDBRESET;
c5aa993b 1126
c3f6f71d
JM
1127 arg[1] = flag;
1128 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1129 }
1130#else
1131#ifdef PIOCSET /* Irix/Sol5 method */
1132 if (mode == FLAG_SET) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1133 {
1134 win = (ioctl (pi->ctl_fd, PIOCSET, &flag) >= 0);
1135 }
1136 else /* Reset the flag */
1137 {
1138 win = (ioctl (pi->ctl_fd, PIOCRESET, &flag) >= 0);
1139 }
c906108c 1140
c3f6f71d
JM
1141#else
1142#ifdef PIOCSRLC /* Oldest method: OSF */
1143 switch (flag) {
1144 case PR_RLC:
1145 if (mode == FLAG_SET) /* Set run-on-last-close */
1146 {
1147 win = (ioctl (pi->ctl_fd, PIOCSRLC, NULL) >= 0);
1148 }
1149 else /* Clear run-on-last-close */
1150 {
1151 win = (ioctl (pi->ctl_fd, PIOCRRLC, NULL) >= 0);
1152 }
1153 break;
1154 case PR_FORK:
1155 if (mode == FLAG_SET) /* Set inherit-on-fork */
1156 {
1157 win = (ioctl (pi->ctl_fd, PIOCSFORK, NULL) >= 0);
1158 }
1159 else /* Clear inherit-on-fork */
1160 {
1161 win = (ioctl (pi->ctl_fd, PIOCRFORK, NULL) >= 0);
1162 }
1163 break;
1164 default:
1165 win = 0; /* fail -- unknown flag (can't do PR_ASYNC) */
1166 break;
1167 }
1168#endif
1169#endif
1170#endif
1171#undef GDBRESET
1172 /* The above operation renders the procinfo's cached pstatus obsolete. */
1173 pi->status_valid = 0;
c906108c 1174
c3f6f71d
JM
1175 if (!win)
1176 warning ("procfs: modify_flag failed to turn %s %s",
1177 flag == PR_FORK ? "PR_FORK" :
1178 flag == PR_RLC ? "PR_RLC" :
1179#ifdef PR_ASYNC
1180 flag == PR_ASYNC ? "PR_ASYNC" :
0d06e24b
JM
1181#endif
1182#ifdef PR_KLC
1183 flag == PR_KLC ? "PR_KLC" :
c3f6f71d
JM
1184#endif
1185 "<unknown flag>",
1186 mode == FLAG_RESET ? "off" : "on");
c906108c 1187
c3f6f71d
JM
1188 return win;
1189}
c906108c 1190
c3f6f71d
JM
1191/*
1192 * Function: proc_set_run_on_last_close
1193 *
1194 * Set the run_on_last_close flag.
1195 * Process with all threads will become runnable
1196 * when debugger closes all /proc fds.
1197 *
1198 * Returns non-zero for success, zero for failure.
c906108c
SS
1199 */
1200
c3f6f71d
JM
1201int
1202proc_set_run_on_last_close (pi)
1203 procinfo *pi;
c906108c 1204{
c3f6f71d
JM
1205 return proc_modify_flag (pi, PR_RLC, FLAG_SET);
1206}
c906108c 1207
c3f6f71d
JM
1208/*
1209 * Function: proc_unset_run_on_last_close
1210 *
1211 * Reset the run_on_last_close flag.
1212 * Process will NOT become runnable
1213 * when debugger closes its file handles.
1214 *
1215 * Returns non-zero for success, zero for failure.
1216 */
c906108c 1217
c3f6f71d
JM
1218int
1219proc_unset_run_on_last_close (pi)
1220 procinfo *pi;
1221{
1222 return proc_modify_flag (pi, PR_RLC, FLAG_RESET);
c906108c
SS
1223}
1224
0d06e24b
JM
1225#ifdef PR_KLC
1226/*
1227 * Function: proc_set_kill_on_last_close
1228 *
1229 * Set the kill_on_last_close flag.
1230 * Process with all threads will be killed when debugger
1231 * closes all /proc fds (or debugger exits or dies).
1232 *
1233 * Returns non-zero for success, zero for failure.
1234 */
1235
1236int
1237proc_set_kill_on_last_close (pi)
1238 procinfo *pi;
1239{
1240 return proc_modify_flag (pi, PR_KLC, FLAG_SET);
1241}
1242
1243/*
1244 * Function: proc_unset_kill_on_last_close
1245 *
1246 * Reset the kill_on_last_close flag.
1247 * Process will NOT be killed when debugger
1248 * closes its file handles (or exits or dies).
1249 *
1250 * Returns non-zero for success, zero for failure.
1251 */
1252
1253int
1254proc_unset_kill_on_last_close (pi)
1255 procinfo *pi;
1256{
1257 return proc_modify_flag (pi, PR_KLC, FLAG_RESET);
1258}
1259#endif /* PR_KLC */
1260
c906108c 1261/*
c3f6f71d
JM
1262 * Function: proc_set_inherit_on_fork
1263 *
1264 * Set inherit_on_fork flag.
1265 * If the process forks a child while we are registered for events
1266 * in the parent, then we will also recieve events from the child.
1267 *
1268 * Returns non-zero for success, zero for failure.
1269 */
c906108c 1270
c3f6f71d
JM
1271int
1272proc_set_inherit_on_fork (pi)
1273 procinfo *pi;
1274{
1275 return proc_modify_flag (pi, PR_FORK, FLAG_SET);
1276}
c5aa993b 1277
c3f6f71d
JM
1278/*
1279 * Function: proc_unset_inherit_on_fork
1280 *
1281 * Reset inherit_on_fork flag.
1282 * If the process forks a child while we are registered for events
1283 * in the parent, then we will NOT recieve events from the child.
1284 *
1285 * Returns non-zero for success, zero for failure.
1286 */
c906108c 1287
c3f6f71d
JM
1288int
1289proc_unset_inherit_on_fork (pi)
1290 procinfo *pi;
1291{
1292 return proc_modify_flag (pi, PR_FORK, FLAG_RESET);
1293}
c906108c 1294
c3f6f71d
JM
1295#ifdef PR_ASYNC
1296/*
1297 * Function: proc_set_async
1298 *
1299 * Set PR_ASYNC flag.
1300 * If one LWP stops because of a debug event (signal etc.),
1301 * the remaining LWPs will continue to run.
1302 *
1303 * Returns non-zero for success, zero for failure.
1304 */
c906108c 1305
c3f6f71d
JM
1306int
1307proc_set_async (pi)
1308 procinfo *pi;
1309{
1310 return proc_modify_flag (pi, PR_ASYNC, FLAG_SET);
1311}
c906108c 1312
c3f6f71d
JM
1313/*
1314 * Function: proc_unset_async
1315 *
1316 * Reset PR_ASYNC flag.
1317 * If one LWP stops because of a debug event (signal etc.),
1318 * then all other LWPs will stop as well.
1319 *
1320 * Returns non-zero for success, zero for failure.
c906108c
SS
1321 */
1322
c3f6f71d
JM
1323int
1324proc_unset_async (pi)
1325 procinfo *pi;
1326{
1327 return proc_modify_flag (pi, PR_ASYNC, FLAG_RESET);
1328}
1329#endif /* PR_ASYNC */
c906108c
SS
1330
1331/*
c3f6f71d
JM
1332 * Function: proc_stop_process
1333 *
1334 * Request the process/LWP to stop. Does not wait.
1335 * Returns non-zero for success, zero for failure.
1336 */
c906108c 1337
c3f6f71d
JM
1338int
1339proc_stop_process (pi)
1340 procinfo *pi;
1341{
1342 int win;
c906108c 1343
c3f6f71d
JM
1344 /*
1345 * We might conceivably apply this operation to an LWP, and
1346 * the LWP's ctl file descriptor might not be open.
1347 */
c906108c 1348
c3f6f71d
JM
1349 if (pi->ctl_fd == 0 &&
1350 open_procinfo_files (pi, FD_CTL) == 0)
1351 return 0;
1352 else
1353 {
1354#ifdef NEW_PROC_API
6c1a54b2 1355 long cmd = PCSTOP;
c3f6f71d
JM
1356 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1357#else /* ioctl method */
1358 win = (ioctl (pi->ctl_fd, PIOCSTOP, &pi->prstatus) >= 0);
1359 /* Note: the call also reads the prstatus. */
1360 if (win)
1361 {
1362 pi->status_valid = 1;
1363 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1364 proc_why (pi),
1365 proc_what (pi),
1366 proc_get_current_thread (pi));
1367 }
1368#endif
1369 }
c906108c 1370
c3f6f71d
JM
1371 return win;
1372}
c5aa993b 1373
c3f6f71d
JM
1374/*
1375 * Function: proc_wait_for_stop
1376 *
1377 * Wait for the process or LWP to stop (block until it does).
1378 * Returns non-zero for success, zero for failure.
c906108c
SS
1379 */
1380
c3f6f71d
JM
1381int
1382proc_wait_for_stop (pi)
1383 procinfo *pi;
c906108c 1384{
c3f6f71d
JM
1385 int win;
1386
1387 /*
1388 * We should never have to apply this operation to any procinfo
1389 * except the one for the main process. If that ever changes
1390 * for any reason, then take out the following clause and
1391 * replace it with one that makes sure the ctl_fd is open.
1392 */
1393
1394 if (pi->tid != 0)
1395 pi = find_procinfo_or_die (pi->pid, 0);
1396
1397#ifdef NEW_PROC_API
1398 {
6c1a54b2 1399 long cmd = PCWSTOP;
c3f6f71d
JM
1400 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1401 /* We been runnin' and we stopped -- need to update status. */
1402 pi->status_valid = 0;
1403 }
1404#else /* ioctl method */
1405 win = (ioctl (pi->ctl_fd, PIOCWSTOP, &pi->prstatus) >= 0);
1406 /* Above call also refreshes the prstatus. */
1407 if (win)
1408 {
1409 pi->status_valid = 1;
1410 PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1411 proc_why (pi),
1412 proc_what (pi),
1413 proc_get_current_thread (pi));
1414 }
c906108c
SS
1415#endif
1416
c3f6f71d 1417 return win;
c906108c
SS
1418}
1419
1420/*
c3f6f71d
JM
1421 * Function: proc_run_process
1422 *
1423 * Make the process or LWP runnable.
1424 * Options (not all are implemented):
1425 * - single-step
1426 * - clear current fault
1427 * - clear current signal
1428 * - abort the current system call
1429 * - stop as soon as finished with system call
1430 * - (ioctl): set traced signal set
1431 * - (ioctl): set held signal set
1432 * - (ioctl): set traced fault set
1433 * - (ioctl): set start pc (vaddr)
1434 * Always clear the current fault.
1435 * Clear the current signal if 'signo' is zero.
1436 *
1437 * Arguments:
1438 * pi the process or LWP to operate on.
1439 * step if true, set the process or LWP to trap after one instr.
1440 * signo if zero, clear the current signal if any.
1441 * if non-zero, set the current signal to this one.
1442 *
1443 * Returns non-zero for success, zero for failure.
1444 */
1445
1446int
1447proc_run_process (pi, step, signo)
1448 procinfo *pi;
1449 int step;
1450 int signo;
1451{
1452 int win;
1453 int runflags;
1454
1455 /*
1456 * We will probably have to apply this operation to individual threads,
1457 * so make sure the control file descriptor is open.
1458 */
1459
1460 if (pi->ctl_fd == 0 &&
1461 open_procinfo_files (pi, FD_CTL) == 0)
1462 {
1463 return 0;
1464 }
c906108c 1465
c3f6f71d
JM
1466 runflags = PRCFAULT; /* always clear current fault */
1467 if (step)
1468 runflags |= PRSTEP;
1469 if (signo == 0)
1470 runflags |= PRCSIG;
1471 else if (signo != -1) /* -1 means do nothing W.R.T. signals */
1472 proc_set_current_signal (pi, signo);
c5aa993b 1473
c3f6f71d
JM
1474#ifdef NEW_PROC_API
1475 {
6c1a54b2 1476 long cmd[2];
c906108c 1477
c3f6f71d
JM
1478 cmd[0] = PCRUN;
1479 cmd[1] = runflags;
1480 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1481 }
1482#else /* ioctl method */
1483 {
1484 prrun_t prrun;
c906108c 1485
c3f6f71d
JM
1486 memset (&prrun, 0, sizeof (prrun));
1487 prrun.pr_flags = runflags;
1488 win = (ioctl (pi->ctl_fd, PIOCRUN, &prrun) >= 0);
1489 }
1490#endif
c906108c 1491
c3f6f71d
JM
1492 return win;
1493}
c906108c 1494
c3f6f71d
JM
1495/*
1496 * Function: proc_set_traced_signals
1497 *
1498 * Register to trace signals in the process or LWP.
1499 * Returns non-zero for success, zero for failure.
c906108c
SS
1500 */
1501
c3f6f71d
JM
1502int
1503proc_set_traced_signals (pi, sigset)
1504 procinfo *pi;
1505 sigset_t *sigset;
c906108c 1506{
c3f6f71d
JM
1507 int win;
1508
1509 /*
1510 * We should never have to apply this operation to any procinfo
1511 * except the one for the main process. If that ever changes
1512 * for any reason, then take out the following clause and
1513 * replace it with one that makes sure the ctl_fd is open.
1514 */
1515
1516 if (pi->tid != 0)
1517 pi = find_procinfo_or_die (pi->pid, 0);
1518
1519#ifdef NEW_PROC_API
1520 {
1521 struct {
6c1a54b2 1522 long cmd;
c3f6f71d
JM
1523 /* Use char array to avoid alignment issues. */
1524 char sigset[sizeof (sigset_t)];
1525 } arg;
c906108c 1526
c3f6f71d
JM
1527 arg.cmd = PCSTRACE;
1528 memcpy (&arg.sigset, sigset, sizeof (sigset_t));
c906108c 1529
c3f6f71d
JM
1530 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1531 }
1532#else /* ioctl method */
1533 win = (ioctl (pi->ctl_fd, PIOCSTRACE, sigset) >= 0);
1534#endif
1535 /* The above operation renders the procinfo's cached pstatus obsolete. */
1536 pi->status_valid = 0;
c906108c 1537
c3f6f71d
JM
1538 if (!win)
1539 warning ("procfs: set_traced_signals failed");
1540 return win;
c906108c
SS
1541}
1542
1543/*
c3f6f71d
JM
1544 * Function: proc_set_traced_faults
1545 *
1546 * Register to trace hardware faults in the process or LWP.
1547 * Returns non-zero for success, zero for failure.
1548 */
c906108c 1549
c3f6f71d
JM
1550int
1551proc_set_traced_faults (pi, fltset)
1552 procinfo *pi;
1553 fltset_t *fltset;
1554{
1555 int win;
1556
1557 /*
1558 * We should never have to apply this operation to any procinfo
1559 * except the one for the main process. If that ever changes
1560 * for any reason, then take out the following clause and
1561 * replace it with one that makes sure the ctl_fd is open.
1562 */
1563
1564 if (pi->tid != 0)
1565 pi = find_procinfo_or_die (pi->pid, 0);
1566
1567#ifdef NEW_PROC_API
1568 {
1569 struct {
6c1a54b2 1570 long cmd;
c3f6f71d
JM
1571 /* Use char array to avoid alignment issues. */
1572 char fltset[sizeof (fltset_t)];
1573 } arg;
c906108c 1574
c3f6f71d
JM
1575 arg.cmd = PCSFAULT;
1576 memcpy (&arg.fltset, fltset, sizeof (fltset_t));
c906108c 1577
c3f6f71d
JM
1578 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1579 }
1580#else /* ioctl method */
1581 win = (ioctl (pi->ctl_fd, PIOCSFAULT, fltset) >= 0);
1582#endif
1583 /* The above operation renders the procinfo's cached pstatus obsolete. */
1584 pi->status_valid = 0;
c906108c 1585
c3f6f71d
JM
1586 return win;
1587}
c5aa993b 1588
c3f6f71d
JM
1589/*
1590 * Function: proc_set_traced_sysentry
1591 *
1592 * Register to trace entry to system calls in the process or LWP.
1593 * Returns non-zero for success, zero for failure.
c906108c
SS
1594 */
1595
c3f6f71d
JM
1596int
1597proc_set_traced_sysentry (pi, sysset)
1598 procinfo *pi;
1599 sysset_t *sysset;
c906108c 1600{
c3f6f71d
JM
1601 int win;
1602
1603 /*
1604 * We should never have to apply this operation to any procinfo
1605 * except the one for the main process. If that ever changes
1606 * for any reason, then take out the following clause and
1607 * replace it with one that makes sure the ctl_fd is open.
1608 */
1609
1610 if (pi->tid != 0)
1611 pi = find_procinfo_or_die (pi->pid, 0);
1612
1613#ifdef NEW_PROC_API
1614 {
1615 struct {
6c1a54b2 1616 long cmd;
c3f6f71d
JM
1617 /* Use char array to avoid alignment issues. */
1618 char sysset[sizeof (sysset_t)];
1619 } arg;
1620
1621 arg.cmd = PCSENTRY;
1622 memcpy (&arg.sysset, sysset, sizeof (sysset_t));
1623
1624 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1625 }
1626#else /* ioctl method */
1627 win = (ioctl (pi->ctl_fd, PIOCSENTRY, sysset) >= 0);
1628#endif
1629 /* The above operation renders the procinfo's cached pstatus obsolete. */
1630 pi->status_valid = 0;
1631
1632 return win;
c906108c
SS
1633}
1634
1635/*
c3f6f71d
JM
1636 * Function: proc_set_traced_sysexit
1637 *
1638 * Register to trace exit from system calls in the process or LWP.
1639 * Returns non-zero for success, zero for failure.
1640 */
c906108c 1641
c3f6f71d
JM
1642int
1643proc_set_traced_sysexit (pi, sysset)
1644 procinfo *pi;
1645 sysset_t *sysset;
1646{
1647 int win;
1648
1649 /*
1650 * We should never have to apply this operation to any procinfo
1651 * except the one for the main process. If that ever changes
1652 * for any reason, then take out the following clause and
1653 * replace it with one that makes sure the ctl_fd is open.
1654 */
1655
1656 if (pi->tid != 0)
1657 pi = find_procinfo_or_die (pi->pid, 0);
1658
1659#ifdef NEW_PROC_API
1660 {
1661 struct {
6c1a54b2 1662 long cmd;
c3f6f71d
JM
1663 /* Use char array to avoid alignment issues. */
1664 char sysset[sizeof (sysset_t)];
1665 } arg;
c906108c 1666
c3f6f71d
JM
1667 arg.cmd = PCSEXIT;
1668 memcpy (&arg.sysset, sysset, sizeof (sysset_t));
c906108c 1669
c3f6f71d
JM
1670 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1671 }
1672#else /* ioctl method */
1673 win = (ioctl (pi->ctl_fd, PIOCSEXIT, sysset) >= 0);
1674#endif
1675 /* The above operation renders the procinfo's cached pstatus obsolete. */
1676 pi->status_valid = 0;
c906108c 1677
c3f6f71d
JM
1678 return win;
1679}
c906108c 1680
c3f6f71d
JM
1681/*
1682 * Function: proc_set_held_signals
1683 *
1684 * Specify the set of blocked / held signals in the process or LWP.
1685 * Returns non-zero for success, zero for failure.
c906108c
SS
1686 */
1687
c3f6f71d
JM
1688int
1689proc_set_held_signals (pi, sighold)
1690 procinfo *pi;
1691 sigset_t *sighold;
c906108c 1692{
c3f6f71d
JM
1693 int win;
1694
1695 /*
1696 * We should never have to apply this operation to any procinfo
1697 * except the one for the main process. If that ever changes
1698 * for any reason, then take out the following clause and
1699 * replace it with one that makes sure the ctl_fd is open.
1700 */
1701
1702 if (pi->tid != 0)
1703 pi = find_procinfo_or_die (pi->pid, 0);
1704
1705#ifdef NEW_PROC_API
1706 {
1707 struct {
6c1a54b2 1708 long cmd;
c3f6f71d
JM
1709 /* Use char array to avoid alignment issues. */
1710 char hold[sizeof (sigset_t)];
1711 } arg;
1712
1713 arg.cmd = PCSHOLD;
1714 memcpy (&arg.hold, sighold, sizeof (sigset_t));
1715 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1716 }
c906108c 1717#else
c3f6f71d 1718 win = (ioctl (pi->ctl_fd, PIOCSHOLD, sighold) >= 0);
c906108c 1719#endif
c3f6f71d
JM
1720 /* The above operation renders the procinfo's cached pstatus obsolete. */
1721 pi->status_valid = 0;
1722
1723 return win;
c906108c
SS
1724}
1725
1726/*
c3f6f71d
JM
1727 * Function: proc_get_pending_signals
1728 *
1729 * returns the set of signals that are pending in the process or LWP.
1730 * Will also copy the sigset if 'save' is non-zero.
1731 */
c906108c 1732
c3f6f71d
JM
1733sigset_t *
1734proc_get_pending_signals (pi, save)
1735 procinfo *pi;
1736 sigset_t *save;
1737{
1738 sigset_t *ret = NULL;
1739
1740 /*
1741 * We should never have to apply this operation to any procinfo
1742 * except the one for the main process. If that ever changes
1743 * for any reason, then take out the following clause and
1744 * replace it with one that makes sure the ctl_fd is open.
1745 */
1746
1747 if (pi->tid != 0)
1748 pi = find_procinfo_or_die (pi->pid, 0);
1749
1750 if (!pi->status_valid)
1751 if (!proc_get_status (pi))
1752 return NULL;
1753
1754#ifdef NEW_PROC_API
1755 ret = &pi->prstatus.pr_lwp.pr_lwppend;
1756#else
1757 ret = &pi->prstatus.pr_sigpend;
1758#endif
1759 if (save && ret)
1760 memcpy (save, ret, sizeof (sigset_t));
c906108c 1761
c3f6f71d
JM
1762 return ret;
1763}
c906108c 1764
c3f6f71d
JM
1765/*
1766 * Function: proc_get_signal_actions
1767 *
1768 * returns the set of signal actions.
1769 * Will also copy the sigactionset if 'save' is non-zero.
1770 */
c906108c 1771
c3f6f71d
JM
1772struct sigaction *
1773proc_get_signal_actions (pi, save)
1774 procinfo *pi;
1775 struct sigaction *save;
1776{
1777 struct sigaction *ret = NULL;
1778
1779 /*
1780 * We should never have to apply this operation to any procinfo
1781 * except the one for the main process. If that ever changes
1782 * for any reason, then take out the following clause and
1783 * replace it with one that makes sure the ctl_fd is open.
1784 */
1785
1786 if (pi->tid != 0)
1787 pi = find_procinfo_or_die (pi->pid, 0);
1788
1789 if (!pi->status_valid)
1790 if (!proc_get_status (pi))
1791 return NULL;
1792
1793#ifdef NEW_PROC_API
1794 ret = &pi->prstatus.pr_lwp.pr_action;
1795#else
1796 ret = &pi->prstatus.pr_action;
1797#endif
1798 if (save && ret)
1799 memcpy (save, ret, sizeof (struct sigaction));
c906108c 1800
c3f6f71d
JM
1801 return ret;
1802}
c5aa993b 1803
c3f6f71d
JM
1804/*
1805 * Function: proc_get_held_signals
1806 *
1807 * returns the set of signals that are held / blocked.
1808 * Will also copy the sigset if 'save' is non-zero.
c906108c
SS
1809 */
1810
c3f6f71d
JM
1811sigset_t *
1812proc_get_held_signals (pi, save)
1813 procinfo *pi;
1814 sigset_t *save;
c906108c 1815{
c3f6f71d
JM
1816 sigset_t *ret = NULL;
1817
1818 /*
1819 * We should never have to apply this operation to any procinfo
1820 * except the one for the main process. If that ever changes
1821 * for any reason, then take out the following clause and
1822 * replace it with one that makes sure the ctl_fd is open.
1823 */
1824
1825 if (pi->tid != 0)
1826 pi = find_procinfo_or_die (pi->pid, 0);
1827
1828#ifdef NEW_PROC_API
1829 if (!pi->status_valid)
1830 if (!proc_get_status (pi))
1831 return NULL;
1832
1833#ifdef UNIXWARE
1834 ret = &pi->prstatus.pr_lwp.pr_context.uc_sigmask;
c906108c 1835#else
c3f6f71d
JM
1836 ret = &pi->prstatus.pr_lwp.pr_lwphold;
1837#endif /* UNIXWARE */
1838#else /* not NEW_PROC_API */
1839 {
1840 static sigset_t sigheld;
1841
1842 if (ioctl (pi->ctl_fd, PIOCGHOLD, &sigheld) >= 0)
1843 ret = &sigheld;
1844 }
1845#endif /* NEW_PROC_API */
1846 if (save && ret)
1847 memcpy (save, ret, sizeof (sigset_t));
1848
1849 return ret;
c906108c
SS
1850}
1851
c3f6f71d
JM
1852/*
1853 * Function: proc_get_traced_signals
1854 *
1855 * returns the set of signals that are traced / debugged.
1856 * Will also copy the sigset if 'save' is non-zero.
1857 */
1858
1859sigset_t *
1860proc_get_traced_signals (pi, save)
1861 procinfo *pi;
1862 sigset_t *save;
c906108c 1863{
c3f6f71d
JM
1864 sigset_t *ret = NULL;
1865
1866 /*
1867 * We should never have to apply this operation to any procinfo
1868 * except the one for the main process. If that ever changes
1869 * for any reason, then take out the following clause and
1870 * replace it with one that makes sure the ctl_fd is open.
1871 */
1872
1873 if (pi->tid != 0)
1874 pi = find_procinfo_or_die (pi->pid, 0);
1875
1876#ifdef NEW_PROC_API
1877 if (!pi->status_valid)
1878 if (!proc_get_status (pi))
1879 return NULL;
1880
1881 ret = &pi->prstatus.pr_sigtrace;
1882#else
1883 {
1884 static sigset_t sigtrace;
1885
1886 if (ioctl (pi->ctl_fd, PIOCGTRACE, &sigtrace) >= 0)
1887 ret = &sigtrace;
1888 }
c906108c 1889#endif
c3f6f71d
JM
1890 if (save && ret)
1891 memcpy (save, ret, sizeof (sigset_t));
c906108c 1892
c3f6f71d
JM
1893 return ret;
1894}
c906108c 1895
c3f6f71d
JM
1896/*
1897 * Function: proc_trace_signal
1898 *
1899 * Add 'signo' to the set of signals that are traced.
1900 * Returns non-zero for success, zero for failure.
1901 */
c906108c 1902
c3f6f71d
JM
1903int
1904proc_trace_signal (pi, signo)
1905 procinfo *pi;
1906 int signo;
1907{
1908 sigset_t temp;
1909
1910 /*
1911 * We should never have to apply this operation to any procinfo
1912 * except the one for the main process. If that ever changes
1913 * for any reason, then take out the following clause and
1914 * replace it with one that makes sure the ctl_fd is open.
1915 */
1916
1917 if (pi->tid != 0)
1918 pi = find_procinfo_or_die (pi->pid, 0);
1919
1920 if (pi)
c906108c 1921 {
c3f6f71d 1922 if (proc_get_traced_signals (pi, &temp))
c906108c 1923 {
c3f6f71d
JM
1924 praddset (&temp, signo);
1925 return proc_set_traced_signals (pi, &temp);
c906108c
SS
1926 }
1927 }
c5aa993b 1928
c3f6f71d
JM
1929 return 0; /* failure */
1930}
c906108c 1931
c3f6f71d
JM
1932/*
1933 * Function: proc_ignore_signal
1934 *
1935 * Remove 'signo' from the set of signals that are traced.
1936 * Returns non-zero for success, zero for failure.
1937 */
c906108c 1938
c3f6f71d
JM
1939int
1940proc_ignore_signal (pi, signo)
1941 procinfo *pi;
1942 int signo;
1943{
1944 sigset_t temp;
1945
1946 /*
1947 * We should never have to apply this operation to any procinfo
1948 * except the one for the main process. If that ever changes
1949 * for any reason, then take out the following clause and
1950 * replace it with one that makes sure the ctl_fd is open.
1951 */
1952
1953 if (pi->tid != 0)
1954 pi = find_procinfo_or_die (pi->pid, 0);
1955
1956 if (pi)
c906108c 1957 {
c3f6f71d 1958 if (proc_get_traced_signals (pi, &temp))
c906108c 1959 {
c3f6f71d
JM
1960 prdelset (&temp, signo);
1961 return proc_set_traced_signals (pi, &temp);
c906108c 1962 }
c906108c 1963 }
c906108c 1964
c3f6f71d 1965 return 0; /* failure */
c906108c
SS
1966}
1967
1968/*
c3f6f71d
JM
1969 * Function: proc_get_traced_faults
1970 *
1971 * returns the set of hardware faults that are traced /debugged.
1972 * Will also copy the faultset if 'save' is non-zero.
1973 */
1974
1975fltset_t *
1976proc_get_traced_faults (pi, save)
1977 procinfo *pi;
1978 fltset_t *save;
1979{
1980 fltset_t *ret = NULL;
1981
1982 /*
1983 * We should never have to apply this operation to any procinfo
1984 * except the one for the main process. If that ever changes
1985 * for any reason, then take out the following clause and
1986 * replace it with one that makes sure the ctl_fd is open.
1987 */
1988
1989 if (pi->tid != 0)
1990 pi = find_procinfo_or_die (pi->pid, 0);
1991
1992#ifdef NEW_PROC_API
1993 if (!pi->status_valid)
1994 if (!proc_get_status (pi))
1995 return NULL;
1996
1997 ret = &pi->prstatus.pr_flttrace;
1998#else
1999 {
2000 static fltset_t flttrace;
2001
2002 if (ioctl (pi->ctl_fd, PIOCGFAULT, &flttrace) >= 0)
2003 ret = &flttrace;
2004 }
2005#endif
2006 if (save && ret)
2007 memcpy (save, ret, sizeof (fltset_t));
c906108c 2008
c3f6f71d
JM
2009 return ret;
2010}
c906108c 2011
c3f6f71d
JM
2012/*
2013 * Function: proc_get_traced_sysentry
2014 *
2015 * returns the set of syscalls that are traced /debugged on entry.
2016 * Will also copy the syscall set if 'save' is non-zero.
2017 */
c906108c 2018
c3f6f71d
JM
2019sysset_t *
2020proc_get_traced_sysentry (pi, save)
2021 procinfo *pi;
2022 sysset_t *save;
2023{
2024 sysset_t *ret = NULL;
2025
2026 /*
2027 * We should never have to apply this operation to any procinfo
2028 * except the one for the main process. If that ever changes
2029 * for any reason, then take out the following clause and
2030 * replace it with one that makes sure the ctl_fd is open.
2031 */
2032
2033 if (pi->tid != 0)
2034 pi = find_procinfo_or_die (pi->pid, 0);
2035
2036#ifdef NEW_PROC_API
2037 if (!pi->status_valid)
2038 if (!proc_get_status (pi))
2039 return NULL;
2040
2041 ret = &pi->prstatus.pr_sysentry;
2042#else
2043 {
2044 static sysset_t sysentry;
c906108c 2045
c3f6f71d
JM
2046 if (ioctl (pi->ctl_fd, PIOCGENTRY, &sysentry) >= 0)
2047 ret = &sysentry;
2048 }
2049#endif
2050 if (save && ret)
2051 memcpy (save, ret, sizeof (sysset_t));
c906108c 2052
c3f6f71d
JM
2053 return ret;
2054}
c5aa993b 2055
c3f6f71d
JM
2056/*
2057 * Function: proc_get_traced_sysexit
2058 *
2059 * returns the set of syscalls that are traced /debugged on exit.
2060 * Will also copy the syscall set if 'save' is non-zero.
c906108c
SS
2061 */
2062
c3f6f71d
JM
2063sysset_t *
2064proc_get_traced_sysexit (pi, save)
2065 procinfo *pi;
2066 sysset_t *save;
c906108c 2067{
c3f6f71d
JM
2068 sysset_t * ret = NULL;
2069
2070 /*
2071 * We should never have to apply this operation to any procinfo
2072 * except the one for the main process. If that ever changes
2073 * for any reason, then take out the following clause and
2074 * replace it with one that makes sure the ctl_fd is open.
2075 */
2076
2077 if (pi->tid != 0)
2078 pi = find_procinfo_or_die (pi->pid, 0);
2079
2080#ifdef NEW_PROC_API
2081 if (!pi->status_valid)
2082 if (!proc_get_status (pi))
2083 return NULL;
2084
2085 ret = &pi->prstatus.pr_sysexit;
2086#else
2087 {
2088 static sysset_t sysexit;
c5aa993b 2089
c3f6f71d
JM
2090 if (ioctl (pi->ctl_fd, PIOCGEXIT, &sysexit) >= 0)
2091 ret = &sysexit;
2092 }
2093#endif
2094 if (save && ret)
2095 memcpy (save, ret, sizeof (sysset_t));
2096
2097 return ret;
2098}
c906108c 2099
c3f6f71d
JM
2100/*
2101 * Function: proc_clear_current_fault
2102 *
2103 * The current fault (if any) is cleared; the associated signal
2104 * will not be sent to the process or LWP when it resumes.
2105 * Returns non-zero for success, zero for failure.
2106 */
c906108c 2107
c3f6f71d
JM
2108int
2109proc_clear_current_fault (pi)
2110 procinfo *pi;
2111{
2112 int win;
2113
2114 /*
2115 * We should never have to apply this operation to any procinfo
2116 * except the one for the main process. If that ever changes
2117 * for any reason, then take out the following clause and
2118 * replace it with one that makes sure the ctl_fd is open.
2119 */
2120
2121 if (pi->tid != 0)
2122 pi = find_procinfo_or_die (pi->pid, 0);
2123
2124#ifdef NEW_PROC_API
2125 {
6c1a54b2 2126 long cmd = PCCFAULT;
c3f6f71d
JM
2127 win = (write (pi->ctl_fd, (void *) &cmd, sizeof (cmd)) == sizeof (cmd));
2128 }
2129#else
2130 win = (ioctl (pi->ctl_fd, PIOCCFAULT, 0) >= 0);
2131#endif
2132
2133 return win;
c906108c
SS
2134}
2135
2136/*
c3f6f71d
JM
2137 * Function: proc_set_current_signal
2138 *
2139 * Set the "current signal" that will be delivered next to the process.
2140 * NOTE: semantics are different from those of KILL.
2141 * This signal will be delivered to the process or LWP
2142 * immediately when it is resumed (even if the signal is held/blocked);
2143 * it will NOT immediately cause another event of interest, and will NOT
2144 * first trap back to the debugger.
2145 *
2146 * Returns non-zero for success, zero for failure.
2147 */
2148
2149int
2150proc_set_current_signal (pi, signo)
2151 procinfo *pi;
2152 int signo;
2153{
2154 int win;
2155 struct {
6c1a54b2 2156 long cmd;
c3f6f71d
JM
2157 /* Use char array to avoid alignment issues. */
2158 char sinfo[sizeof (struct siginfo)];
2159 } arg;
2160 struct siginfo *mysinfo;
2161
2162 /*
2163 * We should never have to apply this operation to any procinfo
2164 * except the one for the main process. If that ever changes
2165 * for any reason, then take out the following clause and
2166 * replace it with one that makes sure the ctl_fd is open.
2167 */
2168
2169 if (pi->tid != 0)
2170 pi = find_procinfo_or_die (pi->pid, 0);
2171
2172#ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2173 /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2174 * receives a PIOCSSIG with a signal identical to the current signal,
2175 * it messes up the current signal. Work around the kernel bug.
2176 */
2177 if (signo > 0 &&
2178 signo == proc_cursig (pi))
2179 return 1; /* I assume this is a success? */
2180#endif
2181
2182 /* The pointer is just a type alias. */
2183 mysinfo = (struct siginfo *) &arg.sinfo;
2184 mysinfo->si_signo = signo;
2185 mysinfo->si_code = 0;
2186 mysinfo->si_pid = getpid (); /* ?why? */
2187 mysinfo->si_uid = getuid (); /* ?why? */
2188
2189#ifdef NEW_PROC_API
2190 arg.cmd = PCSSIG;
2191 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2192#else
2193 win = (ioctl (pi->ctl_fd, PIOCSSIG, (void *) &arg.sinfo) >= 0);
2194#endif
c906108c 2195
c3f6f71d
JM
2196 return win;
2197}
c906108c 2198
c3f6f71d
JM
2199/*
2200 * Function: proc_clear_current_signal
2201 *
2202 * The current signal (if any) is cleared, and
2203 * is not sent to the process or LWP when it resumes.
2204 * Returns non-zero for success, zero for failure.
2205 */
c906108c 2206
c3f6f71d
JM
2207int
2208proc_clear_current_signal (pi)
2209 procinfo *pi;
2210{
2211 int win;
2212
2213 /*
2214 * We should never have to apply this operation to any procinfo
2215 * except the one for the main process. If that ever changes
2216 * for any reason, then take out the following clause and
2217 * replace it with one that makes sure the ctl_fd is open.
2218 */
2219
2220 if (pi->tid != 0)
2221 pi = find_procinfo_or_die (pi->pid, 0);
2222
2223#ifdef NEW_PROC_API
2224 {
2225 struct {
6c1a54b2 2226 long cmd;
c3f6f71d
JM
2227 /* Use char array to avoid alignment issues. */
2228 char sinfo[sizeof (struct siginfo)];
2229 } arg;
2230 struct siginfo *mysinfo;
2231
2232 arg.cmd = PCSSIG;
2233 /* The pointer is just a type alias. */
2234 mysinfo = (struct siginfo *) &arg.sinfo;
2235 mysinfo->si_signo = 0;
2236 mysinfo->si_code = 0;
2237 mysinfo->si_errno = 0;
2238 mysinfo->si_pid = getpid (); /* ?why? */
2239 mysinfo->si_uid = getuid (); /* ?why? */
2240
2241 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2242 }
2243#else
2244 win = (ioctl (pi->ctl_fd, PIOCSSIG, 0) >= 0);
2245#endif
c906108c 2246
c3f6f71d
JM
2247 return win;
2248}
c906108c 2249
c3f6f71d
JM
2250/*
2251 * Function: proc_get_gregs
2252 *
2253 * Get the general registers for the process or LWP.
2254 * Returns non-zero for success, zero for failure.
2255 */
c906108c 2256
c3f6f71d
JM
2257gdb_gregset_t *
2258proc_get_gregs (pi)
2259 procinfo *pi;
2260{
2261 if (!pi->status_valid || !pi->gregs_valid)
2262 if (!proc_get_status (pi))
2263 return NULL;
2264
2265 /*
2266 * OK, sorry about the ifdef's.
2267 * There's three cases instead of two, because
2268 * in this instance Unixware and Solaris/RW differ.
2269 */
2270
2271#ifdef NEW_PROC_API
2272#ifdef UNIXWARE /* ugh, a true architecture dependency */
2273 return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.gregs;
2274#else /* not Unixware */
2275 return &pi->prstatus.pr_lwp.pr_reg;
2276#endif /* Unixware */
2277#else /* not NEW_PROC_API */
2278 return &pi->prstatus.pr_reg;
2279#endif /* NEW_PROC_API */
2280}
c5aa993b 2281
c3f6f71d
JM
2282/*
2283 * Function: proc_get_fpregs
2284 *
2285 * Get the floating point registers for the process or LWP.
2286 * Returns non-zero for success, zero for failure.
c906108c
SS
2287 */
2288
c3f6f71d
JM
2289gdb_fpregset_t *
2290proc_get_fpregs (pi)
2291 procinfo *pi;
c906108c 2292{
c3f6f71d
JM
2293#ifdef NEW_PROC_API
2294 if (!pi->status_valid || !pi->fpregs_valid)
2295 if (!proc_get_status (pi))
2296 return NULL;
2297
2298#ifdef UNIXWARE /* a true architecture dependency */
2299 return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.fpregs;
2300#else
2301 return &pi->prstatus.pr_lwp.pr_fpreg;
2302#endif /* Unixware */
c5aa993b 2303
c3f6f71d
JM
2304#else /* not NEW_PROC_API */
2305 if (pi->fpregs_valid)
2306 return &pi->fpregset; /* already got 'em */
2307 else
c906108c 2308 {
c3f6f71d
JM
2309 if (pi->ctl_fd == 0 &&
2310 open_procinfo_files (pi, FD_CTL) == 0)
c906108c 2311 {
c3f6f71d 2312 return NULL;
c906108c 2313 }
c3f6f71d 2314 else
c906108c 2315 {
c3f6f71d
JM
2316#ifdef PIOCTGFPREG
2317 struct {
2318 long pr_count;
2319 tid_t pr_error_thread;
2320 tfpregset_t thread_1;
2321 } thread_fpregs;
2322
2323 thread_fpregs.pr_count = 1;
2324 thread_fpregs.thread_1.tid = pi->tid;
2325
2326 if (pi->tid == 0 &&
2327 ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2328 {
2329 pi->fpregs_valid = 1;
2330 return &pi->fpregset; /* got 'em now! */
2331 }
2332 else if (pi->tid != 0 &&
2333 ioctl (pi->ctl_fd, PIOCTGFPREG, &thread_fpregs) >= 0)
2334 {
2335 memcpy (&pi->fpregset, &thread_fpregs.thread_1.pr_fpregs,
2336 sizeof (pi->fpregset));
2337 pi->fpregs_valid = 1;
2338 return &pi->fpregset; /* got 'em now! */
2339 }
2340 else
2341 {
2342 return NULL;
2343 }
2344#else
2345 if (ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2346 {
2347 pi->fpregs_valid = 1;
2348 return &pi->fpregset; /* got 'em now! */
2349 }
2350 else
2351 {
2352 return NULL;
2353 }
2354#endif
c906108c 2355 }
c906108c 2356 }
c3f6f71d 2357#endif
c906108c
SS
2358}
2359
c3f6f71d
JM
2360/*
2361 * Function: proc_set_gregs
2362 *
2363 * Write the general registers back to the process or LWP.
2364 * Returns non-zero for success, zero for failure.
2365 */
2366
2367int
2368proc_set_gregs (pi)
2369 procinfo *pi;
c906108c 2370{
c3f6f71d
JM
2371 gdb_gregset_t *gregs;
2372 int win;
c5aa993b 2373
c3f6f71d
JM
2374 if ((gregs = proc_get_gregs (pi)) == NULL)
2375 return 0; /* get_regs has already warned */
2376
2377 if (pi->ctl_fd == 0 &&
2378 open_procinfo_files (pi, FD_CTL) == 0)
c906108c 2379 {
c3f6f71d 2380 return 0;
c906108c 2381 }
c3f6f71d 2382 else
c906108c 2383 {
c3f6f71d
JM
2384#ifdef NEW_PROC_API
2385 struct {
6c1a54b2 2386 long cmd;
c3f6f71d
JM
2387 /* Use char array to avoid alignment issues. */
2388 char gregs[sizeof (gdb_gregset_t)];
2389 } arg;
2390
2391 arg.cmd = PCSREG;
2392 memcpy (&arg.gregs, gregs, sizeof (arg.gregs));
2393 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2394#else
2395 win = (ioctl (pi->ctl_fd, PIOCSREG, gregs) >= 0);
2396#endif
c906108c 2397 }
c3f6f71d
JM
2398
2399 /* Policy: writing the regs invalidates our cache. */
2400 pi->gregs_valid = 0;
2401 return win;
c906108c
SS
2402}
2403
c3f6f71d
JM
2404/*
2405 * Function: proc_set_fpregs
2406 *
2407 * Modify the floating point register set of the process or LWP.
2408 * Returns non-zero for success, zero for failure.
2409 */
2410
2411int
2412proc_set_fpregs (pi)
2413 procinfo *pi;
c906108c 2414{
c3f6f71d
JM
2415 gdb_fpregset_t *fpregs;
2416 int win;
2417
2418 if ((fpregs = proc_get_fpregs (pi)) == NULL)
2419 return 0; /* get_fpregs has already warned */
c5aa993b 2420
c3f6f71d
JM
2421 if (pi->ctl_fd == 0 &&
2422 open_procinfo_files (pi, FD_CTL) == 0)
c906108c 2423 {
c3f6f71d 2424 return 0;
c906108c 2425 }
c3f6f71d 2426 else
c906108c 2427 {
c3f6f71d
JM
2428#ifdef NEW_PROC_API
2429 struct {
6c1a54b2 2430 long cmd;
c3f6f71d
JM
2431 /* Use char array to avoid alignment issues. */
2432 char fpregs[sizeof (gdb_fpregset_t)];
2433 } arg;
2434
2435 arg.cmd = PCSFPREG;
2436 memcpy (&arg.fpregs, fpregs, sizeof (arg.fpregs));
2437 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2438#else
2439#ifdef PIOCTSFPREG
2440 if (pi->tid == 0)
2441 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2442 else
2443 {
2444 struct {
2445 long pr_count;
2446 tid_t pr_error_thread;
2447 tfpregset_t thread_1;
2448 } thread_fpregs;
2449
2450 thread_fpregs.pr_count = 1;
2451 thread_fpregs.thread_1.tid = pi->tid;
2452 memcpy (&thread_fpregs.thread_1.pr_fpregs, fpregs,
2453 sizeof (*fpregs));
2454 win = (ioctl (pi->ctl_fd, PIOCTSFPREG, &thread_fpregs) >= 0);
2455 }
2456#else
2457 win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2458#endif /* osf PIOCTSFPREG */
2459#endif /* NEW_PROC_API */
c906108c 2460 }
c3f6f71d
JM
2461
2462 /* Policy: writing the regs invalidates our cache. */
2463 pi->fpregs_valid = 0;
2464 return win;
c906108c
SS
2465}
2466
2467/*
c3f6f71d
JM
2468 * Function: proc_kill
2469 *
2470 * Send a signal to the proc or lwp with the semantics of "kill()".
2471 * Returns non-zero for success, zero for failure.
2472 */
c906108c 2473
c3f6f71d
JM
2474int
2475proc_kill (pi, signo)
2476 procinfo *pi;
2477 int signo;
2478{
2479 int win;
c906108c 2480
c3f6f71d
JM
2481 /*
2482 * We might conceivably apply this operation to an LWP, and
2483 * the LWP's ctl file descriptor might not be open.
2484 */
c906108c 2485
c3f6f71d
JM
2486 if (pi->ctl_fd == 0 &&
2487 open_procinfo_files (pi, FD_CTL) == 0)
2488 {
2489 return 0;
2490 }
2491 else
2492 {
2493#ifdef NEW_PROC_API
6c1a54b2 2494 long cmd[2];
c906108c 2495
c3f6f71d
JM
2496 cmd[0] = PCKILL;
2497 cmd[1] = signo;
2498 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
2499#else /* ioctl method */
2500 /* FIXME: do I need the Alpha OSF fixups present in
2501 procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */
2502 win = (ioctl (pi->ctl_fd, PIOCKILL, &signo) >= 0);
2503#endif
2504 }
c906108c 2505
c3f6f71d
JM
2506 return win;
2507}
c906108c 2508
c3f6f71d
JM
2509/*
2510 * Function: proc_parent_pid
2511 *
2512 * Find the pid of the process that started this one.
2513 * Returns the parent process pid, or zero.
c906108c
SS
2514 */
2515
c3f6f71d
JM
2516int
2517proc_parent_pid (pi)
2518 procinfo *pi;
c906108c 2519{
c3f6f71d
JM
2520 /*
2521 * We should never have to apply this operation to any procinfo
2522 * except the one for the main process. If that ever changes
2523 * for any reason, then take out the following clause and
2524 * replace it with one that makes sure the ctl_fd is open.
2525 */
2526
2527 if (pi->tid != 0)
2528 pi = find_procinfo_or_die (pi->pid, 0);
2529
2530 if (!pi->status_valid)
2531 if (!proc_get_status (pi))
2532 return 0;
c5aa993b 2533
c3f6f71d
JM
2534 return pi->prstatus.pr_ppid;
2535}
2536
2537
2538/*
2539 * Function: proc_set_watchpoint
2540 *
2541 */
2542
2543int
2544proc_set_watchpoint (pi, addr, len, wflags)
03905a3c
MS
2545 procinfo *pi;
2546 CORE_ADDR addr;
c3f6f71d
JM
2547 int len;
2548 int wflags;
2549{
2550#if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)
2551 return 0;
2552#else
2553/* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5 */
2554#if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2555 return 0;
2556#else
2557 struct {
6c1a54b2 2558 long cmd;
c3f6f71d
JM
2559 char watch[sizeof (prwatch_t)];
2560 } arg;
2561 prwatch_t *pwatch;
2562
2563 pwatch = (prwatch_t *) &arg.watch;
ac2e2ef7 2564 pwatch->pr_vaddr = address_to_host_pointer (addr);
c3f6f71d
JM
2565 pwatch->pr_size = len;
2566 pwatch->pr_wflags = wflags;
2567#if defined(NEW_PROC_API) && defined (PCWATCH)
2568 arg.cmd = PCWATCH;
2569 return (write (pi->ctl_fd, &arg, sizeof (arg)) == sizeof (arg));
2570#else
2571#if defined (PIOCSWATCH)
2572 return (ioctl (pi->ctl_fd, PIOCSWATCH, pwatch) >= 0);
2573#else
2574 return 0; /* Fail */
2575#endif
2576#endif
2577#endif
2578#endif
c906108c
SS
2579}
2580
2581/*
c3f6f71d
JM
2582 * Function: proc_iterate_over_mappings
2583 *
2584 * Given a pointer to a function, call that function once for every
2585 * mapped address space in the process. The callback function
2586 * receives an open file descriptor for the file corresponding to
2587 * that mapped address space (if there is one), and the base address
2588 * of the mapped space. Quit when the callback function returns a
2589 * nonzero value, or at teh end of the mappings.
2590 *
2591 * Returns: the first non-zero return value of the callback function,
2592 * or zero.
2593 */
c906108c 2594
c3f6f71d
JM
2595/* FIXME: it's probably a waste to cache this FD.
2596 It doesn't get called that often... and if I open it
2597 every time, I don't need to lseek it. */
2598int
2599proc_iterate_over_mappings (func)
507f3c78 2600 int (*func) (int, CORE_ADDR);
c3f6f71d
JM
2601{
2602 struct prmap *map;
2603 procinfo *pi;
0d06e24b 2604#ifndef NEW_PROC_API /* avoid compiler warning */
0fda6bd2
JM
2605 int nmaps = 0;
2606 int i;
2607#else
2608 int map_fd;
2609 char pathname[MAX_PROC_NAME_SIZE];
0d06e24b 2610#endif
c3f6f71d 2611 int funcstat = 0;
0fda6bd2 2612 int fd;
c906108c 2613
c3f6f71d 2614 pi = find_procinfo_or_die (PIDGET (inferior_pid), 0);
c906108c 2615
c3f6f71d
JM
2616#ifdef NEW_PROC_API
2617 /* Open map fd. */
2618 sprintf (pathname, "/proc/%d/map", pi->pid);
2619 if ((map_fd = open (pathname, O_RDONLY)) < 0)
2620 proc_error (pi, "proc_iterate_over_mappings (open)", __LINE__);
c906108c 2621
c3f6f71d 2622 /* Make sure it gets closed again. */
004527cb 2623 make_cleanup_close (map_fd);
c906108c 2624
c3f6f71d
JM
2625 /* Allocate space for mapping (lifetime only for this function). */
2626 map = alloca (sizeof (struct prmap));
c906108c 2627
c3f6f71d
JM
2628 /* Now read the mappings from the file,
2629 open a file descriptor for those that have a name,
2630 and call the callback function. */
2631 while (read (map_fd,
2632 (void *) map,
2633 sizeof (struct prmap)) == sizeof (struct prmap))
2634 {
2635 char name[MAX_PROC_NAME_SIZE + sizeof (map->pr_mapname)];
c906108c 2636
c3f6f71d
JM
2637 if (map->pr_vaddr == 0 && map->pr_size == 0)
2638 break; /* sanity */
c906108c 2639
c3f6f71d
JM
2640 if (map->pr_mapname[0] == 0)
2641 {
2642 fd = -1; /* no map file */
2643 }
2644 else
2645 {
2646 sprintf (name, "/proc/%d/object/%s", pi->pid, map->pr_mapname);
2647 /* Note: caller's responsibility to close this fd! */
2648 fd = open (name, O_RDONLY);
2649 /* Note: we don't test the above call for failure;
2650 we just pass the FD on as given. Sometimes there is
2651 no file, so the ioctl may return failure, but that's
2652 not a problem. */
2653 }
c906108c 2654
c3f6f71d
JM
2655 /* Stop looping if the callback returns non-zero. */
2656 if ((funcstat = (*func) (fd, (CORE_ADDR) map->pr_vaddr)) != 0)
2657 break;
2658 }
2659#else
2660 /* Get the number of mapping entries. */
2661 if (ioctl (pi->ctl_fd, PIOCNMAP, &nmaps) < 0)
2662 proc_error (pi, "proc_iterate_over_mappings (PIOCNMAP)", __LINE__);
2663
2664 /* Allocate space for mappings (lifetime only this function). */
2665 map = (struct prmap *) alloca ((nmaps + 1) * sizeof (struct prmap));
2666
2667 /* Read in all the mappings. */
2668 if (ioctl (pi->ctl_fd, PIOCMAP, map) < 0)
2669 proc_error (pi, "proc_iterate_over_mappings (PIOCMAP)", __LINE__);
2670
2671 /* Now loop through the mappings, open an fd for each, and
2672 call the callback function. */
2673 for (i = 0;
2674 i < nmaps && map[i].pr_size != 0;
2675 i++)
2676 {
2677 /* Note: caller's responsibility to close this fd! */
2678 fd = ioctl (pi->ctl_fd, PIOCOPENM, &map[i].pr_vaddr);
2679 /* Note: we don't test the above call for failure;
2680 we just pass the FD on as given. Sometimes there is
2681 no file, so the ioctl may return failure, but that's
2682 not a problem. */
2683
2684 /* Stop looping if the callback returns non-zero. */
ac2e2ef7
AC
2685 funcstat = (*func) (fd, host_pointer_to_address (map[i].pr_vaddr));
2686 if (funcstat != 0)
c3f6f71d
JM
2687 break;
2688 }
c906108c 2689#endif
c906108c 2690
c3f6f71d
JM
2691 return funcstat;
2692}
c906108c 2693
c3f6f71d 2694#ifdef TM_I386SOL2_H /* Is it hokey to use this? */
c906108c 2695
c3f6f71d 2696#include <sys/sysi86.h>
c906108c 2697
c3f6f71d
JM
2698/*
2699 * Function: proc_get_LDT_entry
2700 *
2701 * Inputs:
2702 * procinfo *pi;
2703 * int key;
2704 *
2705 * The 'key' is actually the value of the lower 16 bits of
2706 * the GS register for the LWP that we're interested in.
2707 *
2708 * Return: matching ssh struct (LDT entry).
c906108c
SS
2709 */
2710
c3f6f71d
JM
2711struct ssd *
2712proc_get_LDT_entry (pi, key)
2713 procinfo *pi;
2714 int key;
c906108c 2715{
c3f6f71d
JM
2716 static struct ssd *ldt_entry = NULL;
2717#ifdef NEW_PROC_API
2718 char pathname[MAX_PROC_NAME_SIZE];
2719 struct cleanup *old_chain = NULL;
2720 int fd;
2721
2722 /* Allocate space for one LDT entry.
2723 This alloc must persist, because we return a pointer to it. */
2724 if (ldt_entry == NULL)
2725 ldt_entry = (struct ssd *) xmalloc (sizeof (struct ssd));
2726
2727 /* Open the file descriptor for the LDT table. */
2728 sprintf (pathname, "/proc/%d/ldt", pi->pid);
2729 if ((fd = open (pathname, O_RDONLY)) < 0)
c906108c 2730 {
c3f6f71d
JM
2731 proc_warn (pi, "proc_get_LDT_entry (open)", __LINE__);
2732 return NULL;
c906108c 2733 }
c3f6f71d 2734 /* Make sure it gets closed again! */
004527cb 2735 old_chain = make_cleanup_close (fd);
c906108c 2736
c3f6f71d
JM
2737 /* Now 'read' thru the table, find a match and return it. */
2738 while (read (fd, ldt_entry, sizeof (struct ssd)) == sizeof (struct ssd))
c906108c 2739 {
c3f6f71d
JM
2740 if (ldt_entry->sel == 0 &&
2741 ldt_entry->bo == 0 &&
2742 ldt_entry->acc1 == 0 &&
2743 ldt_entry->acc2 == 0)
2744 break; /* end of table */
2745 /* If key matches, return this entry. */
2746 if (ldt_entry->sel == key)
2747 return ldt_entry;
c906108c 2748 }
c3f6f71d
JM
2749 /* Loop ended, match not found. */
2750 return NULL;
2751#else
2752 int nldt, i;
2753 static int nalloc = 0;
c906108c 2754
c3f6f71d
JM
2755 /* Get the number of LDT entries. */
2756 if (ioctl (pi->ctl_fd, PIOCNLDT, &nldt) < 0)
c906108c 2757 {
c3f6f71d
JM
2758 proc_warn (pi, "proc_get_LDT_entry (PIOCNLDT)", __LINE__);
2759 return NULL;
c906108c
SS
2760 }
2761
c3f6f71d
JM
2762 /* Allocate space for the number of LDT entries. */
2763 /* This alloc has to persist, 'cause we return a pointer to it. */
2764 if (nldt > nalloc)
c906108c 2765 {
c3f6f71d
JM
2766 ldt_entry = (struct ssd *)
2767 xrealloc (ldt_entry, (nldt + 1) * sizeof (struct ssd));
2768 nalloc = nldt;
2769 }
2770
2771 /* Read the whole table in one gulp. */
2772 if (ioctl (pi->ctl_fd, PIOCLDT, ldt_entry) < 0)
2773 {
2774 proc_warn (pi, "proc_get_LDT_entry (PIOCLDT)", __LINE__);
2775 return NULL;
c906108c
SS
2776 }
2777
c3f6f71d
JM
2778 /* Search the table and return the (first) entry matching 'key'. */
2779 for (i = 0; i < nldt; i++)
2780 if (ldt_entry[i].sel == key)
2781 return &ldt_entry[i];
c906108c 2782
c3f6f71d
JM
2783 /* Loop ended, match not found. */
2784 return NULL;
2785#endif
2786}
c906108c 2787
c3f6f71d 2788#endif /* TM_I386SOL2_H */
c906108c 2789
c3f6f71d 2790/* =============== END, non-thread part of /proc "MODULE" =============== */
c906108c 2791
c3f6f71d 2792/* =================== Thread "MODULE" =================== */
c906108c 2793
c3f6f71d
JM
2794/* NOTE: you'll see more ifdefs and duplication of functions here,
2795 since there is a different way to do threads on every OS. */
c906108c 2796
c3f6f71d
JM
2797/*
2798 * Function: proc_get_nthreads
2799 *
2800 * Return the number of threads for the process
2801 */
c906108c 2802
c3f6f71d
JM
2803#if defined (PIOCNTHR) && defined (PIOCTLIST)
2804/*
2805 * OSF version
2806 */
2807int
2808proc_get_nthreads (pi)
2809 procinfo *pi;
2810{
2811 int nthreads = 0;
c906108c 2812
c3f6f71d
JM
2813 if (ioctl (pi->ctl_fd, PIOCNTHR, &nthreads) < 0)
2814 proc_warn (pi, "procfs: PIOCNTHR failed", __LINE__);
c906108c 2815
c3f6f71d 2816 return nthreads;
c906108c
SS
2817}
2818
c3f6f71d
JM
2819#else
2820#if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2821/*
2822 * Solaris and Unixware version
2823 */
2824int
2825proc_get_nthreads (pi)
2826 procinfo *pi;
c906108c 2827{
c3f6f71d
JM
2828 if (!pi->status_valid)
2829 if (!proc_get_status (pi))
2830 return 0;
c5aa993b 2831
c3f6f71d
JM
2832 /*
2833 * NEW_PROC_API: only works for the process procinfo,
2834 * because the LWP procinfos do not get prstatus filled in.
2835 */
2836#ifdef NEW_PROC_API
2837 if (pi->tid != 0) /* find the parent process procinfo */
2838 pi = find_procinfo_or_die (pi->pid, 0);
c5aa993b 2839#endif
c3f6f71d 2840 return pi->prstatus.pr_nlwp;
c906108c
SS
2841}
2842
c3f6f71d
JM
2843#else
2844/*
2845 * Default version
2846 */
2847int
2848proc_get_nthreads (pi)
2849 procinfo *pi;
c906108c 2850{
c3f6f71d
JM
2851 return 0;
2852}
2853#endif
2854#endif
2855
2856/*
2857 * Function: proc_get_current_thread (LWP version)
2858 *
2859 * Return the ID of the thread that had an event of interest.
2860 * (ie. the one that hit a breakpoint or other traced event).
2861 * All other things being equal, this should be the ID of a
2862 * thread that is currently executing.
2863 */
2864
2865#if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
2866/*
2867 * Solaris and Unixware version
2868 */
2869int
2870proc_get_current_thread (pi)
2871 procinfo *pi;
2872{
2873 /*
2874 * Note: this should be applied to the root procinfo for the process,
2875 * not to the procinfo for an LWP. If applied to the procinfo for
2876 * an LWP, it will simply return that LWP's ID. In that case,
2877 * find the parent process procinfo.
2878 */
2879
2880 if (pi->tid != 0)
2881 pi = find_procinfo_or_die (pi->pid, 0);
2882
2883 if (!pi->status_valid)
2884 if (!proc_get_status (pi))
2885 return 0;
2886
2887#ifdef NEW_PROC_API
2888 return pi->prstatus.pr_lwp.pr_lwpid;
c906108c 2889#else
c3f6f71d 2890 return pi->prstatus.pr_who;
c906108c 2891#endif
c3f6f71d 2892}
c906108c 2893
c3f6f71d
JM
2894#else
2895#if defined (PIOCNTHR) && defined (PIOCTLIST)
2896/*
2897 * OSF version
2898 */
2899int
2900proc_get_current_thread (pi)
2901 procinfo *pi;
2902{
2903#if 0 /* FIXME: not ready for prime time? */
2904 return pi->prstatus.pr_tid;
2905#else
2906 return 0;
2907#endif
c906108c
SS
2908}
2909
c3f6f71d
JM
2910#else
2911/*
2912 * Default version
2913 */
2914int
2915proc_get_current_thread (pi)
2916 procinfo *pi;
c906108c 2917{
c3f6f71d
JM
2918 return 0;
2919}
2920
2921#endif
2922#endif
c906108c 2923
c3f6f71d
JM
2924/*
2925 * Function: proc_update_threads
2926 *
2927 * Discover the IDs of all the threads within the process, and
2928 * create a procinfo for each of them (chained to the parent).
2929 *
2930 * This unfortunately requires a different method on every OS.
2931 *
2932 * Return: non-zero for success, zero for failure.
2933 */
c906108c 2934
c3f6f71d
JM
2935int
2936proc_delete_dead_threads (parent, thread, ignore)
2937 procinfo *parent;
2938 procinfo *thread;
2939 void *ignore;
2940{
2941 if (thread && parent) /* sanity */
c906108c 2942 {
c3f6f71d
JM
2943 thread->status_valid = 0;
2944 if (!proc_get_status (thread))
2945 destroy_one_procinfo (&parent->thread_list, thread);
2946 }
2947 return 0; /* keep iterating */
2948}
c5aa993b 2949
c3f6f71d
JM
2950#if defined (PIOCLSTATUS)
2951/*
2952 * Solaris 2.5 (ioctl) version
2953 */
2954int
2955proc_update_threads (pi)
2956 procinfo *pi;
2957{
2958 gdb_prstatus_t *prstatus;
2959 struct cleanup *old_chain = NULL;
2960 procinfo *thread;
2961 int nlwp, i;
2962
2963 /*
2964 * We should never have to apply this operation to any procinfo
2965 * except the one for the main process. If that ever changes
2966 * for any reason, then take out the following clause and
2967 * replace it with one that makes sure the ctl_fd is open.
2968 */
2969
2970 if (pi->tid != 0)
2971 pi = find_procinfo_or_die (pi->pid, 0);
2972
2973 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
2974
2975 if ((nlwp = proc_get_nthreads (pi)) <= 1)
2976 return 1; /* Process is not multi-threaded; nothing to do. */
2977
2978 if ((prstatus = (gdb_prstatus_t *)
2979 malloc (sizeof (gdb_prstatus_t) * (nlwp + 1))) == 0)
2980 perror_with_name ("procfs: malloc failed in update_threads");
2981
2982 old_chain = make_cleanup (free, prstatus);
2983 if (ioctl (pi->ctl_fd, PIOCLSTATUS, prstatus) < 0)
2984 proc_error (pi, "update_threads (PIOCLSTATUS)", __LINE__);
2985
2986 /* Skip element zero, which represents the process as a whole. */
2987 for (i = 1; i < nlwp + 1; i++)
2988 {
2989 if ((thread = create_procinfo (pi->pid, prstatus[i].pr_who)) == NULL)
2990 proc_error (pi, "update_threads, create_procinfo", __LINE__);
c5aa993b 2991
c3f6f71d
JM
2992 memcpy (&thread->prstatus, &prstatus[i], sizeof (*prstatus));
2993 thread->status_valid = 1;
2994 }
2995 pi->threads_valid = 1;
2996 do_cleanups (old_chain);
2997 return 1;
2998}
2999#else
3000#ifdef NEW_PROC_API
3001/*
3002 * Unixware and Solaris 6 (and later) version
3003 */
004527cb
AC
3004static void
3005do_closedir_cleanup (void *dir)
3006{
3007 closedir (dir);
3008}
3009
c3f6f71d
JM
3010int
3011proc_update_threads (pi)
3012 procinfo *pi;
3013{
3014 char pathname[MAX_PROC_NAME_SIZE + 16];
3015 struct dirent *direntry;
3016 struct cleanup *old_chain = NULL;
3017 procinfo *thread;
3018 DIR *dirp;
3019 int lwpid;
3020
3021 /*
3022 * We should never have to apply this operation to any procinfo
3023 * except the one for the main process. If that ever changes
3024 * for any reason, then take out the following clause and
3025 * replace it with one that makes sure the ctl_fd is open.
3026 */
3027
3028 if (pi->tid != 0)
3029 pi = find_procinfo_or_die (pi->pid, 0);
3030
3031 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3032
3033 /*
3034 * Unixware
3035 *
3036 * Note: this brute-force method is the only way I know of
3037 * to accomplish this task on Unixware. This method will
3038 * also work on Solaris 2.6 and 2.7. There is a much simpler
3039 * and more elegant way to do this on Solaris, but the margins
3040 * of this manuscript are too small to write it here... ;-)
3041 */
3042
3043 strcpy (pathname, pi->pathname);
3044 strcat (pathname, "/lwp");
3045 if ((dirp = opendir (pathname)) == NULL)
3046 proc_error (pi, "update_threads, opendir", __LINE__);
3047
004527cb 3048 old_chain = make_cleanup (do_closedir_cleanup, dirp);
c3f6f71d
JM
3049 while ((direntry = readdir (dirp)) != NULL)
3050 if (direntry->d_name[0] != '.') /* skip '.' and '..' */
3051 {
3052 lwpid = atoi (&direntry->d_name[0]);
3053 if ((thread = create_procinfo (pi->pid, lwpid)) == NULL)
3054 proc_error (pi, "update_threads, create_procinfo", __LINE__);
3055 }
3056 pi->threads_valid = 1;
3057 do_cleanups (old_chain);
3058 return 1;
3059}
3060#else
3061#ifdef PIOCTLIST
3062/*
3063 * OSF version
3064 */
3065int
3066proc_update_threads (pi)
3067 procinfo *pi;
3068{
3069 int nthreads, i;
3070 tid_t *threads;
3071
3072 /*
3073 * We should never have to apply this operation to any procinfo
3074 * except the one for the main process. If that ever changes
3075 * for any reason, then take out the following clause and
3076 * replace it with one that makes sure the ctl_fd is open.
3077 */
3078
3079 if (pi->tid != 0)
3080 pi = find_procinfo_or_die (pi->pid, 0);
3081
3082 proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3083
3084 nthreads = proc_get_nthreads (pi);
3085 if (nthreads < 2)
3086 return 0; /* nothing to do for 1 or fewer threads */
3087
3088 if ((threads = malloc (nthreads * sizeof (tid_t))) == NULL)
3089 proc_error (pi, "update_threads, malloc", __LINE__);
3090
3091 if (ioctl (pi->ctl_fd, PIOCTLIST, threads) < 0)
3092 proc_error (pi, "procfs: update_threads (PIOCTLIST)", __LINE__);
3093
3094 for (i = 0; i < nthreads; i++)
3095 {
3096 if (!find_procinfo (pi->pid, threads[i]))
3097 if (!create_procinfo (pi->pid, threads[i]))
3098 proc_error (pi, "update_threads, create_procinfo", __LINE__);
c906108c 3099 }
c3f6f71d
JM
3100 pi->threads_valid = 1;
3101 return 1;
c906108c 3102}
c3f6f71d
JM
3103#else
3104/*
3105 * Default version
3106 */
3107int
3108proc_update_threads (pi)
3109 procinfo *pi;
3110{
3111 return 0;
3112}
3113#endif /* OSF PIOCTLIST */
3114#endif /* NEW_PROC_API */
3115#endif /* SOL 2.5 PIOCLSTATUS */
c906108c 3116
c3f6f71d
JM
3117/*
3118 * Function: proc_iterate_over_threads
3119 *
3120 * Description:
3121 * Given a pointer to a function, call that function once
3122 * for each lwp in the procinfo list, until the function
3123 * returns non-zero, in which event return the value
3124 * returned by the function.
3125 *
3126 * Note: this function does NOT call update_threads.
3127 * If you want to discover new threads first, you must
3128 * call that function explicitly. This function just makes
3129 * a quick pass over the currently-known procinfos.
3130 *
3131 * Arguments:
3132 * pi - parent process procinfo
3133 * func - per-thread function
3134 * ptr - opaque parameter for function.
3135 *
3136 * Return:
3137 * First non-zero return value from the callee, or zero.
3138 */
3139
3140int
3141proc_iterate_over_threads (pi, func, ptr)
3142 procinfo *pi;
507f3c78 3143 int (*func) (procinfo *, procinfo *, void *);
c3f6f71d 3144 void *ptr;
c906108c 3145{
c3f6f71d
JM
3146 procinfo *thread, *next;
3147 int retval = 0;
c906108c 3148
c3f6f71d
JM
3149 /*
3150 * We should never have to apply this operation to any procinfo
3151 * except the one for the main process. If that ever changes
3152 * for any reason, then take out the following clause and
3153 * replace it with one that makes sure the ctl_fd is open.
3154 */
3155
3156 if (pi->tid != 0)
3157 pi = find_procinfo_or_die (pi->pid, 0);
3158
3159 for (thread = pi->thread_list; thread != NULL; thread = next)
c906108c 3160 {
c3f6f71d
JM
3161 next = thread->next; /* in case thread is destroyed */
3162 if ((retval = (*func) (pi, thread, ptr)) != 0)
3163 break;
c906108c 3164 }
c3f6f71d
JM
3165
3166 return retval;
c906108c
SS
3167}
3168
c3f6f71d
JM
3169/* =================== END, Thread "MODULE" =================== */
3170
3171/* =================== END, /proc "MODULE" =================== */
3172
3173/* =================== GDB "MODULE" =================== */
3174
3175/*
3176 * Here are all of the gdb target vector functions and their friends.
3177 */
3178
a14ed312
KB
3179static int do_attach (int pid);
3180static void do_detach (int signo);
3181static int register_gdb_signals (procinfo *, sigset_t *);
c3f6f71d
JM
3182
3183/*
3184 * Function: procfs_debug_inferior
3185 *
3186 * Sets up the inferior to be debugged.
3187 * Registers to trace signals, hardware faults, and syscalls.
3188 * Note: does not set RLC flag: caller may want to customize that.
3189 *
3190 * Returns: zero for success (note! unlike most functions in this module)
3191 * On failure, returns the LINE NUMBER where it failed!
3192 */
3193
3194static int
3195procfs_debug_inferior (pi)
3196 procinfo *pi;
c906108c 3197{
c3f6f71d
JM
3198 fltset_t traced_faults;
3199 sigset_t traced_signals;
3200 sysset_t traced_syscall_entries;
3201 sysset_t traced_syscall_exits;
c906108c 3202
c3f6f71d
JM
3203#ifdef PROCFS_DONT_TRACE_FAULTS
3204 /* On some systems (OSF), we don't trace hardware faults.
3205 Apparently it's enough that we catch them as signals.
3206 Wonder why we don't just do that in general? */
3207 premptyset (&traced_faults); /* don't trace faults. */
3208#else
3209 /* Register to trace hardware faults in the child. */
3210 prfillset (&traced_faults); /* trace all faults... */
3211 prdelset (&traced_faults, FLTPAGE); /* except page fault. */
3212#endif
3213 if (!proc_set_traced_faults (pi, &traced_faults))
3214 return __LINE__;
c906108c 3215
c3f6f71d
JM
3216 /* Register to trace selected signals in the child. */
3217 premptyset (&traced_signals);
3218 if (!register_gdb_signals (pi, &traced_signals))
3219 return __LINE__;
3220
3221 /* Register to trace the 'exit' system call (on entry). */
3222 premptyset (&traced_syscall_entries);
3223 praddset (&traced_syscall_entries, SYS_exit);
3224#ifdef SYS_lwpexit
3225 praddset (&traced_syscall_entries, SYS_lwpexit); /* And _lwp_exit... */
3226#endif
3227#ifdef SYS_lwp_exit
3228 praddset (&traced_syscall_entries, SYS_lwp_exit);
c906108c
SS
3229#endif
3230
c3f6f71d
JM
3231 if (!proc_set_traced_sysentry (pi, &traced_syscall_entries))
3232 return __LINE__;
3233
3234#ifdef PRFS_STOPEXEC /* defined on OSF */
3235 /* OSF method for tracing exec syscalls. Quoting:
3236 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3237 exits from exec system calls because of the user level loader. */
3238 /* FIXME: make nice and maybe move into an access function. */
3239 {
3240 int prfs_flags;
3241
3242 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
3243 return __LINE__;
3244
3245 prfs_flags |= PRFS_STOPEXEC;
3246
3247 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
3248 return __LINE__;
3249 }
3250#else /* not PRFS_STOPEXEC */
3251 /* Everyone else's (except OSF) method for tracing exec syscalls */
3252 /* GW: Rationale...
3253 Not all systems with /proc have all the exec* syscalls with the same
3254 names. On the SGI, for example, there is no SYS_exec, but there
3255 *is* a SYS_execv. So, we try to account for that. */
3256
3257 premptyset (&traced_syscall_exits);
3258#ifdef SYS_exec
3259 praddset (&traced_syscall_exits, SYS_exec);
3260#endif
3261#ifdef SYS_execve
3262 praddset (&traced_syscall_exits, SYS_execve);
3263#endif
3264#ifdef SYS_execv
3265 praddset (&traced_syscall_exits, SYS_execv);
3266#endif
c5aa993b 3267
c3f6f71d
JM
3268#ifdef SYS_lwpcreate
3269 praddset (&traced_syscall_exits, SYS_lwpcreate);
3270 praddset (&traced_syscall_exits, SYS_lwpexit);
c906108c 3271#endif
c5aa993b 3272
c3f6f71d
JM
3273#ifdef SYS_lwp_create /* FIXME: once only, please */
3274 praddset (&traced_syscall_exits, SYS_lwp_create);
3275 praddset (&traced_syscall_exits, SYS_lwp_exit);
3276#endif
c5aa993b 3277
c906108c 3278
c3f6f71d
JM
3279 if (!proc_set_traced_sysexit (pi, &traced_syscall_exits))
3280 return __LINE__;
3281
3282#endif /* PRFS_STOPEXEC */
3283 return 0;
c906108c
SS
3284}
3285
c3f6f71d
JM
3286static void
3287procfs_attach (args, from_tty)
3288 char *args;
3289 int from_tty;
c906108c 3290{
c3f6f71d
JM
3291 char *exec_file;
3292 int pid;
3293
3294 if (!args)
3295 error_no_arg ("process-id to attach");
3296
3297 pid = atoi (args);
3298 if (pid == getpid ())
3299 error ("Attaching GDB to itself is not a good idea...");
c906108c 3300
c3f6f71d 3301 if (from_tty)
c906108c 3302 {
c3f6f71d
JM
3303 exec_file = get_exec_file (0);
3304
3305 if (exec_file)
3306 printf_filtered ("Attaching to program `%s', %s\n",
3307 exec_file, target_pid_to_str (pid));
3308 else
3309 printf_filtered ("Attaching to %s\n", target_pid_to_str (pid));
3310
3311 fflush (stdout);
c906108c 3312 }
c3f6f71d
JM
3313 inferior_pid = do_attach (pid);
3314 push_target (&procfs_ops);
3315}
3316
3317static void
3318procfs_detach (args, from_tty)
3319 char *args;
3320 int from_tty;
3321{
3322 char *exec_file;
3323 int signo = 0;
3324
3325 if (from_tty)
c906108c 3326 {
c3f6f71d
JM
3327 exec_file = get_exec_file (0);
3328 if (exec_file == 0)
3329 exec_file = "";
3330 printf_filtered ("Detaching from program: %s %s\n",
3331 exec_file, target_pid_to_str (inferior_pid));
3332 fflush (stdout);
c906108c 3333 }
c3f6f71d
JM
3334 if (args)
3335 signo = atoi (args);
3336
3337 do_detach (signo);
3338 inferior_pid = 0;
3339 unpush_target (&procfs_ops); /* Pop out of handling an inferior */
c906108c
SS
3340}
3341
c3f6f71d
JM
3342static int
3343do_attach (pid)
3344 int pid;
c906108c 3345{
c3f6f71d
JM
3346 procinfo *pi;
3347 int fail;
3348
3349 if ((pi = create_procinfo (pid, 0)) == NULL)
3350 perror ("procfs: out of memory in 'attach'");
3351
3352 if (!open_procinfo_files (pi, FD_CTL))
3353 {
3354 fprintf_filtered (gdb_stderr, "procfs:%d -- ", __LINE__);
3355 sprintf (errmsg, "do_attach: couldn't open /proc file for process %d",
3356 pid);
3357 dead_procinfo (pi, errmsg, NOKILL);
3358 }
c906108c 3359
c3f6f71d
JM
3360 /* Stop the process (if it isn't already stopped). */
3361 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
c906108c 3362 {
c3f6f71d
JM
3363 pi->was_stopped = 1;
3364 proc_prettyprint_why (proc_why (pi), proc_what (pi), 1);
c906108c
SS
3365 }
3366 else
3367 {
c3f6f71d
JM
3368 pi->was_stopped = 0;
3369 /* Set the process to run again when we close it. */
3370 if (!proc_set_run_on_last_close (pi))
3371 dead_procinfo (pi, "do_attach: couldn't set RLC.", NOKILL);
3372
3373 /* Now stop the process. */
3374 if (!proc_stop_process (pi))
3375 dead_procinfo (pi, "do_attach: couldn't stop the process.", NOKILL);
3376 pi->ignore_next_sigstop = 1;
c906108c 3377 }
c3f6f71d
JM
3378 /* Save some of the /proc state to be restored if we detach. */
3379 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
3380 dead_procinfo (pi, "do_attach: couldn't save traced faults.", NOKILL);
3381 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
3382 dead_procinfo (pi, "do_attach: couldn't save traced signals.", NOKILL);
3383 if (!proc_get_traced_sysentry (pi, &pi->saved_entryset))
3384 dead_procinfo (pi, "do_attach: couldn't save traced syscall entries.",
3385 NOKILL);
3386 if (!proc_get_traced_sysexit (pi, &pi->saved_exitset))
3387 dead_procinfo (pi, "do_attach: couldn't save traced syscall exits.",
3388 NOKILL);
3389 if (!proc_get_held_signals (pi, &pi->saved_sighold))
3390 dead_procinfo (pi, "do_attach: couldn't save held signals.", NOKILL);
3391
3392 if ((fail = procfs_debug_inferior (pi)) != 0)
3393 dead_procinfo (pi, "do_attach: failed in procfs_debug_inferior", NOKILL);
3394
3395 /* Let GDB know that the inferior was attached. */
3396 attach_flag = 1;
3397 return MERGEPID (pi->pid, proc_get_current_thread (pi));
c906108c
SS
3398}
3399
3400static void
c3f6f71d
JM
3401do_detach (signo)
3402 int signo;
c906108c 3403{
c3f6f71d 3404 procinfo *pi;
c906108c 3405
c3f6f71d
JM
3406 /* Find procinfo for the main process */
3407 pi = find_procinfo_or_die (PIDGET (inferior_pid), 0); /* FIXME: threads */
3408 if (signo)
3409 if (!proc_set_current_signal (pi, signo))
3410 proc_warn (pi, "do_detach, set_current_signal", __LINE__);
c5aa993b 3411
c3f6f71d
JM
3412 if (!proc_set_traced_signals (pi, &pi->saved_sigset))
3413 proc_warn (pi, "do_detach, set_traced_signal", __LINE__);
c906108c 3414
c3f6f71d
JM
3415 if (!proc_set_traced_faults (pi, &pi->saved_fltset))
3416 proc_warn (pi, "do_detach, set_traced_faults", __LINE__);
3417
3418 if (!proc_set_traced_sysentry (pi, &pi->saved_entryset))
3419 proc_warn (pi, "do_detach, set_traced_sysentry", __LINE__);
3420
3421 if (!proc_set_traced_sysexit (pi, &pi->saved_exitset))
3422 proc_warn (pi, "do_detach, set_traced_sysexit", __LINE__);
3423
3424 if (!proc_set_held_signals (pi, &pi->saved_sighold))
3425 proc_warn (pi, "do_detach, set_held_signals", __LINE__);
3426
3427 if (signo || (proc_flags (pi) & (PR_STOPPED | PR_ISTOP)))
3428 if (signo || !(pi->was_stopped) ||
3429 query ("Was stopped when attached, make it runnable again? "))
3430 {
3431 /* Clear any pending signal. */
3432 if (!proc_clear_current_fault (pi))
3433 proc_warn (pi, "do_detach, clear_current_fault", __LINE__);
3434
3435 if (!proc_set_run_on_last_close (pi))
3436 proc_warn (pi, "do_detach, set_rlc", __LINE__);
3437 }
3438
3439 attach_flag = 0;
3440 destroy_procinfo (pi);
c906108c
SS
3441}
3442
c3f6f71d
JM
3443/*
3444 * fetch_registers
3445 *
3446 * Since the /proc interface cannot give us individual registers,
3447 * we pay no attention to the (regno) argument, and just fetch them all.
3448 * This results in the possibility that we will do unnecessarily many
3449 * fetches, since we may be called repeatedly for individual registers.
3450 * So we cache the results, and mark the cache invalid when the process
3451 * is resumed.
3452 */
3453
c906108c 3454static void
c3f6f71d
JM
3455procfs_fetch_registers (regno)
3456 int regno;
c906108c 3457{
c3f6f71d
JM
3458 gdb_fpregset_t *fpregs;
3459 gdb_gregset_t *gregs;
3460 procinfo *pi;
3461 int pid;
3462 int tid;
c906108c 3463
c3f6f71d
JM
3464 pid = PIDGET (inferior_pid);
3465 tid = TIDGET (inferior_pid);
3466
3467 /* First look up procinfo for the main process. */
3468 pi = find_procinfo_or_die (pid, 0);
3469
3470 /* If the event thread is not the same as GDB's requested thread
3471 (ie. inferior_pid), then look up procinfo for the requested
3472 thread. */
3473 if ((tid != 0) &&
3474 (tid != proc_get_current_thread (pi)))
3475 pi = find_procinfo_or_die (pid, tid);
3476
3477 if (pi == NULL)
3478 error ("procfs: fetch_registers failed to find procinfo for %s",
3479 target_pid_to_str (inferior_pid));
3480
3481 if ((gregs = proc_get_gregs (pi)) == NULL)
3482 proc_error (pi, "fetch_registers, get_gregs", __LINE__);
3483
3484 supply_gregset (gregs);
3485
60054393
MS
3486 if (FP0_REGNUM >= 0) /* need floating point? */
3487 {
3488 if ((regno >= 0 && regno < FP0_REGNUM) ||
3489 regno == PC_REGNUM ||
3490 (NPC_REGNUM >= 0 && regno == NPC_REGNUM) ||
3491 regno == FP_REGNUM ||
3492 regno == SP_REGNUM)
3493 return; /* not a floating point register */
c5aa993b 3494
60054393
MS
3495 if ((fpregs = proc_get_fpregs (pi)) == NULL)
3496 proc_error (pi, "fetch_registers, get_fpregs", __LINE__);
c906108c 3497
60054393
MS
3498 supply_fpregset (fpregs);
3499 }
c906108c
SS
3500}
3501
c3f6f71d
JM
3502/* Get ready to modify the registers array. On machines which store
3503 individual registers, this doesn't need to do anything. On
3504 machines which store all the registers in one fell swoop, such as
3505 /proc, this makes sure that registers contains all the registers
3506 from the program being debugged. */
3507
c906108c 3508static void
c3f6f71d 3509procfs_prepare_to_store ()
c906108c 3510{
c3f6f71d
JM
3511#ifdef CHILD_PREPARE_TO_STORE
3512 CHILD_PREPARE_TO_STORE ();
c906108c 3513#endif
c906108c
SS
3514}
3515
3516/*
c3f6f71d
JM
3517 * store_registers
3518 *
3519 * Since the /proc interface will not read individual registers,
3520 * we will cache these requests until the process is resumed, and
3521 * only then write them back to the inferior process.
3522 *
3523 * FIXME: is that a really bad idea? Have to think about cases
3524 * where writing one register might affect the value of others, etc.
3525 */
c906108c 3526
c3f6f71d
JM
3527static void
3528procfs_store_registers (regno)
3529 int regno;
3530{
3531 gdb_fpregset_t *fpregs;
3532 gdb_gregset_t *gregs;
3533 procinfo *pi;
3534 int pid;
3535 int tid;
c906108c 3536
c3f6f71d
JM
3537 pid = PIDGET (inferior_pid);
3538 tid = TIDGET (inferior_pid);
c906108c 3539
c3f6f71d
JM
3540 /* First find procinfo for main process */
3541 pi = find_procinfo_or_die (pid, 0);
3542
3543 /* If current lwp for process is not the same as requested thread
3544 (ie. inferior_pid), then find procinfo for the requested thread. */
3545
3546 if ((tid != 0) &&
3547 (tid != proc_get_current_thread (pi)))
3548 pi = find_procinfo_or_die (pid, tid);
3549
3550 if (pi == NULL)
3551 error ("procfs: store_registers: failed to find procinfo for %s",
3552 target_pid_to_str (inferior_pid));
c906108c 3553
c3f6f71d
JM
3554 if ((gregs = proc_get_gregs (pi)) == NULL)
3555 proc_error (pi, "store_registers, get_gregs", __LINE__);
c906108c 3556
c3f6f71d
JM
3557 fill_gregset (gregs, regno);
3558 if (!proc_set_gregs (pi))
3559 proc_error (pi, "store_registers, set_gregs", __LINE__);
c906108c 3560
60054393
MS
3561 if (FP0_REGNUM >= 0) /* need floating point? */
3562 {
3563 if ((regno >= 0 && regno < FP0_REGNUM) ||
3564 regno == PC_REGNUM ||
3565 (NPC_REGNUM >= 0 && regno == NPC_REGNUM) ||
3566 regno == FP_REGNUM ||
3567 regno == SP_REGNUM)
3568 return; /* not a floating point register */
3569
3570 if ((fpregs = proc_get_fpregs (pi)) == NULL)
3571 proc_error (pi, "store_registers, get_fpregs", __LINE__);
3572
3573 fill_fpregset (fpregs, regno);
3574 if (!proc_set_fpregs (pi))
3575 proc_error (pi, "store_registers, set_fpregs", __LINE__);
3576 }
c3f6f71d 3577}
c906108c 3578
c3f6f71d
JM
3579/*
3580 * Function: target_wait
3581 *
3582 * Retrieve the next stop event from the child process.
3583 * If child has not stopped yet, wait for it to stop.
3584 * Translate /proc eventcodes (or possibly wait eventcodes)
3585 * into gdb internal event codes.
3586 *
3587 * Return: id of process (and possibly thread) that incurred the event.
3588 * event codes are returned thru a pointer parameter.
c906108c
SS
3589 */
3590
c3f6f71d
JM
3591static int
3592procfs_wait (pid, status)
3593 int pid;
3594 struct target_waitstatus *status;
c906108c 3595{
c3f6f71d
JM
3596 /* First cut: loosely based on original version 2.1 */
3597 procinfo *pi;
3598 int temp, wstat;
3599 int retval;
3600 int why, what, flags;
3601 int retry = 0;
c906108c 3602
c3f6f71d 3603wait_again:
c906108c 3604
c3f6f71d
JM
3605 retry++;
3606 wstat = 0;
3607 retval = -1;
c906108c 3608
c3f6f71d
JM
3609 /* Find procinfo for main process */
3610 pi = find_procinfo_or_die (PIDGET (inferior_pid), 0);
3611 if (pi)
c906108c 3612 {
c3f6f71d
JM
3613 /* We must assume that the status is stale now... */
3614 pi->status_valid = 0;
3615 pi->gregs_valid = 0;
3616 pi->fpregs_valid = 0;
3617
3618#if 0 /* just try this out... */
3619 flags = proc_flags (pi);
3620 why = proc_why (pi);
3621 if ((flags & PR_STOPPED) && (why == PR_REQUESTED))
3622 pi->status_valid = 0; /* re-read again, IMMEDIATELY... */
3623#endif
3624 /* If child is not stopped, wait for it to stop. */
3625 if (!(proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) &&
3626 !proc_wait_for_stop (pi))
c906108c 3627 {
c3f6f71d
JM
3628 /* wait_for_stop failed: has the child terminated? */
3629 if (errno == ENOENT)
c906108c 3630 {
c3f6f71d
JM
3631 /* /proc file not found; presumably child has terminated. */
3632 retval = wait (&wstat); /* "wait" for the child's exit */
3633
3634 if (retval != PIDGET (inferior_pid)) /* wrong child? */
3635 error ("procfs: couldn't stop process %d: wait returned %d\n",
3636 inferior_pid, retval);
3637 /* FIXME: might I not just use waitpid?
3638 Or try find_procinfo to see if I know about this child? */
c906108c 3639 }
c3f6f71d 3640 else
c906108c 3641 {
c3f6f71d
JM
3642 /* Unknown error from wait_for_stop. */
3643 proc_error (pi, "target_wait (wait_for_stop)", __LINE__);
c906108c 3644 }
c3f6f71d
JM
3645 }
3646 else
3647 {
3648 /* This long block is reached if either:
3649 a) the child was already stopped, or
3650 b) we successfully waited for the child with wait_for_stop.
3651 This block will analyze the /proc status, and translate it
3652 into a waitstatus for GDB.
3653
3654 If we actually had to call wait because the /proc file
3655 is gone (child terminated), then we skip this block,
3656 because we already have a waitstatus. */
3657
3658 flags = proc_flags (pi);
3659 why = proc_why (pi);
3660 what = proc_what (pi);
3661
c3f6f71d 3662 if (flags & (PR_STOPPED | PR_ISTOP))
c906108c 3663 {
c3f6f71d
JM
3664#ifdef PR_ASYNC
3665 /* If it's running async (for single_thread control),
3666 set it back to normal again. */
3667 if (flags & PR_ASYNC)
3668 if (!proc_unset_async (pi))
3669 proc_error (pi, "target_wait, unset_async", __LINE__);
3670#endif
3671
3672 if (info_verbose)
3673 proc_prettyprint_why (why, what, 1);
3674
3675 /* The 'pid' we will return to GDB is composed of
3676 the process ID plus the lwp ID. */
3677 retval = MERGEPID (pi->pid, proc_get_current_thread (pi));
3678
3679 switch (why) {
3680 case PR_SIGNALLED:
3681 wstat = (what << 8) | 0177;
3682 break;
3683 case PR_SYSENTRY:
3684 switch (what) {
3685#ifdef SYS_lwp_exit
3686 case SYS_lwp_exit:
3687#endif
3688#ifdef SYS_lwpexit
3689 case SYS_lwpexit:
3690#endif
3691#if defined (SYS_lwp_exit) || defined (SYS_lwpexit)
3692 printf_filtered ("[%s exited]\n",
3693 target_pid_to_str (retval));
3694 delete_thread (retval);
3695 status->kind = TARGET_WAITKIND_SPURIOUS;
3696 return retval;
3697#endif /* _lwp_exit */
3698
3699 case SYS_exit:
3700 /* Handle SYS_exit call only */
3701 /* Stopped at entry to SYS_exit.
3702 Make it runnable, resume it, then use
3703 the wait system call to get its exit code.
3704 Proc_run_process always clears the current
3705 fault and signal.
3706 Then return its exit status. */
3707 pi->status_valid = 0;
3708 wstat = 0;
3709 /* FIXME: what we should do is return
3710 TARGET_WAITKIND_SPURIOUS. */
3711 if (!proc_run_process (pi, 0, 0))
3712 proc_error (pi, "target_wait, run_process", __LINE__);
3713 if (attach_flag)
3714 {
3715 /* Don't call wait: simulate waiting for exit,
3716 return a "success" exit code. Bogus: what if
3717 it returns something else? */
3718 wstat = 0;
02d5252f 3719 retval = inferior_pid; /* ? ? ? */
c3f6f71d
JM
3720 }
3721 else
3722 {
3723 int temp = wait (&wstat);
3724
3725 /* FIXME: shouldn't I make sure I get the right
3726 event from the right process? If (for
3727 instance) I have killed an earlier inferior
3728 process but failed to clean up after it
3729 somehow, I could get its termination event
3730 here. */
3731
3732 /* If wait returns -1, that's what we return to GDB. */
3733 if (temp < 0)
3734 retval = temp;
3735 }
3736 break;
3737 default:
3738 printf_filtered ("procfs: trapped on entry to ");
3739 proc_prettyprint_syscall (proc_what (pi), 0);
3740 printf_filtered ("\n");
3741#ifndef PIOCSSPCACT
3742 {
3743 long i, nsysargs, *sysargs;
3744
3745 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
3746 (sysargs = proc_sysargs (pi)) != NULL)
3747 {
0fda6bd2 3748 printf_filtered ("%ld syscall arguments:\n", nsysargs);
c3f6f71d 3749 for (i = 0; i < nsysargs; i++)
02d5252f 3750 printf_filtered ("#%ld: 0x%08lx\n",
c3f6f71d
JM
3751 i, sysargs[i]);
3752 }
3753
3754 }
3755#endif
3756 if (status)
3757 {
3758 /* How to exit gracefully, returning "unknown event" */
3759 status->kind = TARGET_WAITKIND_SPURIOUS;
3760 return inferior_pid;
3761 }
3762 else
3763 {
3764 /* How to keep going without returning to wfi: */
3765 target_resume (pid, 0, TARGET_SIGNAL_0);
3766 goto wait_again;
3767 }
3768 break;
3769 }
3770 break;
3771 case PR_SYSEXIT:
3772 switch (what) {
3773#ifdef SYS_exec
3774 case SYS_exec:
3775#endif
3776#ifdef SYS_execv
3777 case SYS_execv:
3778#endif
3779#ifdef SYS_execve
3780 case SYS_execve:
3781#endif
3782 /* Hopefully this is our own "fork-child" execing
3783 the real child. Hoax this event into a trap, and
3784 GDB will see the child about to execute its start
3785 address. */
3786 wstat = (SIGTRAP << 8) | 0177;
3787 break;
3788#ifdef SYS_lwp_create
3789 case SYS_lwp_create:
3790#endif
3791#ifdef SYS_lwpcreate
3792 case SYS_lwpcreate:
3793#endif
3794#if defined(SYS_lwp_create) || defined(SYS_lwpcreate)
3795 /*
3796 * This syscall is somewhat like fork/exec.
3797 * We will get the event twice: once for the parent LWP,
3798 * and once for the child. We should already know about
3799 * the parent LWP, but the child will be new to us. So,
3800 * whenever we get this event, if it represents a new
3801 * thread, simply add the thread to the list.
3802 */
3803
3804 /* If not in procinfo list, add it. */
3805 temp = proc_get_current_thread (pi);
3806 if (!find_procinfo (pi->pid, temp))
3807 create_procinfo (pi->pid, temp);
3808
3809 temp = MERGEPID (pi->pid, temp);
3810 /* If not in GDB's thread list, add it. */
3811 if (!in_thread_list (temp))
3812 {
3813 printf_filtered ("[New %s]\n", target_pid_to_str (temp));
3814 add_thread (temp);
3815 }
3816 /* Return to WFI, but tell it to immediately resume. */
3817 status->kind = TARGET_WAITKIND_SPURIOUS;
3818 return inferior_pid;
3819#endif /* _lwp_create */
c906108c 3820
c3f6f71d
JM
3821#ifdef SYS_lwp_exit
3822 case SYS_lwp_exit:
3823#endif
3824#ifdef SYS_lwpexit
3825 case SYS_lwpexit:
3826#endif
3827#if defined (SYS_lwp_exit) || defined (SYS_lwpexit)
3828 printf_filtered ("[%s exited]\n",
3829 target_pid_to_str (retval));
3830 delete_thread (retval);
3831 status->kind = TARGET_WAITKIND_SPURIOUS;
3832 return retval;
3833#endif /* _lwp_exit */
3834
3835#ifdef SYS_sproc
3836 case SYS_sproc:
3837 /* Nothing to do here for now. The old procfs
3838 seemed to use this event to handle threads on
3839 older (non-LWP) systems, where I'm assuming that
3840 threads were actually separate processes. Irix,
3841 maybe? Anyway, low priority for now. */
3842#endif
3843#ifdef SYS_fork
3844 case SYS_fork:
3845 /* FIXME: do we need to handle this? Investigate. */
3846#endif
3847#ifdef SYS_vfork
3848 case SYS_vfork:
3849 /* FIXME: see above. */
3850#endif
3851 default:
3852 printf_filtered ("procfs: trapped on exit from ");
3853 proc_prettyprint_syscall (proc_what (pi), 0);
3854 printf_filtered ("\n");
3855#ifndef PIOCSSPCACT
3856 {
3857 long i, nsysargs, *sysargs;
3858
3859 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
3860 (sysargs = proc_sysargs (pi)) != NULL)
3861 {
0fda6bd2 3862 printf_filtered ("%ld syscall arguments:\n", nsysargs);
c3f6f71d 3863 for (i = 0; i < nsysargs; i++)
02d5252f 3864 printf_filtered ("#%ld: 0x%08lx\n",
c3f6f71d
JM
3865 i, sysargs[i]);
3866 }
3867 }
3868#endif
3869 status->kind = TARGET_WAITKIND_SPURIOUS;
3870 return inferior_pid;
3871 }
3872 break;
3873 case PR_REQUESTED:
3874#if 0 /* FIXME */
3875 wstat = (SIGSTOP << 8) | 0177;
3876 break;
3877#else
3878 if (retry < 5)
3879 {
3880 printf_filtered ("Retry #%d:\n", retry);
3881 pi->status_valid = 0;
3882 goto wait_again;
3883 }
3884 else
3885 {
3886 /* If not in procinfo list, add it. */
3887 temp = proc_get_current_thread (pi);
3888 if (!find_procinfo (pi->pid, temp))
3889 create_procinfo (pi->pid, temp);
3890
3891 /* If not in GDB's thread list, add it. */
3892 temp = MERGEPID (pi->pid, temp);
3893 if (!in_thread_list (temp))
3894 {
0d06e24b 3895 printf_filtered ("[New %s]\n",
c3f6f71d
JM
3896 target_pid_to_str (temp));
3897 add_thread (temp);
3898 }
3899
3900 status->kind = TARGET_WAITKIND_STOPPED;
3901 status->value.sig = 0;
3902 return retval;
3903 }
3904#endif
3905 case PR_JOBCONTROL:
3906 wstat = (what << 8) | 0177;
3907 break;
3908 case PR_FAULTED:
3909 switch (what) { /* FIXME: FAULTED_USE_SIGINFO */
3910#ifdef FLTWATCH
3911 case FLTWATCH:
3912 wstat = (SIGTRAP << 8) | 0177;
3913 break;
3914#endif
3915#ifdef FLTKWATCH
3916 case FLTKWATCH:
3917 wstat = (SIGTRAP << 8) | 0177;
3918 break;
3919#endif
3920 /* FIXME: use si_signo where possible. */
3921 case FLTPRIV:
3922#if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
3923 case FLTILL:
3924#endif
3925 wstat = (SIGILL << 8) | 0177;
3926 break;
3927 case FLTBPT:
3928#if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
3929 case FLTTRACE:
3930#endif
3931 wstat = (SIGTRAP << 8) | 0177;
3932 break;
3933 case FLTSTACK:
3934 case FLTACCESS:
3935#if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
3936 case FLTBOUNDS:
3937#endif
3938 wstat = (SIGSEGV << 8) | 0177;
3939 break;
3940 case FLTIOVF:
3941 case FLTIZDIV:
3942#if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
3943 case FLTFPE:
3944#endif
3945 wstat = (SIGFPE << 8) | 0177;
3946 break;
3947 case FLTPAGE: /* Recoverable page fault */
3948 default: /* FIXME: use si_signo if possible for fault */
3949 retval = -1;
3950 printf_filtered ("procfs:%d -- ", __LINE__);
3951 printf_filtered ("child stopped for unknown reason:\n");
3952 proc_prettyprint_why (why, what, 1);
3953 error ("... giving up...");
3954 break;
3955 }
3956 break; /* case PR_FAULTED: */
3957 default: /* switch (why) unmatched */
3958 printf_filtered ("procfs:%d -- ", __LINE__);
3959 printf_filtered ("child stopped for unknown reason:\n");
3960 proc_prettyprint_why (why, what, 1);
3961 error ("... giving up...");
3962 break;
3963 }
3964 /*
3965 * Got this far without error:
3966 * If retval isn't in the threads database, add it.
3967 */
3968 if (retval > 0 &&
3969 retval != inferior_pid &&
3970 !in_thread_list (retval))
c906108c 3971 {
c3f6f71d
JM
3972 /*
3973 * We have a new thread.
3974 * We need to add it both to GDB's list and to our own.
3975 * If we don't create a procinfo, resume may be unhappy
3976 * later.
3977 */
3978 printf_filtered ("[New %s]\n", target_pid_to_str (retval));
3979 add_thread (retval);
3980 if (find_procinfo (PIDGET (retval), TIDGET (retval)) == NULL)
3981 create_procinfo (PIDGET (retval), TIDGET (retval));
3982
3983 /* In addition, it's possible that this is the first
3984 * new thread we've seen, in which case we may not
3985 * have created entries for inferior_pid yet.
3986 */
3987 if (TIDGET (inferior_pid) != 0)
3988 {
3989 if (!in_thread_list (inferior_pid))
3990 add_thread (inferior_pid);
3991 if (find_procinfo (PIDGET (inferior_pid),
3992 TIDGET (inferior_pid)) == NULL)
3993 create_procinfo (PIDGET (inferior_pid),
3994 TIDGET (inferior_pid));
3995 }
c906108c 3996 }
c906108c 3997 }
c3f6f71d 3998 else /* flags do not indicate STOPPED */
c906108c 3999 {
c3f6f71d
JM
4000 /* surely this can't happen... */
4001 printf_filtered ("procfs:%d -- process not stopped.\n",
4002 __LINE__);
4003 proc_prettyprint_flags (flags, 1);
4004 error ("procfs: ...giving up...");
c906108c 4005 }
c906108c 4006 }
c906108c 4007
c3f6f71d
JM
4008 if (status)
4009 store_waitstatus (status, wstat);
c906108c
SS
4010 }
4011
c3f6f71d
JM
4012 return retval;
4013}
c906108c 4014
c3f6f71d
JM
4015static int
4016procfs_xfer_memory (memaddr, myaddr, len, dowrite, target)
4017 CORE_ADDR memaddr;
4018 char *myaddr;
4019 int len;
4020 int dowrite;
4021 struct target_ops *target; /* ignored */
4022{
4023 procinfo *pi;
4024 int nbytes = 0;
c906108c 4025
c3f6f71d
JM
4026 /* Find procinfo for main process */
4027 pi = find_procinfo_or_die (PIDGET (inferior_pid), 0);
4028 if (pi->as_fd == 0 &&
4029 open_procinfo_files (pi, FD_AS) == 0)
c906108c 4030 {
c3f6f71d
JM
4031 proc_warn (pi, "xfer_memory, open_proc_files", __LINE__);
4032 return 0;
c906108c 4033 }
c906108c 4034
c3f6f71d 4035 if (lseek (pi->as_fd, (off_t) memaddr, SEEK_SET) == (off_t) memaddr)
c906108c 4036 {
c3f6f71d 4037 if (dowrite)
c906108c 4038 {
c3f6f71d
JM
4039#ifdef NEW_PROC_API
4040 PROCFS_NOTE ("write memory: ");
c906108c 4041#else
c3f6f71d 4042 PROCFS_NOTE ("write memory: \n");
c906108c 4043#endif
c3f6f71d 4044 nbytes = write (pi->as_fd, myaddr, len);
c906108c 4045 }
c3f6f71d 4046 else
c906108c 4047 {
c3f6f71d
JM
4048 PROCFS_NOTE ("read memory: \n");
4049 nbytes = read (pi->as_fd, myaddr, len);
c906108c 4050 }
c3f6f71d 4051 if (nbytes < 0)
c906108c 4052 {
c3f6f71d 4053 nbytes = 0;
c906108c 4054 }
c906108c 4055 }
c3f6f71d 4056 return nbytes;
c906108c
SS
4057}
4058
4059/*
c3f6f71d
JM
4060 * Function: invalidate_cache
4061 *
4062 * Called by target_resume before making child runnable.
4063 * Mark cached registers and status's invalid.
4064 * If there are "dirty" caches that need to be written back
4065 * to the child process, do that.
4066 *
4067 * File descriptors are also cached.
4068 * As they are a limited resource, we cannot hold onto them indefinitely.
4069 * However, as they are expensive to open, we don't want to throw them
4070 * away indescriminately either. As a compromise, we will keep the
4071 * file descriptors for the parent process, but discard any file
4072 * descriptors we may have accumulated for the threads.
4073 *
4074 * Return value:
4075 * As this function is called by iterate_over_threads, it always
4076 * returns zero (so that iterate_over_threads will keep iterating).
c906108c
SS
4077 */
4078
c3f6f71d
JM
4079
4080static int
4081invalidate_cache (parent, pi, ptr)
4082 procinfo *parent;
4083 procinfo *pi;
4084 void *ptr;
c906108c 4085{
c3f6f71d
JM
4086 /*
4087 * About to run the child; invalidate caches and do any other cleanup.
4088 */
c906108c 4089
c3f6f71d
JM
4090#if 0
4091 if (pi->gregs_dirty)
4092 if (parent == NULL ||
4093 proc_get_current_thread (parent) != pi->tid)
4094 if (!proc_set_gregs (pi)) /* flush gregs cache */
4095 proc_warn (pi, "target_resume, set_gregs",
4096 __LINE__);
60054393
MS
4097 if (FP0_REGNUM >= 0)
4098 if (pi->fpregs_dirty)
4099 if (parent == NULL ||
4100 proc_get_current_thread (parent) != pi->tid)
4101 if (!proc_set_fpregs (pi)) /* flush fpregs cache */
4102 proc_warn (pi, "target_resume, set_fpregs",
4103 __LINE__);
c906108c 4104#endif
c906108c 4105
c3f6f71d 4106 if (parent != NULL)
c906108c 4107 {
c3f6f71d
JM
4108 /* The presence of a parent indicates that this is an LWP.
4109 Close any file descriptors that it might have open.
4110 We don't do this to the master (parent) procinfo. */
4111
4112 close_procinfo_files (pi);
c906108c 4113 }
c3f6f71d
JM
4114 pi->gregs_valid = 0;
4115 pi->fpregs_valid = 0;
4116#if 0
4117 pi->gregs_dirty = 0;
4118 pi->fpregs_dirty = 0;
c906108c 4119#endif
c3f6f71d
JM
4120 pi->status_valid = 0;
4121 pi->threads_valid = 0;
c906108c 4122
c3f6f71d 4123 return 0;
c906108c
SS
4124}
4125
0fda6bd2 4126#if 0
c906108c 4127/*
c3f6f71d
JM
4128 * Function: make_signal_thread_runnable
4129 *
4130 * A callback function for iterate_over_threads.
4131 * Find the asynchronous signal thread, and make it runnable.
4132 * See if that helps matters any.
c906108c
SS
4133 */
4134
c3f6f71d
JM
4135static int
4136make_signal_thread_runnable (process, pi, ptr)
4137 procinfo *process;
4138 procinfo *pi;
4139 void *ptr;
c906108c 4140{
c3f6f71d
JM
4141#ifdef PR_ASLWP
4142 if (proc_flags (pi) & PR_ASLWP)
c906108c 4143 {
c3f6f71d
JM
4144 if (!proc_run_process (pi, 0, -1))
4145 proc_error (pi, "make_signal_thread_runnable", __LINE__);
4146 return 1;
c906108c 4147 }
c906108c 4148#endif
c3f6f71d 4149 return 0;
c906108c 4150}
0fda6bd2 4151#endif
c906108c
SS
4152
4153/*
c3f6f71d
JM
4154 * Function: target_resume
4155 *
4156 * Make the child process runnable. Normally we will then call
4157 * procfs_wait and wait for it to stop again (unles gdb is async).
4158 *
4159 * Arguments:
4160 * step: if true, then arrange for the child to stop again
4161 * after executing a single instruction.
4162 * signo: if zero, then cancel any pending signal.
4163 * If non-zero, then arrange for the indicated signal
4164 * to be delivered to the child when it runs.
4165 * pid: if -1, then allow any child thread to run.
4166 * if non-zero, then allow only the indicated thread to run.
4167 ******* (not implemented yet)
c906108c
SS
4168 */
4169
4170static void
c3f6f71d
JM
4171procfs_resume (pid, step, signo)
4172 int pid;
4173 int step;
4174 enum target_signal signo;
c906108c 4175{
c3f6f71d
JM
4176 procinfo *pi, *thread;
4177 int native_signo;
4178
4179 /* 2.1:
4180 prrun.prflags |= PRSVADDR;
4181 prrun.pr_vaddr = $PC; set resume address
4182 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4183 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4184 prrun.prflags |= PRCFAULT; clear current fault.
4185
4186 PRSTRACE and PRSFAULT can be done by other means
4187 (proc_trace_signals, proc_trace_faults)
4188 PRSVADDR is unnecessary.
4189 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4190 This basically leaves PRSTEP and PRCSIG.
4191 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4192 So basically PR_STEP is the sole argument that must be passed
4193 to proc_run_process (for use in the prrun struct by ioctl). */
4194
4195 /* Find procinfo for main process */
4196 pi = find_procinfo_or_die (PIDGET (inferior_pid), 0);
4197
4198 /* First cut: ignore pid argument */
4199 errno = 0;
c906108c 4200
c3f6f71d
JM
4201 /* Convert signal to host numbering. */
4202 if (signo == 0 ||
0fda6bd2 4203 (signo == TARGET_SIGNAL_STOP && pi->ignore_next_sigstop))
c3f6f71d
JM
4204 native_signo = 0;
4205 else
4206 native_signo = target_signal_to_host (signo);
c906108c 4207
c3f6f71d 4208 pi->ignore_next_sigstop = 0;
c906108c 4209
c3f6f71d
JM
4210 /* Running the process voids all cached registers and status. */
4211 /* Void the threads' caches first */
4212 proc_iterate_over_threads (pi, invalidate_cache, NULL);
4213 /* Void the process procinfo's caches. */
4214 invalidate_cache (NULL, pi, NULL);
c906108c 4215
c3f6f71d 4216 if (pid != -1)
c906108c 4217 {
c3f6f71d
JM
4218 /* Resume a specific thread, presumably suppressing the others. */
4219 thread = find_procinfo (PIDGET (pid), TIDGET (pid));
4220 if (thread == NULL)
4221 warning ("procfs: resume can't find thread %d -- resuming all.",
4222 TIDGET (pid));
4223 else
c906108c 4224 {
c3f6f71d
JM
4225 if (thread->tid != 0)
4226 {
4227 /* We're to resume a specific thread, and not the others.
4228 * Set the child process's PR_ASYNC flag.
4229 */
4230#ifdef PR_ASYNC
4231 if (!proc_set_async (pi))
4232 proc_error (pi, "target_resume, set_async", __LINE__);
4233#endif
4234#if 0
4235 proc_iterate_over_threads (pi,
4236 make_signal_thread_runnable,
4237 NULL);
4238#endif
4239 pi = thread; /* substitute the thread's procinfo for run */
4240 }
c906108c
SS
4241 }
4242 }
c906108c 4243
c3f6f71d 4244 if (!proc_run_process (pi, step, native_signo))
c906108c 4245 {
c3f6f71d
JM
4246 if (errno == EBUSY)
4247 warning ("resume: target already running. Pretend to resume, and hope for the best!\n");
4248 else
4249 proc_error (pi, "target_resume", __LINE__);
c906108c 4250 }
c3f6f71d 4251}
c906108c 4252
c3f6f71d
JM
4253/*
4254 * Function: register_gdb_signals
4255 *
4256 * Traverse the list of signals that GDB knows about
4257 * (see "handle" command), and arrange for the target
4258 * to be stopped or not, according to these settings.
4259 *
4260 * Returns non-zero for success, zero for failure.
4261 */
c906108c 4262
c3f6f71d
JM
4263static int
4264register_gdb_signals (pi, signals)
4265 procinfo *pi;
4266 sigset_t *signals;
4267{
4268 int signo;
c906108c 4269
c3f6f71d
JM
4270 for (signo = 0; signo < NSIG; signo ++)
4271 if (signal_stop_state (target_signal_from_host (signo)) == 0 &&
4272 signal_print_state (target_signal_from_host (signo)) == 0 &&
4273 signal_pass_state (target_signal_from_host (signo)) == 1)
4274 prdelset (signals, signo);
4275 else
4276 praddset (signals, signo);
c906108c 4277
c3f6f71d 4278 return proc_set_traced_signals (pi, signals);
c906108c
SS
4279}
4280
4281/*
c3f6f71d
JM
4282 * Function: target_notice_signals
4283 *
4284 * Set up to trace signals in the child process.
4285 */
c906108c 4286
c3f6f71d
JM
4287static void
4288procfs_notice_signals (pid)
4289 int pid;
4290{
4291 sigset_t signals;
4292 procinfo *pi = find_procinfo_or_die (PIDGET (pid), 0);
c906108c 4293
c3f6f71d
JM
4294 if (proc_get_traced_signals (pi, &signals) &&
4295 register_gdb_signals (pi, &signals))
4296 return;
4297 else
4298 proc_error (pi, "notice_signals", __LINE__);
4299}
c906108c 4300
c3f6f71d
JM
4301/*
4302 * Function: target_files_info
4303 *
4304 * Print status information about the child process.
4305 */
c906108c 4306
c3f6f71d
JM
4307static void
4308procfs_files_info (ignore)
4309 struct target_ops *ignore;
4310{
4311 printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4312 attach_flag? "attached": "child",
4313 target_pid_to_str (inferior_pid));
4314}
c906108c 4315
c3f6f71d
JM
4316/*
4317 * Function: target_open
4318 *
4319 * A dummy: you don't open procfs.
c906108c
SS
4320 */
4321
4322static void
c3f6f71d
JM
4323procfs_open (args, from_tty)
4324 char *args;
4325 int from_tty;
c906108c 4326{
c3f6f71d
JM
4327 error ("Use the \"run\" command to start a Unix child process.");
4328}
c906108c 4329
c3f6f71d
JM
4330/*
4331 * Function: target_can_run
4332 *
4333 * This tells GDB that this target vector can be invoked
4334 * for "run" or "attach".
4335 */
c906108c 4336
c3f6f71d
JM
4337int procfs_suppress_run = 0; /* Non-zero if procfs should pretend not to
4338 be a runnable target. Used by targets
4339 that can sit atop procfs, such as solaris
4340 thread support. */
c906108c 4341
c906108c 4342
c3f6f71d
JM
4343static int
4344procfs_can_run ()
4345{
4346 /* This variable is controlled by modules that sit atop procfs that
4347 may layer their own process structure atop that provided here.
4348 sol-thread.c does this because of the Solaris two-level thread
4349 model. */
4350
4351 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
c906108c 4352
c3f6f71d
JM
4353 return !procfs_suppress_run;
4354}
c906108c 4355
c3f6f71d
JM
4356/*
4357 * Function: target_stop
4358 *
4359 * Stop the child process asynchronously, as when the
4360 * gdb user types control-c or presses a "stop" button.
4361 *
4362 * Works by sending kill(SIGINT) to the child's process group.
4363 */
c906108c 4364
c3f6f71d
JM
4365static void
4366procfs_stop ()
4367{
4368 extern pid_t inferior_process_group;
c906108c 4369
c3f6f71d 4370 kill (-inferior_process_group, SIGINT);
c906108c
SS
4371}
4372
c906108c 4373/*
c3f6f71d
JM
4374 * Function: unconditionally_kill_inferior
4375 *
4376 * Make it die. Wait for it to die. Clean up after it.
4377 * Note: this should only be applied to the real process,
4378 * not to an LWP, because of the check for parent-process.
4379 * If we need this to work for an LWP, it needs some more logic.
4380 */
c906108c 4381
c3f6f71d
JM
4382static void
4383unconditionally_kill_inferior (pi)
4384 procinfo *pi;
4385{
4386 int parent_pid;
c906108c 4387
c3f6f71d
JM
4388 parent_pid = proc_parent_pid (pi);
4389#ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4390 /* FIXME: use access functions */
4391 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4392 before the PIOCKILL, otherwise it might generate a corrupted core
4393 file for the inferior. */
4394 if (ioctl (pi->ctl_fd, PIOCSSIG, NULL) < 0)
4395 {
4396 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4397 }
4398#endif
4399#ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4400 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4401 to kill the inferior, otherwise it might remain stopped with a
4402 pending SIGKILL.
4403 We do not check the result of the PIOCSSIG, the inferior might have
4404 died already. */
4405 {
4406 struct siginfo newsiginfo;
c906108c 4407
c3f6f71d
JM
4408 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
4409 newsiginfo.si_signo = SIGKILL;
4410 newsiginfo.si_code = 0;
4411 newsiginfo.si_errno = 0;
4412 newsiginfo.si_pid = getpid ();
4413 newsiginfo.si_uid = getuid ();
4414 /* FIXME: use proc_set_current_signal */
4415 ioctl (pi->ctl_fd, PIOCSSIG, &newsiginfo);
4416 }
4417#else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4418 if (!proc_kill (pi, SIGKILL))
103b3ef5 4419 proc_error (pi, "unconditionally_kill, proc_kill", __LINE__);
c3f6f71d
JM
4420#endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4421 destroy_procinfo (pi);
c906108c 4422
c3f6f71d
JM
4423 /* If pi is GDB's child, wait for it to die. */
4424 if (parent_pid == getpid ())
4425 /* FIXME: should we use waitpid to make sure we get the right event?
4426 Should we check the returned event? */
4427 {
0d06e24b 4428#if 0
c3f6f71d 4429 int status, ret;
c906108c 4430
c3f6f71d
JM
4431 ret = waitpid (pi->pid, &status, 0);
4432#else
4433 wait (NULL);
4434#endif
4435 }
4436}
c906108c 4437
c3f6f71d
JM
4438/*
4439 * Function: target_kill_inferior
4440 *
4441 * We're done debugging it, and we want it to go away.
4442 * Then we want GDB to forget all about it.
c906108c
SS
4443 */
4444
c3f6f71d
JM
4445static void
4446procfs_kill_inferior ()
c906108c 4447{
c3f6f71d
JM
4448 if (inferior_pid != 0) /* ? */
4449 {
4450 /* Find procinfo for main process */
4451 procinfo *pi = find_procinfo (PIDGET (inferior_pid), 0);
c906108c 4452
c3f6f71d
JM
4453 if (pi)
4454 unconditionally_kill_inferior (pi);
4455 target_mourn_inferior ();
c906108c 4456 }
c3f6f71d
JM
4457}
4458
4459/*
4460 * Function: target_mourn_inferior
4461 *
4462 * Forget we ever debugged this thing!
4463 */
c906108c 4464
c3f6f71d
JM
4465static void
4466procfs_mourn_inferior ()
4467{
4468 procinfo *pi;
c906108c 4469
c3f6f71d
JM
4470 if (inferior_pid != 0)
4471 {
4472 /* Find procinfo for main process */
4473 pi = find_procinfo (PIDGET (inferior_pid), 0);
4474 if (pi)
4475 destroy_procinfo (pi);
c906108c 4476 }
c3f6f71d
JM
4477 unpush_target (&procfs_ops);
4478 generic_mourn_inferior ();
4479}
c906108c 4480
c3f6f71d
JM
4481/*
4482 * Function: init_inferior
4483 *
4484 * When GDB forks to create a runnable inferior process,
4485 * this function is called on the parent side of the fork.
4486 * It's job is to do whatever is necessary to make the child
4487 * ready to be debugged, and then wait for the child to synchronize.
4488 */
c906108c 4489
c3f6f71d
JM
4490static void
4491procfs_init_inferior (pid)
4492 int pid;
4493{
4494 procinfo *pi;
4495 sigset_t signals;
4496 int fail;
c906108c 4497
c3f6f71d
JM
4498 /* This routine called on the parent side (GDB side)
4499 after GDB forks the inferior. */
c906108c 4500
c3f6f71d 4501 push_target (&procfs_ops);
c906108c 4502
c3f6f71d
JM
4503 if ((pi = create_procinfo (pid, 0)) == NULL)
4504 perror ("procfs: out of memory in 'init_inferior'");
4505
4506 if (!open_procinfo_files (pi, FD_CTL))
4507 proc_error (pi, "init_inferior, open_proc_files", __LINE__);
4508
4509 /*
4510 xmalloc // done
4511 open_procinfo_files // done
4512 link list // done
4513 prfillset (trace)
4514 procfs_notice_signals
4515 prfillset (fault)
4516 prdelset (FLTPAGE)
4517 PIOCWSTOP
4518 PIOCSFAULT
4519 */
4520
4521 /* If not stopped yet, wait for it to stop. */
4522 if (!(proc_flags (pi) & PR_STOPPED) &&
4523 !(proc_wait_for_stop (pi)))
4524 dead_procinfo (pi, "init_inferior: wait_for_stop failed", KILL);
4525
4526 /* Save some of the /proc state to be restored if we detach. */
4527 /* FIXME: Why? In case another debugger was debugging it?
4528 We're it's parent, for Ghu's sake! */
4529 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
4530 proc_error (pi, "init_inferior, get_traced_signals", __LINE__);
4531 if (!proc_get_held_signals (pi, &pi->saved_sighold))
4532 proc_error (pi, "init_inferior, get_held_signals", __LINE__);
4533 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
4534 proc_error (pi, "init_inferior, get_traced_faults", __LINE__);
4535 if (!proc_get_traced_sysentry (pi, &pi->saved_entryset))
4536 proc_error (pi, "init_inferior, get_traced_sysentry", __LINE__);
4537 if (!proc_get_traced_sysexit (pi, &pi->saved_exitset))
4538 proc_error (pi, "init_inferior, get_traced_sysexit", __LINE__);
4539
4540 /* Register to trace selected signals in the child. */
4541 prfillset (&signals);
4542 if (!register_gdb_signals (pi, &signals))
4543 proc_error (pi, "init_inferior, register_signals", __LINE__);
4544
4545 if ((fail = procfs_debug_inferior (pi)) != 0)
4546 proc_error (pi, "init_inferior (procfs_debug_inferior)", fail);
4547
0d06e24b
JM
4548 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4549 and possibly even turning ON kill-on-last-close at this point. But
4550 I can't make that change without careful testing which I don't have
4551 time to do right now... */
c3f6f71d
JM
4552 /* Turn on run-on-last-close flag so that the child
4553 will die if GDB goes away for some reason. */
4554 if (!proc_set_run_on_last_close (pi))
4555 proc_error (pi, "init_inferior, set_RLC", __LINE__);
4556
4557 /* The 'process ID' we return to GDB is composed of
4558 the actual process ID plus the lwp ID. */
4559 inferior_pid = MERGEPID (pi->pid, proc_get_current_thread (pi));
c906108c 4560
c3f6f71d
JM
4561#ifdef START_INFERIOR_TRAPS_EXPECTED
4562 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
4563#else
4564 /* One trap to exec the shell, one to exec the program being debugged. */
4565 startup_inferior (2);
0d06e24b 4566#endif /* START_INFERIOR_TRAPS_EXPECTED */
c3f6f71d 4567}
c906108c 4568
c3f6f71d
JM
4569/*
4570 * Function: set_exec_trap
4571 *
4572 * When GDB forks to create a new process, this function is called
4573 * on the child side of the fork before GDB exec's the user program.
4574 * Its job is to make the child minimally debuggable, so that the
4575 * parent GDB process can connect to the child and take over.
4576 * This function should do only the minimum to make that possible,
4577 * and to synchronize with the parent process. The parent process
4578 * should take care of the details.
4579 */
4580
4581static void
4582procfs_set_exec_trap ()
4583{
4584 /* This routine called on the child side (inferior side)
4585 after GDB forks the inferior. It must use only local variables,
4586 because it may be sharing data space with its parent. */
c906108c 4587
c3f6f71d
JM
4588 procinfo *pi;
4589 sysset_t exitset;
c906108c 4590
c3f6f71d
JM
4591 if ((pi = create_procinfo (getpid (), 0)) == NULL)
4592 perror_with_name ("procfs: create_procinfo failed in child.");
c906108c 4593
c3f6f71d
JM
4594 if (open_procinfo_files (pi, FD_CTL) == 0)
4595 {
4596 proc_warn (pi, "set_exec_trap, open_proc_files", __LINE__);
4597 gdb_flush (gdb_stderr);
4598 /* no need to call "dead_procinfo", because we're going to exit. */
4599 _exit (127);
4600 }
c906108c 4601
c3f6f71d
JM
4602#ifdef PRFS_STOPEXEC /* defined on OSF */
4603 /* OSF method for tracing exec syscalls. Quoting:
4604 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4605 exits from exec system calls because of the user level loader. */
4606 /* FIXME: make nice and maybe move into an access function. */
4607 {
4608 int prfs_flags;
c906108c 4609
c3f6f71d
JM
4610 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
4611 {
4612 proc_warn (pi, "set_exec_trap (PIOCGSPCACT)", __LINE__);
4613 gdb_flush (gdb_stderr);
4614 _exit (127);
4615 }
4616 prfs_flags |= PRFS_STOPEXEC;
c906108c 4617
c3f6f71d
JM
4618 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
4619 {
4620 proc_warn (pi, "set_exec_trap (PIOCSSPCACT)", __LINE__);
4621 gdb_flush (gdb_stderr);
4622 _exit (127);
4623 }
4624 }
4625#else /* not PRFS_STOPEXEC */
4626 /* Everyone else's (except OSF) method for tracing exec syscalls */
4627 /* GW: Rationale...
4628 Not all systems with /proc have all the exec* syscalls with the same
4629 names. On the SGI, for example, there is no SYS_exec, but there
4630 *is* a SYS_execv. So, we try to account for that. */
c906108c 4631
c3f6f71d
JM
4632 premptyset (&exitset);
4633#ifdef SYS_exec
4634 praddset (&exitset, SYS_exec);
4635#endif
4636#ifdef SYS_execve
4637 praddset (&exitset, SYS_execve);
4638#endif
4639#ifdef SYS_execv
4640 praddset (&exitset, SYS_execv);
c906108c 4641#endif
c906108c 4642
c3f6f71d 4643 if (!proc_set_traced_sysexit (pi, &exitset))
c906108c 4644 {
c3f6f71d
JM
4645 proc_warn (pi, "set_exec_trap, set_traced_sysexit", __LINE__);
4646 gdb_flush (gdb_stderr);
4647 _exit (127);
c906108c 4648 }
c3f6f71d
JM
4649#endif /* PRFS_STOPEXEC */
4650
4651 /* FIXME: should this be done in the parent instead? */
4652 /* Turn off inherit on fork flag so that all grand-children
4653 of gdb start with tracing flags cleared. */
4654 if (!proc_unset_inherit_on_fork (pi))
4655 proc_warn (pi, "set_exec_trap, unset_inherit", __LINE__);
4656
4657 /* Turn off run on last close flag, so that the child process
4658 cannot run away just because we close our handle on it.
4659 We want it to wait for the parent to attach. */
4660 if (!proc_unset_run_on_last_close (pi))
4661 proc_warn (pi, "set_exec_trap, unset_RLC", __LINE__);
4662
4663 /* FIXME: No need to destroy the procinfo --
4664 we have our own address space, and we're about to do an exec! */
4665 /*destroy_procinfo (pi);*/
c906108c 4666}
c906108c 4667
c3f6f71d
JM
4668/*
4669 * Function: create_inferior
4670 *
4671 * This function is called BEFORE gdb forks the inferior process.
4672 * Its only real responsibility is to set things up for the fork,
4673 * and tell GDB which two functions to call after the fork (one
4674 * for the parent, and one for the child).
4675 *
4676 * This function does a complicated search for a unix shell program,
4677 * which it then uses to parse arguments and environment variables
4678 * to be sent to the child. I wonder whether this code could not
4679 * be abstracted out and shared with other unix targets such as
4680 * infptrace?
4681 */
c906108c
SS
4682
4683static void
4684procfs_create_inferior (exec_file, allargs, env)
4685 char *exec_file;
4686 char *allargs;
4687 char **env;
4688{
4689 char *shell_file = getenv ("SHELL");
4690 char *tryname;
4691 if (shell_file != NULL && strchr (shell_file, '/') == NULL)
4692 {
4693
4694 /* We will be looking down the PATH to find shell_file. If we
c3f6f71d
JM
4695 just do this the normal way (via execlp, which operates by
4696 attempting an exec for each element of the PATH until it
4697 finds one which succeeds), then there will be an exec for
4698 each failed attempt, each of which will cause a PR_SYSEXIT
4699 stop, and we won't know how to distinguish the PR_SYSEXIT's
4700 for these failed execs with the ones for successful execs
4701 (whether the exec has succeeded is stored at that time in the
4702 carry bit or some such architecture-specific and
4703 non-ABI-specified place).
4704
4705 So I can't think of anything better than to search the PATH
4706 now. This has several disadvantages: (1) There is a race
4707 condition; if we find a file now and it is deleted before we
4708 exec it, we lose, even if the deletion leaves a valid file
4709 further down in the PATH, (2) there is no way to know exactly
4710 what an executable (in the sense of "capable of being
4711 exec'd") file is. Using access() loses because it may lose
4712 if the caller is the superuser; failing to use it loses if
4713 there are ACLs or some such. */
c906108c
SS
4714
4715 char *p;
4716 char *p1;
4717 /* FIXME-maybe: might want "set path" command so user can change what
c3f6f71d 4718 path is used from within GDB. */
c906108c
SS
4719 char *path = getenv ("PATH");
4720 int len;
4721 struct stat statbuf;
4722
4723 if (path == NULL)
4724 path = "/bin:/usr/bin";
4725
4726 tryname = alloca (strlen (path) + strlen (shell_file) + 2);
c3f6f71d 4727 for (p = path; p != NULL; p = p1 ? p1 + 1: NULL)
c906108c
SS
4728 {
4729 p1 = strchr (p, ':');
4730 if (p1 != NULL)
4731 len = p1 - p;
4732 else
4733 len = strlen (p);
4734 strncpy (tryname, p, len);
4735 tryname[len] = '\0';
4736 strcat (tryname, "/");
4737 strcat (tryname, shell_file);
4738 if (access (tryname, X_OK) < 0)
4739 continue;
4740 if (stat (tryname, &statbuf) < 0)
4741 continue;
4742 if (!S_ISREG (statbuf.st_mode))
4743 /* We certainly need to reject directories. I'm not quite
4744 as sure about FIFOs, sockets, etc., but I kind of doubt
4745 that people want to exec() these things. */
4746 continue;
4747 break;
4748 }
4749 if (p == NULL)
4750 /* Not found. This must be an error rather than merely passing
4751 the file to execlp(), because execlp() would try all the
4752 exec()s, causing GDB to get confused. */
c3f6f71d
JM
4753 error ("procfs:%d -- Can't find shell %s in PATH",
4754 __LINE__, shell_file);
c906108c
SS
4755
4756 shell_file = tryname;
4757 }
4758
c3f6f71d
JM
4759 fork_inferior (exec_file, allargs, env, procfs_set_exec_trap,
4760 procfs_init_inferior, NULL, shell_file);
c906108c
SS
4761
4762 /* We are at the first instruction we care about. */
4763 /* Pedal to the metal... */
4764
2acceee2 4765 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
c906108c
SS
4766}
4767
c3f6f71d
JM
4768/*
4769 * Function: notice_thread
4770 *
4771 * Callback for find_new_threads.
4772 * Calls "add_thread".
4773 */
c906108c 4774
c3f6f71d
JM
4775static int
4776procfs_notice_thread (pi, thread, ptr)
4777 procinfo *pi;
4778 procinfo *thread;
4779 void *ptr;
c906108c 4780{
c3f6f71d 4781 int gdb_threadid = MERGEPID (pi->pid, thread->tid);
c906108c 4782
c3f6f71d
JM
4783 if (!in_thread_list (gdb_threadid))
4784 add_thread (gdb_threadid);
c906108c 4785
c3f6f71d
JM
4786 return 0;
4787}
4788
4789/*
4790 * Function: target_find_new_threads
4791 *
4792 * Query all the threads that the target knows about,
4793 * and give them back to GDB to add to its list.
4794 */
4795
4796void
4797procfs_find_new_threads ()
4798{
4799 procinfo *pi;
4800
4801 /* Find procinfo for main process */
4802 pi = find_procinfo_or_die (PIDGET (inferior_pid), 0);
4803 proc_update_threads (pi);
4804 proc_iterate_over_threads (pi, procfs_notice_thread, NULL);
c906108c
SS
4805}
4806
c3f6f71d
JM
4807/*
4808 * Function: target_thread_alive
4809 *
4810 * Return true if the thread is still 'alive'.
4811 *
4812 * This guy doesn't really seem to be doing his job.
4813 * Got to investigate how to tell when a thread is really gone.
4814 */
c906108c 4815
c906108c 4816static int
c3f6f71d
JM
4817procfs_thread_alive (pid)
4818 int pid;
c906108c 4819{
c3f6f71d
JM
4820 int proc, thread;
4821 procinfo *pi;
c906108c 4822
c3f6f71d
JM
4823 proc = PIDGET (pid);
4824 thread = TIDGET (pid);
4825 /* If I don't know it, it ain't alive! */
4826 if ((pi = find_procinfo (proc, thread)) == NULL)
4827 return 0;
4828
4829 /* If I can't get its status, it ain't alive!
4830 What's more, I need to forget about it! */
4831 if (!proc_get_status (pi))
4832 {
4833 destroy_procinfo (pi);
4834 return 0;
4835 }
4836 /* I couldn't have got its status if it weren't alive, so it's alive. */
4837 return 1;
c906108c 4838}
c3f6f71d
JM
4839
4840/*
4841 * Function: target_pid_to_str
4842 *
4843 * Return a string to be used to identify the thread in
4844 * the "info threads" display.
4845 */
4846
4847char *
4848procfs_pid_to_str (pid)
c5aa993b 4849 int pid;
c3f6f71d
JM
4850{
4851 static char buf[80];
4852 int proc, thread;
4853 procinfo *pi;
4854
4855 proc = PIDGET (pid);
4856 thread = TIDGET (pid);
4857 pi = find_procinfo (proc, thread);
4858
4859 if (thread == 0)
4860 sprintf (buf, "Process %d", proc);
4861 else
4862 sprintf (buf, "LWP %d", thread);
4863 return &buf[0];
4864}
4865
4866/*
4867 * Function: procfs_set_watchpoint
4868 * Insert a watchpoint
4869 */
4870
4871int
4872procfs_set_watchpoint (pid, addr, len, rwflag, after)
4873 int pid;
c5aa993b 4874 CORE_ADDR addr;
c3f6f71d
JM
4875 int len;
4876 int rwflag;
4877 int after;
c906108c 4878{
c3f6f71d
JM
4879#ifndef UNIXWARE
4880 int pflags = 0;
4881 procinfo *pi;
4882
4883 pi = find_procinfo_or_die (pid == -1 ?
4884 PIDGET (inferior_pid) : PIDGET (pid), 0);
4885
4886 /* Translate from GDB's flags to /proc's */
4887 if (len > 0) /* len == 0 means delete watchpoint */
c906108c 4888 {
c3f6f71d
JM
4889 switch (rwflag) { /* FIXME: need an enum! */
4890 case hw_write: /* default watchpoint (write) */
4891 pflags = WRITE_WATCHFLAG;
4892 break;
4893 case hw_read: /* read watchpoint */
4894 pflags = READ_WATCHFLAG;
4895 break;
4896 case hw_access: /* access watchpoint */
4897 pflags = READ_WATCHFLAG | WRITE_WATCHFLAG;
4898 break;
4899 case hw_execute: /* execution HW breakpoint */
4900 pflags = EXEC_WATCHFLAG;
4901 break;
4902 default: /* Something weird. Return error. */
c906108c 4903 return -1;
c3f6f71d
JM
4904 }
4905 if (after) /* Stop after r/w access is completed. */
4906 pflags |= AFTER_WATCHFLAG;
4907 }
4908
4909 if (!proc_set_watchpoint (pi, addr, len, pflags))
4910 {
4911 if (errno == E2BIG) /* Typical error for no resources */
4912 return -1; /* fail */
4913 /* GDB may try to remove the same watchpoint twice.
4914 If a remove request returns no match, don't error. */
c906108c 4915 if (errno == ESRCH && len == 0)
c3f6f71d
JM
4916 return 0; /* ignore */
4917 proc_error (pi, "set_watchpoint", __LINE__);
c906108c 4918 }
c3f6f71d 4919#endif
c906108c
SS
4920 return 0;
4921}
4922
c3f6f71d
JM
4923/*
4924 * Function: stopped_by_watchpoint
4925 *
4926 * Returns non-zero if process is stopped on a hardware watchpoint fault,
4927 * else returns zero.
4928 */
4929
c906108c 4930int
c5aa993b 4931procfs_stopped_by_watchpoint (pid)
c3f6f71d 4932 int pid;
c906108c 4933{
c3f6f71d 4934 procinfo *pi;
c906108c 4935
c3f6f71d
JM
4936 pi = find_procinfo_or_die (pid == -1 ?
4937 PIDGET (inferior_pid) : PIDGET (pid), 0);
4938 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
c906108c 4939 {
c3f6f71d
JM
4940 if (proc_why (pi) == PR_FAULTED)
4941 {
c906108c 4942#ifdef FLTWATCH
c3f6f71d
JM
4943 if (proc_what (pi) == FLTWATCH)
4944 return 1;
c906108c
SS
4945#endif
4946#ifdef FLTKWATCH
c3f6f71d
JM
4947 if (proc_what (pi) == FLTKWATCH)
4948 return 1;
c906108c 4949#endif
c3f6f71d 4950 }
c906108c
SS
4951 }
4952 return 0;
4953}
c906108c 4954
c3f6f71d
JM
4955#ifdef TM_I386SOL2_H
4956/*
4957 * Function: procfs_find_LDT_entry
4958 *
4959 * Input:
4960 * int pid; // The GDB-style pid-plus-LWP.
4961 *
4962 * Return:
4963 * pointer to the corresponding LDT entry.
4964 */
c906108c 4965
c3f6f71d
JM
4966struct ssd *
4967procfs_find_LDT_entry (pid)
c906108c
SS
4968 int pid;
4969{
c3f6f71d
JM
4970 gdb_gregset_t *gregs;
4971 int key;
4972 procinfo *pi;
c906108c 4973
c3f6f71d
JM
4974 /* Find procinfo for the lwp. */
4975 if ((pi = find_procinfo (PIDGET (pid), TIDGET (pid))) == NULL)
c906108c 4976 {
c3f6f71d
JM
4977 warning ("procfs_find_LDT_entry: could not find procinfi for %d.",
4978 pid);
4979 return NULL;
c906108c 4980 }
c3f6f71d
JM
4981 /* get its general registers. */
4982 if ((gregs = proc_get_gregs (pi)) == NULL)
4983 {
4984 warning ("procfs_find_LDT_entry: could not read gregs for %d.",
4985 pid);
4986 return NULL;
4987 }
4988 /* Now extract the GS register's lower 16 bits. */
4989 key = (*gregs)[GS] & 0xffff;
4990
4991 /* Find the matching entry and return it. */
4992 return proc_get_LDT_entry (pi, key);
c906108c 4993}
c3f6f71d 4994#endif /* TM_I386SOL2_H */
c906108c 4995
c3f6f71d
JM
4996
4997
4998static void
4999info_proc_cmd (args, from_tty)
5000 char *args;
5001 int from_tty;
c906108c 5002{
c3f6f71d
JM
5003 struct cleanup *old_chain;
5004 procinfo *process = NULL;
5005 procinfo *thread = NULL;
5006 char **argv = NULL;
5007 char *tmp = NULL;
5008 int pid = 0;
5009 int tid = 0;
c906108c 5010
c3f6f71d
JM
5011 old_chain = make_cleanup (null_cleanup, 0);
5012 if (args)
0fda6bd2
JM
5013 {
5014 if ((argv = buildargv (args)) == NULL)
5015 nomem (0);
5016 else
004527cb 5017 make_cleanup_freeargv (argv);
0fda6bd2 5018 }
c3f6f71d
JM
5019 while (argv != NULL && *argv != NULL)
5020 {
5021 if (isdigit (argv[0][0]))
5022 {
5023 pid = strtoul (argv[0], &tmp, 10);
5024 if (*tmp == '/')
5025 tid = strtoul (++tmp, NULL, 10);
5026 }
5027 else if (argv[0][0] == '/')
5028 {
5029 tid = strtoul (argv[0] + 1, NULL, 10);
5030 }
5031 else
5032 {
5033 /* [...] */
5034 }
5035 argv++;
5036 }
5037 if (pid == 0)
5038 pid = PIDGET (inferior_pid);
5039 if (pid == 0)
5040 error ("No current process: you must name one.");
5041 else
c906108c 5042 {
c3f6f71d
JM
5043 /* Have pid, will travel.
5044 First see if it's a process we're already debugging. */
5045 process = find_procinfo (pid, 0);
5046 if (process == NULL)
5047 {
5048 /* No. So open a procinfo for it, but
5049 remember to close it again when finished. */
5050 process = create_procinfo (pid, 0);
004527cb 5051 make_cleanup (do_destroy_procinfo_cleanup, process);
c3f6f71d
JM
5052 if (!open_procinfo_files (process, FD_CTL))
5053 proc_error (process, "info proc, open_procinfo_files", __LINE__);
5054 }
c906108c 5055 }
c3f6f71d
JM
5056 if (tid != 0)
5057 thread = create_procinfo (pid, tid);
5058
5059 if (process)
5060 {
5061 printf_filtered ("process %d flags:\n", process->pid);
5062 proc_prettyprint_flags (proc_flags (process), 1);
5063 if (proc_flags (process) & (PR_STOPPED | PR_ISTOP))
5064 proc_prettyprint_why (proc_why (process), proc_what (process), 1);
5065 if (proc_get_nthreads (process) > 1)
5066 printf_filtered ("Process has %d threads.\n",
5067 proc_get_nthreads (process));
5068 }
5069 if (thread)
5070 {
5071 printf_filtered ("thread %d flags:\n", thread->tid);
5072 proc_prettyprint_flags (proc_flags (thread), 1);
5073 if (proc_flags (thread) & (PR_STOPPED | PR_ISTOP))
5074 proc_prettyprint_why (proc_why (thread), proc_what (thread), 1);
5075 }
5076
5077 do_cleanups (old_chain);
c906108c
SS
5078}
5079
c3f6f71d
JM
5080static void
5081proc_trace_syscalls (args, from_tty, entry_or_exit, mode)
5082 char *args;
5083 int from_tty;
5084 int entry_or_exit;
5085 int mode;
c906108c 5086{
c3f6f71d
JM
5087 procinfo *pi;
5088 sysset_t *sysset;
5089 int syscallnum = 0;
c906108c 5090
c3f6f71d
JM
5091 if (inferior_pid <= 0)
5092 error ("you must be debugging a process to use this command.");
c906108c 5093
c3f6f71d
JM
5094 if (args == NULL || args[0] == 0)
5095 error_no_arg ("system call to trace");
5096
5097 pi = find_procinfo_or_die (PIDGET (inferior_pid), 0);
5098 if (isdigit (args[0]))
5099 {
5100 syscallnum = atoi (args);
5101 if (entry_or_exit == PR_SYSENTRY)
5102 sysset = proc_get_traced_sysentry (pi, NULL);
5103 else
5104 sysset = proc_get_traced_sysexit (pi, NULL);
c906108c 5105
c3f6f71d
JM
5106 if (sysset == NULL)
5107 proc_error (pi, "proc-trace, get_traced_sysset", __LINE__);
c906108c 5108
c3f6f71d
JM
5109 if (mode == FLAG_SET)
5110 praddset (sysset, syscallnum);
5111 else
5112 prdelset (sysset, syscallnum);
c906108c 5113
c3f6f71d
JM
5114 if (entry_or_exit == PR_SYSENTRY)
5115 {
5116 if (!proc_set_traced_sysentry (pi, sysset))
5117 proc_error (pi, "proc-trace, set_traced_sysentry", __LINE__);
5118 }
5119 else
5120 {
5121 if (!proc_set_traced_sysexit (pi, sysset))
5122 proc_error (pi, "proc-trace, set_traced_sysexit", __LINE__);
5123 }
5124 }
5125}
5126
5127static void
5128proc_trace_sysentry_cmd (args, from_tty)
5129 char *args;
5130 int from_tty;
c906108c 5131{
c3f6f71d
JM
5132 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_SET);
5133}
c906108c 5134
c3f6f71d
JM
5135static void
5136proc_trace_sysexit_cmd (args, from_tty)
5137 char *args;
5138 int from_tty;
5139{
5140 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_SET);
c906108c 5141}
c906108c 5142
c3f6f71d
JM
5143static void
5144proc_untrace_sysentry_cmd (args, from_tty)
5145 char *args;
5146 int from_tty;
5147{
5148 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_RESET);
5149}
5150
5151static void
5152proc_untrace_sysexit_cmd (args, from_tty)
5153 char *args;
5154 int from_tty;
c906108c 5155{
c3f6f71d
JM
5156 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_RESET);
5157}
c906108c 5158
c906108c 5159
c906108c
SS
5160void
5161_initialize_procfs ()
5162{
c906108c
SS
5163 init_procfs_ops ();
5164 add_target (&procfs_ops);
c3f6f71d
JM
5165 add_info ("proc", info_proc_cmd,
5166 "Show /proc process information about any running process.\
5167Default is the process being debugged.");
5168 add_com ("proc-trace-entry", no_class, proc_trace_sysentry_cmd,
5169 "Give a trace of entries into the syscall.");
5170 add_com ("proc-trace-exit", no_class, proc_trace_sysexit_cmd,
5171 "Give a trace of exits from the syscall.");
5172 add_com ("proc-untrace-entry", no_class, proc_untrace_sysentry_cmd,
5173 "Cancel a trace of entries into the syscall.");
5174 add_com ("proc-untrace-exit", no_class, proc_untrace_sysexit_cmd,
5175 "Cancel a trace of exits from the syscall.");
c3f6f71d
JM
5176}
5177
5178/* =================== END, GDB "MODULE" =================== */
5179
5180
5181
5182/* miscelaneous stubs: */
5183/* The following satisfy a few random symbols mostly created by */
5184/* the solaris threads implementation, which I will chase down */
5185/* later. */
5186
5187/*
5188 * Return a pid for which we guarantee
5189 * we will be able to find a 'live' procinfo.
5190 */
5191
5192int
5193procfs_first_available ()
5194{
5195 if (procinfo_list)
5196 return procinfo_list->pid;
5197 else
5198 return -1;
5199}