netbsd-nat.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /* Native-dependent code for NetBSD.
  2. Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "netbsd-nat.h"
  16. #include "nat/netbsd-nat.h"
  17. #include "gdbthread.h"
  18. #include "netbsd-tdep.h"
  19. #include "inferior.h"
  20. #include "gdbarch.h"
  21. #include <sys/types.h>
  22. #include <sys/ptrace.h>
  23. #include <sys/sysctl.h>
  24. #include <sys/wait.h>
  25. /* Return the name of a file that can be opened to get the symbols for
  26. the child process identified by PID. */
  27. char *
  28. nbsd_nat_target::pid_to_exec_file (int pid)
  29. {
  30. return const_cast<char *> (netbsd_nat::pid_to_exec_file (pid));
  31. }
  32. /* Return the current directory for the process identified by PID. */
  33. static std::string
  34. nbsd_pid_to_cwd (int pid)
  35. {
  36. char buf[PATH_MAX];
  37. size_t buflen;
  38. int mib[4] = {CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_CWD};
  39. buflen = sizeof (buf);
  40. if (sysctl (mib, ARRAY_SIZE (mib), buf, &buflen, NULL, 0))
  41. return "";
  42. return buf;
  43. }
  44. /* Return the kinfo_proc2 structure for the process identified by PID. */
  45. static bool
  46. nbsd_pid_to_kinfo_proc2 (pid_t pid, struct kinfo_proc2 *kp)
  47. {
  48. gdb_assert (kp != nullptr);
  49. size_t size = sizeof (*kp);
  50. int mib[6] = {CTL_KERN, KERN_PROC2, KERN_PROC_PID, pid,
  51. static_cast<int> (size), 1};
  52. return !sysctl (mib, ARRAY_SIZE (mib), kp, &size, NULL, 0);
  53. }
  54. /* Return the command line for the process identified by PID. */
  55. static gdb::unique_xmalloc_ptr<char[]>
  56. nbsd_pid_to_cmdline (int pid)
  57. {
  58. int mib[4] = {CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_ARGV};
  59. size_t size = 0;
  60. if (::sysctl (mib, ARRAY_SIZE (mib), NULL, &size, NULL, 0) == -1 || size == 0)
  61. return nullptr;
  62. gdb::unique_xmalloc_ptr<char[]> args (XNEWVAR (char, size));
  63. if (::sysctl (mib, ARRAY_SIZE (mib), args.get (), &size, NULL, 0) == -1
  64. || size == 0)
  65. return nullptr;
  66. /* Arguments are returned as a flattened string with NUL separators.
  67. Join the arguments with spaces to form a single string. */
  68. for (size_t i = 0; i < size - 1; i++)
  69. if (args[i] == '\0')
  70. args[i] = ' ';
  71. args[size - 1] = '\0';
  72. return args;
  73. }
  74. /* Return true if PTID is still active in the inferior. */
  75. bool
  76. nbsd_nat_target::thread_alive (ptid_t ptid)
  77. {
  78. return netbsd_nat::thread_alive (ptid);
  79. }
  80. /* Return the name assigned to a thread by an application. Returns
  81. the string in a static buffer. */
  82. const char *
  83. nbsd_nat_target::thread_name (struct thread_info *thr)
  84. {
  85. ptid_t ptid = thr->ptid;
  86. return netbsd_nat::thread_name (ptid);
  87. }
  88. /* Implement the "post_attach" target_ops method. */
  89. static void
  90. nbsd_add_threads (nbsd_nat_target *target, pid_t pid)
  91. {
  92. auto fn
  93. = [&target] (ptid_t ptid)
  94. {
  95. if (!in_thread_list (target, ptid))
  96. {
  97. if (inferior_ptid.lwp () == 0)
  98. thread_change_ptid (target, inferior_ptid, ptid);
  99. else
  100. add_thread (target, ptid);
  101. }
  102. };
  103. netbsd_nat::for_each_thread (pid, fn);
  104. }
  105. /* Implement the virtual inf_ptrace_target::post_startup_inferior method. */
  106. void
  107. nbsd_nat_target::post_startup_inferior (ptid_t ptid)
  108. {
  109. netbsd_nat::enable_proc_events (ptid.pid ());
  110. }
  111. /* Implement the "post_attach" target_ops method. */
  112. void
  113. nbsd_nat_target::post_attach (int pid)
  114. {
  115. netbsd_nat::enable_proc_events (pid);
  116. nbsd_add_threads (this, pid);
  117. }
  118. /* Implement the "update_thread_list" target_ops method. */
  119. void
  120. nbsd_nat_target::update_thread_list ()
  121. {
  122. delete_exited_threads ();
  123. }
  124. /* Convert PTID to a string. */
  125. std::string
  126. nbsd_nat_target::pid_to_str (ptid_t ptid)
  127. {
  128. int lwp = ptid.lwp ();
  129. if (lwp != 0)
  130. {
  131. pid_t pid = ptid.pid ();
  132. return string_printf ("LWP %d of process %d", lwp, pid);
  133. }
  134. return normal_pid_to_str (ptid);
  135. }
  136. /* Retrieve all the memory regions in the specified process. */
  137. static gdb::unique_xmalloc_ptr<struct kinfo_vmentry[]>
  138. nbsd_kinfo_get_vmmap (pid_t pid, size_t *size)
  139. {
  140. int mib[5] = {CTL_VM, VM_PROC, VM_PROC_MAP, pid,
  141. sizeof (struct kinfo_vmentry)};
  142. size_t length = 0;
  143. if (sysctl (mib, ARRAY_SIZE (mib), NULL, &length, NULL, 0))
  144. {
  145. *size = 0;
  146. return NULL;
  147. }
  148. /* Prereserve more space. The length argument is volatile and can change
  149. between the sysctl(3) calls as this function can be called against a
  150. running process. */
  151. length = length * 5 / 3;
  152. gdb::unique_xmalloc_ptr<struct kinfo_vmentry[]> kiv
  153. (XNEWVAR (kinfo_vmentry, length));
  154. if (sysctl (mib, ARRAY_SIZE (mib), kiv.get (), &length, NULL, 0))
  155. {
  156. *size = 0;
  157. return NULL;
  158. }
  159. *size = length / sizeof (struct kinfo_vmentry);
  160. return kiv;
  161. }
  162. /* Iterate over all the memory regions in the current inferior,
  163. calling FUNC for each memory region. OBFD is passed as the last
  164. argument to FUNC. */
  165. int
  166. nbsd_nat_target::find_memory_regions (find_memory_region_ftype func,
  167. void *data)
  168. {
  169. pid_t pid = inferior_ptid.pid ();
  170. size_t nitems;
  171. gdb::unique_xmalloc_ptr<struct kinfo_vmentry[]> vmentl
  172. = nbsd_kinfo_get_vmmap (pid, &nitems);
  173. if (vmentl == NULL)
  174. perror_with_name (_("Couldn't fetch VM map entries."));
  175. for (size_t i = 0; i < nitems; i++)
  176. {
  177. struct kinfo_vmentry *kve = &vmentl[i];
  178. /* Skip unreadable segments and those where MAP_NOCORE has been set. */
  179. if (!(kve->kve_protection & KVME_PROT_READ)
  180. || kve->kve_flags & KVME_FLAG_NOCOREDUMP)
  181. continue;
  182. /* Skip segments with an invalid type. */
  183. switch (kve->kve_type)
  184. {
  185. case KVME_TYPE_VNODE:
  186. case KVME_TYPE_ANON:
  187. case KVME_TYPE_SUBMAP:
  188. case KVME_TYPE_OBJECT:
  189. break;
  190. default:
  191. continue;
  192. }
  193. size_t size = kve->kve_end - kve->kve_start;
  194. if (info_verbose)
  195. {
  196. gdb_printf ("Save segment, %ld bytes at %s (%c%c%c)\n",
  197. (long) size,
  198. paddress (target_gdbarch (), kve->kve_start),
  199. kve->kve_protection & KVME_PROT_READ ? 'r' : '-',
  200. kve->kve_protection & KVME_PROT_WRITE ? 'w' : '-',
  201. kve->kve_protection & KVME_PROT_EXEC ? 'x' : '-');
  202. }
  203. /* Invoke the callback function to create the corefile segment.
  204. Pass MODIFIED as true, we do not know the real modification state. */
  205. func (kve->kve_start, size, kve->kve_protection & KVME_PROT_READ,
  206. kve->kve_protection & KVME_PROT_WRITE,
  207. kve->kve_protection & KVME_PROT_EXEC, 1, data);
  208. }
  209. return 0;
  210. }
  211. /* Implement the "info_proc" target_ops method. */
  212. bool
  213. nbsd_nat_target::info_proc (const char *args, enum info_proc_what what)
  214. {
  215. pid_t pid;
  216. bool do_cmdline = false;
  217. bool do_cwd = false;
  218. bool do_exe = false;
  219. bool do_mappings = false;
  220. bool do_status = false;
  221. switch (what)
  222. {
  223. case IP_MINIMAL:
  224. do_cmdline = true;
  225. do_cwd = true;
  226. do_exe = true;
  227. break;
  228. case IP_STAT:
  229. case IP_STATUS:
  230. do_status = true;
  231. break;
  232. case IP_MAPPINGS:
  233. do_mappings = true;
  234. break;
  235. case IP_CMDLINE:
  236. do_cmdline = true;
  237. break;
  238. case IP_EXE:
  239. do_exe = true;
  240. break;
  241. case IP_CWD:
  242. do_cwd = true;
  243. break;
  244. case IP_ALL:
  245. do_cmdline = true;
  246. do_cwd = true;
  247. do_exe = true;
  248. do_mappings = true;
  249. do_status = true;
  250. break;
  251. default:
  252. error (_("Not supported on this target."));
  253. }
  254. gdb_argv built_argv (args);
  255. if (built_argv.count () == 0)
  256. {
  257. pid = inferior_ptid.pid ();
  258. if (pid == 0)
  259. error (_("No current process: you must name one."));
  260. }
  261. else if (built_argv.count () == 1 && isdigit (built_argv[0][0]))
  262. pid = strtol (built_argv[0], NULL, 10);
  263. else
  264. error (_("Invalid arguments."));
  265. gdb_printf (_("process %d\n"), pid);
  266. if (do_cmdline)
  267. {
  268. gdb::unique_xmalloc_ptr<char[]> cmdline = nbsd_pid_to_cmdline (pid);
  269. if (cmdline != nullptr)
  270. gdb_printf ("cmdline = '%s'\n", cmdline.get ());
  271. else
  272. warning (_("unable to fetch command line"));
  273. }
  274. if (do_cwd)
  275. {
  276. std::string cwd = nbsd_pid_to_cwd (pid);
  277. if (cwd != "")
  278. gdb_printf ("cwd = '%s'\n", cwd.c_str ());
  279. else
  280. warning (_("unable to fetch current working directory"));
  281. }
  282. if (do_exe)
  283. {
  284. const char *exe = pid_to_exec_file (pid);
  285. if (exe != nullptr)
  286. gdb_printf ("exe = '%s'\n", exe);
  287. else
  288. warning (_("unable to fetch executable path name"));
  289. }
  290. if (do_mappings)
  291. {
  292. size_t nvment;
  293. gdb::unique_xmalloc_ptr<struct kinfo_vmentry[]> vmentl
  294. = nbsd_kinfo_get_vmmap (pid, &nvment);
  295. if (vmentl != nullptr)
  296. {
  297. int addr_bit = TARGET_CHAR_BIT * sizeof (void *);
  298. nbsd_info_proc_mappings_header (addr_bit);
  299. struct kinfo_vmentry *kve = vmentl.get ();
  300. for (int i = 0; i < nvment; i++, kve++)
  301. nbsd_info_proc_mappings_entry (addr_bit, kve->kve_start,
  302. kve->kve_end, kve->kve_offset,
  303. kve->kve_flags, kve->kve_protection,
  304. kve->kve_path);
  305. }
  306. else
  307. warning (_("unable to fetch virtual memory map"));
  308. }
  309. if (do_status)
  310. {
  311. struct kinfo_proc2 kp;
  312. if (!nbsd_pid_to_kinfo_proc2 (pid, &kp))
  313. warning (_("Failed to fetch process information"));
  314. else
  315. {
  316. auto process_status
  317. = [] (int8_t stat)
  318. {
  319. switch (stat)
  320. {
  321. case SIDL:
  322. return "IDL";
  323. case SACTIVE:
  324. return "ACTIVE";
  325. case SDYING:
  326. return "DYING";
  327. case SSTOP:
  328. return "STOP";
  329. case SZOMB:
  330. return "ZOMB";
  331. case SDEAD:
  332. return "DEAD";
  333. default:
  334. return "? (unknown)";
  335. }
  336. };
  337. gdb_printf ("Name: %s\n", kp.p_comm);
  338. gdb_printf ("State: %s\n", process_status(kp.p_realstat));
  339. gdb_printf ("Parent process: %" PRId32 "\n", kp.p_ppid);
  340. gdb_printf ("Process group: %" PRId32 "\n", kp.p__pgid);
  341. gdb_printf ("Session id: %" PRId32 "\n", kp.p_sid);
  342. gdb_printf ("TTY: %" PRId32 "\n", kp.p_tdev);
  343. gdb_printf ("TTY owner process group: %" PRId32 "\n", kp.p_tpgid);
  344. gdb_printf ("User IDs (real, effective, saved): "
  345. "%" PRIu32 " %" PRIu32 " %" PRIu32 "\n",
  346. kp.p_ruid, kp.p_uid, kp.p_svuid);
  347. gdb_printf ("Group IDs (real, effective, saved): "
  348. "%" PRIu32 " %" PRIu32 " %" PRIu32 "\n",
  349. kp.p_rgid, kp.p_gid, kp.p_svgid);
  350. gdb_printf ("Groups:");
  351. for (int i = 0; i < kp.p_ngroups; i++)
  352. gdb_printf (" %" PRIu32, kp.p_groups[i]);
  353. gdb_printf ("\n");
  354. gdb_printf ("Minor faults (no memory page): %" PRIu64 "\n",
  355. kp.p_uru_minflt);
  356. gdb_printf ("Major faults (memory page faults): %" PRIu64 "\n",
  357. kp.p_uru_majflt);
  358. gdb_printf ("utime: %" PRIu32 ".%06" PRIu32 "\n",
  359. kp.p_uutime_sec, kp.p_uutime_usec);
  360. gdb_printf ("stime: %" PRIu32 ".%06" PRIu32 "\n",
  361. kp.p_ustime_sec, kp.p_ustime_usec);
  362. gdb_printf ("utime+stime, children: %" PRIu32 ".%06" PRIu32 "\n",
  363. kp.p_uctime_sec, kp.p_uctime_usec);
  364. gdb_printf ("'nice' value: %" PRIu8 "\n", kp.p_nice);
  365. gdb_printf ("Start time: %" PRIu32 ".%06" PRIu32 "\n",
  366. kp.p_ustart_sec, kp.p_ustart_usec);
  367. int pgtok = getpagesize () / 1024;
  368. gdb_printf ("Data size: %" PRIuMAX " kB\n",
  369. (uintmax_t) kp.p_vm_dsize * pgtok);
  370. gdb_printf ("Stack size: %" PRIuMAX " kB\n",
  371. (uintmax_t) kp.p_vm_ssize * pgtok);
  372. gdb_printf ("Text size: %" PRIuMAX " kB\n",
  373. (uintmax_t) kp.p_vm_tsize * pgtok);
  374. gdb_printf ("Resident set size: %" PRIuMAX " kB\n",
  375. (uintmax_t) kp.p_vm_rssize * pgtok);
  376. gdb_printf ("Maximum RSS: %" PRIu64 " kB\n", kp.p_uru_maxrss);
  377. gdb_printf ("Pending Signals:");
  378. for (size_t i = 0; i < ARRAY_SIZE (kp.p_siglist.__bits); i++)
  379. gdb_printf (" %08" PRIx32, kp.p_siglist.__bits[i]);
  380. gdb_printf ("\n");
  381. gdb_printf ("Ignored Signals:");
  382. for (size_t i = 0; i < ARRAY_SIZE (kp.p_sigignore.__bits); i++)
  383. gdb_printf (" %08" PRIx32, kp.p_sigignore.__bits[i]);
  384. gdb_printf ("\n");
  385. gdb_printf ("Caught Signals:");
  386. for (size_t i = 0; i < ARRAY_SIZE (kp.p_sigcatch.__bits); i++)
  387. gdb_printf (" %08" PRIx32, kp.p_sigcatch.__bits[i]);
  388. gdb_printf ("\n");
  389. }
  390. }
  391. return true;
  392. }
  393. /* Resume execution of a specified PTID, that points to a process or a thread
  394. within a process. If one thread is specified, all other threads are
  395. suspended. If STEP is nonzero, single-step it. If SIGNAL is nonzero,
  396. give it that signal. */
  397. static void
  398. nbsd_resume(nbsd_nat_target *target, ptid_t ptid, int step,
  399. enum gdb_signal signal)
  400. {
  401. int request;
  402. gdb_assert (minus_one_ptid != ptid);
  403. if (ptid.lwp_p ())
  404. {
  405. /* If ptid is a specific LWP, suspend all other LWPs in the process. */
  406. inferior *inf = find_inferior_ptid (target, ptid);
  407. for (thread_info *tp : inf->non_exited_threads ())
  408. {
  409. if (tp->ptid.lwp () == ptid.lwp ())
  410. request = PT_RESUME;
  411. else
  412. request = PT_SUSPEND;
  413. if (ptrace (request, tp->ptid.pid (), NULL, tp->ptid.lwp ()) == -1)
  414. perror_with_name (("ptrace"));
  415. }
  416. }
  417. else
  418. {
  419. /* If ptid is a wildcard, resume all matching threads (they won't run
  420. until the process is continued however). */
  421. for (thread_info *tp : all_non_exited_threads (target, ptid))
  422. if (ptrace (PT_RESUME, tp->ptid.pid (), NULL, tp->ptid.lwp ()) == -1)
  423. perror_with_name (("ptrace"));
  424. }
  425. if (step)
  426. {
  427. for (thread_info *tp : all_non_exited_threads (target, ptid))
  428. if (ptrace (PT_SETSTEP, tp->ptid.pid (), NULL, tp->ptid.lwp ()) == -1)
  429. perror_with_name (("ptrace"));
  430. }
  431. else
  432. {
  433. for (thread_info *tp : all_non_exited_threads (target, ptid))
  434. if (ptrace (PT_CLEARSTEP, tp->ptid.pid (), NULL, tp->ptid.lwp ()) == -1)
  435. perror_with_name (("ptrace"));
  436. }
  437. if (catch_syscall_enabled () > 0)
  438. request = PT_SYSCALL;
  439. else
  440. request = PT_CONTINUE;
  441. /* An address of (void *)1 tells ptrace to continue from
  442. where it was. If GDB wanted it to start some other way, we have
  443. already written a new program counter value to the child. */
  444. if (ptrace (request, ptid.pid (), (void *)1, gdb_signal_to_host (signal)) == -1)
  445. perror_with_name (("ptrace"));
  446. }
  447. /* Resume execution of thread PTID, or all threads of all inferiors
  448. if PTID is -1. If STEP is nonzero, single-step it. If SIGNAL is nonzero,
  449. give it that signal. */
  450. void
  451. nbsd_nat_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
  452. {
  453. if (minus_one_ptid != ptid)
  454. nbsd_resume (this, ptid, step, signal);
  455. else
  456. {
  457. for (inferior *inf : all_non_exited_inferiors (this))
  458. nbsd_resume (this, ptid_t (inf->pid, 0, 0), step, signal);
  459. }
  460. }
  461. /* Implement a safe wrapper around waitpid(). */
  462. static pid_t
  463. nbsd_wait (ptid_t ptid, struct target_waitstatus *ourstatus,
  464. target_wait_flags options)
  465. {
  466. pid_t pid;
  467. int status;
  468. set_sigint_trap ();
  469. do
  470. {
  471. /* The common code passes WNOHANG that leads to crashes, overwrite it. */
  472. pid = waitpid (ptid.pid (), &status, 0);
  473. }
  474. while (pid == -1 && errno == EINTR);
  475. clear_sigint_trap ();
  476. if (pid == -1)
  477. perror_with_name (_("Child process unexpectedly missing"));
  478. *ourstatus = host_status_to_waitstatus (status);
  479. return pid;
  480. }
  481. /* Wait for the child specified by PTID to do something. Return the
  482. process ID of the child, or MINUS_ONE_PTID in case of error; store
  483. the status in *OURSTATUS. */
  484. ptid_t
  485. nbsd_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
  486. target_wait_flags target_options)
  487. {
  488. pid_t pid = nbsd_wait (ptid, ourstatus, target_options);
  489. ptid_t wptid = ptid_t (pid);
  490. /* If the child stopped, keep investigating its status. */
  491. if (ourstatus->kind () != TARGET_WAITKIND_STOPPED)
  492. return wptid;
  493. /* Extract the event and thread that received a signal. */
  494. ptrace_siginfo_t psi;
  495. if (ptrace (PT_GET_SIGINFO, pid, &psi, sizeof (psi)) == -1)
  496. perror_with_name (("ptrace"));
  497. /* Pick child's siginfo_t. */
  498. siginfo_t *si = &psi.psi_siginfo;
  499. int lwp = psi.psi_lwpid;
  500. int signo = si->si_signo;
  501. const int code = si->si_code;
  502. /* Construct PTID with a specified thread that received the event.
  503. If a signal was targeted to the whole process, lwp is 0. */
  504. wptid = ptid_t (pid, lwp, 0);
  505. /* Bail out on non-debugger oriented signals.. */
  506. if (signo != SIGTRAP)
  507. return wptid;
  508. /* Stop examining non-debugger oriented SIGTRAP codes. */
  509. if (code <= SI_USER || code == SI_NOINFO)
  510. return wptid;
  511. /* Process state for threading events */
  512. ptrace_state_t pst = {};
  513. if (code == TRAP_LWP)
  514. {
  515. if (ptrace (PT_GET_PROCESS_STATE, pid, &pst, sizeof (pst)) == -1)
  516. perror_with_name (("ptrace"));
  517. }
  518. if (code == TRAP_LWP && pst.pe_report_event == PTRACE_LWP_EXIT)
  519. {
  520. /* If GDB attaches to a multi-threaded process, exiting
  521. threads might be skipped during post_attach that
  522. have not yet reported their PTRACE_LWP_EXIT event.
  523. Ignore exited events for an unknown LWP. */
  524. thread_info *thr = find_thread_ptid (this, wptid);
  525. if (thr == nullptr)
  526. ourstatus->set_spurious ();
  527. else
  528. {
  529. /* NetBSD does not store an LWP exit status. */
  530. ourstatus->set_thread_exited (0);
  531. if (print_thread_events)
  532. gdb_printf (_("[%s exited]\n"),
  533. target_pid_to_str (wptid).c_str ());
  534. delete_thread (thr);
  535. }
  536. /* The GDB core expects that the rest of the threads are running. */
  537. if (ptrace (PT_CONTINUE, pid, (void *) 1, 0) == -1)
  538. perror_with_name (("ptrace"));
  539. return wptid;
  540. }
  541. if (in_thread_list (this, ptid_t (pid)))
  542. thread_change_ptid (this, ptid_t (pid), wptid);
  543. if (code == TRAP_LWP && pst.pe_report_event == PTRACE_LWP_CREATE)
  544. {
  545. /* If GDB attaches to a multi-threaded process, newborn
  546. threads might be added by nbsd_add_threads that have
  547. not yet reported their PTRACE_LWP_CREATE event. Ignore
  548. born events for an already-known LWP. */
  549. if (in_thread_list (this, wptid))
  550. ourstatus->set_spurious ();
  551. else
  552. {
  553. add_thread (this, wptid);
  554. ourstatus->set_thread_created ();
  555. }
  556. return wptid;
  557. }
  558. if (code == TRAP_EXEC)
  559. {
  560. ourstatus->set_execd (make_unique_xstrdup (pid_to_exec_file (pid)));
  561. return wptid;
  562. }
  563. if (code == TRAP_TRACE)
  564. {
  565. /* Unhandled at this level. */
  566. return wptid;
  567. }
  568. if (code == TRAP_SCE || code == TRAP_SCX)
  569. {
  570. int sysnum = si->si_sysnum;
  571. if (!catch_syscall_enabled () || !catching_syscall_number (sysnum))
  572. {
  573. /* If the core isn't interested in this event, ignore it. */
  574. ourstatus->set_spurious ();
  575. return wptid;
  576. }
  577. if (code == TRAP_SCE)
  578. ourstatus->set_syscall_entry (sysnum);
  579. else
  580. ourstatus->set_syscall_return (sysnum);
  581. return wptid;
  582. }
  583. if (code == TRAP_BRKPT)
  584. {
  585. /* Unhandled at this level. */
  586. return wptid;
  587. }
  588. /* Unclassified SIGTRAP event. */
  589. ourstatus->set_spurious ();
  590. return wptid;
  591. }
  592. /* Implement the "insert_exec_catchpoint" target_ops method. */
  593. int
  594. nbsd_nat_target::insert_exec_catchpoint (int pid)
  595. {
  596. /* Nothing to do. */
  597. return 0;
  598. }
  599. /* Implement the "remove_exec_catchpoint" target_ops method. */
  600. int
  601. nbsd_nat_target::remove_exec_catchpoint (int pid)
  602. {
  603. /* Nothing to do. */
  604. return 0;
  605. }
  606. /* Implement the "set_syscall_catchpoint" target_ops method. */
  607. int
  608. nbsd_nat_target::set_syscall_catchpoint (int pid, bool needed,
  609. int any_count,
  610. gdb::array_view<const int> syscall_counts)
  611. {
  612. /* Ignore the arguments. inf-ptrace.c will use PT_SYSCALL which
  613. will catch all system call entries and exits. The system calls
  614. are filtered by GDB rather than the kernel. */
  615. return 0;
  616. }
  617. /* Implement the "supports_multi_process" target_ops method. */
  618. bool
  619. nbsd_nat_target::supports_multi_process ()
  620. {
  621. return true;
  622. }
  623. /* Implement the "xfer_partial" target_ops method. */
  624. enum target_xfer_status
  625. nbsd_nat_target::xfer_partial (enum target_object object,
  626. const char *annex, gdb_byte *readbuf,
  627. const gdb_byte *writebuf,
  628. ULONGEST offset, ULONGEST len,
  629. ULONGEST *xfered_len)
  630. {
  631. pid_t pid = inferior_ptid.pid ();
  632. switch (object)
  633. {
  634. case TARGET_OBJECT_SIGNAL_INFO:
  635. {
  636. len = netbsd_nat::qxfer_siginfo(pid, annex, readbuf, writebuf, offset,
  637. len);
  638. if (len == -1)
  639. return TARGET_XFER_E_IO;
  640. *xfered_len = len;
  641. return TARGET_XFER_OK;
  642. }
  643. case TARGET_OBJECT_MEMORY:
  644. {
  645. size_t xfered;
  646. int res;
  647. if (writebuf != nullptr)
  648. res = netbsd_nat::write_memory (pid, writebuf, offset, len, &xfered);
  649. else
  650. res = netbsd_nat::read_memory (pid, readbuf, offset, len, &xfered);
  651. if (res != 0)
  652. {
  653. if (res == EACCES)
  654. gdb_printf (gdb_stderr, "Cannot %s process at %s (%s). "
  655. "Is PaX MPROTECT active? See security(7), "
  656. "sysctl(7), paxctl(8)\n",
  657. (writebuf ? "write to" : "read from"),
  658. pulongest (offset), safe_strerror (errno));
  659. return TARGET_XFER_E_IO;
  660. }
  661. if (xfered == 0)
  662. return TARGET_XFER_EOF;
  663. *xfered_len = (ULONGEST) xfered;
  664. return TARGET_XFER_OK;
  665. }
  666. default:
  667. return inf_ptrace_target::xfer_partial (object, annex,
  668. readbuf, writebuf, offset,
  669. len, xfered_len);
  670. }
  671. }
  672. /* Implement the "supports_dumpcore" target_ops method. */
  673. bool
  674. nbsd_nat_target::supports_dumpcore ()
  675. {
  676. return true;
  677. }
  678. /* Implement the "dumpcore" target_ops method. */
  679. void
  680. nbsd_nat_target::dumpcore (const char *filename)
  681. {
  682. pid_t pid = inferior_ptid.pid ();
  683. if (ptrace (PT_DUMPCORE, pid, const_cast<char *>(filename),
  684. strlen (filename)) == -1)
  685. perror_with_name (("ptrace"));
  686. }