LCOV - code coverage report
Current view: top level - kernel/trace - trace_probe.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 264 0.0 %
Date: 2014-04-16 Functions: 0 47 0.0 %
Branches: 0 381 0.0 %

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

Generated by: LCOV version 1.9