netbsd-low.cc 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. /* Copyright (C) 2020-2022 Free Software Foundation, Inc.
  2. This file is part of GDB.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include "server.h"
  14. #include "target.h"
  15. #include "netbsd-low.h"
  16. #include "nat/netbsd-nat.h"
  17. #include <sys/param.h>
  18. #include <sys/types.h>
  19. #include <sys/ptrace.h>
  20. #include <sys/sysctl.h>
  21. #include <limits.h>
  22. #include <unistd.h>
  23. #include <signal.h>
  24. #include <elf.h>
  25. #include <type_traits>
  26. #include "gdbsupport/eintr.h"
  27. #include "gdbsupport/gdb_wait.h"
  28. #include "gdbsupport/filestuff.h"
  29. #include "gdbsupport/common-inferior.h"
  30. #include "nat/fork-inferior.h"
  31. #include "hostio.h"
  32. int using_threads = 1;
  33. /* Callback used by fork_inferior to start tracing the inferior. */
  34. static void
  35. netbsd_ptrace_fun ()
  36. {
  37. /* Switch child to its own process group so that signals won't
  38. directly affect GDBserver. */
  39. if (setpgid (0, 0) < 0)
  40. trace_start_error_with_name (("setpgid"));
  41. if (ptrace (PT_TRACE_ME, 0, nullptr, 0) < 0)
  42. trace_start_error_with_name (("ptrace"));
  43. /* If GDBserver is connected to gdb via stdio, redirect the inferior's
  44. stdout to stderr so that inferior i/o doesn't corrupt the connection.
  45. Also, redirect stdin to /dev/null. */
  46. if (remote_connection_is_stdio ())
  47. {
  48. if (close (0) < 0)
  49. trace_start_error_with_name (("close"));
  50. if (open ("/dev/null", O_RDONLY) < 0)
  51. trace_start_error_with_name (("open"));
  52. if (dup2 (2, 1) < 0)
  53. trace_start_error_with_name (("dup2"));
  54. if (write (2, "stdin/stdout redirected\n",
  55. sizeof ("stdin/stdout redirected\n") - 1) < 0)
  56. {
  57. /* Errors ignored. */
  58. }
  59. }
  60. }
  61. /* Implement the create_inferior method of the target_ops vector. */
  62. int
  63. netbsd_process_target::create_inferior (const char *program,
  64. const std::vector<char *> &program_args)
  65. {
  66. std::string str_program_args = construct_inferior_arguments (program_args);
  67. pid_t pid = fork_inferior (program, str_program_args.c_str (),
  68. get_environ ()->envp (), netbsd_ptrace_fun,
  69. nullptr, nullptr, nullptr, nullptr);
  70. add_process (pid, 0);
  71. post_fork_inferior (pid, program);
  72. return pid;
  73. }
  74. /* Implement the post_create_inferior target_ops method. */
  75. void
  76. netbsd_process_target::post_create_inferior ()
  77. {
  78. pid_t pid = current_process ()->pid;
  79. netbsd_nat::enable_proc_events (pid);
  80. low_arch_setup ();
  81. }
  82. /* Implement the attach target_ops method. */
  83. int
  84. netbsd_process_target::attach (unsigned long pid)
  85. {
  86. /* Unimplemented. */
  87. return -1;
  88. }
  89. /* Returns true if GDB is interested in any child syscalls. */
  90. static bool
  91. gdb_catching_syscalls_p (pid_t pid)
  92. {
  93. struct process_info *proc = find_process_pid (pid);
  94. return !proc->syscalls_to_catch.empty ();
  95. }
  96. /* Implement the resume target_ops method. */
  97. void
  98. netbsd_process_target::resume (struct thread_resume *resume_info, size_t n)
  99. {
  100. ptid_t resume_ptid = resume_info[0].thread;
  101. const int signal = resume_info[0].sig;
  102. const bool step = resume_info[0].kind == resume_step;
  103. if (resume_ptid == minus_one_ptid)
  104. resume_ptid = ptid_of (current_thread);
  105. const pid_t pid = resume_ptid.pid ();
  106. const lwpid_t lwp = resume_ptid.lwp ();
  107. regcache_invalidate_pid (pid);
  108. auto fn
  109. = [&] (ptid_t ptid)
  110. {
  111. if (step)
  112. {
  113. if (ptid.lwp () == lwp || n != 1)
  114. {
  115. if (ptrace (PT_SETSTEP, pid, NULL, ptid.lwp ()) == -1)
  116. perror_with_name (("ptrace"));
  117. if (ptrace (PT_RESUME, pid, NULL, ptid.lwp ()) == -1)
  118. perror_with_name (("ptrace"));
  119. }
  120. else
  121. {
  122. if (ptrace (PT_CLEARSTEP, pid, NULL, ptid.lwp ()) == -1)
  123. perror_with_name (("ptrace"));
  124. if (ptrace (PT_SUSPEND, pid, NULL, ptid.lwp ()) == -1)
  125. perror_with_name (("ptrace"));
  126. }
  127. }
  128. else
  129. {
  130. if (ptrace (PT_CLEARSTEP, pid, NULL, ptid.lwp ()) == -1)
  131. perror_with_name (("ptrace"));
  132. if (ptrace (PT_RESUME, pid, NULL, ptid.lwp ()) == -1)
  133. perror_with_name (("ptrace"));
  134. }
  135. };
  136. netbsd_nat::for_each_thread (pid, fn);
  137. int request = gdb_catching_syscalls_p (pid) ? PT_CONTINUE : PT_SYSCALL;
  138. errno = 0;
  139. ptrace (request, pid, (void *)1, signal);
  140. if (errno)
  141. perror_with_name (("ptrace"));
  142. }
  143. /* Returns true if GDB is interested in the reported SYSNO syscall. */
  144. static bool
  145. netbsd_catch_this_syscall (int sysno)
  146. {
  147. struct process_info *proc = current_process ();
  148. if (proc->syscalls_to_catch.empty ())
  149. return false;
  150. if (proc->syscalls_to_catch[0] == ANY_SYSCALL)
  151. return true;
  152. for (int iter : proc->syscalls_to_catch)
  153. if (iter == sysno)
  154. return true;
  155. return false;
  156. }
  157. /* Helper function for child_wait and the derivatives of child_wait.
  158. HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
  159. translation of that in OURSTATUS. */
  160. static void
  161. netbsd_store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
  162. {
  163. if (WIFEXITED (hoststatus))
  164. ourstatus->set_exited (WEXITSTATUS (hoststatus));
  165. else if (!WIFSTOPPED (hoststatus))
  166. ourstatus->set_signalled (gdb_signal_from_host (WTERMSIG (hoststatus)));
  167. else
  168. ourstatus->set_stopped (gdb_signal_from_host (WSTOPSIG (hoststatus)));
  169. }
  170. /* Implement a safe wrapper around waitpid(). */
  171. static pid_t
  172. netbsd_waitpid (ptid_t ptid, struct target_waitstatus *ourstatus,
  173. target_wait_flags target_options)
  174. {
  175. int status;
  176. int options = (target_options & TARGET_WNOHANG) ? WNOHANG : 0;
  177. pid_t pid
  178. = gdb::handle_eintr (-1, ::waitpid, ptid.pid (), &status, options);
  179. if (pid == -1)
  180. perror_with_name (_("Child process unexpectedly missing"));
  181. netbsd_store_waitstatus (ourstatus, status);
  182. return pid;
  183. }
  184. /* Implement the wait target_ops method.
  185. Wait for the child specified by PTID to do something. Return the
  186. process ID of the child, or MINUS_ONE_PTID in case of error; store
  187. the status in *OURSTATUS. */
  188. static ptid_t
  189. netbsd_wait (ptid_t ptid, struct target_waitstatus *ourstatus,
  190. target_wait_flags target_options)
  191. {
  192. pid_t pid = netbsd_waitpid (ptid, ourstatus, target_options);
  193. ptid_t wptid = ptid_t (pid);
  194. if (pid == 0)
  195. {
  196. gdb_assert (target_options & TARGET_WNOHANG);
  197. ourstatus->set_ignore ();
  198. return null_ptid;
  199. }
  200. gdb_assert (pid != -1);
  201. /* If the child stopped, keep investigating its status. */
  202. if (ourstatus->kind () != TARGET_WAITKIND_STOPPED)
  203. return wptid;
  204. /* Extract the event and thread that received a signal. */
  205. ptrace_siginfo_t psi;
  206. if (ptrace (PT_GET_SIGINFO, pid, &psi, sizeof (psi)) == -1)
  207. perror_with_name (("ptrace"));
  208. /* Pick child's siginfo_t. */
  209. siginfo_t *si = &psi.psi_siginfo;
  210. lwpid_t lwp = psi.psi_lwpid;
  211. int signo = si->si_signo;
  212. const int code = si->si_code;
  213. /* Construct PTID with a specified thread that received the event.
  214. If a signal was targeted to the whole process, lwp is 0. */
  215. wptid = ptid_t (pid, lwp, 0);
  216. /* Bail out on non-debugger oriented signals. */
  217. if (signo != SIGTRAP)
  218. return wptid;
  219. /* Stop examining non-debugger oriented SIGTRAP codes. */
  220. if (code <= SI_USER || code == SI_NOINFO)
  221. return wptid;
  222. /* Process state for threading events. */
  223. ptrace_state_t pst = {};
  224. if (code == TRAP_LWP)
  225. if (ptrace (PT_GET_PROCESS_STATE, pid, &pst, sizeof (pst)) == -1)
  226. perror_with_name (("ptrace"));
  227. if (code == TRAP_LWP && pst.pe_report_event == PTRACE_LWP_EXIT)
  228. {
  229. /* If GDB attaches to a multi-threaded process, exiting
  230. threads might be skipped during post_attach that
  231. have not yet reported their PTRACE_LWP_EXIT event.
  232. Ignore exited events for an unknown LWP. */
  233. thread_info *thr = find_thread_ptid (wptid);
  234. if (thr == nullptr)
  235. ourstatus->set_spurious ();
  236. else
  237. {
  238. /* NetBSD does not store an LWP exit status. */
  239. ourstatus->set_thread_exited (0);
  240. remove_thread (thr);
  241. }
  242. return wptid;
  243. }
  244. if (find_thread_ptid (ptid_t (pid)))
  245. switch_to_thread (find_thread_ptid (wptid));
  246. if (code == TRAP_LWP && pst.pe_report_event == PTRACE_LWP_CREATE)
  247. {
  248. /* If GDB attaches to a multi-threaded process, newborn
  249. threads might be added by nbsd_add_threads that have
  250. not yet reported their PTRACE_LWP_CREATE event. Ignore
  251. born events for an already-known LWP. */
  252. if (find_thread_ptid (wptid))
  253. ourstatus->set_spurious ();
  254. else
  255. {
  256. add_thread (wptid, NULL);
  257. ourstatus->set_thread_created ();
  258. }
  259. return wptid;
  260. }
  261. if (code == TRAP_EXEC)
  262. {
  263. ourstatus->set_execd
  264. (make_unique_xstrdup (netbsd_nat::pid_to_exec_file (pid)));
  265. return wptid;
  266. }
  267. if (code == TRAP_TRACE)
  268. return wptid;
  269. if (code == TRAP_SCE || code == TRAP_SCX)
  270. {
  271. int sysnum = si->si_sysnum;
  272. if (!netbsd_catch_this_syscall(sysnum))
  273. {
  274. /* If the core isn't interested in this event, ignore it. */
  275. ourstatus->set_spurious ();
  276. return wptid;
  277. }
  278. if (code == TRAP_SCE)
  279. ourstatus->set_syscall_entry (sysnum);
  280. else
  281. ourstatus->set_syscall_return (sysnum);
  282. return wptid;
  283. }
  284. if (code == TRAP_BRKPT)
  285. {
  286. #ifdef PTRACE_BREAKPOINT_ADJ
  287. CORE_ADDR pc;
  288. struct reg r;
  289. ptrace (PT_GETREGS, pid, &r, psi.psi_lwpid);
  290. pc = PTRACE_REG_PC (&r);
  291. PTRACE_REG_SET_PC (&r, pc - PTRACE_BREAKPOINT_ADJ);
  292. ptrace (PT_SETREGS, pid, &r, psi.psi_lwpid);
  293. #endif
  294. return wptid;
  295. }
  296. /* Unclassified SIGTRAP event. */
  297. ourstatus->set_spurious ();
  298. return wptid;
  299. }
  300. /* Implement the wait target_ops method. */
  301. ptid_t
  302. netbsd_process_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
  303. target_wait_flags target_options)
  304. {
  305. while (true)
  306. {
  307. ptid_t wptid = netbsd_wait (ptid, ourstatus, target_options);
  308. /* Register thread in the gdbcore if a thread was not reported earlier.
  309. This is required after ::create_inferior, when the gdbcore does not
  310. know about the first internal thread.
  311. This may also happen on attach, when an event is registered on a thread
  312. that was not fully initialized during the attach stage. */
  313. if (wptid.lwp () != 0 && !find_thread_ptid (wptid)
  314. && ourstatus->kind () != TARGET_WAITKIND_THREAD_EXITED)
  315. add_thread (wptid, nullptr);
  316. switch (ourstatus->kind ())
  317. {
  318. case TARGET_WAITKIND_EXITED:
  319. case TARGET_WAITKIND_STOPPED:
  320. case TARGET_WAITKIND_SIGNALLED:
  321. case TARGET_WAITKIND_FORKED:
  322. case TARGET_WAITKIND_VFORKED:
  323. case TARGET_WAITKIND_EXECD:
  324. case TARGET_WAITKIND_VFORK_DONE:
  325. case TARGET_WAITKIND_SYSCALL_ENTRY:
  326. case TARGET_WAITKIND_SYSCALL_RETURN:
  327. /* Pass the result to the generic code. */
  328. return wptid;
  329. case TARGET_WAITKIND_THREAD_CREATED:
  330. case TARGET_WAITKIND_THREAD_EXITED:
  331. /* The core needlessly stops on these events. */
  332. /* FALLTHROUGH */
  333. case TARGET_WAITKIND_SPURIOUS:
  334. /* Spurious events are unhandled by the gdbserver core. */
  335. if (ptrace (PT_CONTINUE, current_process ()->pid, (void *) 1, 0)
  336. == -1)
  337. perror_with_name (("ptrace"));
  338. break;
  339. default:
  340. error (("Unknown stopped status"));
  341. }
  342. }
  343. }
  344. /* Implement the kill target_ops method. */
  345. int
  346. netbsd_process_target::kill (process_info *process)
  347. {
  348. pid_t pid = process->pid;
  349. if (ptrace (PT_KILL, pid, nullptr, 0) == -1)
  350. return -1;
  351. int status;
  352. if (gdb::handle_eintr (-1, ::waitpid, pid, &status, 0) == -1)
  353. return -1;
  354. mourn (process);
  355. return 0;
  356. }
  357. /* Implement the detach target_ops method. */
  358. int
  359. netbsd_process_target::detach (process_info *process)
  360. {
  361. pid_t pid = process->pid;
  362. ptrace (PT_DETACH, pid, (void *) 1, 0);
  363. mourn (process);
  364. return 0;
  365. }
  366. /* Implement the mourn target_ops method. */
  367. void
  368. netbsd_process_target::mourn (struct process_info *proc)
  369. {
  370. for_each_thread (proc->pid, remove_thread);
  371. remove_process (proc);
  372. }
  373. /* Implement the join target_ops method. */
  374. void
  375. netbsd_process_target::join (int pid)
  376. {
  377. /* The PT_DETACH is sufficient to detach from the process.
  378. So no need to do anything extra. */
  379. }
  380. /* Implement the thread_alive target_ops method. */
  381. bool
  382. netbsd_process_target::thread_alive (ptid_t ptid)
  383. {
  384. return netbsd_nat::thread_alive (ptid);
  385. }
  386. /* Implement the fetch_registers target_ops method. */
  387. void
  388. netbsd_process_target::fetch_registers (struct regcache *regcache, int regno)
  389. {
  390. const netbsd_regset_info *regset = get_regs_info ();
  391. ptid_t inferior_ptid = ptid_of (current_thread);
  392. while (regset->size >= 0)
  393. {
  394. std::vector<char> buf;
  395. buf.resize (regset->size);
  396. int res = ptrace (regset->get_request, inferior_ptid.pid (), buf.data (),
  397. inferior_ptid.lwp ());
  398. if (res == -1)
  399. perror_with_name (("ptrace"));
  400. regset->store_function (regcache, buf.data ());
  401. regset++;
  402. }
  403. }
  404. /* Implement the store_registers target_ops method. */
  405. void
  406. netbsd_process_target::store_registers (struct regcache *regcache, int regno)
  407. {
  408. const netbsd_regset_info *regset = get_regs_info ();
  409. ptid_t inferior_ptid = ptid_of (current_thread);
  410. while (regset->size >= 0)
  411. {
  412. std::vector<char> buf;
  413. buf.resize (regset->size);
  414. int res = ptrace (regset->get_request, inferior_ptid.pid (), buf.data (),
  415. inferior_ptid.lwp ());
  416. if (res == -1)
  417. perror_with_name (("ptrace"));
  418. /* Then overlay our cached registers on that. */
  419. regset->fill_function (regcache, buf.data ());
  420. /* Only now do we write the register set. */
  421. res = ptrace (regset->set_request, inferior_ptid.pid (), buf. data (),
  422. inferior_ptid.lwp ());
  423. if (res == -1)
  424. perror_with_name (("ptrace"));
  425. regset++;
  426. }
  427. }
  428. /* Implement the read_memory target_ops method. */
  429. int
  430. netbsd_process_target::read_memory (CORE_ADDR memaddr, unsigned char *myaddr,
  431. int size)
  432. {
  433. pid_t pid = current_process ()->pid;
  434. return netbsd_nat::read_memory (pid, myaddr, memaddr, size, nullptr);
  435. }
  436. /* Implement the write_memory target_ops method. */
  437. int
  438. netbsd_process_target::write_memory (CORE_ADDR memaddr,
  439. const unsigned char *myaddr, int size)
  440. {
  441. pid_t pid = current_process ()->pid;
  442. return netbsd_nat::write_memory (pid, myaddr, memaddr, size, nullptr);
  443. }
  444. /* Implement the request_interrupt target_ops method. */
  445. void
  446. netbsd_process_target::request_interrupt ()
  447. {
  448. ptid_t inferior_ptid = ptid_of (get_first_thread ());
  449. ::kill (inferior_ptid.pid (), SIGINT);
  450. }
  451. /* Read the AUX Vector for the specified PID, wrapping the ptrace(2) call
  452. with the PIOD_READ_AUXV operation and using the PT_IO standard input
  453. and output arguments. */
  454. static size_t
  455. netbsd_read_auxv(pid_t pid, void *offs, void *addr, size_t len)
  456. {
  457. struct ptrace_io_desc pio;
  458. pio.piod_op = PIOD_READ_AUXV;
  459. pio.piod_offs = offs;
  460. pio.piod_addr = addr;
  461. pio.piod_len = len;
  462. if (ptrace (PT_IO, pid, &pio, 0) == -1)
  463. perror_with_name (("ptrace"));
  464. return pio.piod_len;
  465. }
  466. /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
  467. to debugger memory starting at MYADDR. */
  468. int
  469. netbsd_process_target::read_auxv (CORE_ADDR offset,
  470. unsigned char *myaddr, unsigned int len)
  471. {
  472. pid_t pid = pid_of (current_thread);
  473. return netbsd_read_auxv (pid, (void *) (intptr_t) offset, myaddr, len);
  474. }
  475. bool
  476. netbsd_process_target::supports_z_point_type (char z_type)
  477. {
  478. switch (z_type)
  479. {
  480. case Z_PACKET_SW_BP:
  481. return true;
  482. case Z_PACKET_HW_BP:
  483. case Z_PACKET_WRITE_WP:
  484. case Z_PACKET_READ_WP:
  485. case Z_PACKET_ACCESS_WP:
  486. default:
  487. return false; /* Not supported. */
  488. }
  489. }
  490. /* Insert {break/watch}point at address ADDR. SIZE is not used. */
  491. int
  492. netbsd_process_target::insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
  493. int size, struct raw_breakpoint *bp)
  494. {
  495. switch (type)
  496. {
  497. case raw_bkpt_type_sw:
  498. return insert_memory_breakpoint (bp);
  499. case raw_bkpt_type_hw:
  500. case raw_bkpt_type_write_wp:
  501. case raw_bkpt_type_read_wp:
  502. case raw_bkpt_type_access_wp:
  503. default:
  504. return 1; /* Not supported. */
  505. }
  506. }
  507. /* Remove {break/watch}point at address ADDR. SIZE is not used. */
  508. int
  509. netbsd_process_target::remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
  510. int size, struct raw_breakpoint *bp)
  511. {
  512. switch (type)
  513. {
  514. case raw_bkpt_type_sw:
  515. return remove_memory_breakpoint (bp);
  516. case raw_bkpt_type_hw:
  517. case raw_bkpt_type_write_wp:
  518. case raw_bkpt_type_read_wp:
  519. case raw_bkpt_type_access_wp:
  520. default:
  521. return 1; /* Not supported. */
  522. }
  523. }
  524. /* Implement the stopped_by_sw_breakpoint target_ops method. */
  525. bool
  526. netbsd_process_target::stopped_by_sw_breakpoint ()
  527. {
  528. ptrace_siginfo_t psi;
  529. pid_t pid = current_process ()->pid;
  530. if (ptrace (PT_GET_SIGINFO, pid, &psi, sizeof (psi)) == -1)
  531. perror_with_name (("ptrace"));
  532. return psi.psi_siginfo.si_signo == SIGTRAP &&
  533. psi.psi_siginfo.si_code == TRAP_BRKPT;
  534. }
  535. /* Implement the supports_stopped_by_sw_breakpoint target_ops method. */
  536. bool
  537. netbsd_process_target::supports_stopped_by_sw_breakpoint ()
  538. {
  539. return true;
  540. }
  541. /* Implement the supports_qxfer_siginfo target_ops method. */
  542. bool
  543. netbsd_process_target::supports_qxfer_siginfo ()
  544. {
  545. return true;
  546. }
  547. /* Implement the qxfer_siginfo target_ops method. */
  548. int
  549. netbsd_process_target::qxfer_siginfo (const char *annex, unsigned char *readbuf,
  550. unsigned const char *writebuf,
  551. CORE_ADDR offset, int len)
  552. {
  553. if (current_thread == nullptr)
  554. return -1;
  555. pid_t pid = current_process ()->pid;
  556. return netbsd_nat::qxfer_siginfo(pid, annex, readbuf, writebuf, offset, len);
  557. }
  558. /* Implement the supports_non_stop target_ops method. */
  559. bool
  560. netbsd_process_target::supports_non_stop ()
  561. {
  562. return false;
  563. }
  564. /* Implement the supports_multi_process target_ops method. */
  565. bool
  566. netbsd_process_target::supports_multi_process ()
  567. {
  568. return true;
  569. }
  570. /* Check if fork events are supported. */
  571. bool
  572. netbsd_process_target::supports_fork_events ()
  573. {
  574. return false;
  575. }
  576. /* Check if vfork events are supported. */
  577. bool
  578. netbsd_process_target::supports_vfork_events ()
  579. {
  580. return false;
  581. }
  582. /* Check if exec events are supported. */
  583. bool
  584. netbsd_process_target::supports_exec_events ()
  585. {
  586. return true;
  587. }
  588. /* Implement the supports_disable_randomization target_ops method. */
  589. bool
  590. netbsd_process_target::supports_disable_randomization ()
  591. {
  592. return false;
  593. }
  594. /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
  595. template <typename T>
  596. int get_phdr_phnum_from_proc_auxv (const pid_t pid,
  597. CORE_ADDR *phdr_memaddr, int *num_phdr)
  598. {
  599. typedef typename std::conditional<sizeof(T) == sizeof(int64_t),
  600. Aux64Info, Aux32Info>::type auxv_type;
  601. const size_t auxv_size = sizeof (auxv_type);
  602. const size_t auxv_buf_size = 128 * sizeof (auxv_type);
  603. std::vector<char> auxv_buf;
  604. auxv_buf.resize (auxv_buf_size);
  605. netbsd_read_auxv (pid, nullptr, auxv_buf.data (), auxv_buf_size);
  606. *phdr_memaddr = 0;
  607. *num_phdr = 0;
  608. for (char *buf = auxv_buf.data ();
  609. buf < (auxv_buf.data () + auxv_buf_size);
  610. buf += auxv_size)
  611. {
  612. auxv_type *const aux = (auxv_type *) buf;
  613. switch (aux->a_type)
  614. {
  615. case AT_PHDR:
  616. *phdr_memaddr = aux->a_v;
  617. break;
  618. case AT_PHNUM:
  619. *num_phdr = aux->a_v;
  620. break;
  621. }
  622. if (*phdr_memaddr != 0 && *num_phdr != 0)
  623. break;
  624. }
  625. if (*phdr_memaddr == 0 || *num_phdr == 0)
  626. {
  627. warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
  628. "phdr_memaddr = %s, phdr_num = %d",
  629. core_addr_to_string (*phdr_memaddr), *num_phdr);
  630. return 2;
  631. }
  632. return 0;
  633. }
  634. /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
  635. template <typename T>
  636. static CORE_ADDR
  637. get_dynamic (const pid_t pid)
  638. {
  639. typedef typename std::conditional<sizeof(T) == sizeof(int64_t),
  640. Elf64_Phdr, Elf32_Phdr>::type phdr_type;
  641. const int phdr_size = sizeof (phdr_type);
  642. CORE_ADDR phdr_memaddr;
  643. int num_phdr;
  644. if (get_phdr_phnum_from_proc_auxv<T> (pid, &phdr_memaddr, &num_phdr))
  645. return 0;
  646. std::vector<unsigned char> phdr_buf;
  647. phdr_buf.resize (num_phdr * phdr_size);
  648. if (netbsd_nat::read_memory (pid, phdr_buf.data (), phdr_memaddr,
  649. phdr_buf.size (), nullptr))
  650. return 0;
  651. /* Compute relocation: it is expected to be 0 for "regular" executables,
  652. non-zero for PIE ones. */
  653. CORE_ADDR relocation = -1;
  654. for (int i = 0; relocation == -1 && i < num_phdr; i++)
  655. {
  656. phdr_type *const p = (phdr_type *) (phdr_buf.data () + i * phdr_size);
  657. if (p->p_type == PT_PHDR)
  658. relocation = phdr_memaddr - p->p_vaddr;
  659. }
  660. if (relocation == -1)
  661. {
  662. /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
  663. any real world executables, including PIE executables, have always
  664. PT_PHDR present. PT_PHDR is not present in some shared libraries or
  665. in fpc (Free Pascal 2.4) binaries but neither of those have a need for
  666. or present DT_DEBUG anyway (fpc binaries are statically linked).
  667. Therefore if there exists DT_DEBUG there is always also PT_PHDR.
  668. GDB could find RELOCATION also from AT_ENTRY - e_entry. */
  669. return 0;
  670. }
  671. for (int i = 0; i < num_phdr; i++)
  672. {
  673. phdr_type *const p = (phdr_type *) (phdr_buf.data () + i * phdr_size);
  674. if (p->p_type == PT_DYNAMIC)
  675. return p->p_vaddr + relocation;
  676. }
  677. return 0;
  678. }
  679. /* Return &_r_debug in the inferior, or -1 if not present. Return value
  680. can be 0 if the inferior does not yet have the library list initialized.
  681. We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
  682. DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
  683. template <typename T>
  684. static CORE_ADDR
  685. get_r_debug (const pid_t pid)
  686. {
  687. typedef typename std::conditional<sizeof(T) == sizeof(int64_t),
  688. Elf64_Dyn, Elf32_Dyn>::type dyn_type;
  689. const int dyn_size = sizeof (dyn_type);
  690. unsigned char buf[sizeof (dyn_type)]; /* The larger of the two. */
  691. CORE_ADDR map = -1;
  692. CORE_ADDR dynamic_memaddr = get_dynamic<T> (pid);
  693. if (dynamic_memaddr == 0)
  694. return map;
  695. while (netbsd_nat::read_memory (pid, buf, dynamic_memaddr, dyn_size, nullptr)
  696. == 0)
  697. {
  698. dyn_type *const dyn = (dyn_type *) buf;
  699. #if defined DT_MIPS_RLD_MAP
  700. union
  701. {
  702. T map;
  703. unsigned char buf[sizeof (T)];
  704. }
  705. rld_map;
  706. if (dyn->d_tag == DT_MIPS_RLD_MAP)
  707. {
  708. if (netbsd_nat::read_memory (pid, rld_map.buf, dyn->d_un.d_val,
  709. sizeof (rld_map.buf), nullptr) == 0)
  710. return rld_map.map;
  711. else
  712. break;
  713. }
  714. #endif /* DT_MIPS_RLD_MAP */
  715. if (dyn->d_tag == DT_DEBUG && map == -1)
  716. map = dyn->d_un.d_val;
  717. if (dyn->d_tag == DT_NULL)
  718. break;
  719. dynamic_memaddr += dyn_size;
  720. }
  721. return map;
  722. }
  723. /* Read one pointer from MEMADDR in the inferior. */
  724. static int
  725. read_one_ptr (const pid_t pid, CORE_ADDR memaddr, CORE_ADDR *ptr, int ptr_size)
  726. {
  727. /* Go through a union so this works on either big or little endian
  728. hosts, when the inferior's pointer size is smaller than the size
  729. of CORE_ADDR. It is assumed the inferior's endianness is the
  730. same of the superior's. */
  731. union
  732. {
  733. CORE_ADDR core_addr;
  734. unsigned int ui;
  735. unsigned char uc;
  736. } addr;
  737. int ret = netbsd_nat::read_memory (pid, &addr.uc, memaddr, ptr_size, nullptr);
  738. if (ret == 0)
  739. {
  740. if (ptr_size == sizeof (CORE_ADDR))
  741. *ptr = addr.core_addr;
  742. else if (ptr_size == sizeof (unsigned int))
  743. *ptr = addr.ui;
  744. else
  745. gdb_assert_not_reached ("unhandled pointer size");
  746. }
  747. return ret;
  748. }
  749. /* Construct qXfer:libraries-svr4:read reply. */
  750. template <typename T>
  751. int
  752. netbsd_qxfer_libraries_svr4 (const pid_t pid, const char *annex,
  753. unsigned char *readbuf,
  754. unsigned const char *writebuf,
  755. CORE_ADDR offset, int len)
  756. {
  757. struct link_map_offsets
  758. {
  759. /* Offset and size of r_debug.r_version. */
  760. int r_version_offset;
  761. /* Offset and size of r_debug.r_map. */
  762. int r_map_offset;
  763. /* Offset to l_addr field in struct link_map. */
  764. int l_addr_offset;
  765. /* Offset to l_name field in struct link_map. */
  766. int l_name_offset;
  767. /* Offset to l_ld field in struct link_map. */
  768. int l_ld_offset;
  769. /* Offset to l_next field in struct link_map. */
  770. int l_next_offset;
  771. /* Offset to l_prev field in struct link_map. */
  772. int l_prev_offset;
  773. };
  774. static const struct link_map_offsets lmo_32bit_offsets =
  775. {
  776. 0, /* r_version offset. */
  777. 4, /* r_debug.r_map offset. */
  778. 0, /* l_addr offset in link_map. */
  779. 4, /* l_name offset in link_map. */
  780. 8, /* l_ld offset in link_map. */
  781. 12, /* l_next offset in link_map. */
  782. 16 /* l_prev offset in link_map. */
  783. };
  784. static const struct link_map_offsets lmo_64bit_offsets =
  785. {
  786. 0, /* r_version offset. */
  787. 8, /* r_debug.r_map offset. */
  788. 0, /* l_addr offset in link_map. */
  789. 8, /* l_name offset in link_map. */
  790. 16, /* l_ld offset in link_map. */
  791. 24, /* l_next offset in link_map. */
  792. 32 /* l_prev offset in link_map. */
  793. };
  794. CORE_ADDR lm_addr = 0, lm_prev = 0;
  795. CORE_ADDR l_name, l_addr, l_ld, l_next, l_prev;
  796. int header_done = 0;
  797. const struct link_map_offsets *lmo
  798. = ((sizeof (T) == sizeof (int64_t))
  799. ? &lmo_64bit_offsets : &lmo_32bit_offsets);
  800. int ptr_size = sizeof (T);
  801. while (annex[0] != '\0')
  802. {
  803. const char *sep = strchr (annex, '=');
  804. if (sep == nullptr)
  805. break;
  806. int name_len = sep - annex;
  807. CORE_ADDR *addrp;
  808. if (name_len == 5 && startswith (annex, "start"))
  809. addrp = &lm_addr;
  810. else if (name_len == 4 && startswith (annex, "prev"))
  811. addrp = &lm_prev;
  812. else
  813. {
  814. annex = strchr (sep, ';');
  815. if (annex == nullptr)
  816. break;
  817. annex++;
  818. continue;
  819. }
  820. annex = decode_address_to_semicolon (addrp, sep + 1);
  821. }
  822. if (lm_addr == 0)
  823. {
  824. CORE_ADDR r_debug = get_r_debug<T> (pid);
  825. /* We failed to find DT_DEBUG. Such situation will not change
  826. for this inferior - do not retry it. Report it to GDB as
  827. E01, see for the reasons at the GDB solib-svr4.c side. */
  828. if (r_debug == (CORE_ADDR) -1)
  829. return -1;
  830. if (r_debug != 0)
  831. {
  832. CORE_ADDR map_offset = r_debug + lmo->r_map_offset;
  833. if (read_one_ptr (pid, map_offset, &lm_addr, ptr_size) != 0)
  834. warning ("unable to read r_map from %s",
  835. core_addr_to_string (map_offset));
  836. }
  837. }
  838. std::string document = "<library-list-svr4 version=\"1.0\"";
  839. while (lm_addr
  840. && read_one_ptr (pid, lm_addr + lmo->l_name_offset,
  841. &l_name, ptr_size) == 0
  842. && read_one_ptr (pid, lm_addr + lmo->l_addr_offset,
  843. &l_addr, ptr_size) == 0
  844. && read_one_ptr (pid, lm_addr + lmo->l_ld_offset,
  845. &l_ld, ptr_size) == 0
  846. && read_one_ptr (pid, lm_addr + lmo->l_prev_offset,
  847. &l_prev, ptr_size) == 0
  848. && read_one_ptr (pid, lm_addr + lmo->l_next_offset,
  849. &l_next, ptr_size) == 0)
  850. {
  851. if (lm_prev != l_prev)
  852. {
  853. warning ("Corrupted shared library list: 0x%lx != 0x%lx",
  854. (long) lm_prev, (long) l_prev);
  855. break;
  856. }
  857. /* Ignore the first entry even if it has valid name as the first entry
  858. corresponds to the main executable. The first entry should not be
  859. skipped if the dynamic loader was loaded late by a static executable
  860. (see solib-svr4.c parameter ignore_first). But in such case the main
  861. executable does not have PT_DYNAMIC present and this function already
  862. exited above due to failed get_r_debug. */
  863. if (lm_prev == 0)
  864. string_appendf (document, " main-lm=\"0x%lx\"",
  865. (unsigned long) lm_addr);
  866. else
  867. {
  868. unsigned char libname[PATH_MAX];
  869. /* Not checking for error because reading may stop before
  870. we've got PATH_MAX worth of characters. */
  871. libname[0] = '\0';
  872. netbsd_nat::read_memory (pid, libname, l_name, sizeof (libname) - 1,
  873. nullptr);
  874. libname[sizeof (libname) - 1] = '\0';
  875. if (libname[0] != '\0')
  876. {
  877. if (!header_done)
  878. {
  879. /* Terminate `<library-list-svr4'. */
  880. document += '>';
  881. header_done = 1;
  882. }
  883. string_appendf (document, "<library name=\"");
  884. xml_escape_text_append (&document, (char *) libname);
  885. string_appendf (document, "\" lm=\"0x%lx\" "
  886. "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
  887. (unsigned long) lm_addr, (unsigned long) l_addr,
  888. (unsigned long) l_ld);
  889. }
  890. }
  891. lm_prev = lm_addr;
  892. lm_addr = l_next;
  893. }
  894. if (!header_done)
  895. {
  896. /* Empty list; terminate `<library-list-svr4'. */
  897. document += "/>";
  898. }
  899. else
  900. document += "</library-list-svr4>";
  901. int document_len = document.length ();
  902. if (offset < document_len)
  903. document_len -= offset;
  904. else
  905. document_len = 0;
  906. if (len > document_len)
  907. len = document_len;
  908. memcpy (readbuf, document.data () + offset, len);
  909. return len;
  910. }
  911. /* Return true if FILE is a 64-bit ELF file,
  912. false if the file is not a 64-bit ELF file,
  913. and error if the file is not accessible or doesn't exist. */
  914. static bool
  915. elf_64_file_p (const char *file)
  916. {
  917. int fd = gdb::handle_eintr (-1, ::open, file, O_RDONLY);
  918. if (fd < 0)
  919. perror_with_name (("open"));
  920. Elf64_Ehdr header;
  921. ssize_t ret = gdb::handle_eintr (-1, ::read, fd, &header, sizeof (header));
  922. if (ret == -1)
  923. perror_with_name (("read"));
  924. gdb::handle_eintr (-1, ::close, fd);
  925. if (ret != sizeof (header))
  926. error ("Cannot read ELF file header: %s", file);
  927. if (header.e_ident[EI_MAG0] != ELFMAG0
  928. || header.e_ident[EI_MAG1] != ELFMAG1
  929. || header.e_ident[EI_MAG2] != ELFMAG2
  930. || header.e_ident[EI_MAG3] != ELFMAG3)
  931. error ("Unrecognized ELF file header: %s", file);
  932. return header.e_ident[EI_CLASS] == ELFCLASS64;
  933. }
  934. /* Construct qXfer:libraries-svr4:read reply. */
  935. int
  936. netbsd_process_target::qxfer_libraries_svr4 (const char *annex,
  937. unsigned char *readbuf,
  938. unsigned const char *writebuf,
  939. CORE_ADDR offset, int len)
  940. {
  941. if (writebuf != nullptr)
  942. return -2;
  943. if (readbuf == nullptr)
  944. return -1;
  945. struct process_info *proc = current_process ();
  946. pid_t pid = proc->pid;
  947. bool is_elf64 = elf_64_file_p (netbsd_nat::pid_to_exec_file (pid));
  948. if (is_elf64)
  949. return netbsd_qxfer_libraries_svr4<int64_t> (pid, annex, readbuf,
  950. writebuf, offset, len);
  951. else
  952. return netbsd_qxfer_libraries_svr4<int32_t> (pid, annex, readbuf,
  953. writebuf, offset, len);
  954. }
  955. /* Implement the supports_qxfer_libraries_svr4 target_ops method. */
  956. bool
  957. netbsd_process_target::supports_qxfer_libraries_svr4 ()
  958. {
  959. return true;
  960. }
  961. /* Return the name of a file that can be opened to get the symbols for
  962. the child process identified by PID. */
  963. const char *
  964. netbsd_process_target::pid_to_exec_file (pid_t pid)
  965. {
  966. return netbsd_nat::pid_to_exec_file (pid);
  967. }
  968. /* Implementation of the target_ops method "supports_pid_to_exec_file". */
  969. bool
  970. netbsd_process_target::supports_pid_to_exec_file ()
  971. {
  972. return true;
  973. }
  974. /* Implementation of the target_ops method "supports_hardware_single_step". */
  975. bool
  976. netbsd_process_target::supports_hardware_single_step ()
  977. {
  978. return true;
  979. }
  980. /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
  981. const gdb_byte *
  982. netbsd_process_target::sw_breakpoint_from_kind (int kind, int *size)
  983. {
  984. static gdb_byte brkpt[PTRACE_BREAKPOINT_SIZE] = {*PTRACE_BREAKPOINT};
  985. *size = PTRACE_BREAKPOINT_SIZE;
  986. return brkpt;
  987. }
  988. /* Implement the thread_name target_ops method. */
  989. const char *
  990. netbsd_process_target::thread_name (ptid_t ptid)
  991. {
  992. return netbsd_nat::thread_name (ptid);
  993. }
  994. /* Implement the supports_catch_syscall target_ops method. */
  995. bool
  996. netbsd_process_target::supports_catch_syscall ()
  997. {
  998. return true;
  999. }
  1000. /* Implement the supports_read_auxv target_ops method. */
  1001. bool
  1002. netbsd_process_target::supports_read_auxv ()
  1003. {
  1004. return true;
  1005. }
  1006. void
  1007. initialize_low ()
  1008. {
  1009. set_target_ops (the_netbsd_target);
  1010. }