-/* Returns the (complete) size of a sysset_t struct. Normally, this
- is just sizeof (sysset_t), but in the case of Monterey/64, the
- actual size of sysset_t isn't known until runtime. */
-
-static int
-sysset_t_size (procinfo * pi)
-{
-#ifndef DYNAMIC_SYSCALLS
- return sizeof (sysset_t);
-#else
- return sizeof (sysset_t) - sizeof (uint64_t)
- + sizeof (uint64_t) * ((pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
- / (8 * sizeof (uint64_t)));
-#endif
-}
-
-/* Allocate and (partially) initialize a sysset_t struct. */
-
-static sysset_t *
-sysset_t_alloc (procinfo * pi)
-{
- sysset_t *ret;
- int size = sysset_t_size (pi);
-
- ret = xmalloc (size);
-#ifdef DYNAMIC_SYSCALLS
- ret->pr_size = ((pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
- / (8 * sizeof (uint64_t)));
-#endif
- return ret;
-}
-
-#ifdef DYNAMIC_SYSCALLS
-
-/* Extract syscall numbers and names from /proc/<pid>/sysent. Initialize
- pi->num_syscalls with the number of syscalls and pi->syscall_names
- with the names. (Certain numbers may be skipped in which case the
- names for these numbers will be left as NULL.) */
-
-#define MAX_SYSCALL_NAME_LENGTH 256
-#define MAX_SYSCALLS 65536
-
-static void
-load_syscalls (procinfo *pi)
-{
- char pathname[MAX_PROC_NAME_SIZE];
- int sysent_fd;
- prsysent_t header;
- prsyscall_t *syscalls;
- int i, size, maxcall;
- struct cleanup *cleanups;
-
- pi->num_syscalls = 0;
- pi->syscall_names = 0;
-
- /* Open the file descriptor for the sysent file. */
- sprintf (pathname, "/proc/%d/sysent", pi->pid);
- sysent_fd = open_with_retry (pathname, O_RDONLY);
- if (sysent_fd < 0)
- {
- error (_("load_syscalls: Can't open /proc/%d/sysent"), pi->pid);
- }
- cleanups = make_cleanup_close (sysent_fd);
-
- size = sizeof header - sizeof (prsyscall_t);
- if (read (sysent_fd, &header, size) != size)
- {
- error (_("load_syscalls: Error reading /proc/%d/sysent"), pi->pid);
- }
-
- if (header.pr_nsyscalls == 0)
- {
- error (_("load_syscalls: /proc/%d/sysent contains no syscalls!"),
- pi->pid);
- }
-
- size = header.pr_nsyscalls * sizeof (prsyscall_t);
- syscalls = xmalloc (size);
- make_cleanup (free_current_contents, &syscalls);
-
- if (read (sysent_fd, syscalls, size) != size)
- error (_("load_syscalls: Error reading /proc/%d/sysent"), pi->pid);
-
- /* Find maximum syscall number. This may not be the same as
- pr_nsyscalls since that value refers to the number of entries
- in the table. (Also, the docs indicate that some system
- call numbers may be skipped.) */
-
- maxcall = syscalls[0].pr_number;
-
- for (i = 1; i < header.pr_nsyscalls; i++)
- if (syscalls[i].pr_number > maxcall
- && syscalls[i].pr_nameoff > 0
- && syscalls[i].pr_number < MAX_SYSCALLS)
- maxcall = syscalls[i].pr_number;
-
- pi->num_syscalls = maxcall+1;
- pi->syscall_names = XNEWVEC (char *, pi->num_syscalls);
-
- for (i = 0; i < pi->num_syscalls; i++)
- pi->syscall_names[i] = NULL;
-
- /* Read the syscall names in. */
- for (i = 0; i < header.pr_nsyscalls; i++)
- {
- char namebuf[MAX_SYSCALL_NAME_LENGTH];
- int nread;
- int callnum;
-
- if (syscalls[i].pr_number >= MAX_SYSCALLS
- || syscalls[i].pr_number < 0
- || syscalls[i].pr_nameoff <= 0
- || (lseek (sysent_fd, (off_t) syscalls[i].pr_nameoff, SEEK_SET)
- != (off_t) syscalls[i].pr_nameoff))
- continue;
-
- nread = read (sysent_fd, namebuf, sizeof namebuf);
- if (nread <= 0)
- continue;
-
- callnum = syscalls[i].pr_number;
-
- if (pi->syscall_names[callnum] != NULL)
- {
- /* FIXME: Generate warning. */
- continue;
- }
-
- namebuf[nread-1] = '\0';
- size = strlen (namebuf) + 1;
- pi->syscall_names[callnum] = xmalloc (size);
- strncpy (pi->syscall_names[callnum], namebuf, size-1);
- pi->syscall_names[callnum][size-1] = '\0';
- }
-
- do_cleanups (cleanups);
-}
-
-/* Free the space allocated for the syscall names from the procinfo
- structure. */
-
-static void
-free_syscalls (procinfo *pi)
-{
- if (pi->syscall_names)
- {
- int i;
-
- for (i = 0; i < pi->num_syscalls; i++)
- if (pi->syscall_names[i] != NULL)
- xfree (pi->syscall_names[i]);
-
- xfree (pi->syscall_names);
- pi->syscall_names = 0;
- }
-}
-
-/* Given a name, look up (and return) the corresponding syscall number.
- If no match is found, return -1. */
-
-static int
-find_syscall (procinfo *pi, char *name)
-{
- int i;
-
- for (i = 0; i < pi->num_syscalls; i++)
- {
- if (pi->syscall_names[i] && strcmp (name, pi->syscall_names[i]) == 0)
- return i;
- }
- return -1;
-}
-#endif
-