1 /* Native-dependent code for NetBSD.
3 Copyright (C) 2006-2020 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdbthread.h"
24 #include "nbsd-tdep.h"
28 #include <sys/types.h>
29 #include <sys/ptrace.h>
30 #include <sys/sysctl.h>
32 /* Return the name of a file that can be opened to get the symbols for
33 the child process identified by PID. */
36 nbsd_nat_target::pid_to_exec_file (int pid
)
38 static char buf
[PATH_MAX
];
40 int mib
[4] = {CTL_KERN
, KERN_PROC_ARGS
, pid
, KERN_PROC_PATHNAME
};
41 buflen
= sizeof (buf
);
42 if (sysctl (mib
, ARRAY_SIZE (mib
), buf
, &buflen
, NULL
, 0))
47 /* Return the current directory for the process identified by PID. */
50 nbsd_pid_to_cwd (int pid
)
54 int mib
[4] = {CTL_KERN
, KERN_PROC_ARGS
, pid
, KERN_PROC_CWD
};
55 buflen
= sizeof (buf
);
56 if (sysctl (mib
, ARRAY_SIZE (mib
), buf
, &buflen
, NULL
, 0))
61 /* Return the kinfo_proc2 structure for the process identified by PID. */
64 nbsd_pid_to_kinfo_proc2 (pid_t pid
, struct kinfo_proc2
*kp
)
66 gdb_assert (kp
!= nullptr);
68 size_t size
= sizeof (*kp
);
69 int mib
[6] = {CTL_KERN
, KERN_PROC2
, KERN_PROC_PID
, pid
,
70 static_cast<int> (size
), 1};
71 return !sysctl (mib
, ARRAY_SIZE (mib
), kp
, &size
, NULL
, 0);
74 /* Return the command line for the process identified by PID. */
76 static gdb::unique_xmalloc_ptr
<char[]>
77 nbsd_pid_to_cmdline (int pid
)
79 int mib
[4] = {CTL_KERN
, KERN_PROC_ARGS
, pid
, KERN_PROC_ARGV
};
82 if (sysctl (mib
, ARRAY_SIZE (mib
), NULL
, &size
, NULL
, 0) == -1 || size
== 0)
85 gdb::unique_xmalloc_ptr
<char[]> args (XNEWVAR (char, size
));
87 if (sysctl (mib
, ARRAY_SIZE (mib
), args
.get (), &size
, NULL
, 0) == -1
91 /* Arguments are returned as a flattened string with NUL separators.
92 Join the arguments with spaces to form a single string. */
93 for (size_t i
= 0; i
< size
- 1; i
++)
96 args
[size
- 1] = '\0';
101 /* Generic thread (LWP) lister within a specified process. The callback
102 parameters is a C++ function that is called for each detected thread. */
105 nbsd_thread_lister (const pid_t pid
,
106 gdb::function_view
<bool (const struct kinfo_lwp
*)>
109 int mib
[5] = {CTL_KERN
, KERN_LWP
, pid
, sizeof (struct kinfo_lwp
), 0};
112 if (sysctl (mib
, ARRAY_SIZE (mib
), NULL
, &size
, NULL
, 0) == -1 || size
== 0)
113 perror_with_name (("sysctl"));
115 mib
[4] = size
/ sizeof (size_t);
117 gdb::unique_xmalloc_ptr
<struct kinfo_lwp
[]> kl
118 ((struct kinfo_lwp
*) xcalloc (size
, 1));
120 if (sysctl (mib
, ARRAY_SIZE (mib
), kl
.get (), &size
, NULL
, 0) == -1
122 perror_with_name (("sysctl"));
124 for (size_t i
= 0; i
< size
/ sizeof (struct kinfo_lwp
); i
++)
126 struct kinfo_lwp
*l
= &kl
[i
];
128 /* Return true if the specified thread is alive. */
130 = [] (struct kinfo_lwp
*lwp
)
145 /* Ignore embryonic or demised threads. */
156 /* Return true if PTID is still active in the inferior. */
159 nbsd_nat_target::thread_alive (ptid_t ptid
)
161 pid_t pid
= ptid
.pid ();
162 int lwp
= ptid
.lwp ();
165 = [&lwp
] (const struct kinfo_lwp
*kl
)
167 return kl
->l_lid
== lwp
;
170 return nbsd_thread_lister (pid
, fn
);
173 /* Return the name assigned to a thread by an application. Returns
174 the string in a static buffer. */
177 nbsd_nat_target::thread_name (struct thread_info
*thr
)
179 ptid_t ptid
= thr
->ptid
;
180 pid_t pid
= ptid
.pid ();
181 int lwp
= ptid
.lwp ();
183 static char buf
[KI_LNAMELEN
] = {};
186 = [&lwp
] (const struct kinfo_lwp
*kl
)
188 if (kl
->l_lid
== lwp
)
190 xsnprintf (buf
, sizeof buf
, "%s", kl
->l_name
);
196 if (nbsd_thread_lister (pid
, fn
))
202 /* Implement the "post_attach" target_ops method. */
205 nbsd_add_threads (nbsd_nat_target
*target
, pid_t pid
)
208 = [&target
, &pid
] (const struct kinfo_lwp
*kl
)
210 ptid_t ptid
= ptid_t (pid
, kl
->l_lid
, 0);
211 if (!in_thread_list (target
, ptid
))
213 if (inferior_ptid
.lwp () == 0)
214 thread_change_ptid (target
, inferior_ptid
, ptid
);
216 add_thread (target
, ptid
);
221 nbsd_thread_lister (pid
, fn
);
224 /* Implement the "post_attach" target_ops method. */
227 nbsd_nat_target::post_attach (int pid
)
229 nbsd_add_threads (this, pid
);
232 /* Implement the "update_thread_list" target_ops method. */
235 nbsd_nat_target::update_thread_list ()
239 nbsd_add_threads (this, inferior_ptid
.pid ());
242 /* Convert PTID to a string. */
245 nbsd_nat_target::pid_to_str (ptid_t ptid
)
247 int lwp
= ptid
.lwp ();
251 pid_t pid
= ptid
.pid ();
253 return string_printf ("LWP %d of process %d", lwp
, pid
);
256 return normal_pid_to_str (ptid
);
259 /* Retrieve all the memory regions in the specified process. */
261 static gdb::unique_xmalloc_ptr
<struct kinfo_vmentry
[]>
262 nbsd_kinfo_get_vmmap (pid_t pid
, size_t *size
)
264 int mib
[5] = {CTL_VM
, VM_PROC
, VM_PROC_MAP
, pid
,
265 sizeof (struct kinfo_vmentry
)};
268 if (sysctl (mib
, ARRAY_SIZE (mib
), NULL
, &length
, NULL
, 0))
274 /* Prereserve more space. The length argument is volatile and can change
275 between the sysctl(3) calls as this function can be called against a
277 length
= length
* 5 / 3;
279 gdb::unique_xmalloc_ptr
<struct kinfo_vmentry
[]> kiv
280 (XNEWVAR (kinfo_vmentry
, length
));
282 if (sysctl (mib
, ARRAY_SIZE (mib
), kiv
.get (), &length
, NULL
, 0))
288 *size
= length
/ sizeof (struct kinfo_vmentry
);
292 /* Iterate over all the memory regions in the current inferior,
293 calling FUNC for each memory region. OBFD is passed as the last
297 nbsd_nat_target::find_memory_regions (find_memory_region_ftype func
,
300 pid_t pid
= inferior_ptid
.pid ();
303 gdb::unique_xmalloc_ptr
<struct kinfo_vmentry
[]> vmentl
304 = nbsd_kinfo_get_vmmap (pid
, &nitems
);
306 perror_with_name (_("Couldn't fetch VM map entries."));
308 for (size_t i
= 0; i
< nitems
; i
++)
310 struct kinfo_vmentry
*kve
= &vmentl
[i
];
312 /* Skip unreadable segments and those where MAP_NOCORE has been set. */
313 if (!(kve
->kve_protection
& KVME_PROT_READ
)
314 || kve
->kve_flags
& KVME_FLAG_NOCOREDUMP
)
317 /* Skip segments with an invalid type. */
318 switch (kve
->kve_type
)
320 case KVME_TYPE_VNODE
:
322 case KVME_TYPE_SUBMAP
:
323 case KVME_TYPE_OBJECT
:
329 size_t size
= kve
->kve_end
- kve
->kve_start
;
332 fprintf_filtered (gdb_stdout
,
333 "Save segment, %ld bytes at %s (%c%c%c)\n",
335 paddress (target_gdbarch (), kve
->kve_start
),
336 kve
->kve_protection
& KVME_PROT_READ
? 'r' : '-',
337 kve
->kve_protection
& KVME_PROT_WRITE
? 'w' : '-',
338 kve
->kve_protection
& KVME_PROT_EXEC
? 'x' : '-');
341 /* Invoke the callback function to create the corefile segment.
342 Pass MODIFIED as true, we do not know the real modification state. */
343 func (kve
->kve_start
, size
, kve
->kve_protection
& KVME_PROT_READ
,
344 kve
->kve_protection
& KVME_PROT_WRITE
,
345 kve
->kve_protection
& KVME_PROT_EXEC
, 1, data
);
350 /* Implement the "info_proc" target_ops method. */
353 nbsd_nat_target::info_proc (const char *args
, enum info_proc_what what
)
356 bool do_cmdline
= false;
359 bool do_mappings
= false;
360 bool do_status
= false;
393 error (_("Not supported on this target."));
396 gdb_argv
built_argv (args
);
397 if (built_argv
.count () == 0)
399 pid
= inferior_ptid
.pid ();
401 error (_("No current process: you must name one."));
403 else if (built_argv
.count () == 1 && isdigit (built_argv
[0][0]))
404 pid
= strtol (built_argv
[0], NULL
, 10);
406 error (_("Invalid arguments."));
408 printf_filtered (_("process %d\n"), pid
);
412 gdb::unique_xmalloc_ptr
<char[]> cmdline
= nbsd_pid_to_cmdline (pid
);
413 if (cmdline
!= nullptr)
414 printf_filtered ("cmdline = '%s'\n", cmdline
.get ());
416 warning (_("unable to fetch command line"));
420 std::string cwd
= nbsd_pid_to_cwd (pid
);
422 printf_filtered ("cwd = '%s'\n", cwd
.c_str ());
424 warning (_("unable to fetch current working directory"));
428 const char *exe
= pid_to_exec_file (pid
);
430 printf_filtered ("exe = '%s'\n", exe
);
432 warning (_("unable to fetch executable path name"));
437 gdb::unique_xmalloc_ptr
<struct kinfo_vmentry
[]> vmentl
438 = nbsd_kinfo_get_vmmap (pid
, &nvment
);
440 if (vmentl
!= nullptr)
442 int addr_bit
= TARGET_CHAR_BIT
* sizeof (void *);
443 nbsd_info_proc_mappings_header (addr_bit
);
445 struct kinfo_vmentry
*kve
= vmentl
.get ();
446 for (int i
= 0; i
< nvment
; i
++, kve
++)
447 nbsd_info_proc_mappings_entry (addr_bit
, kve
->kve_start
,
448 kve
->kve_end
, kve
->kve_offset
,
449 kve
->kve_flags
, kve
->kve_protection
,
453 warning (_("unable to fetch virtual memory map"));
457 struct kinfo_proc2 kp
;
458 if (!nbsd_pid_to_kinfo_proc2 (pid
, &kp
))
459 warning (_("Failed to fetch process information"));
480 return "? (unknown)";
484 printf_filtered ("Name: %s\n", kp
.p_comm
);
485 printf_filtered ("State: %s\n", process_status(kp
.p_realstat
));
486 printf_filtered ("Parent process: %" PRId32
"\n", kp
.p_ppid
);
487 printf_filtered ("Process group: %" PRId32
"\n", kp
.p__pgid
);
488 printf_filtered ("Session id: %" PRId32
"\n", kp
.p_sid
);
489 printf_filtered ("TTY: %" PRId32
"\n", kp
.p_tdev
);
490 printf_filtered ("TTY owner process group: %" PRId32
"\n", kp
.p_tpgid
);
491 printf_filtered ("User IDs (real, effective, saved): "
492 "%" PRIu32
" %" PRIu32
" %" PRIu32
"\n",
493 kp
.p_ruid
, kp
.p_uid
, kp
.p_svuid
);
494 printf_filtered ("Group IDs (real, effective, saved): "
495 "%" PRIu32
" %" PRIu32
" %" PRIu32
"\n",
496 kp
.p_rgid
, kp
.p_gid
, kp
.p_svgid
);
498 printf_filtered ("Groups:");
499 for (int i
= 0; i
< kp
.p_ngroups
; i
++)
500 printf_filtered (" %" PRIu32
, kp
.p_groups
[i
]);
501 printf_filtered ("\n");
502 printf_filtered ("Minor faults (no memory page): %" PRIu64
"\n",
504 printf_filtered ("Major faults (memory page faults): %" PRIu64
"\n",
506 printf_filtered ("utime: %" PRIu32
".%06" PRIu32
"\n",
507 kp
.p_uutime_sec
, kp
.p_uutime_usec
);
508 printf_filtered ("stime: %" PRIu32
".%06" PRIu32
"\n",
509 kp
.p_ustime_sec
, kp
.p_ustime_usec
);
510 printf_filtered ("utime+stime, children: %" PRIu32
".%06" PRIu32
"\n",
511 kp
.p_uctime_sec
, kp
.p_uctime_usec
);
512 printf_filtered ("'nice' value: %" PRIu8
"\n", kp
.p_nice
);
513 printf_filtered ("Start time: %" PRIu32
".%06" PRIu32
"\n",
514 kp
.p_ustart_sec
, kp
.p_ustart_usec
);
515 int pgtok
= getpagesize () / 1024;
516 printf_filtered ("Data size: %" PRIuMAX
" kB\n",
517 (uintmax_t) kp
.p_vm_dsize
* pgtok
);
518 printf_filtered ("Stack size: %" PRIuMAX
" kB\n",
519 (uintmax_t) kp
.p_vm_ssize
* pgtok
);
520 printf_filtered ("Text size: %" PRIuMAX
" kB\n",
521 (uintmax_t) kp
.p_vm_tsize
* pgtok
);
522 printf_filtered ("Resident set size: %" PRIuMAX
" kB\n",
523 (uintmax_t) kp
.p_vm_rssize
* pgtok
);
524 printf_filtered ("Maximum RSS: %" PRIu64
" kB\n", kp
.p_uru_maxrss
);
525 printf_filtered ("Pending Signals:");
526 for (size_t i
= 0; i
< ARRAY_SIZE (kp
.p_siglist
.__bits
); i
++)
527 printf_filtered (" %08" PRIx32
, kp
.p_siglist
.__bits
[i
]);
528 printf_filtered ("\n");
529 printf_filtered ("Ignored Signals:");
530 for (size_t i
= 0; i
< ARRAY_SIZE (kp
.p_sigignore
.__bits
); i
++)
531 printf_filtered (" %08" PRIx32
, kp
.p_sigignore
.__bits
[i
]);
532 printf_filtered ("\n");
533 printf_filtered ("Caught Signals:");
534 for (size_t i
= 0; i
< ARRAY_SIZE (kp
.p_sigcatch
.__bits
); i
++)
535 printf_filtered (" %08" PRIx32
, kp
.p_sigcatch
.__bits
[i
]);
536 printf_filtered ("\n");