perf_regs.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <string.h>
  4. #include <regex.h>
  5. #include "../../perf.h"
  6. #include "../../util/util.h"
  7. #include "../../util/perf_regs.h"
  8. #include "../../util/debug.h"
  9. const struct sample_reg sample_reg_masks[] = {
  10. SMPL_REG(AX, PERF_REG_X86_AX),
  11. SMPL_REG(BX, PERF_REG_X86_BX),
  12. SMPL_REG(CX, PERF_REG_X86_CX),
  13. SMPL_REG(DX, PERF_REG_X86_DX),
  14. SMPL_REG(SI, PERF_REG_X86_SI),
  15. SMPL_REG(DI, PERF_REG_X86_DI),
  16. SMPL_REG(BP, PERF_REG_X86_BP),
  17. SMPL_REG(SP, PERF_REG_X86_SP),
  18. SMPL_REG(IP, PERF_REG_X86_IP),
  19. SMPL_REG(FLAGS, PERF_REG_X86_FLAGS),
  20. SMPL_REG(CS, PERF_REG_X86_CS),
  21. SMPL_REG(SS, PERF_REG_X86_SS),
  22. #ifdef HAVE_ARCH_X86_64_SUPPORT
  23. SMPL_REG(R8, PERF_REG_X86_R8),
  24. SMPL_REG(R9, PERF_REG_X86_R9),
  25. SMPL_REG(R10, PERF_REG_X86_R10),
  26. SMPL_REG(R11, PERF_REG_X86_R11),
  27. SMPL_REG(R12, PERF_REG_X86_R12),
  28. SMPL_REG(R13, PERF_REG_X86_R13),
  29. SMPL_REG(R14, PERF_REG_X86_R14),
  30. SMPL_REG(R15, PERF_REG_X86_R15),
  31. #endif
  32. SMPL_REG_END
  33. };
  34. struct sdt_name_reg {
  35. const char *sdt_name;
  36. const char *uprobe_name;
  37. };
  38. #define SDT_NAME_REG(n, m) {.sdt_name = "%" #n, .uprobe_name = "%" #m}
  39. #define SDT_NAME_REG_END {.sdt_name = NULL, .uprobe_name = NULL}
  40. static const struct sdt_name_reg sdt_reg_tbl[] = {
  41. SDT_NAME_REG(eax, ax),
  42. SDT_NAME_REG(rax, ax),
  43. SDT_NAME_REG(al, ax),
  44. SDT_NAME_REG(ah, ax),
  45. SDT_NAME_REG(ebx, bx),
  46. SDT_NAME_REG(rbx, bx),
  47. SDT_NAME_REG(bl, bx),
  48. SDT_NAME_REG(bh, bx),
  49. SDT_NAME_REG(ecx, cx),
  50. SDT_NAME_REG(rcx, cx),
  51. SDT_NAME_REG(cl, cx),
  52. SDT_NAME_REG(ch, cx),
  53. SDT_NAME_REG(edx, dx),
  54. SDT_NAME_REG(rdx, dx),
  55. SDT_NAME_REG(dl, dx),
  56. SDT_NAME_REG(dh, dx),
  57. SDT_NAME_REG(esi, si),
  58. SDT_NAME_REG(rsi, si),
  59. SDT_NAME_REG(sil, si),
  60. SDT_NAME_REG(edi, di),
  61. SDT_NAME_REG(rdi, di),
  62. SDT_NAME_REG(dil, di),
  63. SDT_NAME_REG(ebp, bp),
  64. SDT_NAME_REG(rbp, bp),
  65. SDT_NAME_REG(bpl, bp),
  66. SDT_NAME_REG(rsp, sp),
  67. SDT_NAME_REG(esp, sp),
  68. SDT_NAME_REG(spl, sp),
  69. /* rNN registers */
  70. SDT_NAME_REG(r8b, r8),
  71. SDT_NAME_REG(r8w, r8),
  72. SDT_NAME_REG(r8d, r8),
  73. SDT_NAME_REG(r9b, r9),
  74. SDT_NAME_REG(r9w, r9),
  75. SDT_NAME_REG(r9d, r9),
  76. SDT_NAME_REG(r10b, r10),
  77. SDT_NAME_REG(r10w, r10),
  78. SDT_NAME_REG(r10d, r10),
  79. SDT_NAME_REG(r11b, r11),
  80. SDT_NAME_REG(r11w, r11),
  81. SDT_NAME_REG(r11d, r11),
  82. SDT_NAME_REG(r12b, r12),
  83. SDT_NAME_REG(r12w, r12),
  84. SDT_NAME_REG(r12d, r12),
  85. SDT_NAME_REG(r13b, r13),
  86. SDT_NAME_REG(r13w, r13),
  87. SDT_NAME_REG(r13d, r13),
  88. SDT_NAME_REG(r14b, r14),
  89. SDT_NAME_REG(r14w, r14),
  90. SDT_NAME_REG(r14d, r14),
  91. SDT_NAME_REG(r15b, r15),
  92. SDT_NAME_REG(r15w, r15),
  93. SDT_NAME_REG(r15d, r15),
  94. SDT_NAME_REG_END,
  95. };
  96. /*
  97. * Perf only supports OP which is in +/-NUM(REG) form.
  98. * Here plus-minus sign, NUM and parenthesis are optional,
  99. * only REG is mandatory.
  100. *
  101. * SDT events also supports indirect addressing mode with a
  102. * symbol as offset, scaled mode and constants in OP. But
  103. * perf does not support them yet. Below are few examples.
  104. *
  105. * OP with scaled mode:
  106. * (%rax,%rsi,8)
  107. * 10(%ras,%rsi,8)
  108. *
  109. * OP with indirect addressing mode:
  110. * check_action(%rip)
  111. * mp_+52(%rip)
  112. * 44+mp_(%rip)
  113. *
  114. * OP with constant values:
  115. * $0
  116. * $123
  117. * $-1
  118. */
  119. #define SDT_OP_REGEX "^([+\\-]?)([0-9]*)(\\(?)(%[a-z][a-z0-9]+)(\\)?)$"
  120. static regex_t sdt_op_regex;
  121. static int sdt_init_op_regex(void)
  122. {
  123. static int initialized;
  124. int ret = 0;
  125. if (initialized)
  126. return 0;
  127. ret = regcomp(&sdt_op_regex, SDT_OP_REGEX, REG_EXTENDED);
  128. if (ret < 0) {
  129. pr_debug4("Regex compilation error.\n");
  130. return ret;
  131. }
  132. initialized = 1;
  133. return 0;
  134. }
  135. /*
  136. * Max x86 register name length is 5(ex: %r15d). So, 6th char
  137. * should always contain NULL. This helps to find register name
  138. * length using strlen, insted of maintaing one more variable.
  139. */
  140. #define SDT_REG_NAME_SIZE 6
  141. /*
  142. * The uprobe parser does not support all gas register names;
  143. * so, we have to replace them (ex. for x86_64: %rax -> %ax).
  144. * Note: If register does not require renaming, just copy
  145. * paste as it is, but don't leave it empty.
  146. */
  147. static void sdt_rename_register(char *sdt_reg, int sdt_len, char *uprobe_reg)
  148. {
  149. int i = 0;
  150. for (i = 0; sdt_reg_tbl[i].sdt_name != NULL; i++) {
  151. if (!strncmp(sdt_reg_tbl[i].sdt_name, sdt_reg, sdt_len)) {
  152. strcpy(uprobe_reg, sdt_reg_tbl[i].uprobe_name);
  153. return;
  154. }
  155. }
  156. strncpy(uprobe_reg, sdt_reg, sdt_len);
  157. }
  158. int arch_sdt_arg_parse_op(char *old_op, char **new_op)
  159. {
  160. char new_reg[SDT_REG_NAME_SIZE] = {0};
  161. int new_len = 0, ret;
  162. /*
  163. * rm[0]: +/-NUM(REG)
  164. * rm[1]: +/-
  165. * rm[2]: NUM
  166. * rm[3]: (
  167. * rm[4]: REG
  168. * rm[5]: )
  169. */
  170. regmatch_t rm[6];
  171. /*
  172. * Max prefix length is 2 as it may contains sign(+/-)
  173. * and displacement 0 (Both sign and displacement 0 are
  174. * optional so it may be empty). Use one more character
  175. * to hold last NULL so that strlen can be used to find
  176. * prefix length, instead of maintaing one more variable.
  177. */
  178. char prefix[3] = {0};
  179. ret = sdt_init_op_regex();
  180. if (ret < 0)
  181. return ret;
  182. /*
  183. * If unsupported OR does not match with regex OR
  184. * register name too long, skip it.
  185. */
  186. if (strchr(old_op, ',') || strchr(old_op, '$') ||
  187. regexec(&sdt_op_regex, old_op, 6, rm, 0) ||
  188. rm[4].rm_eo - rm[4].rm_so > SDT_REG_NAME_SIZE) {
  189. pr_debug4("Skipping unsupported SDT argument: %s\n", old_op);
  190. return SDT_ARG_SKIP;
  191. }
  192. /*
  193. * Prepare prefix.
  194. * If SDT OP has parenthesis but does not provide
  195. * displacement, add 0 for displacement.
  196. * SDT Uprobe Prefix
  197. * -----------------------------
  198. * +24(%rdi) +24(%di) +
  199. * 24(%rdi) +24(%di) +
  200. * %rdi %di
  201. * (%rdi) +0(%di) +0
  202. * -80(%rbx) -80(%bx) -
  203. */
  204. if (rm[3].rm_so != rm[3].rm_eo) {
  205. if (rm[1].rm_so != rm[1].rm_eo)
  206. prefix[0] = *(old_op + rm[1].rm_so);
  207. else if (rm[2].rm_so != rm[2].rm_eo)
  208. prefix[0] = '+';
  209. else
  210. scnprintf(prefix, sizeof(prefix), "+0");
  211. }
  212. /* Rename register */
  213. sdt_rename_register(old_op + rm[4].rm_so, rm[4].rm_eo - rm[4].rm_so,
  214. new_reg);
  215. /* Prepare final OP which should be valid for uprobe_events */
  216. new_len = strlen(prefix) +
  217. (rm[2].rm_eo - rm[2].rm_so) +
  218. (rm[3].rm_eo - rm[3].rm_so) +
  219. strlen(new_reg) +
  220. (rm[5].rm_eo - rm[5].rm_so) +
  221. 1; /* NULL */
  222. *new_op = zalloc(new_len);
  223. if (!*new_op)
  224. return -ENOMEM;
  225. scnprintf(*new_op, new_len, "%.*s%.*s%.*s%.*s%.*s",
  226. strlen(prefix), prefix,
  227. (int)(rm[2].rm_eo - rm[2].rm_so), old_op + rm[2].rm_so,
  228. (int)(rm[3].rm_eo - rm[3].rm_so), old_op + rm[3].rm_so,
  229. strlen(new_reg), new_reg,
  230. (int)(rm[5].rm_eo - rm[5].rm_so), old_op + rm[5].rm_so);
  231. return SDT_ARG_VALID;
  232. }