gdb/gdbserver/linux-i386-ipa.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* GNU/Linux/x86 specific low level interface, for the in-process
  2.    agent library for GDB.

  3.    Copyright (C) 2010-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 <stdint.h>
  17. #include <sys/mman.h>
  18. #include "tracepoint.h"

  19. /* GDB register numbers.  */

  20. enum i386_gdb_regnum
  21. {
  22.   I386_EAX_REGNUM,                /* %eax */
  23.   I386_ECX_REGNUM,                /* %ecx */
  24.   I386_EDX_REGNUM,                /* %edx */
  25.   I386_EBX_REGNUM,                /* %ebx */
  26.   I386_ESP_REGNUM,                /* %esp */
  27.   I386_EBP_REGNUM,                /* %ebp */
  28.   I386_ESI_REGNUM,                /* %esi */
  29.   I386_EDI_REGNUM,                /* %edi */
  30.   I386_EIP_REGNUM,                /* %eip */
  31.   I386_EFLAGS_REGNUM,                /* %eflags */
  32.   I386_CS_REGNUM,                /* %cs */
  33.   I386_SS_REGNUM,                /* %ss */
  34.   I386_DS_REGNUM,                /* %ds */
  35.   I386_ES_REGNUM,                /* %es */
  36.   I386_FS_REGNUM,                /* %fs */
  37.   I386_GS_REGNUM,                /* %gs */
  38.   I386_ST0_REGNUM                /* %st(0) */
  39. };

  40. #define i386_num_regs 16

  41. /* Defined in auto-generated file i386-linux.c.  */
  42. void init_registers_i386_linux (void);
  43. extern const struct target_desc *tdesc_i386_linux;

  44. #define FT_CR_EAX 15
  45. #define FT_CR_ECX 14
  46. #define FT_CR_EDX 13
  47. #define FT_CR_EBX 12
  48. #define FT_CR_UESP 11
  49. #define FT_CR_EBP 10
  50. #define FT_CR_ESI 9
  51. #define FT_CR_EDI 8
  52. #define FT_CR_EIP 7
  53. #define FT_CR_EFL 6
  54. #define FT_CR_DS 5
  55. #define FT_CR_ES 4
  56. #define FT_CR_FS 3
  57. #define FT_CR_GS 2
  58. #define FT_CR_SS 1
  59. #define FT_CR_CS 0

  60. /* Mapping between the general-purpose registers in jump tracepoint
  61.    format and GDB's register array layout.  */

  62. static const int i386_ft_collect_regmap[] =
  63. {
  64.   FT_CR_EAX * 4, FT_CR_ECX * 4, FT_CR_EDX * 4, FT_CR_EBX * 4,
  65.   FT_CR_UESP * 4, FT_CR_EBP * 4, FT_CR_ESI * 4, FT_CR_EDI * 4,
  66.   FT_CR_EIP * 4, FT_CR_EFL * 4, FT_CR_CS * 4, FT_CR_SS * 4,
  67.   FT_CR_DS * 4, FT_CR_ES * 4, FT_CR_FS * 4, FT_CR_GS * 4
  68. };

  69. void
  70. supply_fast_tracepoint_registers (struct regcache *regcache,
  71.                                   const unsigned char *buf)
  72. {
  73.   int i;

  74.   for (i = 0; i < i386_num_regs; i++)
  75.     {
  76.       int regval;

  77.       if (i >= I386_CS_REGNUM && i <= I386_GS_REGNUM)
  78.         regval = *(short *) (((char *) buf) + i386_ft_collect_regmap[i]);
  79.       else
  80.         regval = *(int *) (((char *) buf) + i386_ft_collect_regmap[i]);

  81.       supply_register (regcache, i, &regval);
  82.     }
  83. }

  84. ULONGEST __attribute__ ((visibility("default"), used))
  85. gdb_agent_get_raw_reg (unsigned char *raw_regs, int regnum)
  86. {
  87.   /* This should maybe be allowed to return an error code, or perhaps
  88.      better, have the emit_reg detect this, and emit a constant zero,
  89.      or something.  */

  90.   if (regnum > i386_num_regs)
  91.     return 0;
  92.   else if (regnum >= I386_CS_REGNUM && regnum <= I386_GS_REGNUM)
  93.     return *(short *) (raw_regs + i386_ft_collect_regmap[regnum]);
  94.   else
  95.     return *(int *) (raw_regs + i386_ft_collect_regmap[regnum]);
  96. }

  97. #ifdef HAVE_UST

  98. #include <ust/processor.h>

  99. /* "struct registers" is the UST object type holding the registers at
  100.    the time of the static tracepoint marker call.  This doesn't
  101.    contain EIP, but we know what it must have been (the marker
  102.    address).  */

  103. #define ST_REGENTRY(REG)                        \
  104.   {                                                \
  105.     offsetof (struct registers, REG),                \
  106.     sizeof (((struct registers *) NULL)->REG)        \
  107.   }

  108. static struct
  109. {
  110.   int offset;
  111.   int size;
  112. } i386_st_collect_regmap[] =
  113.   {
  114.     ST_REGENTRY(eax),
  115.     ST_REGENTRY(ecx),
  116.     ST_REGENTRY(edx),
  117.     ST_REGENTRY(ebx),
  118.     ST_REGENTRY(esp),
  119.     ST_REGENTRY(ebp),
  120.     ST_REGENTRY(esi),
  121.     ST_REGENTRY(edi),
  122.     { -1, 0 }, /* eip */
  123.     ST_REGENTRY(eflags),
  124.     ST_REGENTRY(cs),
  125.     ST_REGENTRY(ss),
  126.   };

  127. #define i386_NUM_ST_COLLECT_GREGS \
  128.   (sizeof (i386_st_collect_regmap) / sizeof (i386_st_collect_regmap[0]))

  129. void
  130. supply_static_tracepoint_registers (struct regcache *regcache,
  131.                                     const unsigned char *buf,
  132.                                     CORE_ADDR pc)
  133. {
  134.   int i;
  135.   unsigned int newpc = pc;

  136.   supply_register (regcache, I386_EIP_REGNUM, &newpc);

  137.   for (i = 0; i < i386_NUM_ST_COLLECT_GREGS; i++)
  138.     if (i386_st_collect_regmap[i].offset != -1)
  139.       {
  140.         switch (i386_st_collect_regmap[i].size)
  141.           {
  142.           case 4:
  143.             supply_register (regcache, i,
  144.                              ((char *) buf)
  145.                              + i386_st_collect_regmap[i].offset);
  146.             break;
  147.           case 2:
  148.             {
  149.               unsigned long reg
  150.                 = * (short *) (((char *) buf)
  151.                                + i386_st_collect_regmap[i].offset);
  152.               reg &= 0xffff;
  153.               supply_register (regcache, i, &reg);
  154.             }
  155.             break;
  156.           default:
  157.             internal_error (__FILE__, __LINE__, "unhandled register size: %d",
  158.                             i386_st_collect_regmap[i].size);
  159.           }
  160.       }
  161. }

  162. #endif /* HAVE_UST */


  163. /* This is only needed because reg-i386-linux-lib.o references it.  We
  164.    may use it proper at some point.  */
  165. const char *gdbserver_xmltarget;

  166. /* Attempt to allocate memory for trampolines in the first 64 KiB of
  167.    memory to enable smaller jump patches.  */

  168. static void
  169. initialize_fast_tracepoint_trampoline_buffer (void)
  170. {
  171.   const CORE_ADDR buffer_end = 64 * 1024;
  172.   /* Ensure that the buffer will be at least 1 KiB in size, which is
  173.      enough space for over 200 fast tracepoints.  */
  174.   const int min_buffer_size = 1024;
  175.   char buf[IPA_BUFSIZ];
  176.   CORE_ADDR mmap_min_addr = buffer_end + 1;
  177.   ULONGEST buffer_size;
  178.   FILE *f = fopen ("/proc/sys/vm/mmap_min_addr", "r");

  179.   if (!f)
  180.     {
  181.       snprintf (buf, sizeof (buf), "mmap_min_addr open failed: %s",
  182.                 strerror (errno));
  183.       set_trampoline_buffer_space (0, 0, buf);
  184.       return;
  185.     }

  186.   if (fgets (buf, IPA_BUFSIZ, f))
  187.     sscanf (buf, "%llu", &mmap_min_addr);

  188.   fclose (f);

  189.   buffer_size = buffer_end - mmap_min_addr;

  190.   if (buffer_size >= min_buffer_size)
  191.     {
  192.       if (mmap ((void *) (uintptr_t) mmap_min_addr, buffer_size,
  193.                 PROT_READ | PROT_EXEC | PROT_WRITE,
  194.                 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
  195.                 -1, 0)
  196.           != MAP_FAILED)
  197.         set_trampoline_buffer_space (mmap_min_addr, buffer_end, NULL);
  198.       else
  199.         {
  200.           snprintf (buf, IPA_BUFSIZ, "low-64K-buffer mmap() failed: %s",
  201.                     strerror (errno));
  202.           set_trampoline_buffer_space (0, 0, buf);
  203.         }
  204.     }
  205.   else
  206.     {
  207.       snprintf (buf, IPA_BUFSIZ, "mmap_min_addr is %d, must be %d or less",
  208.                 (int) mmap_min_addr, (int) buffer_end - min_buffer_size);
  209.       set_trampoline_buffer_space (0, 0, buf);
  210.     }
  211. }

  212. void
  213. initialize_low_tracepoint (void)
  214. {
  215.   init_registers_i386_linux ();
  216.   ipa_tdesc = tdesc_i386_linux;
  217.   initialize_fast_tracepoint_trampoline_buffer ();
  218. }