gdb/gdbserver/lynx-low.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Copyright (C) 2009-2015 Free Software Foundation, Inc.

  2.    This file is part of GDB.

  3.    This program is free software; you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation; either version 3 of the License, or
  6.    (at your option) any later version.

  7.    This program is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.    GNU General Public License for more details.

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

  13. #include "server.h"
  14. #include "target.h"
  15. #include "lynx-low.h"

  16. #include <limits.h>
  17. #include <sys/ptrace.h>
  18. #include <sys/piddef.h> /* Provides PIDGET, TIDGET, BUILDPID, etc.  */
  19. #include <unistd.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/types.h>
  22. #include "gdb_wait.h"
  23. #include <signal.h>
  24. #include "filestuff.h"

  25. int using_threads = 1;

  26. const struct target_desc *lynx_tdesc;

  27. /* Per-process private data.  */

  28. struct process_info_private
  29. {
  30.   /* The PTID obtained from the last wait performed on this process.
  31.      Initialized to null_ptid until the first wait is performed.  */
  32.   ptid_t last_wait_event_ptid;
  33. };

  34. /* Print a debug trace on standard output if debug_threads is set.  */

  35. static void
  36. lynx_debug (char *string, ...)
  37. {
  38.   va_list args;

  39.   if (!debug_threads)
  40.     return;

  41.   va_start (args, string);
  42.   fprintf (stderr, "DEBUG(lynx): ");
  43.   vfprintf (stderr, string, args);
  44.   fprintf (stderr, "\n");
  45.   va_end (args);
  46. }

  47. /* Build a ptid_t given a PID and a LynxOS TID.  */

  48. static ptid_t
  49. lynx_ptid_build (int pid, long tid)
  50. {
  51.   /* brobecker/2010-06-21: It looks like the LWP field in ptids
  52.      should be distinct for each thread (see write_ptid where it
  53.      writes the thread ID from the LWP).  So instead of storing
  54.      the LynxOS tid in the tid field of the ptid, we store it in
  55.      the lwp field.  */
  56.   return ptid_build (pid, tid, 0);
  57. }

  58. /* Return the process ID of the given PTID.

  59.    This function has little reason to exist, it's just a wrapper around
  60.    ptid_get_pid.  But since we have a getter function for the lynxos
  61.    ptid, it feels cleaner to have a getter for the pid as well.  */

  62. static int
  63. lynx_ptid_get_pid (ptid_t ptid)
  64. {
  65.   return ptid_get_pid (ptid);
  66. }

  67. /* Return the LynxOS tid of the given PTID.  */

  68. static long
  69. lynx_ptid_get_tid (ptid_t ptid)
  70. {
  71.   /* See lynx_ptid_build: The LynxOS tid is stored inside the lwp field
  72.      of the ptid.  */
  73.   return ptid_get_lwp (ptid);
  74. }

  75. /* For a given PTID, return the associated PID as known by the LynxOS
  76.    ptrace layer.  */

  77. static int
  78. lynx_ptrace_pid_from_ptid (ptid_t ptid)
  79. {
  80.   return BUILDPID (lynx_ptid_get_pid (ptid), lynx_ptid_get_tid (ptid));
  81. }

  82. /* Return a string image of the ptrace REQUEST number.  */

  83. static char *
  84. ptrace_request_to_str (int request)
  85. {
  86. #define CASE(X) case X: return #X
  87.   switch (request)
  88.     {
  89.       CASE(PTRACE_TRACEME);
  90.       CASE(PTRACE_PEEKTEXT);
  91.       CASE(PTRACE_PEEKDATA);
  92.       CASE(PTRACE_PEEKUSER);
  93.       CASE(PTRACE_POKETEXT);
  94.       CASE(PTRACE_POKEDATA);
  95.       CASE(PTRACE_POKEUSER);
  96.       CASE(PTRACE_CONT);
  97.       CASE(PTRACE_KILL);
  98.       CASE(PTRACE_SINGLESTEP);
  99.       CASE(PTRACE_ATTACH);
  100.       CASE(PTRACE_DETACH);
  101.       CASE(PTRACE_GETREGS);
  102.       CASE(PTRACE_SETREGS);
  103.       CASE(PTRACE_GETFPREGS);
  104.       CASE(PTRACE_SETFPREGS);
  105.       CASE(PTRACE_READDATA);
  106.       CASE(PTRACE_WRITEDATA);
  107.       CASE(PTRACE_READTEXT);
  108.       CASE(PTRACE_WRITETEXT);
  109.       CASE(PTRACE_GETFPAREGS);
  110.       CASE(PTRACE_SETFPAREGS);
  111.       CASE(PTRACE_GETWINDOW);
  112.       CASE(PTRACE_SETWINDOW);
  113.       CASE(PTRACE_SYSCALL);
  114.       CASE(PTRACE_DUMPCORE);
  115.       CASE(PTRACE_SETWRBKPT);
  116.       CASE(PTRACE_SETACBKPT);
  117.       CASE(PTRACE_CLRBKPT);
  118.       CASE(PTRACE_GET_UCODE);
  119. #ifdef PT_READ_GPR
  120.       CASE(PT_READ_GPR);
  121. #endif
  122. #ifdef PT_WRITE_GPR
  123.       CASE(PT_WRITE_GPR);
  124. #endif
  125. #ifdef PT_READ_FPR
  126.       CASE(PT_READ_FPR);
  127. #endif
  128. #ifdef PT_WRITE_FPR
  129.       CASE(PT_WRITE_FPR);
  130. #endif
  131. #ifdef PT_READ_VPR
  132.       CASE(PT_READ_VPR);
  133. #endif
  134. #ifdef PT_WRITE_VPR
  135.       CASE(PT_WRITE_VPR);
  136. #endif
  137. #ifdef PTRACE_PEEKUSP
  138.       CASE(PTRACE_PEEKUSP);
  139. #endif
  140. #ifdef PTRACE_POKEUSP
  141.       CASE(PTRACE_POKEUSP);
  142. #endif
  143.       CASE(PTRACE_PEEKTHREAD);
  144.       CASE(PTRACE_THREADUSER);
  145.       CASE(PTRACE_FPREAD);
  146.       CASE(PTRACE_FPWRITE);
  147.       CASE(PTRACE_SETSIG);
  148.       CASE(PTRACE_CONT_ONE);
  149.       CASE(PTRACE_KILL_ONE);
  150.       CASE(PTRACE_SINGLESTEP_ONE);
  151.       CASE(PTRACE_GETLOADINFO);
  152.       CASE(PTRACE_GETTRACESIG);
  153. #ifdef PTRACE_GETTHREADLIST
  154.       CASE(PTRACE_GETTHREADLIST);
  155. #endif
  156.     }
  157. #undef CASE

  158.   return "<unknown-request>";
  159. }

  160. /* A wrapper around ptrace that allows us to print debug traces of
  161.    ptrace calls if debug traces are activated.  */

  162. static int
  163. lynx_ptrace (int request, ptid_t ptid, int addr, int data, int addr2)
  164. {
  165.   int result;
  166.   const int pid = lynx_ptrace_pid_from_ptid (ptid);
  167.   int saved_errno;

  168.   if (debug_threads)
  169.     fprintf (stderr, "PTRACE (%s, pid=%d(pid=%d, tid=%d), addr=0x%x, "
  170.              "data=0x%x, addr2=0x%x)",
  171.              ptrace_request_to_str (request), pid, PIDGET (pid), TIDGET (pid),
  172.              addr, data, addr2);
  173.   result = ptrace (request, pid, addr, data, addr2);
  174.   saved_errno = errno;
  175.   if (debug_threads)
  176.     fprintf (stderr, " -> %d (=0x%x)\n", result, result);

  177.   errno = saved_errno;
  178.   return result;
  179. }

  180. /* Call add_process with the given parameters, and initializes
  181.    the process' private data.  */

  182. static struct process_info *
  183. lynx_add_process (int pid, int attached)
  184. {
  185.   struct process_info *proc;

  186.   proc = add_process (pid, attached);
  187.   proc->tdesc = lynx_tdesc;
  188.   proc->private = xcalloc (1, sizeof (*proc->private));
  189.   proc->private->last_wait_event_ptid = null_ptid;

  190.   return proc;
  191. }

  192. /* Implement the create_inferior method of the target_ops vector.  */

  193. static int
  194. lynx_create_inferior (char *program, char **allargs)
  195. {
  196.   int pid;

  197.   lynx_debug ("lynx_create_inferior ()");

  198.   pid = fork ();
  199.   if (pid < 0)
  200.     perror_with_name ("fork");

  201.   if (pid == 0)
  202.     {
  203.       int pgrp;

  204.       close_most_fds ();

  205.       /* Switch child to its own process group so that signals won't
  206.          directly affect gdbserver. */
  207.       pgrp = getpid();
  208.       setpgid (0, pgrp);
  209.       ioctl (0, TIOCSPGRP, &pgrp);
  210.       lynx_ptrace (PTRACE_TRACEME, null_ptid, 0, 0, 0);
  211.       execv (program, allargs);
  212.       fprintf (stderr, "Cannot exec %s: %s.\n", program, strerror (errno));
  213.       fflush (stderr);
  214.       _exit (0177);
  215.     }

  216.   lynx_add_process (pid, 0);
  217.   /* Do not add the process thread just yet, as we do not know its tid.
  218.      We will add it later, during the wait for the STOP event corresponding
  219.      to the lynx_ptrace (PTRACE_TRACEME) call above.  */
  220.   return pid;
  221. }

  222. /* Assuming we've just attached to a running inferior whose pid is PID,
  223.    add all threads running in that process.  */

  224. static void
  225. lynx_add_threads_after_attach (int pid)
  226. {
  227.   /* Ugh!  There appears to be no way to get the list of threads
  228.      in the program we just attached to.  So get the list by calling
  229.      the "ps" command.  This is only needed now, as we will then
  230.      keep the thread list up to date thanks to thread creation and
  231.      exit notifications.  */
  232.   FILE *f;
  233.   char buf[256];
  234.   int thread_pid, thread_tid;

  235.   f = popen ("ps atx", "r");
  236.   if (f == NULL)
  237.     perror_with_name ("Cannot get thread list");

  238.   while (fgets (buf, sizeof (buf), f) != NULL)
  239.     if ((sscanf (buf, "%d %d", &thread_pid, &thread_tid) == 2
  240.          && thread_pid == pid))
  241.     {
  242.       ptid_t thread_ptid = lynx_ptid_build (pid, thread_tid);

  243.       if (!find_thread_ptid (thread_ptid))
  244.         {
  245.           lynx_debug ("New thread: (pid = %d, tid = %d)",
  246.                       pid, thread_tid);
  247.           add_thread (thread_ptid, NULL);
  248.         }
  249.     }

  250.   pclose (f);
  251. }

  252. /* Implement the attach target_ops method.  */

  253. static int
  254. lynx_attach (unsigned long pid)
  255. {
  256.   ptid_t ptid = lynx_ptid_build (pid, 0);

  257.   if (lynx_ptrace (PTRACE_ATTACH, ptid, 0, 0, 0) != 0)
  258.     error ("Cannot attach to process %lu: %s (%d)\n", pid,
  259.            strerror (errno), errno);

  260.   lynx_add_process (pid, 1);
  261.   lynx_add_threads_after_attach (pid);

  262.   return 0;
  263. }

  264. /* Implement the resume target_ops method.  */

  265. static void
  266. lynx_resume (struct thread_resume *resume_info, size_t n)
  267. {
  268.   ptid_t ptid = resume_info[0].thread;
  269.   const int request
  270.     = (resume_info[0].kind == resume_step
  271.        ? (n == 1 ? PTRACE_SINGLESTEP_ONE : PTRACE_SINGLESTEP)
  272.        : PTRACE_CONT);
  273.   const int signal = resume_info[0].sig;

  274.   /* If given a minus_one_ptid, then try using the current_process'
  275.      private->last_wait_event_ptid.  On most LynxOS versions,
  276.      using any of the process' thread works well enough, but
  277.      LynxOS 178 is a little more sensitive, and triggers some
  278.      unexpected signals (Eg SIG61) when we resume the inferior
  279.      using a different thread.  */
  280.   if (ptid_equal (ptid, minus_one_ptid))
  281.     ptid = current_process()->private->last_wait_event_ptid;

  282.   /* The ptid might still be minus_one_ptid; this can happen between
  283.      the moment we create the inferior or attach to a process, and
  284.      the moment we resume its execution for the first time.  It is
  285.      fine to use the current_thread's ptid in those cases.  */
  286.   if (ptid_equal (ptid, minus_one_ptid))
  287.     ptid = thread_to_gdb_id (current_thread);

  288.   regcache_invalidate ();

  289.   errno = 0;
  290.   lynx_ptrace (request, ptid, 1, signal, 0);
  291.   if (errno)
  292.     perror_with_name ("ptrace");
  293. }

  294. /* Resume the execution of the given PTID.  */

  295. static void
  296. lynx_continue (ptid_t ptid)
  297. {
  298.   struct thread_resume resume_info;

  299.   resume_info.thread = ptid;
  300.   resume_info.kind = resume_continue;
  301.   resume_info.sig = 0;

  302.   lynx_resume (&resume_info, 1);
  303. }

  304. /* A wrapper around waitpid that handles the various idiosyncrasies
  305.    of LynxOS' waitpid.  */

  306. static int
  307. lynx_waitpid (int pid, int *stat_loc)
  308. {
  309.   int ret = 0;

  310.   while (1)
  311.     {
  312.       ret = waitpid (pid, stat_loc, WNOHANG);
  313.       if (ret < 0)
  314.         {
  315.           /* An ECHILD error is not indicative of a real problem.
  316.              It happens for instance while waiting for the inferior
  317.              to stop after attaching to it.  */
  318.           if (errno != ECHILD)
  319.             perror_with_name ("waitpid (WNOHANG)");
  320.         }
  321.       if (ret > 0)
  322.         break;
  323.       /* No event with WNOHANG.  See if there is one with WUNTRACED.  */
  324.       ret = waitpid (pid, stat_loc, WNOHANG | WUNTRACED);
  325.       if (ret < 0)
  326.         {
  327.           /* An ECHILD error is not indicative of a real problem.
  328.              It happens for instance while waiting for the inferior
  329.              to stop after attaching to it.  */
  330.           if (errno != ECHILD)
  331.             perror_with_name ("waitpid (WNOHANG|WUNTRACED)");
  332.         }
  333.       if (ret > 0)
  334.         break;
  335.       usleep (1000);
  336.     }
  337.   return ret;
  338. }

  339. /* Implement the wait target_ops method.  */

  340. static ptid_t
  341. lynx_wait_1 (ptid_t ptid, struct target_waitstatus *status, int options)
  342. {
  343.   int pid;
  344.   int ret;
  345.   int wstat;
  346.   ptid_t new_ptid;

  347.   if (ptid_equal (ptid, minus_one_ptid))
  348.     pid = lynx_ptid_get_pid (thread_to_gdb_id (current_thread));
  349.   else
  350.     pid = BUILDPID (lynx_ptid_get_pid (ptid), lynx_ptid_get_tid (ptid));

  351. retry:

  352.   ret = lynx_waitpid (pid, &wstat);
  353.   new_ptid = lynx_ptid_build (ret, ((union wait *) &wstat)->w_tid);
  354.   find_process_pid (ret)->private->last_wait_event_ptid = new_ptid;

  355.   /* If this is a new thread, then add it now.  The reason why we do
  356.      this here instead of when handling new-thread events is because
  357.      we need to add the thread associated to the "main" thread - even
  358.      for non-threaded applications where the new-thread events are not
  359.      generated.  */
  360.   if (!find_thread_ptid (new_ptid))
  361.     {
  362.       lynx_debug ("New thread: (pid = %d, tid = %d)",
  363.                   lynx_ptid_get_pid (new_ptid), lynx_ptid_get_tid (new_ptid));
  364.       add_thread (new_ptid, NULL);
  365.     }

  366.   if (WIFSTOPPED (wstat))
  367.     {
  368.       status->kind = TARGET_WAITKIND_STOPPED;
  369.       status->value.integer = gdb_signal_from_host (WSTOPSIG (wstat));
  370.       lynx_debug ("process stopped with signal: %d",
  371.                   status->value.integer);
  372.     }
  373.   else if (WIFEXITED (wstat))
  374.     {
  375.       status->kind = TARGET_WAITKIND_EXITED;
  376.       status->value.integer = WEXITSTATUS (wstat);
  377.       lynx_debug ("process exited with code: %d", status->value.integer);
  378.     }
  379.   else if (WIFSIGNALED (wstat))
  380.     {
  381.       status->kind = TARGET_WAITKIND_SIGNALLED;
  382.       status->value.integer = gdb_signal_from_host (WTERMSIG (wstat));
  383.       lynx_debug ("process terminated with code: %d",
  384.                   status->value.integer);
  385.     }
  386.   else
  387.     {
  388.       /* Not sure what happened if we get here, or whether we can
  389.          in fact get here.  But if we do, handle the event the best
  390.          we can.  */
  391.       status->kind = TARGET_WAITKIND_STOPPED;
  392.       status->value.integer = gdb_signal_from_host (0);
  393.       lynx_debug ("unknown event ????");
  394.     }

  395.   /* SIGTRAP events are generated for situations other than single-step/
  396.      breakpoint events (Eg. new-thread events).  Handle those other types
  397.      of events, and resume the execution if necessary.  */
  398.   if (status->kind == TARGET_WAITKIND_STOPPED
  399.       && status->value.integer == GDB_SIGNAL_TRAP)
  400.     {
  401.       const int realsig = lynx_ptrace (PTRACE_GETTRACESIG, new_ptid, 0, 0, 0);

  402.       lynx_debug ("(realsig = %d)", realsig);
  403.       switch (realsig)
  404.         {
  405.           case SIGNEWTHREAD:
  406.             /* We just added the new thread above.  No need to do anything
  407.                further.  Just resume the execution again.  */
  408.             lynx_continue (new_ptid);
  409.             goto retry;

  410.           case SIGTHREADEXIT:
  411.             remove_thread (find_thread_ptid (new_ptid));
  412.             lynx_continue (new_ptid);
  413.             goto retry;
  414.         }
  415.     }

  416.   return new_ptid;
  417. }

  418. /* A wrapper around lynx_wait_1 that also prints debug traces when
  419.    such debug traces have been activated.  */

  420. static ptid_t
  421. lynx_wait (ptid_t ptid, struct target_waitstatus *status, int options)
  422. {
  423.   ptid_t new_ptid;

  424.   lynx_debug ("lynx_wait (pid = %d, tid = %ld)",
  425.               lynx_ptid_get_pid (ptid), lynx_ptid_get_tid (ptid));
  426.   new_ptid = lynx_wait_1 (ptid, status, options);
  427.   lynx_debug ("          -> (pid=%d, tid=%ld, status->kind = %d)",
  428.               lynx_ptid_get_pid (new_ptid), lynx_ptid_get_tid (new_ptid),
  429.               status->kind);
  430.   return new_ptid;
  431. }

  432. /* Implement the kill target_ops method.  */

  433. static int
  434. lynx_kill (int pid)
  435. {
  436.   ptid_t ptid = lynx_ptid_build (pid, 0);
  437.   struct target_waitstatus status;
  438.   struct process_info *process;

  439.   process = find_process_pid (pid);
  440.   if (process == NULL)
  441.     return -1;

  442.   lynx_ptrace (PTRACE_KILL, ptid, 0, 0, 0);
  443.   lynx_wait (ptid, &status, 0);
  444.   the_target->mourn (process);
  445.   return 0;
  446. }

  447. /* Implement the detach target_ops method.  */

  448. static int
  449. lynx_detach (int pid)
  450. {
  451.   ptid_t ptid = lynx_ptid_build (pid, 0);
  452.   struct process_info *process;

  453.   process = find_process_pid (pid);
  454.   if (process == NULL)
  455.     return -1;

  456.   lynx_ptrace (PTRACE_DETACH, ptid, 0, 0, 0);
  457.   the_target->mourn (process);
  458.   return 0;
  459. }

  460. /* Implement the mourn target_ops method.  */

  461. static void
  462. lynx_mourn (struct process_info *proc)
  463. {
  464.   /* Free our private data.  */
  465.   free (proc->private);
  466.   proc->private = NULL;

  467.   clear_inferiors ();
  468. }

  469. /* Implement the join target_ops method.  */

  470. static void
  471. lynx_join (int pid)
  472. {
  473.   /* The PTRACE_DETACH is sufficient to detach from the process.
  474.      So no need to do anything extra.  */
  475. }

  476. /* Implement the thread_alive target_ops method.  */

  477. static int
  478. lynx_thread_alive (ptid_t ptid)
  479. {
  480.   /* The list of threads is updated at the end of each wait, so it
  481.      should be up to date.  No need to re-fetch it.  */
  482.   return (find_thread_ptid (ptid) != NULL);
  483. }

  484. /* Implement the fetch_registers target_ops method.  */

  485. static void
  486. lynx_fetch_registers (struct regcache *regcache, int regno)
  487. {
  488.   struct lynx_regset_info *regset = lynx_target_regsets;
  489.   ptid_t inferior_ptid = thread_to_gdb_id (current_thread);

  490.   lynx_debug ("lynx_fetch_registers (regno = %d)", regno);

  491.   while (regset->size >= 0)
  492.     {
  493.       char *buf;
  494.       int res;

  495.       buf = xmalloc (regset->size);
  496.       res = lynx_ptrace (regset->get_request, inferior_ptid, (int) buf, 0, 0);
  497.       if (res < 0)
  498.         perror ("ptrace");
  499.       regset->store_function (regcache, buf);
  500.       free (buf);
  501.       regset++;
  502.     }
  503. }

  504. /* Implement the store_registers target_ops method.  */

  505. static void
  506. lynx_store_registers (struct regcache *regcache, int regno)
  507. {
  508.   struct lynx_regset_info *regset = lynx_target_regsets;
  509.   ptid_t inferior_ptid = thread_to_gdb_id (current_thread);

  510.   lynx_debug ("lynx_store_registers (regno = %d)", regno);

  511.   while (regset->size >= 0)
  512.     {
  513.       char *buf;
  514.       int res;

  515.       buf = xmalloc (regset->size);
  516.       res = lynx_ptrace (regset->get_request, inferior_ptid, (int) buf, 0, 0);
  517.       if (res == 0)
  518.         {
  519.           /* Then overlay our cached registers on that.  */
  520.           regset->fill_function (regcache, buf);
  521.           /* Only now do we write the register set.  */
  522.           res = lynx_ptrace (regset->set_request, inferior_ptid, (int) buf,
  523.                              0, 0);
  524.         }
  525.       if (res < 0)
  526.         perror ("ptrace");
  527.       free (buf);
  528.       regset++;
  529.     }
  530. }

  531. /* Implement the read_memory target_ops method.  */

  532. static int
  533. lynx_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
  534. {
  535.   /* On LynxOS, memory reads needs to be performed in chunks the size
  536.      of int types, and they should also be aligned accordingly.  */
  537.   int buf;
  538.   const int xfer_size = sizeof (buf);
  539.   CORE_ADDR addr = memaddr & -(CORE_ADDR) xfer_size;
  540.   ptid_t inferior_ptid = thread_to_gdb_id (current_thread);

  541.   while (addr < memaddr + len)
  542.     {
  543.       int skip = 0;
  544.       int truncate = 0;

  545.       errno = 0;
  546.       if (addr < memaddr)
  547.         skip = memaddr - addr;
  548.       if (addr + xfer_size > memaddr + len)
  549.         truncate = addr + xfer_size - memaddr - len;
  550.       buf = lynx_ptrace (PTRACE_PEEKTEXT, inferior_ptid, addr, 0, 0);
  551.       if (errno)
  552.         return errno;
  553.       memcpy (myaddr + (addr - memaddr) + skip, (gdb_byte *) &buf + skip,
  554.               xfer_size - skip - truncate);
  555.       addr += xfer_size;
  556.     }

  557.   return 0;
  558. }

  559. /* Implement the write_memory target_ops method.  */

  560. static int
  561. lynx_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
  562. {
  563.   /* On LynxOS, memory writes needs to be performed in chunks the size
  564.      of int types, and they should also be aligned accordingly.  */
  565.   int buf;
  566.   const int xfer_size = sizeof (buf);
  567.   CORE_ADDR addr = memaddr & -(CORE_ADDR) xfer_size;
  568.   ptid_t inferior_ptid = thread_to_gdb_id (current_thread);

  569.   while (addr < memaddr + len)
  570.     {
  571.       int skip = 0;
  572.       int truncate = 0;

  573.       if (addr < memaddr)
  574.         skip = memaddr - addr;
  575.       if (addr + xfer_size > memaddr + len)
  576.         truncate = addr + xfer_size - memaddr - len;
  577.       if (skip > 0 || truncate > 0)
  578.         {
  579.           /* We need to read the memory at this address in order to preserve
  580.              the data that we are not overwriting.  */
  581.           lynx_read_memory (addr, (unsigned char *) &buf, xfer_size);
  582.           if (errno)
  583.             return errno;
  584.         }
  585.       memcpy ((gdb_byte *) &buf + skip, myaddr + (addr - memaddr) + skip,
  586.               xfer_size - skip - truncate);
  587.       errno = 0;
  588.       lynx_ptrace (PTRACE_POKETEXT, inferior_ptid, addr, buf, 0);
  589.       if (errno)
  590.         return errno;
  591.       addr += xfer_size;
  592.     }

  593.   return 0;
  594. }

  595. /* Implement the kill_request target_ops method.  */

  596. static void
  597. lynx_request_interrupt (void)
  598. {
  599.   ptid_t inferior_ptid = thread_to_gdb_id (current_thread);

  600.   kill (lynx_ptid_get_pid (inferior_ptid), SIGINT);
  601. }

  602. /* The LynxOS target_ops vector.  */

  603. static struct target_ops lynx_target_ops = {
  604.   lynx_create_inferior,
  605.   lynx_attach,
  606.   lynx_kill,
  607.   lynx_detach,
  608.   lynx_mourn,
  609.   lynx_join,
  610.   lynx_thread_alive,
  611.   lynx_resume,
  612.   lynx_wait,
  613.   lynx_fetch_registers,
  614.   lynx_store_registers,
  615.   NULL/* prepare_to_access_memory */
  616.   NULL/* done_accessing_memory */
  617.   lynx_read_memory,
  618.   lynx_write_memory,
  619.   NULL/* look_up_symbols */
  620.   lynx_request_interrupt,
  621.   NULL/* read_auxv */
  622.   NULL/* supports_z_point_type */
  623.   NULL/* insert_point */
  624.   NULL/* remove_point */
  625.   NULL/* stopped_by_watchpoint */
  626.   NULL/* stopped_data_address */
  627.   NULL/* read_offsets */
  628.   NULL/* get_tls_address */
  629.   NULL/* qxfer_spu */
  630.   NULL/* hostio_last_error */
  631.   NULL/* qxfer_osdata */
  632.   NULL/* qxfer_siginfo */
  633.   NULL/* supports_non_stop */
  634.   NULL/* async */
  635.   NULL/* start_non_stop */
  636.   NULL/* supports_multi_process */
  637.   NULL/* handle_monitor_command */
  638. };

  639. void
  640. initialize_low (void)
  641. {
  642.   set_target_ops (&lynx_target_ops);
  643.   the_low_target.arch_setup ();
  644. }