trace_probe_tmpl.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Traceprobe fetch helper inlines
  4. */
  5. static nokprobe_inline void
  6. fetch_store_raw(unsigned long val, struct fetch_insn *code, void *buf)
  7. {
  8. switch (code->size) {
  9. case 1:
  10. *(u8 *)buf = (u8)val;
  11. break;
  12. case 2:
  13. *(u16 *)buf = (u16)val;
  14. break;
  15. case 4:
  16. *(u32 *)buf = (u32)val;
  17. break;
  18. case 8:
  19. //TBD: 32bit signed
  20. *(u64 *)buf = (u64)val;
  21. break;
  22. default:
  23. *(unsigned long *)buf = val;
  24. }
  25. }
  26. static nokprobe_inline void
  27. fetch_apply_bitfield(struct fetch_insn *code, void *buf)
  28. {
  29. switch (code->basesize) {
  30. case 1:
  31. *(u8 *)buf <<= code->lshift;
  32. *(u8 *)buf >>= code->rshift;
  33. break;
  34. case 2:
  35. *(u16 *)buf <<= code->lshift;
  36. *(u16 *)buf >>= code->rshift;
  37. break;
  38. case 4:
  39. *(u32 *)buf <<= code->lshift;
  40. *(u32 *)buf >>= code->rshift;
  41. break;
  42. case 8:
  43. *(u64 *)buf <<= code->lshift;
  44. *(u64 *)buf >>= code->rshift;
  45. break;
  46. }
  47. }
  48. /*
  49. * These functions must be defined for each callsite.
  50. * Return consumed dynamic data size (>= 0), or error (< 0).
  51. * If dest is NULL, don't store result and return required dynamic data size.
  52. */
  53. static int
  54. process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs,
  55. void *dest, void *base);
  56. static nokprobe_inline int fetch_store_strlen(unsigned long addr);
  57. static nokprobe_inline int
  58. fetch_store_string(unsigned long addr, void *dest, void *base);
  59. static nokprobe_inline int
  60. probe_mem_read(void *dest, void *src, size_t size);
  61. /* From the 2nd stage, routine is same */
  62. static nokprobe_inline int
  63. process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,
  64. void *dest, void *base)
  65. {
  66. struct fetch_insn *s3 = NULL;
  67. int total = 0, ret = 0, i = 0;
  68. u32 loc = 0;
  69. unsigned long lval = val;
  70. stage2:
  71. /* 2nd stage: dereference memory if needed */
  72. while (code->op == FETCH_OP_DEREF) {
  73. lval = val;
  74. ret = probe_mem_read(&val, (void *)val + code->offset,
  75. sizeof(val));
  76. if (ret)
  77. return ret;
  78. code++;
  79. }
  80. s3 = code;
  81. stage3:
  82. /* 3rd stage: store value to buffer */
  83. if (unlikely(!dest)) {
  84. if (code->op == FETCH_OP_ST_STRING) {
  85. ret += fetch_store_strlen(val + code->offset);
  86. code++;
  87. goto array;
  88. } else
  89. return -EILSEQ;
  90. }
  91. switch (code->op) {
  92. case FETCH_OP_ST_RAW:
  93. fetch_store_raw(val, code, dest);
  94. break;
  95. case FETCH_OP_ST_MEM:
  96. probe_mem_read(dest, (void *)val + code->offset, code->size);
  97. break;
  98. case FETCH_OP_ST_STRING:
  99. loc = *(u32 *)dest;
  100. ret = fetch_store_string(val + code->offset, dest, base);
  101. break;
  102. default:
  103. return -EILSEQ;
  104. }
  105. code++;
  106. /* 4th stage: modify stored value if needed */
  107. if (code->op == FETCH_OP_MOD_BF) {
  108. fetch_apply_bitfield(code, dest);
  109. code++;
  110. }
  111. array:
  112. /* the last stage: Loop on array */
  113. if (code->op == FETCH_OP_LP_ARRAY) {
  114. total += ret;
  115. if (++i < code->param) {
  116. code = s3;
  117. if (s3->op != FETCH_OP_ST_STRING) {
  118. dest += s3->size;
  119. val += s3->size;
  120. goto stage3;
  121. }
  122. code--;
  123. val = lval + sizeof(char *);
  124. if (dest) {
  125. dest += sizeof(u32);
  126. *(u32 *)dest = update_data_loc(loc, ret);
  127. }
  128. goto stage2;
  129. }
  130. code++;
  131. ret = total;
  132. }
  133. return code->op == FETCH_OP_END ? ret : -EILSEQ;
  134. }
  135. /* Sum up total data length for dynamic arraies (strings) */
  136. static nokprobe_inline int
  137. __get_data_size(struct trace_probe *tp, struct pt_regs *regs)
  138. {
  139. struct probe_arg *arg;
  140. int i, len, ret = 0;
  141. for (i = 0; i < tp->nr_args; i++) {
  142. arg = tp->args + i;
  143. if (unlikely(arg->dynamic)) {
  144. len = process_fetch_insn(arg->code, regs, NULL, NULL);
  145. if (len > 0)
  146. ret += len;
  147. }
  148. }
  149. return ret;
  150. }
  151. /* Store the value of each argument */
  152. static nokprobe_inline void
  153. store_trace_args(void *data, struct trace_probe *tp, struct pt_regs *regs,
  154. int header_size, int maxlen)
  155. {
  156. struct probe_arg *arg;
  157. void *base = data - header_size;
  158. void *dyndata = data + tp->size;
  159. u32 *dl; /* Data location */
  160. int ret, i;
  161. for (i = 0; i < tp->nr_args; i++) {
  162. arg = tp->args + i;
  163. dl = data + arg->offset;
  164. /* Point the dynamic data area if needed */
  165. if (unlikely(arg->dynamic))
  166. *dl = make_data_loc(maxlen, dyndata - base);
  167. ret = process_fetch_insn(arg->code, regs, dl, base);
  168. if (unlikely(ret < 0 && arg->dynamic))
  169. *dl = make_data_loc(0, dyndata - base);
  170. else
  171. dyndata += ret;
  172. }
  173. }
  174. static inline int
  175. print_probe_args(struct trace_seq *s, struct probe_arg *args, int nr_args,
  176. u8 *data, void *field)
  177. {
  178. void *p;
  179. int i, j;
  180. for (i = 0; i < nr_args; i++) {
  181. struct probe_arg *a = args + i;
  182. trace_seq_printf(s, " %s=", a->name);
  183. if (likely(!a->count)) {
  184. if (!a->type->print(s, data + a->offset, field))
  185. return -ENOMEM;
  186. continue;
  187. }
  188. trace_seq_putc(s, '{');
  189. p = data + a->offset;
  190. for (j = 0; j < a->count; j++) {
  191. if (!a->type->print(s, p, field))
  192. return -ENOMEM;
  193. trace_seq_putc(s, j == a->count - 1 ? '}' : ',');
  194. p += a->type->size;
  195. }
  196. }
  197. return 0;
  198. }