gdb/gdbserver/nto-low.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* QNX Neutrino specific low level interface, for the remote server
  2.    for GDB.
  3.    Copyright (C) 2009-2015 Free Software Foundation, Inc.

  4.    This file is part of GDB.

  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.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */


  15. #include "server.h"
  16. #include "gdbthread.h"
  17. #include "nto-low.h"
  18. #include "hostio.h"

  19. #include <limits.h>
  20. #include <fcntl.h>
  21. #include <spawn.h>
  22. #include <sys/procfs.h>
  23. #include <sys/auxv.h>
  24. #include <sys/iomgr.h>
  25. #include <sys/neutrino.h>


  26. extern int using_threads;
  27. int using_threads = 1;

  28. const struct target_desc *nto_tdesc;

  29. static void
  30. nto_trace (const char *fmt, ...)
  31. {
  32.   va_list arg_list;

  33.   if (debug_threads == 0)
  34.     return;
  35.   fprintf (stderr, "nto:");
  36.   va_start (arg_list, fmt);
  37.   vfprintf (stderr, fmt, arg_list);
  38.   va_end (arg_list);
  39. }

  40. #define TRACE nto_trace

  41. /* Structure holding neutrino specific information about
  42.    inferior.  */

  43. struct nto_inferior
  44. {
  45.   char nto_procfs_path[PATH_MAX];
  46.   int ctl_fd;
  47.   pid_t pid;
  48.   int exit_signo; /* For tracking exit status.  */
  49. };

  50. static struct nto_inferior nto_inferior;

  51. static void
  52. init_nto_inferior (struct nto_inferior *nto_inferior)
  53. {
  54.   memset (nto_inferior, 0, sizeof (struct nto_inferior));
  55.   nto_inferior->ctl_fd = -1;
  56.   nto_inferior->pid = -1;
  57. }

  58. static void
  59. do_detach (void)
  60. {
  61.   if (nto_inferior.ctl_fd != -1)
  62.     {
  63.       nto_trace ("Closing fd\n");
  64.       close (nto_inferior.ctl_fd);
  65.       init_nto_inferior (&nto_inferior);
  66.     }
  67. }

  68. /* Set current thread. Return 1 on success, 0 otherwise.  */

  69. static int
  70. nto_set_thread (ptid_t ptid)
  71. {
  72.   int res = 0;

  73.   TRACE ("%s pid: %d tid: %ld\n", __func__, ptid_get_pid (ptid),
  74.          ptid_get_lwp (ptid));
  75.   if (nto_inferior.ctl_fd != -1
  76.       && !ptid_equal (ptid, null_ptid)
  77.       && !ptid_equal (ptid, minus_one_ptid))
  78.     {
  79.       pthread_t tid = ptid_get_lwp (ptid);

  80.       if (EOK == devctl (nto_inferior.ctl_fd, DCMD_PROC_CURTHREAD, &tid,
  81.           sizeof (tid), 0))
  82.         res = 1;
  83.       else
  84.         TRACE ("%s: Error: failed to set current thread\n", __func__);
  85.     }
  86.   return res;
  87. }

  88. /* This function will determine all alive threads.  Note that we do not list
  89.    dead but unjoined threads even though they are still in the process' thread
  90.    list.

  91.    NTO_INFERIOR must not be NULL.  */

  92. static void
  93. nto_find_new_threads (struct nto_inferior *nto_inferior)
  94. {
  95.   pthread_t tid;

  96.   TRACE ("%s pid:%d\n", __func__, nto_inferior->pid);

  97.   if (nto_inferior->ctl_fd == -1)
  98.     return;

  99.   for (tid = 1;; ++tid)
  100.     {
  101.       procfs_status status;
  102.       ptid_t ptid;
  103.       int err;

  104.       status.tid = tid;
  105.       err = devctl (nto_inferior->ctl_fd, DCMD_PROC_TIDSTATUS, &status,
  106.                     sizeof (status), 0);

  107.       if (err != EOK || status.tid == 0)
  108.         break;

  109.       /* All threads in between are gone.  */
  110.       while (tid != status.tid || status.state == STATE_DEAD)
  111.         {
  112.           struct thread_info *ti;

  113.           ptid = ptid_build (nto_inferior->pid, tid, 0);
  114.           ti = find_thread_ptid (ptid);
  115.           if (ti != NULL)
  116.             {
  117.               TRACE ("Removing thread %d\n", tid);
  118.               remove_thread (ti);
  119.             }
  120.           if (tid == status.tid)
  121.             break;
  122.           ++tid;
  123.         }

  124.       if (status.state != STATE_DEAD)
  125.         {
  126.           TRACE ("Adding thread %d\n", tid);
  127.           ptid = ptid_build (nto_inferior->pid, tid, 0);
  128.           if (!find_thread_ptid (ptid))
  129.             add_thread (ptid, NULL);
  130.         }
  131.     }
  132. }

  133. /* Given pid, open procfs path.  */

  134. static pid_t
  135. do_attach (pid_t pid)
  136. {
  137.   procfs_status status;
  138.   struct sigevent event;

  139.   if (nto_inferior.ctl_fd != -1)
  140.     {
  141.       close (nto_inferior.ctl_fd);
  142.       init_nto_inferior (&nto_inferior);
  143.     }
  144.   xsnprintf (nto_inferior.nto_procfs_path, PATH_MAX - 1, "/proc/%d/as", pid);
  145.   nto_inferior.ctl_fd = open (nto_inferior.nto_procfs_path, O_RDWR);
  146.   if (nto_inferior.ctl_fd == -1)
  147.     {
  148.       TRACE ("Failed to open %s\n", nto_inferior.nto_procfs_path);
  149.       init_nto_inferior (&nto_inferior);
  150.       return -1;
  151.     }
  152.   if (devctl (nto_inferior.ctl_fd, DCMD_PROC_STOP, &status, sizeof (status), 0)
  153.       != EOK)
  154.     {
  155.       do_detach ();
  156.       return -1;
  157.     }
  158.   nto_inferior.pid = pid;
  159.   /* Define a sigevent for process stopped notification.  */
  160.   event.sigev_notify = SIGEV_SIGNAL_THREAD;
  161.   event.sigev_signo = SIGUSR1;
  162.   event.sigev_code = 0;
  163.   event.sigev_value.sival_ptr = NULL;
  164.   event.sigev_priority = -1;
  165.   devctl (nto_inferior.ctl_fd, DCMD_PROC_EVENT, &event, sizeof (event), 0);

  166.   if (devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status),
  167.               0) == EOK
  168.       && (status.flags & _DEBUG_FLAG_STOPPED))
  169.     {
  170.       ptid_t ptid;
  171.       struct process_info *proc;

  172.       kill (pid, SIGCONT);
  173.       ptid = ptid_build (status.pid, status.tid, 0);
  174.       the_low_target.arch_setup ();
  175.       proc = add_process (status.pid, 1);
  176.       proc->tdesc = nto_tdesc;
  177.       TRACE ("Adding thread: pid=%d tid=%ld\n", status.pid,
  178.              ptid_get_lwp (ptid));
  179.       nto_find_new_threads (&nto_inferior);
  180.     }
  181.   else
  182.     {
  183.       do_detach ();
  184.       return -1;
  185.     }

  186.   return pid;
  187. }

  188. /* Read or write LEN bytes from/to inferior's MEMADDR memory address
  189.    into gdbservers's MYADDR buffer.  Return number of bytes actually
  190.    transfered.  */

  191. static int
  192. nto_xfer_memory (off_t memaddr, unsigned char *myaddr, int len,
  193.                  int dowrite)
  194. {
  195.   int nbytes = 0;

  196.   if (lseek (nto_inferior.ctl_fd, memaddr, SEEK_SET) == memaddr)
  197.     {
  198.       if (dowrite)
  199.         nbytes = write (nto_inferior.ctl_fd, myaddr, len);
  200.       else
  201.         nbytes = read (nto_inferior.ctl_fd, myaddr, len);
  202.       if (nbytes < 0)
  203.         nbytes = 0;
  204.     }
  205.   if (nbytes == 0)
  206.     {
  207.       int e = errno;
  208.       TRACE ("Error in %s : errno=%d (%s)\n", __func__, e, strerror (e));
  209.     }
  210.   return nbytes;
  211. }

  212. /* Insert or remove breakpoint or watchpoint at address ADDR.
  213.    TYPE can be one of Neutrino breakpoint types.  SIZE must be 0 for
  214.    inserting the point, -1 for removing it.

  215.    Return 0 on success, 1 otherwise.  */

  216. static int
  217. nto_breakpoint (CORE_ADDR addr, int type, int size)
  218. {
  219.   procfs_break brk;

  220.   brk.type = type;
  221.   brk.addr = addr;
  222.   brk.size = size;
  223.   if (devctl (nto_inferior.ctl_fd, DCMD_PROC_BREAK, &brk, sizeof (brk), 0)
  224.       != EOK)
  225.     return 1;
  226.   return 0;
  227. }

  228. /* Read auxiliary vector from inferior's initial stack into gdbserver's
  229.    MYADDR buffer, up to LEN bytes.

  230.    Return number of bytes read.  */

  231. static int
  232. nto_read_auxv_from_initial_stack (CORE_ADDR initial_stack,
  233.                                   unsigned char *myaddr,
  234.                                   unsigned int len)
  235. {
  236.   int data_ofs = 0;
  237.   int anint;
  238.   unsigned int len_read = 0;

  239.   /* Skip over argc, argv and envp... Comment from ldd.c:

  240.      The startup frame is set-up so that we have:
  241.      auxv
  242.      NULL
  243.      ...
  244.      envp2
  245.      envp1 <----- void *frame + (argc + 2) * sizeof(char *)
  246.      NULL
  247.      ...
  248.      argv2
  249.      argv1
  250.      argc  <------ void * frame

  251.      On entry to ldd, frame gives the address of argc on the stack.  */
  252.   if (nto_xfer_memory (initial_stack, (unsigned char *)&anint,
  253.                        sizeof (anint), 0) != sizeof (anint))
  254.     return 0;

  255.   /* Size of pointer is assumed to be 4 bytes (32 bit arch. ) */
  256.   data_ofs += (anint + 2) * sizeof (void *); /* + 2 comes from argc itself and
  257.                                                 NULL terminating pointer in
  258.                                                 argv.  */

  259.   /* Now loop over env table:  */
  260.   while (nto_xfer_memory (initial_stack + data_ofs,
  261.                           (unsigned char *)&anint, sizeof (anint), 0)
  262.          == sizeof (anint))
  263.     {
  264.       data_ofs += sizeof (anint);
  265.       if (anint == 0)
  266.         break;
  267.     }
  268.   initial_stack += data_ofs;

  269.   memset (myaddr, 0, len);
  270.   while (len_read <= len - sizeof (auxv_t))
  271.     {
  272.       auxv_t *auxv = (auxv_t *)myaddr;

  273.       /* Search backwards until we have read AT_PHDR (num. 3),
  274.          AT_PHENT (num 4), AT_PHNUM (num 5)  */
  275.       if (nto_xfer_memory (initial_stack, (unsigned char *)auxv,
  276.                            sizeof (auxv_t), 0) == sizeof (auxv_t))
  277.         {
  278.           if (auxv->a_type != AT_NULL)
  279.             {
  280.               auxv++;
  281.               len_read += sizeof (auxv_t);
  282.             }
  283.           if (auxv->a_type == AT_PHNUM) /* That's all we need.  */
  284.             break;
  285.           initial_stack += sizeof (auxv_t);
  286.         }
  287.       else
  288.         break;
  289.     }
  290.   TRACE ("auxv: len_read: %d\n", len_read);
  291.   return len_read;
  292. }

  293. /* Start inferior specified by PROGRAM passing arguments ALLARGS.  */

  294. static int
  295. nto_create_inferior (char *program, char **allargs)
  296. {
  297.   struct inheritance inherit;
  298.   pid_t pid;
  299.   sigset_t set;

  300.   TRACE ("%s %s\n", __func__, program);
  301.   /* Clear any pending SIGUSR1's but keep the behavior the same.  */
  302.   signal (SIGUSR1, signal (SIGUSR1, SIG_IGN));

  303.   sigemptyset (&set);
  304.   sigaddset (&set, SIGUSR1);
  305.   sigprocmask (SIG_UNBLOCK, &set, NULL);

  306.   memset (&inherit, 0, sizeof (inherit));
  307.   inherit.flags |= SPAWN_SETGROUP | SPAWN_HOLD;
  308.   inherit.pgroup = SPAWN_NEWPGROUP;
  309.   pid = spawnp (program, 0, NULL, &inherit, allargs, 0);
  310.   sigprocmask (SIG_BLOCK, &set, NULL);

  311.   if (pid == -1)
  312.     return -1;

  313.   if (do_attach (pid) != pid)
  314.     return -1;

  315.   return pid;
  316. }

  317. /* Attach to process PID.  */

  318. static int
  319. nto_attach (unsigned long pid)
  320. {
  321.   TRACE ("%s %ld\n", __func__, pid);
  322.   if (do_attach (pid) != pid)
  323.     error ("Unable to attach to %ld\n", pid);
  324.   return 0;
  325. }

  326. /* Send signal to process PID.  */

  327. static int
  328. nto_kill (int pid)
  329. {
  330.   TRACE ("%s %d\n", __func__, pid);
  331.   kill (pid, SIGKILL);
  332.   do_detach ();
  333.   return 0;
  334. }

  335. /* Detach from process PID.  */

  336. static int
  337. nto_detach (int pid)
  338. {
  339.   TRACE ("%s %d\n", __func__, pid);
  340.   do_detach ();
  341.   return 0;
  342. }

  343. static void
  344. nto_mourn (struct process_info *process)
  345. {
  346.   remove_process (process);
  347. }

  348. /* Check if the given thread is alive.

  349.    Return 1 if alive, 0 otherwise.  */

  350. static int
  351. nto_thread_alive (ptid_t ptid)
  352. {
  353.   int res;

  354.   TRACE ("%s pid:%d tid:%d\n", __func__, ptid_get_pid (ptid),
  355.          ptid_get_lwp (ptid));
  356.   if (SignalKill (0, ptid_get_pid (ptid), ptid_get_lwp (ptid),
  357.                   0, 0, 0) == -1)
  358.     res = 0;
  359.   else
  360.     res = 1;
  361.   TRACE ("%s: %s\n", __func__, res ? "yes" : "no");
  362.   return res;
  363. }

  364. /* Resume inferior's execution.  */

  365. static void
  366. nto_resume (struct thread_resume *resume_info, size_t n)
  367. {
  368.   /* We can only work in all-stop mode.  */
  369.   procfs_status status;
  370.   procfs_run run;
  371.   int err;

  372.   TRACE ("%s\n", __func__);
  373.   /* Workaround for aliasing rules violation. */
  374.   sigset_t *run_fault = (sigset_t *) (void *) &run.fault;

  375.   nto_set_thread (resume_info->thread);

  376.   run.flags = _DEBUG_RUN_FAULT | _DEBUG_RUN_TRACE;
  377.   if (resume_info->kind == resume_step)
  378.     run.flags |= _DEBUG_RUN_STEP;
  379.   run.flags |= _DEBUG_RUN_ARM;

  380.   sigemptyset (run_fault);
  381.   sigaddset (run_fault, FLTBPT);
  382.   sigaddset (run_fault, FLTTRACE);
  383.   sigaddset (run_fault, FLTILL);
  384.   sigaddset (run_fault, FLTPRIV);
  385.   sigaddset (run_fault, FLTBOUNDS);
  386.   sigaddset (run_fault, FLTIOVF);
  387.   sigaddset (run_fault, FLTIZDIV);
  388.   sigaddset (run_fault, FLTFPE);
  389.   sigaddset (run_fault, FLTPAGE);
  390.   sigaddset (run_fault, FLTSTACK);
  391.   sigaddset (run_fault, FLTACCESS);

  392.   sigemptyset (&run.trace);
  393.   if (resume_info->sig)
  394.     {
  395.       int signal_to_pass;

  396.       devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status),
  397.               0);
  398.       signal_to_pass = resume_info->sig;
  399.       if (status.why & (_DEBUG_WHY_SIGNALLED | _DEBUG_WHY_FAULTED))
  400.         {
  401.           if (signal_to_pass != status.info.si_signo)
  402.             {
  403.               kill (status.pid, signal_to_pass);
  404.               run.flags |= _DEBUG_RUN_CLRFLT | _DEBUG_RUN_CLRSIG;
  405.             }
  406.           else                /* Let it kill the program without telling us.  */
  407.             sigdelset (&run.trace, signal_to_pass);
  408.         }
  409.     }
  410.   else
  411.     run.flags |= _DEBUG_RUN_CLRSIG | _DEBUG_RUN_CLRFLT;

  412.   sigfillset (&run.trace);

  413.   regcache_invalidate ();

  414.   err = devctl (nto_inferior.ctl_fd, DCMD_PROC_RUN, &run, sizeof (run), 0);
  415.   if (err != EOK)
  416.     TRACE ("Error: %d \"%s\"\n", err, strerror (err));
  417. }

  418. /* Wait for inferior's event.

  419.    Return ptid of thread that caused the event.  */

  420. static ptid_t
  421. nto_wait (ptid_t ptid,
  422.           struct target_waitstatus *ourstatus, int target_options)
  423. {
  424.   sigset_t set;
  425.   siginfo_t info;
  426.   procfs_status status;
  427.   const int trace_mask = (_DEBUG_FLAG_TRACE_EXEC | _DEBUG_FLAG_TRACE_RD
  428.                           | _DEBUG_FLAG_TRACE_WR | _DEBUG_FLAG_TRACE_MODIFY);

  429.   TRACE ("%s\n", __func__);

  430.   ourstatus->kind = TARGET_WAITKIND_SPURIOUS;

  431.   sigemptyset (&set);
  432.   sigaddset (&set, SIGUSR1);

  433.   devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status), 0);
  434.   while (!(status.flags & _DEBUG_FLAG_ISTOP))
  435.     {
  436.       sigwaitinfo (&set, &info);
  437.       devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status, sizeof (status),
  438.               0);
  439.     }
  440.   nto_find_new_threads (&nto_inferior);

  441.   if (status.flags & _DEBUG_FLAG_SSTEP)
  442.     {
  443.       TRACE ("SSTEP\n");
  444.       ourstatus->kind = TARGET_WAITKIND_STOPPED;
  445.       ourstatus->value.sig = GDB_SIGNAL_TRAP;
  446.     }
  447.   /* Was it a breakpoint?  */
  448.   else if (status.flags & trace_mask)
  449.     {
  450.       TRACE ("STOPPED\n");
  451.       ourstatus->kind = TARGET_WAITKIND_STOPPED;
  452.       ourstatus->value.sig = GDB_SIGNAL_TRAP;
  453.     }
  454.   else if (status.flags & _DEBUG_FLAG_ISTOP)
  455.     {
  456.       TRACE ("ISTOP\n");
  457.       switch (status.why)
  458.         {
  459.         case _DEBUG_WHY_SIGNALLED:
  460.           TRACE ("  SIGNALLED\n");
  461.           ourstatus->kind = TARGET_WAITKIND_STOPPED;
  462.           ourstatus->value.sig =
  463.             gdb_signal_from_host (status.info.si_signo);
  464.           nto_inferior.exit_signo = ourstatus->value.sig;
  465.           break;
  466.         case _DEBUG_WHY_FAULTED:
  467.           TRACE ("  FAULTED\n");
  468.           ourstatus->kind = TARGET_WAITKIND_STOPPED;
  469.           if (status.info.si_signo == SIGTRAP)
  470.             {
  471.               ourstatus->value.sig = 0;
  472.               nto_inferior.exit_signo = 0;
  473.             }
  474.           else
  475.             {
  476.               ourstatus->value.sig =
  477.                 gdb_signal_from_host (status.info.si_signo);
  478.               nto_inferior.exit_signo = ourstatus->value.sig;
  479.             }
  480.           break;

  481.         case _DEBUG_WHY_TERMINATED:
  482.           {
  483.             int waitval = 0;

  484.             TRACE ("  TERMINATED\n");
  485.             waitpid (ptid_get_pid (ptid), &waitval, WNOHANG);
  486.             if (nto_inferior.exit_signo)
  487.               {
  488.                 /* Abnormal death.  */
  489.                 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
  490.                 ourstatus->value.sig = nto_inferior.exit_signo;
  491.               }
  492.             else
  493.               {
  494.                 /* Normal death.  */
  495.                 ourstatus->kind = TARGET_WAITKIND_EXITED;
  496.                 ourstatus->value.integer = WEXITSTATUS (waitval);
  497.               }
  498.             nto_inferior.exit_signo = 0;
  499.             break;
  500.           }

  501.         case _DEBUG_WHY_REQUESTED:
  502.           TRACE ("REQUESTED\n");
  503.           /* We are assuming a requested stop is due to a SIGINT.  */
  504.           ourstatus->kind = TARGET_WAITKIND_STOPPED;
  505.           ourstatus->value.sig = GDB_SIGNAL_INT;
  506.           nto_inferior.exit_signo = 0;
  507.           break;
  508.         }
  509.     }

  510.   return ptid_build (status.pid, status.tid, 0);
  511. }

  512. /* Fetch inferior's registers for currently selected thread (CURRENT_INFERIOR).
  513.    If REGNO is -1, fetch all registers, or REGNO register only otherwise.  */

  514. static void
  515. nto_fetch_registers (struct regcache *regcache, int regno)
  516. {
  517.   int regsize;
  518.   procfs_greg greg;
  519.   ptid_t ptid;

  520.   TRACE ("%s (regno=%d)\n", __func__, regno);
  521.   if (regno >= the_low_target.num_regs)
  522.     return;

  523.   if (current_thread == NULL)
  524.     {
  525.       TRACE ("current_thread is NULL\n");
  526.       return;
  527.     }
  528.   ptid = thread_to_gdb_id (current_thread);
  529.   if (!nto_set_thread (ptid))
  530.     return;

  531.   if (devctl (nto_inferior.ctl_fd, DCMD_PROC_GETGREG, &greg, sizeof (greg),
  532.               &regsize) == EOK)
  533.     {
  534.       if (regno == -1) /* All registers. */
  535.         {
  536.           for (regno = 0; regno != the_low_target.num_regs; ++regno)
  537.             {
  538.               const unsigned int registeroffset
  539.                 = the_low_target.register_offset (regno);
  540.               supply_register (regcache, regno,
  541.                                ((char *)&greg) + registeroffset);
  542.             }
  543.         }
  544.       else
  545.         {
  546.           const unsigned int registeroffset
  547.             = the_low_target.register_offset (regno);
  548.           if (registeroffset == -1)
  549.             return;
  550.           supply_register (regcache, regno, ((char *)&greg) + registeroffset);
  551.         }
  552.     }
  553.   else
  554.     TRACE ("ERROR reading registers from inferior.\n");
  555. }

  556. /* Store registers for currently selected thread (CURRENT_INFERIOR).
  557.    We always store all registers, regardless of REGNO.  */

  558. static void
  559. nto_store_registers (struct regcache *regcache, int regno)
  560. {
  561.   procfs_greg greg;
  562.   int err;
  563.   ptid_t ptid;

  564.   TRACE ("%s (regno:%d)\n", __func__, regno);

  565.   if (current_thread == NULL)
  566.     {
  567.       TRACE ("current_thread is NULL\n");
  568.       return;
  569.     }
  570.   ptid = thread_to_gdb_id (current_thread);
  571.   if (!nto_set_thread (ptid))
  572.     return;

  573.   memset (&greg, 0, sizeof (greg));
  574.   for  (regno = 0; regno != the_low_target.num_regs; ++regno)
  575.     {
  576.       const unsigned int regoffset
  577.         = the_low_target.register_offset (regno);
  578.       collect_register (regcache, regno, ((char *)&greg) + regoffset);
  579.     }
  580.   err = devctl (nto_inferior.ctl_fd, DCMD_PROC_SETGREG, &greg, sizeof (greg),
  581.                 0);
  582.   if (err != EOK)
  583.     TRACE ("Error: setting registers.\n");
  584. }

  585. /* Read LEN bytes from inferior's memory address MEMADDR into
  586.    gdbserver's MYADDR buffer.

  587.    Return 0 on success -1 otherwise.  */

  588. static int
  589. nto_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
  590. {
  591.   TRACE ("%s memaddr:0x%08lx, len:%d\n", __func__, memaddr, len);

  592.   if (nto_xfer_memory (memaddr, myaddr, len, 0) != len)
  593.     {
  594.       TRACE ("Failed to read memory\n");
  595.       return -1;
  596.     }

  597.   return 0;
  598. }

  599. /* Write LEN bytes from gdbserver's buffer MYADDR into inferior's
  600.    memory at address MEMADDR.

  601.    Return 0 on success -1 otherwise.  */

  602. static int
  603. nto_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
  604. {
  605.   int len_written;

  606.   TRACE ("%s memaddr: 0x%08llx len: %d\n", __func__, memaddr, len);
  607.   if ((len_written = nto_xfer_memory (memaddr, (unsigned char *)myaddr, len,
  608.                                       1))
  609.       != len)
  610.     {
  611.       TRACE ("Wanted to write: %d but written: %d\n", len, len_written);
  612.       return -1;
  613.     }

  614.   return 0;
  615. }

  616. /* Stop inferior.  We always stop all threads.  */

  617. static void
  618. nto_request_interrupt (void)
  619. {
  620.   TRACE ("%s\n", __func__);
  621.   nto_set_thread (ptid_build (nto_inferior.pid, 1, 0));
  622.   if (EOK != devctl (nto_inferior.ctl_fd, DCMD_PROC_STOP, NULL, 0, 0))
  623.     TRACE ("Error stopping inferior.\n");
  624. }

  625. /* Read auxiliary vector from inferior's memory into gdbserver's buffer
  626.    MYADDR.  We always read whole auxv.

  627.    Return number of bytes stored in MYADDR buffer, 0 if OFFSET > 0
  628.    or -1 on error.  */

  629. static int
  630. nto_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len)
  631. {
  632.   int err;
  633.   CORE_ADDR initial_stack;
  634.   procfs_info procinfo;

  635.   TRACE ("%s\n", __func__);
  636.   if (offset > 0)
  637.     return 0;

  638.   err = devctl (nto_inferior.ctl_fd, DCMD_PROC_INFO, &procinfo,
  639.                 sizeof procinfo, 0);
  640.   if (err != EOK)
  641.     return -1;

  642.   initial_stack = procinfo.initial_stack;

  643.   return nto_read_auxv_from_initial_stack (initial_stack, myaddr, len);
  644. }

  645. static int
  646. nto_supports_z_point_type (char z_type)
  647. {
  648.   switch (z_type)
  649.     {
  650.     case Z_PACKET_SW_BP:
  651.     case Z_PACKET_HW_BP:
  652.     case Z_PACKET_WRITE_WP:
  653.     case Z_PACKET_READ_WP:
  654.     case Z_PACKET_ACCESS_WP:
  655.       return 1;
  656.     default:
  657.       return 0;
  658.     }
  659. }

  660. /* Insert {break/watch}point at address ADDR.  SIZE is not used.  */

  661. static int
  662. nto_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
  663.                   int size, struct raw_breakpoint *bp)
  664. {
  665.   int wtype = _DEBUG_BREAK_HW; /* Always request HW.  */

  666.   TRACE ("%s type:%c addr: 0x%08lx len:%d\n", __func__, (int)type, addr, len);
  667.   switch (type)
  668.     {
  669.     case raw_bkpt_type_sw:
  670.       wtype = _DEBUG_BREAK_EXEC;
  671.       break;
  672.     case raw_bkpt_type_hw:
  673.       wtype |= _DEBUG_BREAK_EXEC;
  674.       break;
  675.     case raw_bkpt_type_write_wp:
  676.       wtype |= _DEBUG_BREAK_RW;
  677.       break;
  678.     case raw_bkpt_type_read_wp:
  679.       wtype |= _DEBUG_BREAK_RD;
  680.       break;
  681.     case raw_bkpt_type_access_wp:
  682.       wtype |= _DEBUG_BREAK_RW;
  683.       break;
  684.     default:
  685.       return 1; /* Not supported.  */
  686.     }
  687.   return nto_breakpoint (addr, wtype, 0);
  688. }

  689. /* Remove {break/watch}point at address ADDR.  SIZE is not used.  */

  690. static int
  691. nto_remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
  692.                   int size, struct raw_breakpoint *bp)
  693. {
  694.   int wtype = _DEBUG_BREAK_HW; /* Always request HW.  */

  695.   TRACE ("%s type:%c addr: 0x%08lx len:%d\n", __func__, (int)type, addr, len);
  696.   switch (type)
  697.     {
  698.     case raw_bkpt_type_sw:
  699.       wtype = _DEBUG_BREAK_EXEC;
  700.       break;
  701.     case raw_bkpt_type_hw:
  702.       wtype |= _DEBUG_BREAK_EXEC;
  703.       break;
  704.     case raw_bkpt_type_write_wp:
  705.       wtype |= _DEBUG_BREAK_RW;
  706.       break;
  707.     case raw_bkpt_type_read_wp:
  708.       wtype |= _DEBUG_BREAK_RD;
  709.       break;
  710.     case raw_bkpt_type_access_wp:
  711.       wtype |= _DEBUG_BREAK_RW;
  712.       break;
  713.     default:
  714.       return 1; /* Not supported.  */
  715.     }
  716.   return nto_breakpoint (addr, wtype, -1);
  717. }

  718. /* Check if the reason of stop for current thread (CURRENT_INFERIOR) is
  719.    a watchpoint.

  720.    Return 1 if stopped by watchpoint, 0 otherwise.  */

  721. static int
  722. nto_stopped_by_watchpoint (void)
  723. {
  724.   int ret = 0;

  725.   TRACE ("%s\n", __func__);
  726.   if (nto_inferior.ctl_fd != -1 && current_thread != NULL)
  727.     {
  728.       ptid_t ptid;

  729.       ptid = thread_to_gdb_id (current_thread);
  730.       if (nto_set_thread (ptid))
  731.         {
  732.           const int watchmask = _DEBUG_FLAG_TRACE_RD | _DEBUG_FLAG_TRACE_WR
  733.                                 | _DEBUG_FLAG_TRACE_MODIFY;
  734.           procfs_status status;
  735.           int err;

  736.           err = devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status,
  737.                         sizeof (status), 0);
  738.           if (err == EOK && (status.flags & watchmask))
  739.             ret = 1;
  740.         }
  741.     }
  742.   TRACE ("%s: %s\n", __func__, ret ? "yes" : "no");
  743.   return ret;
  744. }

  745. /* Get instruction pointer for CURRENT_INFERIOR thread.

  746.    Return inferior's instruction pointer value, or 0 on error.  */

  747. static CORE_ADDR
  748. nto_stopped_data_address (void)
  749. {
  750.   CORE_ADDR ret = (CORE_ADDR)0;

  751.   TRACE ("%s\n", __func__);
  752.   if (nto_inferior.ctl_fd != -1 && current_thread != NULL)
  753.     {
  754.       ptid_t ptid;

  755.       ptid = thread_to_gdb_id (current_thread);

  756.       if (nto_set_thread (ptid))
  757.         {
  758.           procfs_status status;

  759.           if (devctl (nto_inferior.ctl_fd, DCMD_PROC_STATUS, &status,
  760.                       sizeof (status), 0) == EOK)
  761.             ret = status.ip;
  762.         }
  763.     }
  764.   TRACE ("%s: 0x%08lx\n", __func__, ret);
  765.   return ret;
  766. }

  767. /* We do not currently support non-stop.  */

  768. static int
  769. nto_supports_non_stop (void)
  770. {
  771.   TRACE ("%s\n", __func__);
  772.   return 0;
  773. }



  774. static struct target_ops nto_target_ops = {
  775.   nto_create_inferior,
  776.   nto_attach,
  777.   nto_kill,
  778.   nto_detach,
  779.   nto_mourn,
  780.   NULL, /* nto_join */
  781.   nto_thread_alive,
  782.   nto_resume,
  783.   nto_wait,
  784.   nto_fetch_registers,
  785.   nto_store_registers,
  786.   NULL, /* prepare_to_access_memory */
  787.   NULL, /* done_accessing_memory */
  788.   nto_read_memory,
  789.   nto_write_memory,
  790.   NULL, /* nto_look_up_symbols */
  791.   nto_request_interrupt,
  792.   nto_read_auxv,
  793.   nto_supports_z_point_type,
  794.   nto_insert_point,
  795.   nto_remove_point,
  796.   nto_stopped_by_watchpoint,
  797.   nto_stopped_data_address,
  798.   NULL, /* nto_read_offsets */
  799.   NULL, /* thread_db_set_tls_address */
  800.   NULL,
  801.   hostio_last_error_from_errno,
  802.   NULL, /* nto_qxfer_osdata */
  803.   NULL, /* xfer_siginfo */
  804.   nto_supports_non_stop,
  805.   NULL, /* async */
  806.   NULL  /* start_non_stop */
  807. };


  808. /* Global function called by server.c.  Initializes QNX Neutrino
  809.    gdbserver.  */

  810. void
  811. initialize_low (void)
  812. {
  813.   sigset_t set;

  814.   TRACE ("%s\n", __func__);
  815.   set_target_ops (&nto_target_ops);
  816.   set_breakpoint_data (the_low_target.breakpoint,
  817.                        the_low_target.breakpoint_len);

  818.   /* We use SIGUSR1 to gain control after we block waiting for a process.
  819.      We use sigwaitevent to wait.  */
  820.   sigemptyset (&set);
  821.   sigaddset (&set, SIGUSR1);
  822.   sigprocmask (SIG_BLOCK, &set, NULL);
  823. }