dwarf-regs.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * dwarf-regs.c : Mapping of DWARF debug register numbers into register names.
  3. * Extracted from probe-finder.c
  4. *
  5. * Written by Masami Hiramatsu <mhiramat@redhat.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. */
  22. #include <stddef.h>
  23. #include <errno.h> /* for EINVAL */
  24. #include <string.h> /* for strcmp */
  25. #include <linux/ptrace.h> /* for struct pt_regs */
  26. #include <linux/kernel.h> /* for offsetof */
  27. #include <dwarf-regs.h>
  28. /*
  29. * See arch/x86/kernel/ptrace.c.
  30. * Different from it:
  31. *
  32. * - Since struct pt_regs is defined differently for user and kernel,
  33. * but we want to use 'ax, bx' instead of 'rax, rbx' (which is struct
  34. * field name of user's pt_regs), we make REG_OFFSET_NAME to accept
  35. * both string name and reg field name.
  36. *
  37. * - Since accessing x86_32's pt_regs from x86_64 building is difficult
  38. * and vise versa, we simply fill offset with -1, so
  39. * get_arch_regstr() still works but regs_query_register_offset()
  40. * returns error.
  41. * The only inconvenience caused by it now is that we are not allowed
  42. * to generate BPF prologue for a x86_64 kernel if perf is built for
  43. * x86_32. This is really a rare usecase.
  44. *
  45. * - Order is different from kernel's ptrace.c for get_arch_regstr(). Use
  46. * the order defined by dwarf.
  47. */
  48. struct pt_regs_offset {
  49. const char *name;
  50. int offset;
  51. };
  52. #define REG_OFFSET_END {.name = NULL, .offset = 0}
  53. #ifdef __x86_64__
  54. # define REG_OFFSET_NAME_64(n, r) {.name = n, .offset = offsetof(struct pt_regs, r)}
  55. # define REG_OFFSET_NAME_32(n, r) {.name = n, .offset = -1}
  56. #else
  57. # define REG_OFFSET_NAME_64(n, r) {.name = n, .offset = -1}
  58. # define REG_OFFSET_NAME_32(n, r) {.name = n, .offset = offsetof(struct pt_regs, r)}
  59. #endif
  60. /* TODO: switching by dwarf address size */
  61. #ifndef __x86_64__
  62. static const struct pt_regs_offset x86_32_regoffset_table[] = {
  63. REG_OFFSET_NAME_32("%ax", eax),
  64. REG_OFFSET_NAME_32("%cx", ecx),
  65. REG_OFFSET_NAME_32("%dx", edx),
  66. REG_OFFSET_NAME_32("%bx", ebx),
  67. REG_OFFSET_NAME_32("$stack", esp), /* Stack address instead of %sp */
  68. REG_OFFSET_NAME_32("%bp", ebp),
  69. REG_OFFSET_NAME_32("%si", esi),
  70. REG_OFFSET_NAME_32("%di", edi),
  71. REG_OFFSET_END,
  72. };
  73. #define regoffset_table x86_32_regoffset_table
  74. #else
  75. static const struct pt_regs_offset x86_64_regoffset_table[] = {
  76. REG_OFFSET_NAME_64("%ax", rax),
  77. REG_OFFSET_NAME_64("%dx", rdx),
  78. REG_OFFSET_NAME_64("%cx", rcx),
  79. REG_OFFSET_NAME_64("%bx", rbx),
  80. REG_OFFSET_NAME_64("%si", rsi),
  81. REG_OFFSET_NAME_64("%di", rdi),
  82. REG_OFFSET_NAME_64("%bp", rbp),
  83. REG_OFFSET_NAME_64("%sp", rsp),
  84. REG_OFFSET_NAME_64("%r8", r8),
  85. REG_OFFSET_NAME_64("%r9", r9),
  86. REG_OFFSET_NAME_64("%r10", r10),
  87. REG_OFFSET_NAME_64("%r11", r11),
  88. REG_OFFSET_NAME_64("%r12", r12),
  89. REG_OFFSET_NAME_64("%r13", r13),
  90. REG_OFFSET_NAME_64("%r14", r14),
  91. REG_OFFSET_NAME_64("%r15", r15),
  92. REG_OFFSET_END,
  93. };
  94. #define regoffset_table x86_64_regoffset_table
  95. #endif
  96. /* Minus 1 for the ending REG_OFFSET_END */
  97. #define ARCH_MAX_REGS ((sizeof(regoffset_table) / sizeof(regoffset_table[0])) - 1)
  98. /* Return architecture dependent register string (for kprobe-tracer) */
  99. const char *get_arch_regstr(unsigned int n)
  100. {
  101. return (n < ARCH_MAX_REGS) ? regoffset_table[n].name : NULL;
  102. }
  103. /* Reuse code from arch/x86/kernel/ptrace.c */
  104. /**
  105. * regs_query_register_offset() - query register offset from its name
  106. * @name: the name of a register
  107. *
  108. * regs_query_register_offset() returns the offset of a register in struct
  109. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  110. */
  111. int regs_query_register_offset(const char *name)
  112. {
  113. const struct pt_regs_offset *roff;
  114. for (roff = regoffset_table; roff->name != NULL; roff++)
  115. if (!strcmp(roff->name, name))
  116. return roff->offset;
  117. return -EINVAL;
  118. }