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