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