1 /* QNX Neutrino specific low level interface, for the remote server
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include <sys/procfs.h>
31 #include <sys/iomgr.h>
32 #include <sys/neutrino.h>
35 extern int using_threads;
36 int using_threads = 1;
39 nto_trace (const char *fmt, ...)
43 if (debug_threads == 0)
45 fprintf (stderr, "nto:");
46 va_start (arg_list, fmt);
47 vfprintf (stderr, fmt, arg_list);
51 #define TRACE nto_trace
53 /* Structure holding neutrino specific information about
58 char nto_procfs_path[PATH_MAX];
61 int exit_signo; /* For tracking exit status. */
64 static struct nto_inferior nto_inferior;
67 init_nto_inferior (struct nto_inferior *nto_inferior)
69 memset (nto_inferior, 0, sizeof (struct nto_inferior));
70 nto_inferior->ctl_fd = -1;
71 nto_inferior->pid = -1;
77 if (nto_inferior.ctl_fd != -1)
79 nto_trace ("Closing fd\n");
80 close (nto_inferior.ctl_fd);
81 init_nto_inferior (&nto_inferior);
85 /* Set current thread. Return 1 on success, 0 otherwise. */
88 nto_set_thread (ptid_t ptid)
92 TRACE ("%s pid: %d tid: %ld\n", __func__, ptid_get_pid (ptid),
94 if (nto_inferior.ctl_fd != -1
95 && !ptid_equal (ptid, null_ptid)
96 && !ptid_equal (ptid, minus_one_ptid))
98 pthread_t tid = ptid_get_lwp (ptid);
100 if (EOK == devctl (nto_inferior.ctl_fd, DCMD_PROC_CURTHREAD, &tid,
104 TRACE ("%s: Error: failed to set current thread\n", __func__);
109 /* This function will determine all alive threads. Note that we do not list
110 dead but unjoined threads even though they are still in the process' thread
113 NTO_INFERIOR must not be NULL. */
116 nto_find_new_threads (struct nto_inferior *nto_inferior)
120 TRACE ("%s pid:%d\n", __func__, nto_inferior->pid);
122 if (nto_inferior->ctl_fd == -1)
125 for (tid = 1;; ++tid)
127 procfs_status status;
132 err = devctl (nto_inferior->ctl_fd, DCMD_PROC_TIDSTATUS, &status,
135 if (err != EOK || status.tid == 0)
138 /* All threads in between are gone. */
139 while (tid != status.tid || status.state == STATE_DEAD)
141 struct thread_info *ti;
143 ptid = ptid_build (nto_inferior->pid, tid, 0);
144 ti = find_thread_ptid (ptid);
147 TRACE ("Removing thread %d\n", tid);
150 if (tid == status.tid)
155 if (status.state != STATE_DEAD)
157 TRACE ("Adding thread %d\n", tid);
158 ptid = ptid_build (nto_inferior->pid, tid, 0);
159 if (!find_thread_ptid (ptid))
160 add_thread (ptid, NULL);
165 /* Given pid, open procfs path. */
168 do_attach (pid_t pid)
170 procfs_status status;
171 struct sigevent event;
173 if (nto_inferior.ctl_fd != -1)
175 close (nto_inferior.ctl_fd);
176 init_nto_inferior (&nto_inferior);
178 snprintf (nto_inferior.nto_procfs_path, PATH_MAX - 1, "/proc/%d/as", pid);
179 nto_inferior.ctl_fd = open (nto_inferior.nto_procfs_path, O_RDWR);
180 if (nto_inferior.ctl_fd == -1)
182 TRACE ("Failed to open %s\n", nto_inferior.nto_procfs_path);
183 init_nto_inferior (&nto_inferior);
186 if (devctl (nto_inferior.ctl_fd, DCMD_PROC_STOP, &status, sizeof (status), 0)
192 nto_inferior.pid = pid;
193 /* Define a sigevent for process stopped notification. */
194 event.sigev_notify = SIGEV_SIGNAL_THREAD;
195 event.sigev_signo = SIGUSR1;
196 event.sigev_code = 0;
197 event.sigev_value.sival_ptr = NULL;
198 event.sigev_priority = -1;
199 devctl (nto_inferior.ctl_fd, DCMD_PROC_EVENT, &event, sizeof (event), 0);
201 if (devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status),
203 && (status.flags & _DEBUG_FLAG_STOPPED))
208 ptid = ptid_build (status.pid, status.tid, 0);
209 the_low_target.arch_setup ();
210 add_process (status.pid, 1);
211 TRACE ("Adding thread: pid=%d tid=%ld\n", status.pid,
212 ptid_get_lwp (ptid));
213 nto_find_new_threads (&nto_inferior);
224 /* Read or write LEN bytes from/to inferior's MEMADDR memory address
225 into gdbservers's MYADDR buffer. Return number of bytes actually
229 nto_xfer_memory (off_t memaddr, unsigned char *myaddr, int len,
234 if (lseek (nto_inferior.ctl_fd, memaddr, SEEK_SET) == memaddr)
237 nbytes = write (nto_inferior.ctl_fd, myaddr, len);
239 nbytes = read (nto_inferior.ctl_fd, myaddr, len);
246 TRACE ("Error in %s : errno=%d (%s)\n", __func__, e, strerror (e));
251 /* Insert or remove breakpoint or watchpoint at address ADDR.
252 TYPE can be one of Neutrino breakpoint types. SIZE must be 0 for
253 inserting the point, -1 for removing it.
255 Return 0 on success, 1 otherwise. */
258 nto_breakpoint (CORE_ADDR addr, int type, int size)
265 if (devctl (nto_inferior.ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0)
271 /* Read auxiliary vector from inferior's initial stack into gdbserver's
272 MYADDR buffer, up to LEN bytes.
274 Return number of bytes read. */
277 nto_read_auxv_from_initial_stack (CORE_ADDR initial_stack,
278 unsigned char *myaddr,
283 unsigned int len_read = 0;
285 /* Skip over argc, argv and envp... Comment from ldd.c:
287 The startup frame is set-up so that we have:
292 envp1 <----- void *frame + (argc + 2) * sizeof(char *)
297 argc <------ void * frame
299 On entry to ldd, frame gives the address of argc on the stack. */
300 if (nto_xfer_memory (initial_stack, (unsigned char *)&anint,
301 sizeof (anint), 0) != sizeof (anint))
304 /* Size of pointer is assumed to be 4 bytes (32 bit arch. ) */
305 data_ofs += (anint + 2) * sizeof (void *); /* + 2 comes from argc itself and
306 NULL terminating pointer in
309 /* Now loop over env table: */
310 while (nto_xfer_memory (initial_stack + data_ofs,
311 (unsigned char *)&anint, sizeof (anint), 0)
314 data_ofs += sizeof (anint);
318 initial_stack += data_ofs;
320 memset (myaddr, 0, len);
321 while (len_read <= len - sizeof (auxv_t))
323 auxv_t *auxv = (auxv_t *)myaddr;
325 /* Search backwards until we have read AT_PHDR (num. 3),
326 AT_PHENT (num 4), AT_PHNUM (num 5) */
327 if (nto_xfer_memory (initial_stack, (unsigned char *)auxv,
328 sizeof (auxv_t), 0) == sizeof (auxv_t))
330 if (auxv->a_type != AT_NULL)
333 len_read += sizeof (auxv_t);
335 if (auxv->a_type == AT_PHNUM) /* That's all we need. */
337 initial_stack += sizeof (auxv_t);
342 TRACE ("auxv: len_read: %d\n", len_read);
346 /* Start inferior specified by PROGRAM passing arguments ALLARGS. */
349 nto_create_inferior (char *program, char **allargs)
351 struct inheritance inherit;
355 TRACE ("%s %s\n", __func__, program);
356 /* Clear any pending SIGUSR1's but keep the behavior the same. */
357 signal (SIGUSR1, signal (SIGUSR1, SIG_IGN));
360 sigaddset (&set, SIGUSR1);
361 sigprocmask (SIG_UNBLOCK, &set, NULL);
363 memset (&inherit, 0, sizeof (inherit));
364 inherit.flags |= SPAWN_SETGROUP | SPAWN_HOLD;
365 inherit.pgroup = SPAWN_NEWPGROUP;
366 pid = spawnp (program, 0, NULL, &inherit, allargs, 0);
367 sigprocmask (SIG_BLOCK, &set, NULL);
372 if (do_attach (pid) != pid)
378 /* Attach to process PID. */
381 nto_attach (unsigned long pid)
383 TRACE ("%s %ld\n", __func__, pid);
384 if (do_attach (pid) != pid)
385 error ("Unable to attach to %ld\n", pid);
389 /* Send signal to process PID. */
394 TRACE ("%s %d\n", __func__, pid);
400 /* Detach from process PID. */
405 TRACE ("%s %d\n", __func__, pid);
410 /* Check if the given thread is alive.
412 Return 1 if alive, 0 otherwise. */
415 nto_thread_alive (ptid_t ptid)
419 TRACE ("%s pid:%d tid:%d\n", __func__, ptid_get_pid (ptid),
420 ptid_get_lwp (ptid));
421 if (SignalKill (0, ptid_get_pid (ptid), ptid_get_lwp (ptid),
426 TRACE ("%s: %s\n", __func__, res ? "yes" : "no");
430 /* Resume inferior's execution. */
433 nto_resume (struct thread_resume *resume_info, size_t n)
435 /* We can only work in all-stop mode. */
436 procfs_status status;
440 TRACE ("%s\n", __func__);
441 /* Workaround for aliasing rules violation. */
442 sigset_t *run_fault = (sigset_t *) (void *) &run.fault;
444 nto_set_thread (resume_info->thread);
446 run.flags = _DEBUG_RUN_FAULT | _DEBUG_RUN_TRACE;
447 if (resume_info->kind == resume_step)
448 run.flags |= _DEBUG_RUN_STEP;
449 run.flags |= _DEBUG_RUN_ARM;
451 sigemptyset (run_fault);
452 sigaddset (run_fault, FLTBPT);
453 sigaddset (run_fault, FLTTRACE);
454 sigaddset (run_fault, FLTILL);
455 sigaddset (run_fault, FLTPRIV);
456 sigaddset (run_fault, FLTBOUNDS);
457 sigaddset (run_fault, FLTIOVF);
458 sigaddset (run_fault, FLTIZDIV);
459 sigaddset (run_fault, FLTFPE);
460 sigaddset (run_fault, FLTPAGE);
461 sigaddset (run_fault, FLTSTACK);
462 sigaddset (run_fault, FLTACCESS);
464 sigemptyset (&run.trace);
465 if (resume_info->sig)
469 devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status),
471 signal_to_pass = resume_info->sig;
472 if (status.why & (_DEBUG_WHY_SIGNALLED | _DEBUG_WHY_FAULTED))
474 if (signal_to_pass != status.info.si_signo)
476 kill (status.pid, signal_to_pass);
477 run.flags |= _DEBUG_RUN_CLRFLT | _DEBUG_RUN_CLRSIG;
479 else /* Let it kill the program without telling us. */
480 sigdelset (&run.trace, signal_to_pass);
484 run.flags |= _DEBUG_RUN_CLRSIG | _DEBUG_RUN_CLRFLT;
486 sigfillset (&run.trace);
488 regcache_invalidate ();
490 err = devctl (nto_inferior.ctl_fd, DCMD_PROC_RUN, &run, sizeof (run), 0);
492 TRACE ("Error: %d \"%s\"\n", err, strerror (err));
495 /* Wait for inferior's event.
497 Return ptid of thread that caused the event. */
500 nto_wait (ptid_t ptid,
501 struct target_waitstatus *ourstatus, int target_options)
505 procfs_status status;
506 const int trace_mask = (_DEBUG_FLAG_TRACE_EXEC | _DEBUG_FLAG_TRACE_RD
507 | _DEBUG_FLAG_TRACE_WR | _DEBUG_FLAG_TRACE_MODIFY);
509 TRACE ("%s\n", __func__);
511 ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
514 sigaddset (&set, SIGUSR1);
516 devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
517 while (!(status.flags & _DEBUG_FLAG_ISTOP))
519 sigwaitinfo (&set, &info);
520 devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status),
523 nto_find_new_threads (&nto_inferior);
525 if (status.flags & _DEBUG_FLAG_SSTEP)
528 ourstatus->kind = TARGET_WAITKIND_STOPPED;
529 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
531 /* Was it a breakpoint? */
532 else if (status.flags & trace_mask)
535 ourstatus->kind = TARGET_WAITKIND_STOPPED;
536 ourstatus->value.sig = TARGET_SIGNAL_TRAP;
538 else if (status.flags & _DEBUG_FLAG_ISTOP)
543 case _DEBUG_WHY_SIGNALLED:
544 TRACE (" SIGNALLED\n");
545 ourstatus->kind = TARGET_WAITKIND_STOPPED;
546 ourstatus->value.sig =
547 target_signal_from_host (status.info.si_signo);
548 nto_inferior.exit_signo = ourstatus->value.sig;
550 case _DEBUG_WHY_FAULTED:
551 TRACE (" FAULTED\n");
552 ourstatus->kind = TARGET_WAITKIND_STOPPED;
553 if (status.info.si_signo == SIGTRAP)
555 ourstatus->value.sig = 0;
556 nto_inferior.exit_signo = 0;
560 ourstatus->value.sig =
561 target_signal_from_host (status.info.si_signo);
562 nto_inferior.exit_signo = ourstatus->value.sig;
566 case _DEBUG_WHY_TERMINATED:
570 TRACE (" TERMINATED\n");
571 waitpid (ptid_get_pid (ptid), &waitval, WNOHANG);
572 if (nto_inferior.exit_signo)
574 /* Abnormal death. */
575 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
576 ourstatus->value.sig = nto_inferior.exit_signo;
581 ourstatus->kind = TARGET_WAITKIND_EXITED;
582 ourstatus->value.integer = WEXITSTATUS (waitval);
584 nto_inferior.exit_signo = 0;
588 case _DEBUG_WHY_REQUESTED:
589 TRACE ("REQUESTED\n");
590 /* We are assuming a requested stop is due to a SIGINT. */
591 ourstatus->kind = TARGET_WAITKIND_STOPPED;
592 ourstatus->value.sig = TARGET_SIGNAL_INT;
593 nto_inferior.exit_signo = 0;
598 return ptid_build (status.pid, status.tid, 0);
601 /* Fetch inferior's registers for currently selected thread (CURRENT_INFERIOR).
602 If REGNO is -1, fetch all registers, or REGNO register only otherwise. */
605 nto_fetch_registers (int regno)
611 TRACE ("%s (regno=%d)\n", __func__, regno);
612 if (regno >= the_low_target.num_regs)
615 if (current_inferior == NULL)
617 TRACE ("current_inferior is NULL\n");
620 ptid = thread_to_gdb_id (current_inferior);
621 if (!nto_set_thread (ptid))
624 if (devctl (nto_inferior.ctl_fd, DCMD_PROC_GETGREG, &greg, sizeof (greg),
627 if (regno == -1) /* All registers. */
629 for (regno = 0; regno != the_low_target.num_regs; ++regno)
631 const unsigned int registeroffset
632 = the_low_target.register_offset (regno);
633 supply_register (regno, ((char *)&greg) + registeroffset);
638 const unsigned int registeroffset
639 = the_low_target.register_offset (regno);
640 if (registeroffset == -1)
642 supply_register (regno, ((char *)&greg) + registeroffset);
646 TRACE ("ERROR reading registers from inferior.\n");
649 /* Store registers for currently selected thread (CURRENT_INFERIOR).
650 We always store all registers, regardless of REGNO. */
653 nto_store_registers (int regno)
659 TRACE ("%s (regno:%d)\n", __func__, regno);
661 if (current_inferior == NULL)
663 TRACE ("current_inferior is NULL\n");
666 ptid = thread_to_gdb_id (current_inferior);
667 if (!nto_set_thread (ptid))
670 memset (&greg, 0, sizeof (greg));
671 for (regno = 0; regno != the_low_target.num_regs; ++regno)
673 const unsigned int regoffset
674 = the_low_target.register_offset (regno);
675 collect_register (regno, ((char *)&greg) + regoffset);
677 err = devctl (nto_inferior.ctl_fd, DCMD_PROC_SETGREG, &greg, sizeof (greg),
680 TRACE ("Error: setting registers.\n");
683 /* Read LEN bytes from inferior's memory address MEMADDR into
684 gdbserver's MYADDR buffer.
686 Return 0 on success -1 otherwise. */
689 nto_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
691 TRACE ("%s memaddr:0x%08lx, len:%d\n", __func__, memaddr, len);
693 if (nto_xfer_memory (memaddr, myaddr, len, 0) != len)
695 TRACE ("Failed to read memory\n");
702 /* Write LEN bytes from gdbserver's buffer MYADDR into inferior's
703 memory at address MEMADDR.
705 Return 0 on success -1 otherwise. */
708 nto_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
712 TRACE ("%s memaddr: 0x%08llx len: %d\n", __func__, memaddr, len);
713 if ((len_written = nto_xfer_memory (memaddr, (unsigned char *)myaddr, len,
717 TRACE ("Wanted to write: %d but written: %d\n", len, len_written);
724 /* Stop inferior. We always stop all threads. */
727 nto_request_interrupt (void)
729 TRACE ("%s\n", __func__);
730 nto_set_thread (ptid_build (nto_inferior.pid, 1, 0));
731 if (EOK != devctl (nto_inferior.ctl_fd, DCMD_PROC_STOP, NULL, 0, 0))
732 TRACE ("Error stopping inferior.\n");
735 /* Read auxiliary vector from inferior's memory into gdbserver's buffer
736 MYADDR. We always read whole auxv.
738 Return number of bytes stored in MYADDR buffer, 0 if OFFSET > 0
742 nto_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len)
745 CORE_ADDR initial_stack;
746 procfs_info procinfo;
748 TRACE ("%s\n", __func__);
752 err = devctl (nto_inferior.ctl_fd, DCMD_PROC_INFO, &procinfo,
757 initial_stack = procinfo.initial_stack;
759 return nto_read_auxv_from_initial_stack (initial_stack, myaddr, len);
762 /* Insert {break/watch}point at address ADDR.
763 TYPE must be in '0'..'4' range. LEN is not used. */
766 nto_insert_point (char type, CORE_ADDR addr, int len)
768 int wtype = _DEBUG_BREAK_HW; /* Always request HW. */
770 TRACE ("%s type:%c addr: 0x%08lx len:%d\n", __func__, (int)type, addr, len);
773 case '0': /* software-breakpoint */
774 wtype = _DEBUG_BREAK_EXEC;
776 case '1': /* hardware-breakpoint */
777 wtype |= _DEBUG_BREAK_EXEC;
779 case '2': /* write watchpoint */
780 wtype |= _DEBUG_BREAK_RW;
782 case '3': /* read watchpoint */
783 wtype |= _DEBUG_BREAK_RD;
785 case '4': /* access watchpoint */
786 wtype |= _DEBUG_BREAK_RW;
789 return 1; /* Not supported. */
791 return nto_breakpoint (addr, wtype, 0);
794 /* Remove {break/watch}point at address ADDR.
795 TYPE must be in '0'..'4' range. LEN is not used. */
798 nto_remove_point (char type, CORE_ADDR addr, int len)
800 int wtype = _DEBUG_BREAK_HW; /* Always request HW. */
802 TRACE ("%s type:%c addr: 0x%08lx len:%d\n", __func__, (int)type, addr, len);
805 case '0': /* software-breakpoint */
806 wtype = _DEBUG_BREAK_EXEC;
808 case '1': /* hardware-breakpoint */
809 wtype |= _DEBUG_BREAK_EXEC;
811 case '2': /* write watchpoint */
812 wtype |= _DEBUG_BREAK_RW;
814 case '3': /* read watchpoint */
815 wtype |= _DEBUG_BREAK_RD;
817 case '4': /* access watchpoint */
818 wtype |= _DEBUG_BREAK_RW;
821 return 1; /* Not supported. */
823 return nto_breakpoint (addr, wtype, -1);
826 /* Check if the reason of stop for current thread (CURRENT_INFERIOR) is
829 Return 1 if stopped by watchpoint, 0 otherwise. */
832 nto_stopped_by_watchpoint (void)
836 TRACE ("%s\n", __func__);
837 if (nto_inferior.ctl_fd != -1 && current_inferior != NULL)
841 ptid = thread_to_gdb_id (current_inferior);
842 if (nto_set_thread (ptid))
844 const int watchmask = _DEBUG_FLAG_TRACE_RD | _DEBUG_FLAG_TRACE_WR
845 | _DEBUG_FLAG_TRACE_MODIFY;
846 procfs_status status;
849 err = devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status,
851 if (err == EOK && (status.flags & watchmask))
855 TRACE ("%s: %s\n", __func__, ret ? "yes" : "no");
859 /* Get instruction pointer for CURRENT_INFERIOR thread.
861 Return inferior's instruction pointer value, or 0 on error. */
864 nto_stopped_data_address (void)
866 CORE_ADDR ret = (CORE_ADDR)0;
868 TRACE ("%s\n", __func__);
869 if (nto_inferior.ctl_fd != -1 && current_inferior != NULL)
873 ptid = thread_to_gdb_id (current_inferior);
875 if (nto_set_thread (ptid))
877 procfs_status status;
879 if (devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status,
880 sizeof (status), 0) == EOK)
884 TRACE ("%s: 0x%08lx\n", __func__, ret);
888 /* We do not currently support non-stop. */
891 nto_supports_non_stop (void)
893 TRACE ("%s\n", __func__);
899 static struct target_ops nto_target_ops = {
912 NULL, /* nto_look_up_symbols */
913 nto_request_interrupt,
917 nto_stopped_by_watchpoint,
918 nto_stopped_data_address,
919 NULL, /* nto_read_offsets */
920 NULL, /* thread_db_set_tls_address */
922 hostio_last_error_from_errno,
923 NULL, /* nto_qxfer_osdata */
924 NULL, /* xfer_siginfo */
925 nto_supports_non_stop,
927 NULL /* start_non_stop */
931 /* Global function called by server.c. Initializes QNX Neutrino
935 initialize_low (void)
939 TRACE ("%s\n", __func__);
940 set_target_ops (&nto_target_ops);
941 set_breakpoint_data (the_low_target.breakpoint,
942 the_low_target.breakpoint_len);
944 /* We use SIGUSR1 to gain control after we block waiting for a process.
945 We use sigwaitevent to wait. */
947 sigaddset (&set, SIGUSR1);
948 sigprocmask (SIG_BLOCK, &set, NULL);