Steven Rostedt (VMware) | bcea3f9 | 2018-08-16 11:23:53 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Common code for probe-based Dynamic events. |
| 4 | * |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 5 | * This code was copied from kernel/trace/trace_kprobe.c written by |
| 6 | * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> |
| 7 | * |
| 8 | * Updates to make this generic: |
| 9 | * Copyright (C) IBM Corporation, 2010-2011 |
| 10 | * Author: Srikar Dronamraju |
| 11 | */ |
Masami Hiramatsu | 7257634 | 2017-02-07 20:21:28 +0900 | [diff] [blame] | 12 | #define pr_fmt(fmt) "trace_probe: " fmt |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 13 | |
| 14 | #include "trace_probe.h" |
| 15 | |
| 16 | const char *reserved_field_names[] = { |
| 17 | "common_type", |
| 18 | "common_flags", |
| 19 | "common_preempt_count", |
| 20 | "common_pid", |
| 21 | "common_tgid", |
| 22 | FIELD_STRING_IP, |
| 23 | FIELD_STRING_RETIP, |
| 24 | FIELD_STRING_FUNC, |
| 25 | }; |
| 26 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 27 | /* Printing in basic type function template */ |
Masami Hiramatsu | 17ce3dc | 2016-08-18 17:57:50 +0900 | [diff] [blame] | 28 | #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \ |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 29 | int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 30 | { \ |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 31 | trace_seq_printf(s, fmt, *(type *)data); \ |
Steven Rostedt (Red Hat) | d2b0191 | 2014-11-12 17:19:51 -0500 | [diff] [blame] | 32 | return !trace_seq_has_overflowed(s); \ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 33 | } \ |
Masami Hiramatsu | 17ce3dc | 2016-08-18 17:57:50 +0900 | [diff] [blame] | 34 | const char PRINT_TYPE_FMT_NAME(tname)[] = fmt; \ |
| 35 | NOKPROBE_SYMBOL(PRINT_TYPE_FUNC_NAME(tname)); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 36 | |
Masami Hiramatsu | bdca79c2 | 2016-08-18 17:59:21 +0900 | [diff] [blame] | 37 | DEFINE_BASIC_PRINT_TYPE_FUNC(u8, u8, "%u") |
| 38 | DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u") |
| 39 | DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u") |
| 40 | DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu") |
Masami Hiramatsu | 17ce3dc | 2016-08-18 17:57:50 +0900 | [diff] [blame] | 41 | DEFINE_BASIC_PRINT_TYPE_FUNC(s8, s8, "%d") |
| 42 | DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d") |
| 43 | DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d") |
| 44 | DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld") |
| 45 | DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x") |
| 46 | DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x") |
| 47 | DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x") |
| 48 | DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx") |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 49 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 50 | /* Print type function for string type */ |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 51 | int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 52 | { |
| 53 | int len = *(u32 *)data >> 16; |
| 54 | |
| 55 | if (!len) |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 56 | trace_seq_puts(s, "(fault)"); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 57 | else |
Masami Hiramatsu | 56de763 | 2018-04-25 21:16:36 +0900 | [diff] [blame] | 58 | trace_seq_printf(s, "\"%s\"", |
Steven Rostedt (Red Hat) | d2b0191 | 2014-11-12 17:19:51 -0500 | [diff] [blame] | 59 | (const char *)get_loc_data(data, ent)); |
| 60 | return !trace_seq_has_overflowed(s); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 61 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 62 | NOKPROBE_SYMBOL(PRINT_TYPE_FUNC_NAME(string)); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 63 | |
Namhyung Kim | b26c74e | 2013-11-26 14:19:59 +0900 | [diff] [blame] | 64 | const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\""; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 65 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 66 | #define CHECK_FETCH_FUNCS(method, fn) \ |
| 67 | (((FETCH_FUNC_NAME(method, u8) == fn) || \ |
| 68 | (FETCH_FUNC_NAME(method, u16) == fn) || \ |
| 69 | (FETCH_FUNC_NAME(method, u32) == fn) || \ |
| 70 | (FETCH_FUNC_NAME(method, u64) == fn) || \ |
| 71 | (FETCH_FUNC_NAME(method, string) == fn) || \ |
| 72 | (FETCH_FUNC_NAME(method, string_size) == fn)) \ |
| 73 | && (fn != NULL)) |
| 74 | |
| 75 | /* Data fetch function templates */ |
| 76 | #define DEFINE_FETCH_reg(type) \ |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 77 | void FETCH_FUNC_NAME(reg, type)(struct pt_regs *regs, void *offset, void *dest) \ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 78 | { \ |
| 79 | *(type *)dest = (type)regs_get_register(regs, \ |
| 80 | (unsigned int)((unsigned long)offset)); \ |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 81 | } \ |
| 82 | NOKPROBE_SYMBOL(FETCH_FUNC_NAME(reg, type)); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 83 | DEFINE_BASIC_FETCH_FUNCS(reg) |
| 84 | /* No string on the register */ |
| 85 | #define fetch_reg_string NULL |
| 86 | #define fetch_reg_string_size NULL |
| 87 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 88 | #define DEFINE_FETCH_retval(type) \ |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 89 | void FETCH_FUNC_NAME(retval, type)(struct pt_regs *regs, \ |
| 90 | void *dummy, void *dest) \ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 91 | { \ |
| 92 | *(type *)dest = (type)regs_return_value(regs); \ |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 93 | } \ |
| 94 | NOKPROBE_SYMBOL(FETCH_FUNC_NAME(retval, type)); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 95 | DEFINE_BASIC_FETCH_FUNCS(retval) |
| 96 | /* No string on the retval */ |
| 97 | #define fetch_retval_string NULL |
| 98 | #define fetch_retval_string_size NULL |
| 99 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 100 | /* Dereference memory access function */ |
| 101 | struct deref_fetch_param { |
| 102 | struct fetch_param orig; |
| 103 | long offset; |
Hyeoncheol Lee | 3925f4a | 2013-07-01 13:44:32 +0900 | [diff] [blame] | 104 | fetch_func_t fetch; |
| 105 | fetch_func_t fetch_size; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | #define DEFINE_FETCH_deref(type) \ |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 109 | void FETCH_FUNC_NAME(deref, type)(struct pt_regs *regs, \ |
| 110 | void *data, void *dest) \ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 111 | { \ |
| 112 | struct deref_fetch_param *dprm = data; \ |
| 113 | unsigned long addr; \ |
| 114 | call_fetch(&dprm->orig, regs, &addr); \ |
| 115 | if (addr) { \ |
| 116 | addr += dprm->offset; \ |
Hyeoncheol Lee | 3925f4a | 2013-07-01 13:44:32 +0900 | [diff] [blame] | 117 | dprm->fetch(regs, (void *)addr, dest); \ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 118 | } else \ |
| 119 | *(type *)dest = 0; \ |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 120 | } \ |
| 121 | NOKPROBE_SYMBOL(FETCH_FUNC_NAME(deref, type)); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 122 | DEFINE_BASIC_FETCH_FUNCS(deref) |
| 123 | DEFINE_FETCH_deref(string) |
Hyeoncheol Lee | 3925f4a | 2013-07-01 13:44:32 +0900 | [diff] [blame] | 124 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 125 | void FETCH_FUNC_NAME(deref, string_size)(struct pt_regs *regs, |
| 126 | void *data, void *dest) |
Hyeoncheol Lee | 3925f4a | 2013-07-01 13:44:32 +0900 | [diff] [blame] | 127 | { |
| 128 | struct deref_fetch_param *dprm = data; |
| 129 | unsigned long addr; |
| 130 | |
| 131 | call_fetch(&dprm->orig, regs, &addr); |
| 132 | if (addr && dprm->fetch_size) { |
| 133 | addr += dprm->offset; |
| 134 | dprm->fetch_size(regs, (void *)addr, dest); |
| 135 | } else |
| 136 | *(string_size *)dest = 0; |
| 137 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 138 | NOKPROBE_SYMBOL(FETCH_FUNC_NAME(deref, string_size)); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 139 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 140 | static void update_deref_fetch_param(struct deref_fetch_param *data) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 141 | { |
| 142 | if (CHECK_FETCH_FUNCS(deref, data->orig.fn)) |
| 143 | update_deref_fetch_param(data->orig.data); |
| 144 | else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn)) |
| 145 | update_symbol_cache(data->orig.data); |
| 146 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 147 | NOKPROBE_SYMBOL(update_deref_fetch_param); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 148 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 149 | static void free_deref_fetch_param(struct deref_fetch_param *data) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 150 | { |
| 151 | if (CHECK_FETCH_FUNCS(deref, data->orig.fn)) |
| 152 | free_deref_fetch_param(data->orig.data); |
| 153 | else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn)) |
| 154 | free_symbol_cache(data->orig.data); |
| 155 | kfree(data); |
| 156 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 157 | NOKPROBE_SYMBOL(free_deref_fetch_param); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 158 | |
| 159 | /* Bitfield fetch function */ |
| 160 | struct bitfield_fetch_param { |
| 161 | struct fetch_param orig; |
| 162 | unsigned char hi_shift; |
| 163 | unsigned char low_shift; |
| 164 | }; |
| 165 | |
| 166 | #define DEFINE_FETCH_bitfield(type) \ |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 167 | void FETCH_FUNC_NAME(bitfield, type)(struct pt_regs *regs, \ |
| 168 | void *data, void *dest) \ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 169 | { \ |
| 170 | struct bitfield_fetch_param *bprm = data; \ |
| 171 | type buf = 0; \ |
| 172 | call_fetch(&bprm->orig, regs, &buf); \ |
| 173 | if (buf) { \ |
| 174 | buf <<= bprm->hi_shift; \ |
| 175 | buf >>= bprm->low_shift; \ |
| 176 | } \ |
| 177 | *(type *)dest = buf; \ |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 178 | } \ |
| 179 | NOKPROBE_SYMBOL(FETCH_FUNC_NAME(bitfield, type)); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 180 | DEFINE_BASIC_FETCH_FUNCS(bitfield) |
| 181 | #define fetch_bitfield_string NULL |
| 182 | #define fetch_bitfield_string_size NULL |
| 183 | |
Masami Hiramatsu | fbc1963 | 2014-04-17 17:18:00 +0900 | [diff] [blame] | 184 | static void |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 185 | update_bitfield_fetch_param(struct bitfield_fetch_param *data) |
| 186 | { |
| 187 | /* |
| 188 | * Don't check the bitfield itself, because this must be the |
| 189 | * last fetch function. |
| 190 | */ |
| 191 | if (CHECK_FETCH_FUNCS(deref, data->orig.fn)) |
| 192 | update_deref_fetch_param(data->orig.data); |
| 193 | else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn)) |
| 194 | update_symbol_cache(data->orig.data); |
| 195 | } |
| 196 | |
Masami Hiramatsu | fbc1963 | 2014-04-17 17:18:00 +0900 | [diff] [blame] | 197 | static void |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 198 | free_bitfield_fetch_param(struct bitfield_fetch_param *data) |
| 199 | { |
| 200 | /* |
| 201 | * Don't check the bitfield itself, because this must be the |
| 202 | * last fetch function. |
| 203 | */ |
| 204 | if (CHECK_FETCH_FUNCS(deref, data->orig.fn)) |
| 205 | free_deref_fetch_param(data->orig.data); |
| 206 | else if (CHECK_FETCH_FUNCS(symbol, data->orig.fn)) |
| 207 | free_symbol_cache(data->orig.data); |
| 208 | |
| 209 | kfree(data); |
| 210 | } |
| 211 | |
Omar Sandoval | 35abb67 | 2016-06-08 18:38:02 -0700 | [diff] [blame] | 212 | void FETCH_FUNC_NAME(comm, string)(struct pt_regs *regs, |
| 213 | void *data, void *dest) |
| 214 | { |
| 215 | int maxlen = get_rloc_len(*(u32 *)dest); |
| 216 | u8 *dst = get_rloc_data(dest); |
| 217 | long ret; |
| 218 | |
| 219 | if (!maxlen) |
| 220 | return; |
| 221 | |
| 222 | ret = strlcpy(dst, current->comm, maxlen); |
| 223 | *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(*(u32 *)dest)); |
| 224 | } |
| 225 | NOKPROBE_SYMBOL(FETCH_FUNC_NAME(comm, string)); |
| 226 | |
| 227 | void FETCH_FUNC_NAME(comm, string_size)(struct pt_regs *regs, |
| 228 | void *data, void *dest) |
| 229 | { |
| 230 | *(u32 *)dest = strlen(current->comm) + 1; |
| 231 | } |
| 232 | NOKPROBE_SYMBOL(FETCH_FUNC_NAME(comm, string_size)); |
| 233 | |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 234 | static const struct fetch_type *find_fetch_type(const char *type, |
| 235 | const struct fetch_type *ftbl) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 236 | { |
| 237 | int i; |
| 238 | |
| 239 | if (!type) |
| 240 | type = DEFAULT_FETCH_TYPE_STR; |
| 241 | |
| 242 | /* Special case: bitfield */ |
| 243 | if (*type == 'b') { |
| 244 | unsigned long bs; |
| 245 | |
| 246 | type = strchr(type, '/'); |
| 247 | if (!type) |
| 248 | goto fail; |
| 249 | |
| 250 | type++; |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 251 | if (kstrtoul(type, 0, &bs)) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 252 | goto fail; |
| 253 | |
| 254 | switch (bs) { |
| 255 | case 8: |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 256 | return find_fetch_type("u8", ftbl); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 257 | case 16: |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 258 | return find_fetch_type("u16", ftbl); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 259 | case 32: |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 260 | return find_fetch_type("u32", ftbl); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 261 | case 64: |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 262 | return find_fetch_type("u64", ftbl); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 263 | default: |
| 264 | goto fail; |
| 265 | } |
| 266 | } |
| 267 | |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 268 | for (i = 0; ftbl[i].name; i++) { |
| 269 | if (strcmp(type, ftbl[i].name) == 0) |
| 270 | return &ftbl[i]; |
| 271 | } |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 272 | |
| 273 | fail: |
| 274 | return NULL; |
| 275 | } |
| 276 | |
| 277 | /* Special function : only accept unsigned long */ |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 278 | static void fetch_kernel_stack_address(struct pt_regs *regs, void *dummy, void *dest) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 279 | { |
| 280 | *(unsigned long *)dest = kernel_stack_pointer(regs); |
| 281 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 282 | NOKPROBE_SYMBOL(fetch_kernel_stack_address); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 283 | |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 284 | static void fetch_user_stack_address(struct pt_regs *regs, void *dummy, void *dest) |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame] | 285 | { |
| 286 | *(unsigned long *)dest = user_stack_pointer(regs); |
| 287 | } |
Masami Hiramatsu | 3da0f18 | 2014-04-17 17:18:28 +0900 | [diff] [blame] | 288 | NOKPROBE_SYMBOL(fetch_user_stack_address); |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame] | 289 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 290 | static fetch_func_t get_fetch_size_function(const struct fetch_type *type, |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 291 | fetch_func_t orig_fn, |
| 292 | const struct fetch_type *ftbl) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 293 | { |
| 294 | int i; |
| 295 | |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 296 | if (type != &ftbl[FETCH_TYPE_STRING]) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 297 | return NULL; /* Only string type needs size function */ |
| 298 | |
| 299 | for (i = 0; i < FETCH_MTD_END; i++) |
| 300 | if (type->fetch[i] == orig_fn) |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 301 | return ftbl[FETCH_TYPE_STRSIZE].fetch[i]; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 302 | |
| 303 | WARN_ON(1); /* This should not happen */ |
| 304 | |
| 305 | return NULL; |
| 306 | } |
| 307 | |
| 308 | /* Split symbol and offset. */ |
Masami Hiramatsu | c5d343b | 2018-03-17 21:38:10 +0900 | [diff] [blame] | 309 | int traceprobe_split_symbol_offset(char *symbol, long *offset) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 310 | { |
| 311 | char *tmp; |
| 312 | int ret; |
| 313 | |
| 314 | if (!offset) |
| 315 | return -EINVAL; |
| 316 | |
Masami Hiramatsu | c5d343b | 2018-03-17 21:38:10 +0900 | [diff] [blame] | 317 | tmp = strpbrk(symbol, "+-"); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 318 | if (tmp) { |
Masami Hiramatsu | c5d343b | 2018-03-17 21:38:10 +0900 | [diff] [blame] | 319 | ret = kstrtol(tmp, 0, offset); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 320 | if (ret) |
| 321 | return ret; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 322 | *tmp = '\0'; |
| 323 | } else |
| 324 | *offset = 0; |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) |
| 330 | |
| 331 | static int parse_probe_vars(char *arg, const struct fetch_type *t, |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame] | 332 | struct fetch_param *f, bool is_return, |
| 333 | bool is_kprobe) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 334 | { |
| 335 | int ret = 0; |
| 336 | unsigned long param; |
| 337 | |
| 338 | if (strcmp(arg, "retval") == 0) { |
| 339 | if (is_return) |
| 340 | f->fn = t->fetch[FETCH_MTD_retval]; |
| 341 | else |
| 342 | ret = -EINVAL; |
| 343 | } else if (strncmp(arg, "stack", 5) == 0) { |
| 344 | if (arg[5] == '\0') { |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame] | 345 | if (strcmp(t->name, DEFAULT_FETCH_TYPE_STR)) |
| 346 | return -EINVAL; |
| 347 | |
| 348 | if (is_kprobe) |
| 349 | f->fn = fetch_kernel_stack_address; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 350 | else |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame] | 351 | f->fn = fetch_user_stack_address; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 352 | } else if (isdigit(arg[5])) { |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 353 | ret = kstrtoul(arg + 5, 10, ¶m); |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame] | 354 | if (ret || (is_kprobe && param > PARAM_MAX_STACK)) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 355 | ret = -EINVAL; |
| 356 | else { |
| 357 | f->fn = t->fetch[FETCH_MTD_stack]; |
| 358 | f->data = (void *)param; |
| 359 | } |
| 360 | } else |
| 361 | ret = -EINVAL; |
Omar Sandoval | 35abb67 | 2016-06-08 18:38:02 -0700 | [diff] [blame] | 362 | } else if (strcmp(arg, "comm") == 0) { |
| 363 | if (strcmp(t->name, "string") != 0 && |
| 364 | strcmp(t->name, "string_size") != 0) |
| 365 | return -EINVAL; |
| 366 | f->fn = t->fetch[FETCH_MTD_comm]; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 367 | } else |
| 368 | ret = -EINVAL; |
| 369 | |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | /* Recursive argument parser */ |
| 374 | static int parse_probe_arg(char *arg, const struct fetch_type *t, |
Stephen Rothwell | d9a16d3 | 2015-03-12 16:58:34 +1100 | [diff] [blame] | 375 | struct fetch_param *f, bool is_return, bool is_kprobe, |
| 376 | const struct fetch_type *ftbl) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 377 | { |
| 378 | unsigned long param; |
| 379 | long offset; |
| 380 | char *tmp; |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 381 | int ret = 0; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 382 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 383 | switch (arg[0]) { |
| 384 | case '$': |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame] | 385 | ret = parse_probe_vars(arg + 1, t, f, is_return, is_kprobe); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 386 | break; |
| 387 | |
| 388 | case '%': /* named register */ |
| 389 | ret = regs_query_register_offset(arg + 1); |
| 390 | if (ret >= 0) { |
| 391 | f->fn = t->fetch[FETCH_MTD_reg]; |
| 392 | f->data = (void *)(unsigned long)ret; |
| 393 | ret = 0; |
| 394 | } |
| 395 | break; |
| 396 | |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 397 | case '@': /* memory, file-offset or symbol */ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 398 | if (isdigit(arg[1])) { |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 399 | ret = kstrtoul(arg + 1, 0, ¶m); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 400 | if (ret) |
| 401 | break; |
| 402 | |
| 403 | f->fn = t->fetch[FETCH_MTD_memory]; |
| 404 | f->data = (void *)param; |
Namhyung Kim | b7e0bf3 | 2013-11-25 13:42:47 +0900 | [diff] [blame] | 405 | } else if (arg[1] == '+') { |
| 406 | /* kprobes don't support file offsets */ |
| 407 | if (is_kprobe) |
| 408 | return -EINVAL; |
| 409 | |
| 410 | ret = kstrtol(arg + 2, 0, &offset); |
| 411 | if (ret) |
| 412 | break; |
| 413 | |
| 414 | f->fn = t->fetch[FETCH_MTD_file_offset]; |
| 415 | f->data = (void *)offset; |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 416 | } else { |
Namhyung Kim | b079d37 | 2013-07-03 18:34:23 +0900 | [diff] [blame] | 417 | /* uprobes don't support symbols */ |
| 418 | if (!is_kprobe) |
| 419 | return -EINVAL; |
| 420 | |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 421 | ret = traceprobe_split_symbol_offset(arg + 1, &offset); |
| 422 | if (ret) |
| 423 | break; |
| 424 | |
| 425 | f->data = alloc_symbol_cache(arg + 1, offset); |
| 426 | if (f->data) |
| 427 | f->fn = t->fetch[FETCH_MTD_symbol]; |
| 428 | } |
| 429 | break; |
| 430 | |
| 431 | case '+': /* deref memory */ |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 432 | arg++; /* Skip '+', because kstrtol() rejects it. */ |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 433 | case '-': |
| 434 | tmp = strchr(arg, '('); |
| 435 | if (!tmp) |
| 436 | break; |
| 437 | |
| 438 | *tmp = '\0'; |
Daniel Walter | bcd83ea | 2012-09-26 22:08:38 +0200 | [diff] [blame] | 439 | ret = kstrtol(arg, 0, &offset); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 440 | |
| 441 | if (ret) |
| 442 | break; |
| 443 | |
| 444 | arg = tmp + 1; |
| 445 | tmp = strrchr(arg, ')'); |
| 446 | |
| 447 | if (tmp) { |
| 448 | struct deref_fetch_param *dprm; |
| 449 | const struct fetch_type *t2; |
| 450 | |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 451 | t2 = find_fetch_type(NULL, ftbl); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 452 | *tmp = '\0'; |
| 453 | dprm = kzalloc(sizeof(struct deref_fetch_param), GFP_KERNEL); |
| 454 | |
| 455 | if (!dprm) |
| 456 | return -ENOMEM; |
| 457 | |
| 458 | dprm->offset = offset; |
Hyeoncheol Lee | 3925f4a | 2013-07-01 13:44:32 +0900 | [diff] [blame] | 459 | dprm->fetch = t->fetch[FETCH_MTD_memory]; |
| 460 | dprm->fetch_size = get_fetch_size_function(t, |
| 461 | dprm->fetch, ftbl); |
Srikar Dronamraju | f3f096c | 2012-04-11 16:00:43 +0530 | [diff] [blame] | 462 | ret = parse_probe_arg(arg, t2, &dprm->orig, is_return, |
Stephen Rothwell | d9a16d3 | 2015-03-12 16:58:34 +1100 | [diff] [blame] | 463 | is_kprobe, ftbl); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 464 | if (ret) |
| 465 | kfree(dprm); |
| 466 | else { |
| 467 | f->fn = t->fetch[FETCH_MTD_deref]; |
| 468 | f->data = (void *)dprm; |
| 469 | } |
| 470 | } |
| 471 | break; |
| 472 | } |
| 473 | if (!ret && !f->fn) { /* Parsed, but do not find fetch method */ |
| 474 | pr_info("%s type has no corresponding fetch method.\n", t->name); |
| 475 | ret = -EINVAL; |
| 476 | } |
| 477 | |
| 478 | return ret; |
| 479 | } |
| 480 | |
| 481 | #define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long)) |
| 482 | |
| 483 | /* Bitfield type needs to be parsed into a fetch function */ |
| 484 | static int __parse_bitfield_probe_arg(const char *bf, |
| 485 | const struct fetch_type *t, |
| 486 | struct fetch_param *f) |
| 487 | { |
| 488 | struct bitfield_fetch_param *bprm; |
| 489 | unsigned long bw, bo; |
| 490 | char *tail; |
| 491 | |
| 492 | if (*bf != 'b') |
| 493 | return 0; |
| 494 | |
| 495 | bprm = kzalloc(sizeof(*bprm), GFP_KERNEL); |
| 496 | if (!bprm) |
| 497 | return -ENOMEM; |
| 498 | |
| 499 | bprm->orig = *f; |
| 500 | f->fn = t->fetch[FETCH_MTD_bitfield]; |
| 501 | f->data = (void *)bprm; |
| 502 | bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */ |
| 503 | |
| 504 | if (bw == 0 || *tail != '@') |
| 505 | return -EINVAL; |
| 506 | |
| 507 | bf = tail + 1; |
| 508 | bo = simple_strtoul(bf, &tail, 0); |
| 509 | |
| 510 | if (tail == bf || *tail != '/') |
| 511 | return -EINVAL; |
| 512 | |
| 513 | bprm->hi_shift = BYTES_TO_BITS(t->size) - (bw + bo); |
| 514 | bprm->low_shift = bprm->hi_shift + bo; |
| 515 | |
| 516 | return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0; |
| 517 | } |
| 518 | |
| 519 | /* String length checking wrapper */ |
| 520 | int traceprobe_parse_probe_arg(char *arg, ssize_t *size, |
Stephen Rothwell | d9a16d3 | 2015-03-12 16:58:34 +1100 | [diff] [blame] | 521 | struct probe_arg *parg, bool is_return, bool is_kprobe, |
| 522 | const struct fetch_type *ftbl) |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 523 | { |
| 524 | const char *t; |
| 525 | int ret; |
| 526 | |
| 527 | if (strlen(arg) > MAX_ARGSTR_LEN) { |
| 528 | pr_info("Argument is too long.: %s\n", arg); |
| 529 | return -ENOSPC; |
| 530 | } |
| 531 | parg->comm = kstrdup(arg, GFP_KERNEL); |
| 532 | if (!parg->comm) { |
| 533 | pr_info("Failed to allocate memory for command '%s'.\n", arg); |
| 534 | return -ENOMEM; |
| 535 | } |
| 536 | t = strchr(parg->comm, ':'); |
| 537 | if (t) { |
| 538 | arg[t - parg->comm] = '\0'; |
| 539 | t++; |
| 540 | } |
Omar Sandoval | 35abb67 | 2016-06-08 18:38:02 -0700 | [diff] [blame] | 541 | /* |
| 542 | * The default type of $comm should be "string", and it can't be |
| 543 | * dereferenced. |
| 544 | */ |
| 545 | if (!t && strcmp(arg, "$comm") == 0) |
| 546 | t = "string"; |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 547 | parg->type = find_fetch_type(t, ftbl); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 548 | if (!parg->type) { |
| 549 | pr_info("Unsupported type: %s\n", t); |
| 550 | return -EINVAL; |
| 551 | } |
| 552 | parg->offset = *size; |
| 553 | *size += parg->type->size; |
Stephen Rothwell | d9a16d3 | 2015-03-12 16:58:34 +1100 | [diff] [blame] | 554 | ret = parse_probe_arg(arg, parg->type, &parg->fetch, is_return, |
| 555 | is_kprobe, ftbl); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 556 | |
| 557 | if (ret >= 0 && t != NULL) |
| 558 | ret = __parse_bitfield_probe_arg(t, parg->type, &parg->fetch); |
| 559 | |
| 560 | if (ret >= 0) { |
| 561 | parg->fetch_size.fn = get_fetch_size_function(parg->type, |
Namhyung Kim | 34fee3a | 2013-11-26 14:56:28 +0900 | [diff] [blame] | 562 | parg->fetch.fn, |
| 563 | ftbl); |
Srikar Dronamraju | 8ab83f5 | 2012-04-09 14:41:44 +0530 | [diff] [blame] | 564 | parg->fetch_size.data = parg->fetch.data; |
| 565 | } |
| 566 | |
| 567 | return ret; |
| 568 | } |
| 569 | |
| 570 | /* Return 1 if name is reserved or already used by another argument */ |
| 571 | int traceprobe_conflict_field_name(const char *name, |
| 572 | struct probe_arg *args, int narg) |
| 573 | { |
| 574 | int i; |
| 575 | |
| 576 | for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++) |
| 577 | if (strcmp(reserved_field_names[i], name) == 0) |
| 578 | return 1; |
| 579 | |
| 580 | for (i = 0; i < narg; i++) |
| 581 | if (strcmp(args[i].name, name) == 0) |
| 582 | return 1; |
| 583 | |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | void traceprobe_update_arg(struct probe_arg *arg) |
| 588 | { |
| 589 | if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn)) |
| 590 | update_bitfield_fetch_param(arg->fetch.data); |
| 591 | else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn)) |
| 592 | update_deref_fetch_param(arg->fetch.data); |
| 593 | else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn)) |
| 594 | update_symbol_cache(arg->fetch.data); |
| 595 | } |
| 596 | |
| 597 | void traceprobe_free_probe_arg(struct probe_arg *arg) |
| 598 | { |
| 599 | if (CHECK_FETCH_FUNCS(bitfield, arg->fetch.fn)) |
| 600 | free_bitfield_fetch_param(arg->fetch.data); |
| 601 | else if (CHECK_FETCH_FUNCS(deref, arg->fetch.fn)) |
| 602 | free_deref_fetch_param(arg->fetch.data); |
| 603 | else if (CHECK_FETCH_FUNCS(symbol, arg->fetch.fn)) |
| 604 | free_symbol_cache(arg->fetch.data); |
| 605 | |
| 606 | kfree(arg->name); |
| 607 | kfree(arg->comm); |
| 608 | } |
| 609 | |
Namhyung Kim | 5bf652a | 2013-07-03 16:09:02 +0900 | [diff] [blame] | 610 | static int __set_print_fmt(struct trace_probe *tp, char *buf, int len, |
| 611 | bool is_return) |
| 612 | { |
| 613 | int i; |
| 614 | int pos = 0; |
| 615 | |
| 616 | const char *fmt, *arg; |
| 617 | |
| 618 | if (!is_return) { |
| 619 | fmt = "(%lx)"; |
| 620 | arg = "REC->" FIELD_STRING_IP; |
| 621 | } else { |
| 622 | fmt = "(%lx <- %lx)"; |
| 623 | arg = "REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; |
| 624 | } |
| 625 | |
| 626 | /* When len=0, we just calculate the needed length */ |
| 627 | #define LEN_OR_ZERO (len ? len - pos : 0) |
| 628 | |
| 629 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); |
| 630 | |
| 631 | for (i = 0; i < tp->nr_args; i++) { |
| 632 | pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s", |
| 633 | tp->args[i].name, tp->args[i].type->fmt); |
| 634 | } |
| 635 | |
| 636 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg); |
| 637 | |
| 638 | for (i = 0; i < tp->nr_args; i++) { |
| 639 | if (strcmp(tp->args[i].type->name, "string") == 0) |
| 640 | pos += snprintf(buf + pos, LEN_OR_ZERO, |
| 641 | ", __get_str(%s)", |
| 642 | tp->args[i].name); |
| 643 | else |
| 644 | pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s", |
| 645 | tp->args[i].name); |
| 646 | } |
| 647 | |
| 648 | #undef LEN_OR_ZERO |
| 649 | |
| 650 | /* return the length of print_fmt */ |
| 651 | return pos; |
| 652 | } |
| 653 | |
| 654 | int set_print_fmt(struct trace_probe *tp, bool is_return) |
| 655 | { |
| 656 | int len; |
| 657 | char *print_fmt; |
| 658 | |
| 659 | /* First: called with 0 length to calculate the needed length */ |
| 660 | len = __set_print_fmt(tp, NULL, 0, is_return); |
| 661 | print_fmt = kmalloc(len + 1, GFP_KERNEL); |
| 662 | if (!print_fmt) |
| 663 | return -ENOMEM; |
| 664 | |
| 665 | /* Second: actually write the @print_fmt */ |
| 666 | __set_print_fmt(tp, print_fmt, len + 1, is_return); |
| 667 | tp->call.print_fmt = print_fmt; |
| 668 | |
| 669 | return 0; |
| 670 | } |
Masami Hiramatsu | eeb07b0 | 2018-04-25 21:17:05 +0900 | [diff] [blame^] | 671 | |
| 672 | int traceprobe_define_arg_fields(struct trace_event_call *event_call, |
| 673 | size_t offset, struct trace_probe *tp) |
| 674 | { |
| 675 | int ret, i; |
| 676 | |
| 677 | /* Set argument names as fields */ |
| 678 | for (i = 0; i < tp->nr_args; i++) { |
| 679 | struct probe_arg *parg = &tp->args[i]; |
| 680 | |
| 681 | ret = trace_define_field(event_call, parg->type->fmttype, |
| 682 | parg->name, |
| 683 | offset + parg->offset, |
| 684 | parg->type->size, |
| 685 | parg->type->is_signed, |
| 686 | FILTER_OTHER); |
| 687 | if (ret) |
| 688 | return ret; |
| 689 | } |
| 690 | return 0; |
| 691 | } |