/* * trace_events_filter - generic event filtering * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Copyright (C) 2009 Tom Zanussi */ #include #include #include #include #include #include "trace.h" #include "trace_output.h" #define DEFAULT_SYS_FILTER_MESSAGE \ "### global filter ###\n" \ "# Use this to set filters for multiple events.\n" \ "# Only events with the given fields will be affected.\n" \ "# If no events are modified, an error message will be displayed here" enum filter_op_ids { OP_OR, OP_AND, OP_GLOB, OP_NE, OP_EQ, OP_LT, OP_LE, OP_GT, OP_GE, OP_NONE, OP_OPEN_PAREN, }; struct filter_op { int id; char *string; int precedence; }; static struct filter_op filter_ops[] = { { OP_OR, "||", 1 }, { OP_AND, "&&", 2 }, { OP_GLOB, "~", 4 }, { OP_NE, "!=", 4 }, { OP_EQ, "==", 4 }, { OP_LT, "<", 5 }, { OP_LE, "<=", 5 }, { OP_GT, ">", 5 }, { OP_GE, ">=", 5 }, { OP_NONE, "OP_NONE", 0 }, { OP_OPEN_PAREN, "(", 0 }, }; enum { FILT_ERR_NONE, FILT_ERR_INVALID_OP, FILT_ERR_UNBALANCED_PAREN, FILT_ERR_TOO_MANY_OPERANDS, FILT_ERR_OPERAND_TOO_LONG, FILT_ERR_FIELD_NOT_FOUND, FILT_ERR_ILLEGAL_FIELD_OP, FILT_ERR_ILLEGAL_INTVAL, FILT_ERR_BAD_SUBSYS_FILTER, FILT_ERR_TOO_MANY_PREDS, FILT_ERR_MISSING_FIELD, FILT_ERR_INVALID_FILTER, FILT_ERR_IP_FIELD_ONLY, }; static char *err_text[] = { "No error", "Invalid operator", "Unbalanced parens", "Too many operands", "Operand too long", "Field not found", "Illegal operation for field type", "Illegal integer value", "Couldn't find or set field in one of a subsystem's events", "Too many terms in predicate expression", "Missing field name and/or value", "Meaningless filter expression", "Only 'ip' field is supported for function trace", }; struct opstack_op { int op; struct list_head list; }; struct postfix_elt { int op; char *operand; struct list_head list; }; struct filter_parse_state { struct filter_op *ops; struct list_head opstack; struct list_head postfix; int lasterr; int lasterr_pos; struct { char *string; unsigned int cnt; unsigned int tail; } infix; struct { char string[MAX_FILTER_STR_VAL]; int pos; unsigned int tail; } operand; }; struct pred_stack { struct filter_pred **preds; int index; }; #define DEFINE_COMPARISON_PRED(type) \ static int filter_pred_##type(struct filter_pred *pred, void *event) \ { \ type *addr = (type *)(event + pred->offset); \ type val = (type)pred->val; \ int match = 0; \ \ switch (pred->op) { \ case OP_LT: \ match = (*addr < val); \ break; \ case OP_LE: \ match = (*addr <= val); \ break; \ case OP_GT: \ match = (*addr > val); \ break; \ case OP_GE: \ match = (*addr >= val); \ break; \ default: \ break; \ } \ \ return match; \ } #define DEFINE_EQUALITY_PRED(size) \ static int filter_pred_##size(struct filter_pred *pred, void *event) \ { \ u##size *addr = (u##size *)(event + pred->offset); \ u##size val = (u##size)pred->val; \ int match; \ \ match = (val == *addr) ^ pred->not; \ \ return match; \ } DEFINE_COMPARISON_PRED(s64); DEFINE_COMPARISON_PRED(u64); DEFINE_COMPARISON_PRED(s32); DEFINE_COMPARISON_PRED(u32); DEFINE_COMPARISON_PRED(s16); DEFINE_COMPARISON_PRED(u16); DEFINE_COMPARISON_PRED(s8); DEFINE_COMPARISON_PRED(u8); DEFINE_EQUALITY_PRED(64); DEFINE_EQUALITY_PRED(32); DEFINE_EQUALITY_PRED(16); DEFINE_EQUALITY_PRED(8); /* Filter predicate for fixed sized arrays of characters */ static int filter_pred_string(struct filter_pred *pred, void *event) { char *addr = (char *)(event + pred->offset); int cmp, match; cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len); match = cmp ^ pred->not; return match; } /* Filter predicate for char * pointers */ static int filter_pred_pchar(struct filter_pred *pred, void *event) { char **addr = (char **)(event + pred->offset); int cmp, match; int len = strlen(*addr) + 1; /* including tailing '\0' */ cmp = pred->regex.match(*addr, &pred->regex, len); match = cmp ^ pred->not; return match; } /* * Filter predicate for dynamic sized arrays of characters. * These are implemented through a list of strings at the end * of the entry. * Also each of these strings have a field in the entry which * contains its offset from the beginning of the entry. * We have then first to get this field, dereference it * and add it to the address of the entry, and at last we have * the address of the string. */ static int filter_pred_strloc(struct filter_pred *pred, void *event) { u32 str_item = *(u32 *)(event + pred->offset); int str_loc = str_item & 0xffff; int str_len = str_item >> 16; char *addr = (char *)(event + str_loc); int cmp, match; cmp = pred->regex.match(addr, &pred->regex, str_len); match = cmp ^ pred->not; return match; } static int filter_pred_none(struct filter_pred *pred, void *event) { return 0; } /* * regex_match_foo - Basic regex callbacks * * @str: the string to be searched * @r: the regex structure containing the pattern string * @len: the length of the string to be searched (including '\0') * * Note: * - @str might not be NULL-terminated if it's of type DYN_STRING * or STATIC_STRING */ static int regex_match_full(char *str, struct regex *r, int len) { if (strncmp(str, r->pattern, len) == 0) return 1; return 0; } static int regex_match_front(char *str, struct regex *r, int len) { if (strncmp(str, r->pattern, r->len) == 0) return 1; return 0; } static int regex_match_middle(char *str, struct regex *r, int len) { if (strnstr(str, r->pattern, len)) return 1; return 0; } static int regex_match_end(char *str, struct regex *r, int len) { int strlen = len - 1; if (strlen >= r->len && memcmp(str + strlen - r->len, r->pattern, r->len) == 0) return 1; return 0; } /** * filter_parse_regex - parse a basic regex * @buff: the raw regex * @len: length of the regex * @search: will point to the beginning of the string to compare * @not: tell whether the match will have to be inverted * * This passes in a buffer containing a regex and this function will * set search to point to the search part of the buffer and * return the type of search it is (see enum above). * This does modify buff. * * Returns enum type. * search returns the pointer to use for comparison. * not returns 1 if buff started with a '!' * 0 otherwise. */ enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not) { int type = MATCH_FULL; int i; if (buff[0] == '!') { *not = 1; buff++; len--; } else *not = 0; *search = buff; for (i = 0; i < len; i++) { if (buff[i] == '*') { if (!i) { *search = buff + 1; type = MATCH_END_ONLY; } else { if (type == MATCH_END_ONLY) type = MATCH_MIDDLE_ONLY; else type = MATCH_FRONT_ONLY; buff[i] = 0; break; } } } return type; } static void filter_build_regex(struct filter_pred *pred) { struct regex *r = &pred->regex; char *search; enum regex_type type = MATCH_FULL; int not = 0; if (pred->op == OP_GLOB) { type = filter_parse_regex(r->pattern, r->len, &search, ¬); r->len = strlen(search); memmove(r->pattern, search, r->len+1); } switch (type) { case MATCH_FULL: r->match = regex_match_full; break; case MATCH_FRONT_ONLY: r->match = regex_match_front; break; case MATCH_MIDDLE_ONLY: r->match = regex_match_middle; break; case MATCH_END_ONLY: r->match = regex_match_end; break; } pred->not ^= not; } enum move_type { MOVE_DOWN, MOVE_UP_FROM_LEFT, MOVE_UP_FROM_RIGHT }; static struct filter_pred * get_pred_parent(struct filter_pred *pred, struct filter_pred *preds, int index, enum move_type *move) { if (pred->parent & FILTER_PRED_IS_RIGHT) *move = MOVE_UP_FROM_RIGHT; else *move = MOVE_UP_FROM_LEFT; pred = &preds[pred->parent & ~FILTER_PRED_IS_RIGHT]; return pred; } enum walk_return { WALK_PRED_ABORT, WALK_PRED_PARENT, WALK_PRED_DEFAULT, }; typedef int (*filter_pred_walkcb_t) (enum move_type move, struct filter_pred *pred, int *err, void *data); static int walk_pred_tree(struct filter_pred *preds, struct filter_pred *root, filter_pred_walkcb_t cb, void *data) { struct filter_pred *pred = root; enum move_type move = MOVE_DOWN; int done = 0; if (!preds) return -EINVAL; do { int err = 0, ret; ret = cb(move, pred, &err, data); if (ret == WALK_PRED_ABORT) return err; if (ret == WALK_PRED_PARENT) goto get_parent; switch (move) { case MOVE_DOWN: if (pred->left != FILTER_PRED_INVALID) { pred = &preds[pred->left]; continue; } goto get_parent; case MOVE_UP_FROM_LEFT: pred = &preds[pred->right]; move = MOVE_DOWN; continue; case MOVE_UP_FROM_RIGHT: get_parent: if (pred == root) break; pred = get_pred_parent(pred, preds, pred->parent, &move); continue; } done = 1; } while (!done); /* We are fine. */ return 0; } /* * A series of AND or ORs where found together. Instead of * climbing up and down the tree branches, an array of the * ops were made in order of checks. We can just move across * the array and short circuit if needed. */ static int process_ops(struct filter_pred *preds, struct filter_pred *op, void *rec) { struct filter_pred *pred; int match = 0; int type; int i; /* * Micro-optimization: We set type to true if op * is an OR and false otherwise (AND). Then we * just need to test if the match is equal to * the type, and if it is, we can short circuit the * rest of the checks: * * if ((match && op->op == OP_OR) || * (!match && op->op == OP_AND)) * return match; */ type = op->op == OP_OR; for (i = 0; i < op->val; i++) { pred = &preds[op->ops[i]]; if (!WARN_ON_ONCE(!pred->fn)) match = pred->fn(pred, rec); if (!!match == type) return match; } return match; } struct filter_match_preds_data { struct filter_pred *preds; int match; void *rec; }; static int filter_match_preds_cb(enum move_type move, struct filter_pred *pred, int *err, void *data) { struct filter_match_preds_data *d = data; *err = 0; switch (move) { case MOVE_DOWN: /* only AND and OR have children */ if (pred->left != FILTER_PRED_INVALID) { /* If ops is set, then it was folded. */ if (!pred->ops) return WALK_PRED_DEFAULT; /* We can treat folded ops as a leaf node */ d->match = process_ops(d->preds, pred, d->rec); } else { if (!WARN_ON_ONCE(!pred->fn)) d->match = pred->fn(pred, d->rec); } return WALK_PRED_PARENT; case MOVE_UP_FROM_LEFT: /* * Check for short circuits. * * Optimization: !!match == (pred->op == OP_OR) * is the same as: * if ((match && pred->op == OP_OR) || * (!match && pred->op == OP_AND)) */ if (!!d->match == (pred->op == OP_OR)) return WALK_PRED_PARENT; break; case MOVE_UP_FROM_RIGHT: break; } return WALK_PRED_DEFAULT; } /* return 1 if event matches, 0 otherwise (discard) */ int filter_match_preds(struct event_filter *filter, void *rec) { struct filter_pred *preds; struct filter_pred *root; struct filter_match_preds_data data = { /* match is currently meaningless */ .match = -1, .rec = rec, }; int n_preds, ret; /* no filter is considered a match */ if (!filter) return 1; n_preds = filter->n_preds; if (!n_preds) return 1; /* * n_preds, root and filter->preds are protect with preemption disabled. */ root = rcu_dereference_sched(filter->root); if (!root) return 1; data.preds = preds = rcu_dereference_sched(filter->preds); ret = walk_pred_tree(preds, root, filter_match_preds_cb, &data); WARN_ON(ret); return data.match; } EXPORT_SYMBOL_GPL(filter_match_preds); static void parse_error(struct filter_parse_state *ps, int err, int pos) { ps->lasterr = err; ps->lasterr_pos = pos; } static void remove_filter_string(struct event_filter *filter) { if (!filter) return; kfree(filter->filter_string); filter->filter_string = NULL; } static int replace_filter_string(struct event_filter *filter, char *filter_string) { kfree(filter->filter_string); filter->filter_string = kstrdup(filter_string, GFP_KERNEL); if (!filter->filter_string) return -ENOMEM; return 0; } static int append_filter_string(struct event_filter *filter, char *string) { int newlen; char *new_filter_string; BUG_ON(!filter->filter_string); newlen = strlen(filter->filter_string) + strlen(string) + 1; new_filter_string = kmalloc(newlen, GFP_KERNEL); if (!new_filter_string) return -ENOMEM; strcpy(new_filter_string, filter->filter_string); strcat(new_filter_string, string); kfree(filter->filter_string); filter->filter_string = new_filter_string; return 0; } static void append_filter_err(struct filter_parse_state *ps, struct event_filter *filter) { int pos = ps->lasterr_pos; char *buf, *pbuf; buf = (char *)__get_free_page(GFP_TEMPORARY); if (!buf) return; append_filter_string(filter, "\n"); memset(buf, ' ', PAGE_SIZE); if (pos > PAGE_SIZE - 128) pos = 0; buf[pos] = '^'; pbuf = &buf[pos] + 1; sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]); append_filter_string(filter, buf); free_page((unsigned long) buf); } void print_event_filter(struct ftrace_event_call *call, struct trace_seq *s) { struct event_filter *filter; mutex_lock(&event_mutex); filter = call->filter; if (filter && filter->filter_string) trace_seq_printf(s, "%s\n", filter->filter_string); else trace_seq_printf(s, "none\n"); mutex_unlock(&event_mutex); } void print_subsystem_event_filter(struct event_subsystem *system, struct trace_seq *s) { struct event_filter *filter; mutex_lock(&event_mutex); filter = system->filter; if (filter && filter->filter_string) trace_seq_printf(s, "%s\n", filter->filter_string); else trace_seq_printf(s, DEFAULT_SYS_FILTER_MESSAGE "\n"); mutex_unlock(&event_mutex); } static int __alloc_pred_stack(struct pred_stack *stack, int n_preds) { stack->preds = kcalloc(n_preds + 1, sizeof(*stack->preds), GFP_KERNEL); if (!stack->preds) return -ENOMEM; stack->index = n_preds; return 0; } static void __free_pred_stack(struct pred_stack *stack) { kfree(stack->preds); stack->index = 0; } static int __push_pred_stack(struct pred_stack *stack, struct filter_pred *pred) { int index = stack->index; if (WARN_ON(index == 0)) return -ENOSPC; stack->preds[--index] = pred; stack->index = index; return 0; } static struct filter_pred * __pop_pred_stack(struct pred_stack *stack) { struct filter_pred *pred; int index = stack->index; pred = stack->preds[index++]; if (!pred) return NULL; stack->index = index; return pred; } static int filter_set_pred(struct event_filter *filter, int idx, struct pred_stack *stack, struct filter_pred *src) { struct filter_pred *dest = &filter->preds[idx]; struct filter_pred *left; struct filter_pred *right; *dest = *src; dest->index = idx; if (dest->op == OP_OR || dest->op == OP_AND) { right = __pop_pred_stack(stack); left = __pop_pred_stack(stack); if (!left || !right) return -EINVAL; /* * If both children can be folded * and they are the same op as this op or a leaf, * then this op can be folded. */ if (left->index & FILTER_PRED_FOLD && (left->op == dest->op || left->left == FILTER_PRED_INVALID) && right->index & FILTER_PRED_FOLD && (right->op == dest->op || right->left == FILTER_PRED_INVALID)) dest->index |= FILTER_PRED_FOLD; dest->left = left->index & ~FILTER_PRED_FOLD; dest->right = right->index & ~FILTER_PRED_FOLD; left->parent = dest->index & ~FILTER_PRED_FOLD; right->parent = dest->index | FILTER_PRED_IS_RIGHT; } else { /* * Make dest->left invalid to be used as a quick * way to know this is a leaf node. */ dest->left = FILTER_PRED_INVALID; /* All leafs allow folding the parent ops. */ dest->index |= FILTER_PRED_FOLD; } return __push_pred_stack(stack, dest); } static void __free_preds(struct event_filter *filter) { int i; if (filter->preds) { for (i = 0; i < filter->n_preds; i++) kfree(filter->preds[i].ops); kfree(filter->preds); filter->preds = NULL; } filter->a_preds = 0; filter->n_preds = 0; } static void filter_disable(struct ftrace_event_call *call) { call->flags &= ~TRACE_EVENT_FL_FILTERED; } static void __free_filter(struct event_filter *filter) { if (!filter) return; __free_preds(filter); kfree(filter->filter_string); kfree(filter); } /* * Called when destroying the ftrace_event_call. * The call is being freed, so we do not need to worry about * the call being currently used. This is for module code removing * the tracepoints from within it. */ void destroy_preds(struct ftrace_event_call *call) { __free_filter(call->filter); call->filter = NULL; } static struct event_filter *__alloc_filter(void) { struct event_filter *filter; filter = kzalloc(sizeof(*filter), GFP_KERNEL); return filter; } static int __alloc_preds(struct event_filter *filter, int n_preds) { struct filter_pred *pred; int i; if (filter->preds) __free_preds(filter); filter->preds = kcalloc(n_preds, sizeof(*filter->preds), GFP_KERNEL); if (!filter->preds) return -ENOMEM; filter->a_preds = n_preds; filter->n_preds = 0; for (i = 0; i < n_preds; i++) { pred = &filter->preds[i]; pred->fn = filter_pred_none; } return 0; } static void filter_free_subsystem_preds(struct event_subsystem *system) { struct ftrace_event_call *call; list_for_each_entry(call, &ftrace_events, list) { if (strcmp(call->class->system, system->name) != 0) continue; filter_disable(call); remove_filter_string(call->filter); } } static void filter_free_subsystem_filters(struct event_subsystem *system) { struct ftrace_event_call *call; list_for_each_entry(call, &ftrace_events, list) { if (strcmp(call->class->system, system->name) != 0) continue; __free_filter(call->filter); call->filter = NULL; } } static int filter_add_pred(struct filter_parse_state *ps, struct event_filter *filter, struct filter_pred *pred, struct pred_stack *stack) { int err; if (WARN_ON(filter->n_preds == filter->a_preds)) { parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0); return -ENOSPC; } err = filter_set_pred(filter, filter->n_preds, stack, pred); if (err) return err; filter->n_preds++; return 0; } int filter_assign_type(const char *type) { if (strstr(type, "__data_loc") && strstr(type, "char")) return FILTER_DYN_STRING; if (strchr(type, '[') && strstr(type, "char")) return FILTER_STATIC_STRING; return FILTER_OTHER; } static bool is_function_field(struct ftrace_event_field *field) { return field->filter_type == FILTER_TRACE_FN; } static bool is_string_field(struct ftrace_event_field *field) { return field->filter_type == FILTER_DYN_STRING || field->filter_type == FILTER_STATIC_STRING || field->filter_type == FILTER_PTR_STRING; } static int is_legal_op(struct ftrace_event_field *field, int op) { if (is_string_field(field) && (op != OP_EQ && op != OP_NE && op != OP_GLOB)) return 0; if (!is_string_field(field) && op == OP_GLOB) return 0; return 1; } static filter_pred_fn_t select_comparison_fn(int op, int field_size, int field_is_signed) { filter_pred_fn_t fn = NULL; switch (field_size) { case 8: if (op == OP_EQ || op == OP_NE) fn = filter_pred_64; else if (field_is_signed) fn = filter_pred_s64; else fn = filter_pred_u64; break; case 4: if (op == OP_EQ || op == OP_NE) fn = filter_pred_32; else if (field_is_signed) fn = filter_pred_s32; else fn = filter_pred_u32; break; case 2: if (op == OP_EQ || op == OP_NE) fn = filter_pred_16; else if (field_is_signed) fn = filter_pred_s16; else fn = filter_pred_u16; break; case 1: if (op == OP_EQ || op == OP_NE) fn = filter_pred_8; else if (field_is_signed) fn = filter_pred_s8; else fn = filter_pred_u8; break; } return fn; } static int init_pred(struct filter_parse_state *ps, struct ftrace_event_field *field, struct filter_pred *pred) { filter_pred_fn_t fn = filter_pred_none; unsigned long long val; int ret; pred->offset = field->offset; if (!is_legal_op(field, pred->op)) { parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0); return -EINVAL; } if (is_string_field(field)) { filter_build_regex(pred); if (field->filter_type == FILTER_STATIC_STRING) { fn = filter_pred_string; pred->regex.field_len = field->size; } else if (field->filter_type == FILTER_DYN_STRING) fn = filter_pred_strloc; else fn = filter_pred_pchar; } else if (is_function_field(field)) { if (strcmp(field->name, "ip")) { parse_error(ps, FILT_ERR_IP_FIELD_ONLY, 0); return -EINVAL; } } else { if (field->is_signed) ret = kstrtoll(pred->regex.pattern, 0, &val); else ret = kstrtoull(pred->regex.pattern, 0, &val); if (ret) { parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0); return -EINVAL; } pred->val = val; fn = select_comparison_fn(pred->op, field->size, field->is_signed); if (!fn) { parse_error(ps, FILT_ERR_INVALID_OP, 0); return -EINVAL; } } if (pred->op == OP_NE) pred->not = 1; pred->fn = fn; return 0; } static void parse_init(struct filter_parse_state *ps, struct filter_op *ops, char *infix_string) { memset(ps, '\0', sizeof(*ps)); ps->infix.string = infix_string; ps->infix.cnt = strlen(infix_string); ps->ops = ops; INIT_LIST_HEAD(&ps->opstack); INIT_LIST_HEAD(&ps->postfix); } static char infix_next(struct filter_parse_state *ps) { ps->infix.cnt--; return ps->infix.string[ps->infix.tail++]; } static char infix_peek(struct filter_parse_state *ps) { if (ps->infix.tail == strlen(ps->infix.string)) return 0; return ps->infix.string[ps->infix.tail]; } static void infix_advance(struct filter_parse_state *ps) { ps->infix.cnt--; ps->infix.tail++; } static inline int is_precedence_lower(struct filter_parse_state *ps, int a, int b) { return ps->ops[a]./* * docproc is a simple preprocessor for the template files * used as placeholders for the kernel internal documentation. * docproc is used for documentation-frontend and * dependency-generator. * The two usages have in common that they require * some knowledge of the .tmpl syntax, therefore they * are kept together. * * documentation-frontend * Scans the template file and call kernel-doc for * all occurrences of ![EIF]file * Beforehand each referenced file are scanned for * any exported sympols "EXPORT_SYMBOL()" statements. * This is used to create proper -function and * -nofunction arguments in calls to kernel-doc. * Usage: docproc doc file.tmpl * * dependency-generator: * Scans the template file and list all files * referenced in a format recognized by make. * Usage: docproc depend file.tmpl * Writes dependency information to stdout * in the following format: * file.tmpl src.c src2.c * The filenames are obtained from the following constructs: * !Efilename * !Ifilename * !Dfilename * !Ffilename * */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <unistd.h> #include <limits.h> #include <sys/types.h> #include <sys/wait.h> /* exitstatus is used to keep track of any failing calls to kernel-doc, * but execution continues. */ int exitstatus = 0; typedef void DFL(char *); DFL *defaultline; typedef void FILEONLY(char * file); FILEONLY *internalfunctions; FILEONLY *externalfunctions; FILEONLY *symbolsonly; typedef void FILELINE(char * file, char * line); FILELINE * singlefunctions; FILELINE * entity_system; #define MAXLINESZ 2048 #define MAXFILES 250 #define KERNELDOCPATH "scripts/" #define KERNELDOC "kernel-doc" #define DOCBOOK "-docbook" #define FUNCTION "-function" #define NOFUNCTION "-nofunction" void usage (void) { fprintf(stderr, "Usage: docproc {doc|depend} file\n"); fprintf(stderr, "Input is read from file.tmpl. Output is sent to stdout\n"); fprintf(stderr, "doc: frontend when generating kernel documentation\n"); fprintf(stderr, "depend: generate list of files referenced within file\n"); } /* * Execute kernel-doc with parameters givin in svec */ void exec_kernel_doc(char **svec) { pid_t pid; int ret; char real_filename[PATH_MAX + 1]; /* Make sure output generated so far are flushed */ fflush(stdout); switch(pid=fork()) { case -1: perror("fork"); exit(1); case 0: memset(real_filename, 0, sizeof(real_filename)); strncat(real_filename, getenv("SRCTREE"), PATH_MAX); strncat(real_filename, KERNELDOCPATH KERNELDOC, PATH_MAX - strlen(real_filename)); execvp(real_filename, svec); fprintf(stderr, "exec "); perror(real_filename); exit(1); default: waitpid(pid, &ret ,0); } if (WIFEXITED(ret)) exitstatus |= WEXITSTATUS(ret); else exitstatus = 0xff; } /* Types used to create list of all exported symbols in a number of files */ struct symbols { char *name; }; struct symfile { char *filename; struct symbols *symbollist; int symbolcnt; }; struct symfile symfilelist[MAXFILES]; int symfilecnt = 0; void add_new_symbol(struct symfile *sym, char * symname) { sym->symbollist = realloc(sym->symbollist, (sym->symbolcnt + 1) * sizeof(char *)); sym->symbollist[sym->symbolcnt++].name = strdup(symname); } /* Add a filename to the list */ struct symfile * add_new_file(char * filename) { symfilelist[symfilecnt++].filename = strdup(filename); return &symfilelist[symfilecnt - 1]; } /* Check if file already are present in the list */ struct symfile * filename_exist(char * filename) { int i; for (i=0; i < symfilecnt; i++) if (strcmp(symfilelist[i].filename, filename) == 0) return &symfilelist[i]; return NULL; } /* * List all files referenced within the template file. * Files are separated by tabs. */ void adddep(char * file) { printf("\t%s", file); } void adddep2(char * file, char * line) { line = line; adddep(file); } void noaction(char * line) { line = line; } void noaction2(char * file, char * line) { file = file; line = line; } /* Echo the line without further action */ void printline(char * line) { printf("%s", line); } /* * Find all symbols exported with EXPORT_SYMBOL and EXPORT_SYMBOL_GPL * in filename. * All symbols located are stored in symfilelist. */ void find_export_symbols(char * filename) { FILE * fp; struct symfile *sym; char line[MAXLINESZ]; if (filename_exist(filename) == NULL) { char real_filename[PATH_MAX + 1]; memset(real_filename, 0, sizeof(real_filename)); strncat(real_filename, getenv("SRCTREE"), PATH_MAX); strncat(real_filename, filename, PATH_MAX - strlen(real_filename)); sym = add_new_file(filename); fp = fopen(real_filename, "r"); if (fp == NULL) { fprintf(stderr, "docproc: "); perror(real_filename); } while(fgets(line, MAXLINESZ, fp)) { char *p; char *e; if (((p = strstr(line, "EXPORT_SYMBOL_GPL")) != 0) || ((p = strstr(line, "EXPORT_SYMBOL")) != 0)) { /* Skip EXPORT_SYMBOL{_GPL} */ while (isalnum(*p) || *p == '_') p++; /* Remove paranteses and additional ws */ while (isspace(*p)) p++; if (*p != '(') continue; /* Syntax error? */ else p++; while (isspace(*p)) p++; e = p; while (isalnum(*e) || *e == '_') e++; *e = '\0'; add_new_symbol(sym, p); } } fclose(fp); } } /* * Document all external or internal functions in a file. * Call kernel-doc with following parameters: * kernel-doc -docbook -nofunction function_name1 filename * function names are obtained from all the the src files * by find_export_symbols. * intfunc uses -nofunction * extfunc uses -function */ void docfunctions(char * filename, char * type) { int i,j; int symcnt = 0; int idx = 0; char **vec; for (i=0; i <= symfilecnt; i++) symcnt += symfilelist[i].symbolcnt; vec = malloc((2 + 2 * symcnt + 2) * sizeof(char*)); if (vec == NULL) { perror("docproc: "); exit(1); } vec[idx++] = KERNELDOC; vec[idx++] = DOCBOOK; for (i=0; i < symfilecnt; i++) { struct symfile * sym = &symfilelist[i]; for (j=0; j < sym->symbolcnt; j++) { vec[idx++] = type; vec[idx++] = sym->symbollist[j].name; } } vec[idx++] = filename; vec[idx] = NULL; printf("<!-- %s -->\n", filename); exec_kernel_doc(vec); fflush(stdout); free(vec); } void intfunc(char * filename) { docfunctions(filename, NOFUNCTION); } void extfunc(char * filename) { docfunctions(filename, FUNCTION); } /* * Document spåecific function(s) in a file. * Call kernel-doc with the following parameters: * kernel-doc -docbook -function function1 [-function function2] */ void singfunc(char * filename, char * line) { char *vec[200]; /* Enough for specific functions */