From 0101e2f9b933d712acac3a1470654c0a921d6d30 Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Wed, 13 Jan 2021 13:12:34 +0300 Subject: [PATCH] debug: add __smatch_debug_var() This lets you do __smatch_debug_var("variable_name"); to track a given variable and __smatch_debug_off(); to turn it off. Signed-off-by: Dan Carpenter --- check_debug.c | 13 +++++++++++++ check_debug.h | 1 + smatch.c | 1 + smatch.h | 3 ++- smatch_slist.c | 2 +- smatch_states.c | 22 ++++++++++++---------- 6 files changed, 30 insertions(+), 12 deletions(-) diff --git a/check_debug.c b/check_debug.c index 578b2675..f7bb09c7 100644 --- a/check_debug.c +++ b/check_debug.c @@ -494,9 +494,21 @@ static void match_debug_check(const char *fn, struct expression *expr, void *inf sm_msg("arg = '%s'", option_debug_check); } +static void match_debug_var(const char *fn, struct expression *expr, void *info) +{ + struct expression *arg; + + arg = get_argument_from_call_expr(expr->args, 0); + if (!arg || arg->type != EXPR_STRING) + return; + option_debug_var = arg->string->data; + sm_msg("debug var = '%s'", option_debug_var); +} + static void match_debug_off(const char *fn, struct expression *expr, void *info) { option_debug_check = NULL; + option_debug_var = NULL; option_debug = 0; } @@ -825,6 +837,7 @@ void check_debug(int id) add_function_hook("__smatch_compare", &match_compare, NULL); add_function_hook("__smatch_debug_on", &match_debug_on, NULL); add_function_hook("__smatch_debug_check", &match_debug_check, NULL); + add_function_hook("__smatch_debug_var", &match_debug_var, NULL); add_function_hook("__smatch_debug_off", &match_debug_off, NULL); add_function_hook("__smatch_local_debug_on", &match_local_debug_on, NULL); add_function_hook("__smatch_local_debug_off", &match_local_debug_off, NULL); diff --git a/check_debug.h b/check_debug.h index f89b52df..16723f54 100644 --- a/check_debug.h +++ b/check_debug.h @@ -41,6 +41,7 @@ static inline void __smatch_compare(long long one, long long two){} static inline void __smatch_debug_on(void){} static inline void __smatch_debug_check(const char *check_name){} +static inline void __smatch_debug_var(const char *var_name){} static inline void __smatch_debug_off(void){} static inline void __smatch_local_debug_on(void){} diff --git a/smatch.c b/smatch.c index 93358404..3a4a46c1 100644 --- a/smatch.c +++ b/smatch.c @@ -23,6 +23,7 @@ #include "check_list.h" char *option_debug_check; +char *option_debug_var; char *option_project_str = (char *)"smatch_generic"; static char *option_db_file = (char *)"smatch_db.sqlite"; enum project_type option_project = PROJ_NONE; diff --git a/smatch.h b/smatch.h index b49aa8f9..3d714b10 100644 --- a/smatch.h +++ b/smatch.h @@ -214,7 +214,7 @@ extern int option_debug; extern int local_debug; extern int debug_db; bool debug_implied(void); -bool debug_on(const char *check_name); +bool debug_on(const char *check_name, const char *var); extern int option_info; extern int option_spammy; extern int option_print_names; @@ -1067,6 +1067,7 @@ struct token *get_tokens_file(const char *filename); /* smatch.c */ extern char *option_debug_check; +extern char *option_debug_var; extern char *option_project_str; extern char *bin_dir; extern char *data_dir; diff --git a/smatch_slist.c b/smatch_slist.c index cff16f3f..789269d8 100644 --- a/smatch_slist.c +++ b/smatch_slist.c @@ -494,7 +494,7 @@ struct sm_state *merge_sm_states(struct sm_state *one, struct sm_state *two) if (result->state == two->state) result->line = two->line; - if (debug_on(check_name(one->owner))) { + if (debug_on(check_name(one->owner), one->name)) { struct sm_state *tmp; int i = 0; diff --git a/smatch_states.c b/smatch_states.c index 015b7721..7e2ce7b7 100644 --- a/smatch_states.c +++ b/smatch_states.c @@ -148,13 +148,15 @@ void allocate_tracker_array(int num_checks) memset(tracker_hooks, 0, num_checks * sizeof(void *)); } -bool debug_on(const char *check_name) +bool debug_on(const char *check_name, const char *var) { if (option_debug) return true; - if (!option_debug_check) - return false; - return strstr(check_name, option_debug_check); + if (option_debug_check && strstr(check_name, option_debug_check)) + return true; + if (option_debug_var && strcmp(var, option_debug_var) == 0) + return true; + return false; } struct sm_state *set_state(int owner, const char *name, struct symbol *sym, struct smatch_state *state) @@ -167,7 +169,7 @@ struct sm_state *set_state(int owner, const char *name, struct symbol *sym, stru if (read_only) sm_perror("cur_stree is read only."); - if (debug_on(check_name(owner))) { + if (debug_on(check_name(owner), name)) { struct smatch_state *s; s = __get_state(owner, name, sym); @@ -276,7 +278,7 @@ void __set_sm(struct sm_state *sm) if (read_only) sm_perror("cur_stree is read only."); - if (debug_on(check_name(sm->owner))) { + if (debug_on(check_name(sm->owner), sm->name)) { struct smatch_state *s; s = __get_state(sm->owner, sm->name, sm->sym); @@ -301,7 +303,7 @@ void __set_sm_cur_stree(struct sm_state *sm) if (read_only) sm_perror("cur_stree is read only."); - if (debug_on(check_name(sm->owner))) { + if (debug_on(check_name(sm->owner), sm->name)) { struct smatch_state *s; s = __get_state(sm->owner, sm->name, sm->sym); @@ -323,7 +325,7 @@ void __set_sm_fake_stree(struct sm_state *sm) if (read_only) sm_perror("cur_stree is read only."); - if (debug_on(check_name(sm->owner))) { + if (debug_on(check_name(sm->owner), sm->name)) { struct smatch_state *s; s = __get_state(sm->owner, sm->name, sm->sym); @@ -582,7 +584,7 @@ void set_true_false_states(int owner, const char *name, struct symbol *sym, if (read_only) sm_perror("cur_stree is read only."); - if (debug_on(check_name(owner))) { + if (debug_on(check_name(owner), name)) { struct smatch_state *tmp; tmp = __get_state(owner, name, sym); @@ -636,7 +638,7 @@ void __set_true_false_sm(struct sm_state *true_sm, struct sm_state *false_sm) owner = true_sm ? true_sm->owner : false_sm->owner; name = true_sm ? true_sm->name : false_sm->name; sym = true_sm ? true_sm->sym : false_sm->sym; - if (debug_on(check_name(owner))) { + if (debug_on(check_name(owner), name)) { struct smatch_state *tmp; tmp = __get_state(owner, name, sym); -- 2.11.4.GIT