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