implied: cleanup debug output a little
[smatch.git] / smatch_db.c
blob8e72da1a8227f3c15a8dd0dac13f4e080ffd20bd
1 /*
2 * smatch/smatch_db.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <string.h>
11 #include <errno.h>
12 #include <sqlite3.h>
13 #include "smatch.h"
14 #include "smatch_slist.h"
15 #include "smatch_extra.h"
17 static sqlite3 *db;
19 struct def_callback {
20 int hook_type;
21 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
23 ALLOCATOR(def_callback, "definition db hook callbacks");
24 DECLARE_PTR_LIST(callback_list, struct def_callback);
25 static struct callback_list *callbacks;
27 struct member_info_callback {
28 int owner;
29 void (*callback)(char *fn, int param, char *printed_name, struct smatch_state *state);
31 ALLOCATOR(member_info_callback, "caller_info callbacks");
32 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
33 static struct member_info_cb_list *member_callbacks;
35 struct call_implies_callback {
36 int type;
37 void (*callback)(struct expression *arg, char *value);
39 ALLOCATOR(call_implies_callback, "call_implies callbacks");
40 DECLARE_PTR_LIST(call_implies_cb_list, struct call_implies_callback);
41 static struct call_implies_cb_list *call_implies_cb_list;
43 void sql_exec(int (*callback)(void*, int, char**, char**), const char *sql)
45 char *err = NULL;
46 int rc;
48 if (option_no_db || !db)
49 return;
51 rc = sqlite3_exec(db, sql, callback, 0, &err);
52 if (rc != SQLITE_OK)
53 fprintf(stderr, "SQL error #2: %s\n", err);
56 void add_definition_db_callback(void (*callback)(const char *name, struct symbol *sym, char *key, char *value), int type)
58 struct def_callback *def_callback = __alloc_def_callback(0);
60 def_callback->hook_type = type;
61 def_callback->callback = callback;
62 add_ptr_list(&callbacks, def_callback);
65 void add_member_info_callback(int owner, void (*callback)(char *fn, int param, char *printed_name, struct smatch_state *state))
67 struct member_info_callback *member_callback = __alloc_member_info_callback(0);
69 member_callback->owner = owner;
70 member_callback->callback = callback;
71 add_ptr_list(&member_callbacks, member_callback);
74 void add_db_fn_call_callback(int type, void (*callback)(struct expression *arg, char *value))
76 struct call_implies_callback *cb = __alloc_call_implies_callback(0);
78 cb->type = type;
79 cb->callback = callback;
80 add_ptr_list(&call_implies_cb_list, cb);
83 static struct range_list *return_range_list;
84 static int db_return_callback(void *unused, int argc, char **argv, char **azColName)
86 struct range_list *rl = NULL;
88 if (argc != 1)
89 return 0;
91 get_value_ranges(argv[0], &rl);
92 return_range_list = range_list_union(return_range_list, rl);
94 return 0;
97 struct range_list *db_return_vals(struct expression *expr)
99 struct symbol *sym;
100 static char sql_filter[1024];
102 if (expr->type != EXPR_CALL)
103 return NULL;
104 if (expr->fn->type != EXPR_SYMBOL)
105 return NULL;
106 sym = expr->fn->symbol;
107 if (!sym)
108 return NULL;
110 if (sym->ctype.modifiers & MOD_STATIC) {
111 snprintf(sql_filter, 1024, "file = '%s' and function = '%s' and type = %d;",
112 get_filename(), sym->ident->name, RETURN_VALUE);
113 } else {
114 snprintf(sql_filter, 1024, "function = '%s' and type = %d;",
115 sym->ident->name, RETURN_VALUE);
118 return_range_list = NULL;
119 run_sql(db_return_callback, "select value from return_info where %s",
120 sql_filter);
121 return return_range_list;
124 static void match_call_hack(struct expression *expr)
126 char *name;
129 * we just want to record something in the database so that if we have
130 * two calls like: frob(4); frob(some_unkown); then on the recieving
131 * side we know that sometimes frob is called with unknown parameters.
134 name = get_fnptr_name(expr->fn);
135 if (!name)
136 return;
137 if (ptr_list_empty(expr->args))
138 return;
139 sm_msg("info: passes param_value '%s' -1 '$$' min-max", name);
140 free_string(name);
143 static void print_struct_members(char *fn, struct expression *expr, int param, struct state_list *slist,
144 void (*callback)(char *fn, int param, char *printed_name, struct smatch_state *state))
146 struct sm_state *sm;
147 char *name;
148 struct symbol *sym;
149 int len;
150 char printed_name[256];
151 int is_address = 0;
153 expr = strip_expr(expr);
154 if (expr->type == EXPR_PREOP && expr->op == '&') {
155 expr = strip_expr(expr->unop);
156 is_address = 1;
159 name = get_variable_from_expr(expr, &sym);
160 if (!name || !sym)
161 goto free;
163 len = strlen(name);
164 FOR_EACH_PTR(slist, sm) {
165 if (sm->sym != sym)
166 continue;
167 if (strncmp(name, sm->name, len) || sm->name[len] == '\0')
168 continue;
169 if (is_address)
170 snprintf(printed_name, sizeof(printed_name), "$$->%s", sm->name + len + 1);
171 else
172 snprintf(printed_name, sizeof(printed_name), "$$%s", sm->name + len);
173 callback(fn, param, printed_name, sm->state);
174 } END_FOR_EACH_PTR(sm);
175 free:
176 free_string(name);
179 static void match_call_info(struct expression *expr)
181 struct member_info_callback *cb;
182 struct expression *arg;
183 struct state_list *slist;
184 char *name;
185 int i;
187 name = get_fnptr_name(expr->fn);
188 if (!name)
189 return;
191 FOR_EACH_PTR(member_callbacks, cb) {
192 slist = get_all_states(cb->owner);
193 i = 0;
194 FOR_EACH_PTR(expr->args, arg) {
195 print_struct_members(name, arg, i, slist, cb->callback);
196 i++;
197 } END_FOR_EACH_PTR(arg);
198 } END_FOR_EACH_PTR(cb);
200 free_string(name);
201 free_slist(&slist);
204 static int get_param(int param, char **name, struct symbol **sym)
206 struct symbol *arg;
207 int i;
209 i = 0;
210 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
212 * this is a temporary hack to work around a bug (I think in sparse?)
213 * 2.6.37-rc1:fs/reiserfs/journal.o
214 * If there is a function definition without parameter name found
215 * after a function implementation then it causes a crash.
216 * int foo() {}
217 * int bar(char *);
219 if (arg->ident->name < (char *)100)
220 continue;
221 if (i == param && arg->ident->name) {
222 *name = arg->ident->name;
223 *sym = arg;
224 return TRUE;
226 i++;
227 } END_FOR_EACH_PTR(arg);
229 return FALSE;
232 static struct state_list *final_states;
233 static int prev_func_id = -1;
234 static int db_callback(void *unused, int argc, char **argv, char **azColName)
236 int func_id;
237 long type;
238 long param;
239 char *name;
240 struct symbol *sym;
241 struct def_callback *def_callback;
243 if (argc != 5)
244 return 0;
246 func_id = atoi(argv[0]);
247 errno = 0;
248 type = strtol(argv[1], NULL, 10);
249 param = strtol(argv[2], NULL, 10);
250 if (errno)
251 return 0;
253 if (prev_func_id == -1)
254 prev_func_id = func_id;
255 if (func_id != prev_func_id) {
256 merge_slist(&final_states, __pop_fake_cur_slist());
257 __push_fake_cur_slist();
258 __unnullify_path();
259 prev_func_id = func_id;
262 if (param == -1 || !get_param(param, &name, &sym))
263 return 0;
265 FOR_EACH_PTR(callbacks, def_callback) {
266 if (def_callback->hook_type == type)
267 def_callback->callback(name, sym, argv[3], argv[4]);
268 } END_FOR_EACH_PTR(def_callback);
270 return 0;
273 static void get_direct_callers(struct symbol *sym)
275 char sql_filter[1024];
277 if (sym->ctype.modifiers & MOD_STATIC) {
278 snprintf(sql_filter, 1024,
279 "file = '%s' and function = '%s' order by function_id;",
280 get_filename(), sym->ident->name);
281 } else {
282 snprintf(sql_filter, 1024,
283 "function = '%s' order by function_id;",
284 sym->ident->name);
287 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
288 " where %s", sql_filter);
291 static char *ptr_name;
292 static int get_ptr_name(void *unused, int argc, char **argv, char **azColName)
294 if (!ptr_name)
295 ptr_name = alloc_string(argv[0]);
296 return 0;
299 static void get_function_pointer_callers(struct symbol *sym)
301 ptr_name = NULL;
302 run_sql(get_ptr_name, "select ptr from function_ptr where function = '%s'",
303 sym->ident->name);
304 if (!ptr_name)
305 return;
307 run_sql(db_callback, "select function_id, type, parameter, key, value from caller_info"
308 " where function = '%s' order by function_id", ptr_name);
311 static void match_data_from_db(struct symbol *sym)
313 struct sm_state *sm;
315 if (!sym || !sym->ident || !sym->ident->name)
316 return;
318 __push_fake_cur_slist();
319 __unnullify_path();
320 prev_func_id = -1;
322 get_direct_callers(sym);
323 get_function_pointer_callers(sym);
325 merge_slist(&final_states, __pop_fake_cur_slist());
327 FOR_EACH_PTR(final_states, sm) {
328 __set_sm(sm);
329 } END_FOR_EACH_PTR(sm);
331 free_slist(&final_states);
334 static void match_function_assign(struct expression *expr)
336 struct expression *right = expr->right;
337 struct symbol *sym;
338 char *fn_name;
339 char *ptr_name;
341 if (right->type == EXPR_PREOP && right->op == '&')
342 right = right->unop;
343 if (right->type != EXPR_SYMBOL)
344 return;
345 sym = get_type(right);
346 if (!sym || sym->type != SYM_FN)
347 return;
349 fn_name = get_variable_from_expr(right, NULL);
350 ptr_name = get_fnptr_name(expr->left);
351 if (!fn_name || !ptr_name)
352 goto free;
354 sm_msg("info: sets_fn_ptr '%s' '%s'", ptr_name, fn_name);
356 free:
357 free_string(fn_name);
358 free_string(ptr_name);
361 static struct expression *call_implies_call_expr;
362 static int call_implies_callbacks(void *unused, int argc, char **argv, char **azColName)
364 struct call_implies_callback *cb;
365 struct expression *arg;
366 int type;
367 int param;
369 if (argc != 4)
370 return 0;
372 type = atoi(argv[1]);
373 param = atoi(argv[2]);
375 FOR_EACH_PTR(call_implies_cb_list, cb) {
376 if (cb->type != type)
377 continue;
378 arg = get_argument_from_call_expr(call_implies_call_expr->args, param);
379 if (!arg)
380 continue;
381 cb->callback(arg, argv[3]);
382 } END_FOR_EACH_PTR(cb);
384 return 0;
387 static void match_call_implies(struct expression *expr)
389 struct symbol *sym;
390 static char sql_filter[1024];
392 if (expr->fn->type != EXPR_SYMBOL)
393 return;
394 sym = expr->fn->symbol;
395 if (!sym)
396 return;
398 if (sym->ctype.modifiers & MOD_STATIC) {
399 snprintf(sql_filter, 1024, "file = '%s' and function = '%s';",
400 get_filename(), sym->ident->name);
401 } else {
402 snprintf(sql_filter, 1024, "function = '%s';",
403 sym->ident->name);
406 call_implies_call_expr = expr;
407 run_sql(call_implies_callbacks,
408 "select function, type, parameter, value from call_implies where %s",
409 sql_filter);
410 return;
413 static void print_initializer_list(struct expression_list *expr_list,
414 struct symbol *struct_type)
416 struct expression *expr;
417 struct symbol *base_type;
419 FOR_EACH_PTR(expr_list, expr) {
420 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
421 print_initializer_list(expr->idx_expression->expr_list, struct_type);
422 continue;
424 if (expr->type != EXPR_IDENTIFIER)
425 continue;
426 if (!expr->expr_ident)
427 continue;
428 if (!expr->ident_expression || !expr->ident_expression->symbol_name)
429 continue;
430 base_type = get_type(expr->ident_expression);
431 if (!base_type || base_type->type != SYM_FN)
432 continue;
433 sm_msg("info: sets_fn_ptr '(struct %s)->%s' '%s'", struct_type->ident->name,
434 expr->expr_ident->name,
435 expr->ident_expression->symbol_name->name);
436 } END_FOR_EACH_PTR(expr);
440 static void global_variable(struct symbol *sym)
442 struct symbol *struct_type;
444 if (!sym->ident)
445 return;
446 if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
447 return;
448 struct_type = get_base_type(sym);
449 if (!struct_type)
450 return;
451 if (struct_type->type == SYM_ARRAY) {
452 struct_type = get_base_type(struct_type);
453 if (!struct_type)
454 return;
456 if (struct_type->type != SYM_STRUCT || !struct_type->ident)
457 return;
458 print_initializer_list(sym->initializer->expr_list, struct_type);
461 void open_smatch_db(void)
463 #ifdef SQLITE_OPEN_READONLY
464 int rc;
466 if (option_no_db)
467 return;
469 rc = sqlite3_open_v2("smatch_db.sqlite", &db, SQLITE_OPEN_READONLY, NULL);
470 if (rc != SQLITE_OK) {
471 option_no_db = 1;
472 return;
474 return;
475 #else
476 option_no_db = 1;
477 return;
478 #endif
481 void register_definition_db_callbacks(int id)
483 if (option_info) {
484 add_hook(&match_call_info, FUNCTION_CALL_HOOK);
485 add_hook(&match_call_hack, FUNCTION_CALL_HOOK);
486 add_hook(&match_function_assign, ASSIGNMENT_HOOK);
487 add_hook(&global_variable, BASE_HOOK);
488 add_hook(&global_variable, DECLARATION_HOOK);
491 if (option_no_db)
492 return;
494 add_hook(&match_data_from_db, FUNC_DEF_HOOK);
495 add_hook(&match_call_implies, FUNCTION_CALL_HOOK);