4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
14 #include "smatch_slist.h"
15 #include "smatch_extra.h"
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 void sql_exec(int (*callback
)(void*, int, char**, char**), const char *sql
)
32 if (option_no_db
|| !db
)
35 rc
= sqlite3_exec(db
, sql
, callback
, 0, &err
);
36 if (rc
!= SQLITE_OK
) {
37 fprintf(stderr
, "SQL error #2: %s\n", err
);
42 void add_definition_db_callback(void (*callback
)(const char *name
, struct symbol
*sym
, char *key
, char *value
), int type
)
44 struct def_callback
*def_callback
= __alloc_def_callback(0);
46 def_callback
->hook_type
= type
;
47 def_callback
->callback
= callback
;
48 add_ptr_list(&callbacks
, def_callback
);
51 static unsigned long call_count
;
52 static int db_count_callback(void *unused
, int argc
, char **argv
, char **azColName
)
54 call_count
= strtoul(argv
[0], NULL
, 10);
58 static int get_param(int param
, char **name
, struct symbol
**sym
)
64 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
66 * this is a temporary hack to work around a bug (I think in sparse?)
67 * 2.6.37-rc1:fs/reiserfs/journal.o
68 * If there is a function definition without parameter name found
69 * after a function implementation then it causes a crash.
73 if (arg
->ident
->name
< (char *)100)
75 if (i
== param
&& arg
->ident
->name
) {
76 *name
= arg
->ident
->name
;
81 } END_FOR_EACH_PTR(arg
);
86 static struct state_list
*final_states
;
87 static int prev_func_id
= -1;
88 static int db_callback(void *unused
, int argc
, char **argv
, char **azColName
)
95 struct def_callback
*def_callback
;
100 func_id
= atoi(argv
[0]);
101 type
= atoi(argv
[1]);
103 param
= strtoul(argv
[2], NULL
, 10);
107 if (prev_func_id
== -1)
108 prev_func_id
= func_id
;
109 if (func_id
!= prev_func_id
) {
110 merge_slist(&final_states
, __pop_fake_cur_slist());
111 __push_fake_cur_slist();
112 prev_func_id
= func_id
;
115 if (!get_param(param
, &name
, &sym
))
118 FOR_EACH_PTR(callbacks
, def_callback
) {
119 if (def_callback
->hook_type
== type
)
120 def_callback
->callback(name
, sym
, argv
[3], argv
[4]);
121 } END_FOR_EACH_PTR(def_callback
);
126 static void match_data_from_db(struct symbol
*sym
)
128 char sql_filter
[1024];
131 if (!sym
|| !sym
->ident
|| !sym
->ident
->name
)
134 if (sym
->ctype
.modifiers
& MOD_STATIC
) {
135 snprintf(sql_filter
, 1024,
136 "file = '%s' and function = '%s' order by function_id;",
137 get_filename(), sym
->ident
->name
);
139 snprintf(sql_filter
, 1024,
140 "function = '%s' order by function_id;",
145 run_sql(db_count_callback
, "select count(*) from caller_info where %s",
147 if (call_count
== 0 || call_count
> 100)
151 __push_fake_cur_slist();
152 run_sql(db_callback
, "select function_id, type, parameter, key, value from caller_info"
153 " where %s", sql_filter
);
154 merge_slist(&final_states
, __pop_fake_cur_slist());
156 FOR_EACH_PTR(final_states
, sm
) {
158 } END_FOR_EACH_PTR(sm
);
160 free_slist(&final_states
);
163 void open_smatch_db(void)
165 #ifdef SQLITE_OPEN_READONLY
171 rc
= sqlite3_open_v2("smatch_db.sqlite", &db
, SQLITE_OPEN_READONLY
, NULL
);
172 if (rc
!= SQLITE_OK
) {
183 void register_definition_db_callbacks(int id
)
185 #ifdef SQLITE_OPEN_READONLY
186 add_hook(&match_data_from_db
, FUNC_DEF_HOOK
);