1 /* Internal interfaces for the NetBSD code.
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/>. */
20 #include "gdbsupport/common-defs.h"
21 #include "nat/netbsd-nat.h"
22 #include "gdbsupport/common-debug.h"
24 #include <sys/types.h>
25 #include <sys/sysctl.h>
29 #include "gdbsupport/function-view.h"
34 /* See netbsd-nat.h. */
37 pid_to_exec_file (pid_t pid
)
39 static char buf
[PATH_MAX
];
40 int mib
[4] = {CTL_KERN
, KERN_PROC_ARGS
, pid
, KERN_PROC_PATHNAME
};
41 size_t buflen
= sizeof (buf
);
42 if (::sysctl (mib
, ARRAY_SIZE (mib
), buf
, &buflen
, NULL
, 0) != 0)
47 /* Generic thread (LWP) lister within a specified PID. The CALLBACK
48 parameters is a C++ function that is called for each detected thread.
49 When the CALLBACK function returns true, the iteration is interrupted.
51 This function assumes internally that the queried process is stopped
52 and the number of threads does not change between two sysctl () calls. */
55 netbsd_thread_lister (const pid_t pid
,
56 gdb::function_view
<bool (const struct kinfo_lwp
*)>
59 int mib
[5] = {CTL_KERN
, KERN_LWP
, pid
, sizeof (struct kinfo_lwp
), 0};
62 if (sysctl (mib
, ARRAY_SIZE (mib
), NULL
, &size
, NULL
, 0) == -1 || size
== 0)
63 perror_with_name (("sysctl"));
65 mib
[4] = size
/ sizeof (size_t);
67 gdb::unique_xmalloc_ptr
<struct kinfo_lwp
[]> kl
68 ((struct kinfo_lwp
*) xcalloc (size
, 1));
70 if (sysctl (mib
, ARRAY_SIZE (mib
), kl
.get (), &size
, NULL
, 0) == -1
72 perror_with_name (("sysctl"));
74 for (size_t i
= 0; i
< size
/ sizeof (struct kinfo_lwp
); i
++)
76 struct kinfo_lwp
*l
= &kl
[i
];
78 /* Return true if the specified thread is alive. */
80 = [] (struct kinfo_lwp
*lwp
)
95 /* Ignore embryonic or demised threads. */
106 /* See netbsd-nat.h. */
109 thread_alive (ptid_t ptid
)
111 pid_t pid
= ptid
.pid ();
112 lwpid_t lwp
= ptid
.lwp ();
115 = [=] (const struct kinfo_lwp
*kl
)
117 return kl
->l_lid
== lwp
;
120 return netbsd_thread_lister (pid
, fn
);
123 /* See netbsd-nat.h. */
126 thread_name (ptid_t ptid
)
128 pid_t pid
= ptid
.pid ();
129 lwpid_t lwp
= ptid
.lwp ();
131 static char buf
[KI_LNAMELEN
] = {};
134 = [=] (const struct kinfo_lwp
*kl
)
136 if (kl
->l_lid
== lwp
)
138 xsnprintf (buf
, sizeof buf
, "%s", kl
->l_name
);
144 if (netbsd_thread_lister (pid
, fn
))
150 /* See netbsd-nat.h. */
153 for_each_thread (pid_t pid
, gdb::function_view
<void (ptid_t
)> callback
)
156 = [=, &callback
] (const struct kinfo_lwp
*kl
)
158 ptid_t ptid
= ptid_t (pid
, kl
->l_lid
, 0);
163 netbsd_thread_lister (pid
, fn
);