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 command line for the process identified by PID. */
63 static gdb::unique_xmalloc_ptr
<char[]>
64 nbsd_pid_to_cmdline (int pid
)
66 int mib
[4] = {CTL_KERN
, KERN_PROC_ARGS
, pid
, KERN_PROC_ARGV
};
69 if (sysctl (mib
, ARRAY_SIZE (mib
), NULL
, &size
, NULL
, 0) == -1 || size
== 0)
72 gdb::unique_xmalloc_ptr
<char[]> args (XNEWVAR (char, size
));
74 if (sysctl (mib
, ARRAY_SIZE (mib
), args
.get (), &size
, NULL
, 0) == -1
78 /* Arguments are returned as a flattened string with NUL separators.
79 Join the arguments with spaces to form a single string. */
80 for (size_t i
= 0; i
< size
- 1; i
++)
83 args
[size
- 1] = '\0';
88 /* Generic thread (LWP) lister within a specified process. The callback
89 parameters is a C++ function that is called for each detected thread. */
92 nbsd_thread_lister (const pid_t pid
,
93 gdb::function_view
<bool (const struct kinfo_lwp
*)>
96 int mib
[5] = {CTL_KERN
, KERN_LWP
, pid
, sizeof (struct kinfo_lwp
), 0};
99 if (sysctl (mib
, ARRAY_SIZE (mib
), NULL
, &size
, NULL
, 0) == -1 || size
== 0)
100 perror_with_name (("sysctl"));
102 mib
[4] = size
/ sizeof (size_t);
104 gdb::unique_xmalloc_ptr
<struct kinfo_lwp
[]> kl
105 ((struct kinfo_lwp
*) xcalloc (size
, 1));
107 if (sysctl (mib
, ARRAY_SIZE (mib
), kl
.get (), &size
, NULL
, 0) == -1
109 perror_with_name (("sysctl"));
111 for (size_t i
= 0; i
< size
/ sizeof (struct kinfo_lwp
); i
++)
113 struct kinfo_lwp
*l
= &kl
[i
];
115 /* Return true if the specified thread is alive. */
117 = [] (struct kinfo_lwp
*lwp
)
132 /* Ignore embryonic or demised threads. */
143 /* Return true if PTID is still active in the inferior. */
146 nbsd_nat_target::thread_alive (ptid_t ptid
)
148 pid_t pid
= ptid
.pid ();
149 int lwp
= ptid
.lwp ();
152 = [&lwp
] (const struct kinfo_lwp
*kl
)
154 return kl
->l_lid
== lwp
;
157 return nbsd_thread_lister (pid
, fn
);
160 /* Return the name assigned to a thread by an application. Returns
161 the string in a static buffer. */
164 nbsd_nat_target::thread_name (struct thread_info
*thr
)
166 ptid_t ptid
= thr
->ptid
;
167 pid_t pid
= ptid
.pid ();
168 int lwp
= ptid
.lwp ();
170 static char buf
[KI_LNAMELEN
] = {};
173 = [&lwp
] (const struct kinfo_lwp
*kl
)
175 if (kl
->l_lid
== lwp
)
177 xsnprintf (buf
, sizeof buf
, "%s", kl
->l_name
);
183 if (nbsd_thread_lister (pid
, fn
))
189 /* Implement the "post_attach" target_ops method. */
192 nbsd_add_threads (nbsd_nat_target
*target
, pid_t pid
)
195 = [&target
, &pid
] (const struct kinfo_lwp
*kl
)
197 ptid_t ptid
= ptid_t (pid
, kl
->l_lid
, 0);
198 if (!in_thread_list (target
, ptid
))
200 if (inferior_ptid
.lwp () == 0)
201 thread_change_ptid (target
, inferior_ptid
, ptid
);
203 add_thread (target
, ptid
);
208 nbsd_thread_lister (pid
, fn
);
211 /* Implement the "post_attach" target_ops method. */
214 nbsd_nat_target::post_attach (int pid
)
216 nbsd_add_threads (this, pid
);
219 /* Implement the "update_thread_list" target_ops method. */
222 nbsd_nat_target::update_thread_list ()
226 nbsd_add_threads (this, inferior_ptid
.pid ());
229 /* Convert PTID to a string. */
232 nbsd_nat_target::pid_to_str (ptid_t ptid
)
234 int lwp
= ptid
.lwp ();
238 pid_t pid
= ptid
.pid ();
240 return string_printf ("LWP %d of process %d", lwp
, pid
);
243 return normal_pid_to_str (ptid
);
246 /* Retrieve all the memory regions in the specified process. */
248 static gdb::unique_xmalloc_ptr
<struct kinfo_vmentry
[]>
249 nbsd_kinfo_get_vmmap (pid_t pid
, size_t *size
)
251 int mib
[5] = {CTL_VM
, VM_PROC
, VM_PROC_MAP
, pid
,
252 sizeof (struct kinfo_vmentry
)};
255 if (sysctl (mib
, ARRAY_SIZE (mib
), NULL
, &length
, NULL
, 0))
261 /* Prereserve more space. The length argument is volatile and can change
262 between the sysctl(3) calls as this function can be called against a
264 length
= length
* 5 / 3;
266 gdb::unique_xmalloc_ptr
<struct kinfo_vmentry
[]> kiv
267 (XNEWVAR (kinfo_vmentry
, length
));
269 if (sysctl (mib
, ARRAY_SIZE (mib
), kiv
.get (), &length
, NULL
, 0))
275 *size
= length
/ sizeof (struct kinfo_vmentry
);
279 /* Iterate over all the memory regions in the current inferior,
280 calling FUNC for each memory region. OBFD is passed as the last
284 nbsd_nat_target::find_memory_regions (find_memory_region_ftype func
,
287 pid_t pid
= inferior_ptid
.pid ();
290 gdb::unique_xmalloc_ptr
<struct kinfo_vmentry
[]> vmentl
291 = nbsd_kinfo_get_vmmap (pid
, &nitems
);
293 perror_with_name (_("Couldn't fetch VM map entries."));
295 for (size_t i
= 0; i
< nitems
; i
++)
297 struct kinfo_vmentry
*kve
= &vmentl
[i
];
299 /* Skip unreadable segments and those where MAP_NOCORE has been set. */
300 if (!(kve
->kve_protection
& KVME_PROT_READ
)
301 || kve
->kve_flags
& KVME_FLAG_NOCOREDUMP
)
304 /* Skip segments with an invalid type. */
305 switch (kve
->kve_type
)
307 case KVME_TYPE_VNODE
:
309 case KVME_TYPE_SUBMAP
:
310 case KVME_TYPE_OBJECT
:
316 size_t size
= kve
->kve_end
- kve
->kve_start
;
319 fprintf_filtered (gdb_stdout
,
320 "Save segment, %ld bytes at %s (%c%c%c)\n",
322 paddress (target_gdbarch (), kve
->kve_start
),
323 kve
->kve_protection
& KVME_PROT_READ
? 'r' : '-',
324 kve
->kve_protection
& KVME_PROT_WRITE
? 'w' : '-',
325 kve
->kve_protection
& KVME_PROT_EXEC
? 'x' : '-');
328 /* Invoke the callback function to create the corefile segment.
329 Pass MODIFIED as true, we do not know the real modification state. */
330 func (kve
->kve_start
, size
, kve
->kve_protection
& KVME_PROT_READ
,
331 kve
->kve_protection
& KVME_PROT_WRITE
,
332 kve
->kve_protection
& KVME_PROT_EXEC
, 1, data
);
337 /* Implement the "info_proc" target_ops method. */
340 nbsd_nat_target::info_proc (const char *args
, enum info_proc_what what
)
343 bool do_cmdline
= false;
346 bool do_mappings
= false;
363 error (_("Not supported on this target."));
366 gdb_argv
built_argv (args
);
367 if (built_argv
.count () == 0)
369 pid
= inferior_ptid
.pid ();
371 error (_("No current process: you must name one."));
373 else if (built_argv
.count () == 1 && isdigit (built_argv
[0][0]))
374 pid
= strtol (built_argv
[0], NULL
, 10);
376 error (_("Invalid arguments."));
378 printf_filtered (_("process %d\n"), pid
);
382 gdb::unique_xmalloc_ptr
<char[]> cmdline
= nbsd_pid_to_cmdline (pid
);
383 if (cmdline
!= nullptr)
384 printf_filtered ("cmdline = '%s'\n", cmdline
.get ());
386 warning (_("unable to fetch command line"));
390 std::string cwd
= nbsd_pid_to_cwd (pid
);
392 printf_filtered ("cwd = '%s'\n", cwd
.c_str ());
394 warning (_("unable to fetch current working directory"));
398 const char *exe
= pid_to_exec_file (pid
);
400 printf_filtered ("exe = '%s'\n", exe
);
402 warning (_("unable to fetch executable path name"));
407 gdb::unique_xmalloc_ptr
<struct kinfo_vmentry
[]> vmentl
408 = nbsd_kinfo_get_vmmap (pid
, &nvment
);
410 if (vmentl
!= nullptr)
412 int addr_bit
= TARGET_CHAR_BIT
* sizeof (void *);
413 nbsd_info_proc_mappings_header (addr_bit
);
415 struct kinfo_vmentry
*kve
= vmentl
.get ();
416 for (int i
= 0; i
< nvment
; i
++, kve
++)
417 nbsd_info_proc_mappings_entry (addr_bit
, kve
->kve_start
,
418 kve
->kve_end
, kve
->kve_offset
,
419 kve
->kve_flags
, kve
->kve_protection
,
423 warning (_("unable to fetch virtual memory map"));