gdb/common/agent.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Shared utility routines for GDB to interact with agent.

  2.    Copyright (C) 2009-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

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

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

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

  14. #include "common-defs.h"
  15. #include "target/target.h"
  16. #include "common/symbol.h"
  17. #include <unistd.h>
  18. #include "agent.h"
  19. #include "filestuff.h"

  20. int debug_agent = 0;

  21. /* A stdarg wrapper for debug_vprintf.  */

  22. static void ATTRIBUTE_PRINTF (1, 2)
  23. debug_agent_printf (const char *fmt, ...)
  24. {
  25.   va_list ap;

  26.   if (!debug_agent)
  27.     return;
  28.   va_start (ap, fmt);
  29.   debug_vprintf (fmt, ap);
  30.   va_end (ap);
  31. }

  32. #define DEBUG_AGENT debug_agent_printf

  33. /* Global flag to determine using agent or not.  */
  34. int use_agent = 0;

  35. /* Addresses of in-process agent's symbols both GDB and GDBserver cares
  36.    about.  */

  37. struct ipa_sym_addresses
  38. {
  39.   CORE_ADDR addr_helper_thread_id;
  40.   CORE_ADDR addr_cmd_buf;
  41.   CORE_ADDR addr_capability;
  42. };

  43. /* Cache of the helper thread id.  FIXME: this global should be made
  44.    per-process.  */
  45. static uint32_t helper_thread_id = 0;

  46. static struct
  47. {
  48.   const char *name;
  49.   int offset;
  50.   int required;
  51. } symbol_list[] = {
  52.   IPA_SYM(helper_thread_id),
  53.   IPA_SYM(cmd_buf),
  54.   IPA_SYM(capability),
  55. };

  56. static struct ipa_sym_addresses ipa_sym_addrs;

  57. static int all_agent_symbols_looked_up = 0;

  58. int
  59. agent_loaded_p (void)
  60. {
  61.   return all_agent_symbols_looked_up;
  62. }

  63. /* Look up all symbols needed by agent.  Return 0 if all the symbols are
  64.    found, return non-zero otherwise.  */

  65. int
  66. agent_look_up_symbols (void *arg)
  67. {
  68.   int i;

  69.   all_agent_symbols_looked_up = 0;

  70.   for (i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++)
  71.     {
  72.       CORE_ADDR *addrp =
  73.         (CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset);

  74.       if (find_minimal_symbol_address (symbol_list[i].name, addrp,
  75.                                        arg) != 0)
  76.         {
  77.           DEBUG_AGENT ("symbol `%s' not found\n", symbol_list[i].name);
  78.           return -1;
  79.         }
  80.     }

  81.   all_agent_symbols_looked_up = 1;
  82.   return 0;
  83. }

  84. static unsigned int
  85. agent_get_helper_thread_id (void)
  86. {
  87.   if  (helper_thread_id == 0)
  88.     {
  89.       if (target_read_uint32 (ipa_sym_addrs.addr_helper_thread_id,
  90.                               &helper_thread_id))
  91.         warning (_("Error reading helper thread's id in lib"));
  92.     }

  93.   return helper_thread_id;
  94. }

  95. #ifdef HAVE_SYS_UN_H
  96. #include <sys/socket.h>
  97. #include <sys/un.h>
  98. #define SOCK_DIR P_tmpdir

  99. #ifndef UNIX_PATH_MAX
  100. #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
  101. #endif

  102. #endif

  103. /* Connects to synchronization socket.  PID is the pid of inferior, which is
  104.    used to set up the connection socket.  */

  105. static int
  106. gdb_connect_sync_socket (int pid)
  107. {
  108. #ifdef HAVE_SYS_UN_H
  109.   struct sockaddr_un addr;
  110.   int res, fd;
  111.   char path[UNIX_PATH_MAX];

  112.   res = xsnprintf (path, UNIX_PATH_MAX, "%s/gdb_ust%d", P_tmpdir, pid);
  113.   if (res >= UNIX_PATH_MAX)
  114.     return -1;

  115.   res = fd = gdb_socket_cloexec (PF_UNIX, SOCK_STREAM, 0);
  116.   if (res == -1)
  117.     {
  118.       warning (_("error opening sync socket: %s"), strerror (errno));
  119.       return -1;
  120.     }

  121.   addr.sun_family = AF_UNIX;

  122.   res = xsnprintf (addr.sun_path, UNIX_PATH_MAX, "%s", path);
  123.   if (res >= UNIX_PATH_MAX)
  124.     {
  125.       warning (_("string overflow allocating socket name"));
  126.       close (fd);
  127.       return -1;
  128.     }

  129.   res = connect (fd, (struct sockaddr *) &addr, sizeof (addr));
  130.   if (res == -1)
  131.     {
  132.       warning (_("error connecting sync socket (%s): %s. "
  133.                  "Make sure the directory exists and that it is writable."),
  134.                  path, strerror (errno));
  135.       close (fd);
  136.       return -1;
  137.     }

  138.   return fd;
  139. #else
  140.   return -1;
  141. #endif
  142. }

  143. /* Execute an agent command in the inferior.  PID is the value of pid of the
  144.    inferior.  CMD is the buffer for command.  GDB or GDBserver will store the
  145.    command into it and fetch the return result from CMD.  The interaction
  146.    between GDB/GDBserver and the agent is synchronized by a synchronization
  147.    socket.  Return zero if success, otherwise return non-zero.  */

  148. int
  149. agent_run_command (int pid, const char *cmd, int len)
  150. {
  151.   int fd;
  152.   int tid = agent_get_helper_thread_id ();
  153.   ptid_t ptid = ptid_build (pid, tid, 0);

  154.   int ret = target_write_memory (ipa_sym_addrs.addr_cmd_buf,
  155.                                  (gdb_byte *) cmd, len);

  156.   if (ret != 0)
  157.     {
  158.       warning (_("unable to write"));
  159.       return -1;
  160.     }

  161.   DEBUG_AGENT ("agent: resumed helper thread\n");

  162.   /* Resume helper thread.  */
  163.   target_continue_no_signal (ptid);

  164.   fd = gdb_connect_sync_socket (pid);
  165.   if (fd >= 0)
  166.     {
  167.       char buf[1] = "";
  168.       int ret;

  169.       DEBUG_AGENT ("agent: signalling helper thread\n");

  170.       do
  171.         {
  172.           ret = write (fd, buf, 1);
  173.         } while (ret == -1 && errno == EINTR);

  174.         DEBUG_AGENT ("agent: waiting for helper thread's response\n");

  175.       do
  176.         {
  177.           ret = read (fd, buf, 1);
  178.         } while (ret == -1 && errno == EINTR);

  179.       close (fd);

  180.       DEBUG_AGENT ("agent: helper thread's response received\n");
  181.     }
  182.   else
  183.     return -1;

  184.   /* Need to read response with the inferior stopped.  */
  185.   if (!ptid_equal (ptid, null_ptid))
  186.     {
  187.       /* Stop thread PTID.  */
  188.       DEBUG_AGENT ("agent: stop helper thread\n");
  189.       target_stop_and_wait (ptid);
  190.     }

  191.   if (fd >= 0)
  192.     {
  193.       if (target_read_memory (ipa_sym_addrs.addr_cmd_buf, (gdb_byte *) cmd,
  194.                               IPA_CMD_BUF_SIZE))
  195.         {
  196.           warning (_("Error reading command response"));
  197.           return -1;
  198.         }
  199.     }

  200.   return 0;
  201. }

  202. /* Each bit of it stands for a capability of agent.  */
  203. static uint32_t agent_capability = 0;

  204. /* Return true if agent has capability AGENT_CAP, otherwise return false.  */

  205. int
  206. agent_capability_check (enum agent_capa agent_capa)
  207. {
  208.   if (agent_capability == 0)
  209.     {
  210.       if (target_read_uint32 (ipa_sym_addrs.addr_capability,
  211.                               &agent_capability))
  212.         warning (_("Error reading capability of agent"));
  213.     }
  214.   return agent_capability & agent_capa;
  215. }

  216. /* Invalidate the cache of agent capability, so we'll read it from inferior
  217.    again.  Call it when launches a new program or reconnect to remote stub.  */

  218. void
  219. agent_capability_invalidate (void)
  220. {
  221.   agent_capability = 0;
  222. }