trace_probe_tmpl.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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, void *rec, void *edata,
  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 fetch_store_strlen_user(unsigned long addr);
  60. static nokprobe_inline int
  61. fetch_store_string_user(unsigned long addr, void *dest, void *base);
  62. static nokprobe_inline int
  63. probe_mem_read(void *dest, void *src, size_t size);
  64. static nokprobe_inline int
  65. probe_mem_read_user(void *dest, void *src, size_t size);
  66. static nokprobe_inline int
  67. fetch_store_symstrlen(unsigned long addr)
  68. {
  69. char namebuf[KSYM_SYMBOL_LEN];
  70. int ret;
  71. ret = sprint_symbol(namebuf, addr);
  72. if (ret < 0)
  73. return 0;
  74. return ret + 1;
  75. }
  76. /*
  77. * Fetch a null-terminated symbol string + offset. Caller MUST set *(u32 *)buf
  78. * with max length and relative data location.
  79. */
  80. static nokprobe_inline int
  81. fetch_store_symstring(unsigned long addr, void *dest, void *base)
  82. {
  83. int maxlen = get_loc_len(*(u32 *)dest);
  84. void *__dest;
  85. if (unlikely(!maxlen))
  86. return -ENOMEM;
  87. __dest = get_loc_data(dest, base);
  88. return sprint_symbol(__dest, addr);
  89. }
  90. /* common part of process_fetch_insn*/
  91. static nokprobe_inline int
  92. process_common_fetch_insn(struct fetch_insn *code, unsigned long *val)
  93. {
  94. switch (code->op) {
  95. case FETCH_OP_IMM:
  96. *val = code->immediate;
  97. break;
  98. case FETCH_OP_COMM:
  99. *val = (unsigned long)current->comm;
  100. break;
  101. case FETCH_OP_DATA:
  102. *val = (unsigned long)code->data;
  103. break;
  104. default:
  105. return -EILSEQ;
  106. }
  107. return 0;
  108. }
  109. /* From the 2nd stage, routine is same */
  110. static nokprobe_inline int
  111. process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val,
  112. void *dest, void *base)
  113. {
  114. struct fetch_insn *s3 = NULL;
  115. int total = 0, ret = 0, i = 0;
  116. u32 loc = 0;
  117. unsigned long lval = val;
  118. stage2:
  119. /* 2nd stage: dereference memory if needed */
  120. do {
  121. if (code->op == FETCH_OP_DEREF) {
  122. lval = val;
  123. ret = probe_mem_read(&val, (void *)val + code->offset,
  124. sizeof(val));
  125. } else if (code->op == FETCH_OP_UDEREF) {
  126. lval = val;
  127. ret = probe_mem_read_user(&val,
  128. (void *)val + code->offset, sizeof(val));
  129. } else
  130. break;
  131. if (ret)
  132. return ret;
  133. code++;
  134. } while (1);
  135. s3 = code;
  136. stage3:
  137. /* 3rd stage: store value to buffer */
  138. if (unlikely(!dest)) {
  139. switch (code->op) {
  140. case FETCH_OP_ST_STRING:
  141. ret = fetch_store_strlen(val + code->offset);
  142. code++;
  143. goto array;
  144. case FETCH_OP_ST_USTRING:
  145. ret = fetch_store_strlen_user(val + code->offset);
  146. code++;
  147. goto array;
  148. case FETCH_OP_ST_SYMSTR:
  149. ret = fetch_store_symstrlen(val + code->offset);
  150. code++;
  151. goto array;
  152. default:
  153. return -EILSEQ;
  154. }
  155. }
  156. switch (code->op) {
  157. case FETCH_OP_ST_RAW:
  158. fetch_store_raw(val, code, dest);
  159. break;
  160. case FETCH_OP_ST_MEM:
  161. probe_mem_read(dest, (void *)val + code->offset, code->size);
  162. break;
  163. case FETCH_OP_ST_UMEM:
  164. probe_mem_read_user(dest, (void *)val + code->offset, code->size);
  165. break;
  166. case FETCH_OP_ST_STRING:
  167. loc = *(u32 *)dest;
  168. ret = fetch_store_string(val + code->offset, dest, base);
  169. break;
  170. case FETCH_OP_ST_USTRING:
  171. loc = *(u32 *)dest;
  172. ret = fetch_store_string_user(val + code->offset, dest, base);
  173. break;
  174. case FETCH_OP_ST_SYMSTR:
  175. loc = *(u32 *)dest;
  176. ret = fetch_store_symstring(val + code->offset, dest, base);
  177. break;
  178. default:
  179. return -EILSEQ;
  180. }
  181. code++;
  182. /* 4th stage: modify stored value if needed */
  183. if (code->op == FETCH_OP_MOD_BF) {
  184. fetch_apply_bitfield(code, dest);
  185. code++;
  186. }
  187. array:
  188. /* the last stage: Loop on array */
  189. if (code->op == FETCH_OP_LP_ARRAY) {
  190. if (ret < 0)
  191. ret = 0;
  192. total += ret;
  193. if (++i < code->param) {
  194. code = s3;
  195. if (s3->op != FETCH_OP_ST_STRING &&
  196. s3->op != FETCH_OP_ST_USTRING) {
  197. dest += s3->size;
  198. val += s3->size;
  199. goto stage3;
  200. }
  201. code--;
  202. val = lval + sizeof(char *);
  203. if (dest) {
  204. dest += sizeof(u32);
  205. *(u32 *)dest = update_data_loc(loc, ret);
  206. }
  207. goto stage2;
  208. }
  209. code++;
  210. ret = total;
  211. }
  212. return code->op == FETCH_OP_END ? ret : -EILSEQ;
  213. }
  214. /* Sum up total data length for dynamic arrays (strings) */
  215. static nokprobe_inline int
  216. __get_data_size(struct trace_probe *tp, struct pt_regs *regs, void *edata)
  217. {
  218. struct probe_arg *arg;
  219. int i, len, ret = 0;
  220. for (i = 0; i < tp->nr_args; i++) {
  221. arg = tp->args + i;
  222. if (unlikely(arg->dynamic)) {
  223. len = process_fetch_insn(arg->code, regs, edata, NULL, NULL);
  224. if (len > 0)
  225. ret += len;
  226. }
  227. }
  228. return ret;
  229. }
  230. /* Store the value of each argument */
  231. static nokprobe_inline void
  232. store_trace_args(void *data, struct trace_probe *tp, void *rec, void *edata,
  233. int header_size, int maxlen)
  234. {
  235. struct probe_arg *arg;
  236. void *base = data - header_size;
  237. void *dyndata = data + tp->size;
  238. u32 *dl; /* Data location */
  239. int ret, i;
  240. for (i = 0; i < tp->nr_args; i++) {
  241. arg = tp->args + i;
  242. dl = data + arg->offset;
  243. /* Point the dynamic data area if needed */
  244. if (unlikely(arg->dynamic))
  245. *dl = make_data_loc(maxlen, dyndata - base);
  246. ret = process_fetch_insn(arg->code, rec, edata, dl, base);
  247. if (arg->dynamic && likely(ret > 0)) {
  248. dyndata += ret;
  249. maxlen -= ret;
  250. }
  251. }
  252. }