kernel, db: ignore calls to (struct file_operations)->open()
[smatch.git] / check_debug.c
blob13ef2390cb9b78dc60b486ade4dc71bde43255a8
1 /*
2 * sparse/check_debug.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include "smatch.h"
11 #include "smatch_slist.h"
12 #include "smatch_extra.h"
14 int local_debug;
15 static int my_id;
17 static void match_all_values(const char *fn, struct expression *expr, void *info)
19 struct state_list *slist;
21 slist = get_all_states(SMATCH_EXTRA);
22 __print_slist(slist);
23 free_slist(&slist);
26 static void match_cur_slist(const char *fn, struct expression *expr, void *info)
28 __print_cur_slist();
31 static void match_state(const char *fn, struct expression *expr, void *info)
33 struct expression *check_arg, *state_arg;
34 struct sm_state *sm;
35 int found = 0;
37 check_arg = get_argument_from_call_expr(expr->args, 0);
38 if (check_arg->type != EXPR_STRING) {
39 sm_msg("error: the check_name argument to %s is supposed to be a string literal", fn);
40 return;
42 state_arg = get_argument_from_call_expr(expr->args, 1);
43 if (!state_arg || state_arg->type != EXPR_STRING) {
44 sm_msg("error: the state_name argument to %s is supposed to be a string literal", fn);
45 return;
48 FOR_EACH_PTR(__get_cur_slist(), sm) {
49 if (strcmp(check_name(sm->owner), check_arg->string->data) != 0)
50 continue;
51 if (strcmp(sm->name, state_arg->string->data) != 0)
52 continue;
53 sm_msg("'%s' = '%s'", sm->name, sm->state->name);
54 found = 1;
55 } END_FOR_EACH_PTR(sm);
57 if (!found)
58 sm_msg("%s '%s' not found", check_arg->string->data, state_arg->string->data);
61 static void match_states(const char *fn, struct expression *expr, void *info)
63 struct expression *check_arg;
64 struct sm_state *sm;
65 int found = 0;
67 check_arg = get_argument_from_call_expr(expr->args, 0);
68 if (check_arg->type != EXPR_STRING) {
69 sm_msg("error: the check_name argument to %s is supposed to be a string literal", fn);
70 return;
73 FOR_EACH_PTR(__get_cur_slist(), sm) {
74 if (strcmp(check_name(sm->owner), check_arg->string->data) != 0)
75 continue;
76 sm_msg("'%s' = '%s'", sm->name, sm->state->name);
77 found = 1;
78 } END_FOR_EACH_PTR(sm);
80 if (!found)
81 sm_msg("%s: no states", check_arg->string->data);
84 static void match_print_value(const char *fn, struct expression *expr, void *info)
86 struct state_list *slist;
87 struct sm_state *tmp;
88 struct expression *arg_expr;
90 arg_expr = get_argument_from_call_expr(expr->args, 0);
91 if (arg_expr->type != EXPR_STRING) {
92 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
93 return;
96 slist = get_all_states(SMATCH_EXTRA);
97 FOR_EACH_PTR(slist, tmp) {
98 if (!strcmp(tmp->name, arg_expr->string->data))
99 sm_msg("%s = %s", tmp->name, tmp->state->name);
100 } END_FOR_EACH_PTR(tmp);
101 free_slist(&slist);
104 static void match_print_implied(const char *fn, struct expression *expr, void *info)
106 struct expression *arg;
107 struct range_list *rl = NULL;
108 char *name;
110 arg = get_argument_from_call_expr(expr->args, 0);
111 get_implied_rl(arg, &rl);
113 name = expr_to_str(arg);
114 sm_msg("implied: %s = '%s'", name, show_rl(rl));
115 free_string(name);
118 static void match_print_implied_min(const char *fn, struct expression *expr, void *info)
120 struct expression *arg;
121 sval_t sval;
122 char *name;
124 arg = get_argument_from_call_expr(expr->args, 0);
125 name = expr_to_str(arg);
127 if (get_implied_min(arg, &sval))
128 sm_msg("implied min: %s = %s", name, sval_to_str(sval));
129 else
130 sm_msg("implied min: %s = <unknown>", name);
132 free_string(name);
135 static void match_print_implied_max(const char *fn, struct expression *expr, void *info)
137 struct expression *arg;
138 sval_t sval;
139 char *name;
141 arg = get_argument_from_call_expr(expr->args, 0);
142 name = expr_to_str(arg);
144 if (get_implied_max(arg, &sval))
145 sm_msg("implied max: %s = %s", name, sval_to_str(sval));
146 else
147 sm_msg("implied max: %s = <unknown>", name);
149 free_string(name);
152 static void match_print_hard_max(const char *fn, struct expression *expr, void *info)
154 struct expression *arg;
155 sval_t sval;
156 char *name;
158 arg = get_argument_from_call_expr(expr->args, 0);
159 name = expr_to_str(arg);
161 if (get_hard_max(arg, &sval))
162 sm_msg("hard max: %s = %s", name, sval_to_str(sval));
163 else
164 sm_msg("hard max: %s = <unknown>", name);
166 free_string(name);
169 static void match_print_fuzzy_max(const char *fn, struct expression *expr, void *info)
171 struct expression *arg;
172 sval_t sval;
173 char *name;
175 arg = get_argument_from_call_expr(expr->args, 0);
176 name = expr_to_str(arg);
178 if (get_fuzzy_max(arg, &sval))
179 sm_msg("fuzzy max: %s = %s", name, sval_to_str(sval));
180 else
181 sm_msg("fuzzy max: %s = <unknown>", name);
183 free_string(name);
186 static void match_print_absolute_min(const char *fn, struct expression *expr, void *info)
188 struct expression *arg;
189 sval_t sval;
190 char *name;
192 arg = get_argument_from_call_expr(expr->args, 0);
193 name = expr_to_str(arg);
195 if (get_absolute_min(arg, &sval))
196 sm_msg("absolute min: %s = %s", name, sval_to_str(sval));
197 else
198 sm_msg("absolute min: %s = <unknown>", name);
200 free_string(name);
203 static void match_print_absolute_max(const char *fn, struct expression *expr, void *info)
205 struct expression *arg;
206 sval_t sval;
207 char *name;
209 arg = get_argument_from_call_expr(expr->args, 0);
210 get_absolute_max(arg, &sval);
212 name = expr_to_str(arg);
213 sm_msg("absolute max: %s = %s", name, sval_to_str(sval));
214 free_string(name);
217 static void match_sval_info(const char *fn, struct expression *expr, void *info)
219 struct expression *arg;
220 sval_t sval;
221 char *name;
223 arg = get_argument_from_call_expr(expr->args, 0);
224 name = expr_to_str(arg);
226 if (!get_implied_value(arg, &sval)) {
227 sm_msg("no sval for '%s'", name);
228 goto free;
231 sm_msg("implied: %s %c%d ->value = %llx", name, sval_unsigned(sval) ? 'u' : 's', sval_bits(sval), sval.value);
232 free:
233 free_string(name);
236 static void match_member_name(const char *fn, struct expression *expr, void *info)
238 struct expression *arg;
239 char *name, *member_name;
241 arg = get_argument_from_call_expr(expr->args, 0);
242 name = expr_to_str(arg);
243 member_name = get_member_name(arg);
244 sm_msg("member name: '%s => %s'", name, member_name);
245 free_string(member_name);
246 free_string(name);
249 static void print_possible(struct sm_state *sm)
251 struct sm_state *tmp;
253 sm_msg("Possible values for %s", sm->name);
254 FOR_EACH_PTR(sm->possible, tmp) {
255 printf("%s\n", tmp->state->name);
256 } END_FOR_EACH_PTR(tmp);
257 sm_msg("===");
260 static void match_possible(const char *fn, struct expression *expr, void *info)
262 struct state_list *slist;
263 struct sm_state *tmp;
264 struct expression *arg_expr;
266 arg_expr = get_argument_from_call_expr(expr->args, 0);
267 if (arg_expr->type != EXPR_STRING) {
268 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
269 return;
272 slist = get_all_states(SMATCH_EXTRA);
273 FOR_EACH_PTR(slist, tmp) {
274 if (!strcmp(tmp->name, arg_expr->string->data))
275 print_possible(tmp);
276 } END_FOR_EACH_PTR(tmp);
277 free_slist(&slist);
280 static void match_strlen(const char *fn, struct expression *expr, void *info)
282 struct expression *arg;
283 struct range_list *rl;
284 char *name;
286 arg = get_argument_from_call_expr(expr->args, 0);
287 get_implied_strlen(arg, &rl);
289 name = expr_to_str(arg);
290 sm_msg("strlen: '%s' %s characters", name, show_rl(rl));
291 free_string(name);
294 static void match_buf_size(const char *fn, struct expression *expr, void *info)
296 struct expression *arg;
297 int elements, bytes;
298 char *name;
300 arg = get_argument_from_call_expr(expr->args, 0);
301 elements = get_array_size(arg);
302 bytes = get_array_size_bytes(arg);
304 name = expr_to_str(arg);
305 sm_msg("buf size: '%s' %d elements, %d bytes", name, elements, bytes);
306 free_string(name);
309 static void match_buf_size_rl(const char *fn, struct expression *expr, void *info)
311 struct expression *arg;
312 struct range_list *rl;
313 int elements, bytes;
314 char *name;
316 arg = get_argument_from_call_expr(expr->args, 0);
317 rl = get_array_size_bytes_rl(arg);
318 elements = get_array_size(arg);
319 bytes = get_array_size_bytes(arg);
321 name = expr_to_str(arg);
322 sm_msg("buf size: '%s' %s %d elements, %d bytes", name, show_rl(rl), elements, bytes);
323 free_string(name);
326 static void match_note(const char *fn, struct expression *expr, void *info)
328 struct expression *arg_expr;
330 arg_expr = get_argument_from_call_expr(expr->args, 0);
331 if (arg_expr->type != EXPR_STRING) {
332 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
333 return;
335 sm_msg("%s", arg_expr->string->data);
338 static void print_related(struct sm_state *sm)
340 struct relation *rel;
342 if (!estate_related(sm->state))
343 return;
345 sm_prefix();
346 sm_printf("%s: ", sm->name);
347 FOR_EACH_PTR(estate_related(sm->state), rel) {
348 sm_printf("%s ", rel->name);
349 } END_FOR_EACH_PTR(rel);
350 sm_printf("\n");
353 static void match_dump_related(const char *fn, struct expression *expr, void *info)
355 struct state_list *slist;
356 struct sm_state *tmp;
358 slist = get_all_states(SMATCH_EXTRA);
359 FOR_EACH_PTR(slist, tmp) {
360 print_related(tmp);
361 } END_FOR_EACH_PTR(tmp);
362 free_slist(&slist);
365 static void match_compare(const char *fn, struct expression *expr, void *info)
367 struct expression *one, *two;
368 char *one_name, *two_name;
369 int comparison;
370 char buf[16];
372 one = get_argument_from_call_expr(expr->args, 0);
373 two = get_argument_from_call_expr(expr->args, 1);
375 comparison = get_comparison(one, two);
376 if (!comparison)
377 snprintf(buf, sizeof(buf), "<none>");
378 else
379 snprintf(buf, sizeof(buf), "%s", show_special(comparison));
381 one_name = expr_to_str(one);
382 two_name = expr_to_str(two);
384 sm_msg("%s %s %s", one_name, buf, two_name);
386 free_string(one_name);
387 free_string(two_name);
390 static void match_debug_on(const char *fn, struct expression *expr, void *info)
392 option_debug = 1;
395 static void match_debug_off(const char *fn, struct expression *expr, void *info)
397 option_debug = 0;
400 static void match_local_debug_on(const char *fn, struct expression *expr, void *info)
402 local_debug = 1;
405 static void match_local_debug_off(const char *fn, struct expression *expr, void *info)
407 local_debug = 0;
410 static void match_debug_implied_on(const char *fn, struct expression *expr, void *info)
412 option_debug_implied = 1;
415 static void match_debug_implied_off(const char *fn, struct expression *expr, void *info)
417 option_debug_implied = 0;
420 static void match_about(const char *fn, struct expression *expr, void *info)
422 struct expression *arg;
423 struct sm_state *sm;
424 char *name;
426 sm_msg("---- about ----");
427 match_print_implied(fn, expr, NULL);
428 match_buf_size(fn, expr, NULL);
429 match_strlen(fn, expr, NULL);
431 arg = get_argument_from_call_expr(expr->args, 0);
432 name = expr_to_str(arg);
433 if (!name) {
434 sm_msg("info: not a straight forward variable.");
435 return;
438 FOR_EACH_PTR(__get_cur_slist(), sm) {
439 if (strcmp(sm->name, name) != 0)
440 continue;
441 sm_msg("%s '%s' = '%s'", check_name(sm->owner), sm->name, sm->state->name);
442 } END_FOR_EACH_PTR(sm);
445 void check_debug(int id)
447 my_id = id;
448 add_function_hook("__smatch_about", &match_about, NULL);
449 add_function_hook("__smatch_all_values", &match_all_values, NULL);
450 add_function_hook("__smatch_state", &match_state, NULL);
451 add_function_hook("__smatch_states", &match_states, NULL);
452 add_function_hook("__smatch_value", &match_print_value, NULL);
453 add_function_hook("__smatch_implied", &match_print_implied, NULL);
454 add_function_hook("__smatch_implied_min", &match_print_implied_min, NULL);
455 add_function_hook("__smatch_implied_max", &match_print_implied_max, NULL);
456 add_function_hook("__smatch_hard_max", &match_print_hard_max, NULL);
457 add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max, NULL);
458 add_function_hook("__smatch_absolute_min", &match_print_absolute_min, NULL);
459 add_function_hook("__smatch_absolute_max", &match_print_absolute_max, NULL);
460 add_function_hook("__smatch_sval_info", &match_sval_info, NULL);
461 add_function_hook("__smatch_member_name", &match_member_name, NULL);
462 add_function_hook("__smatch_possible", &match_possible, NULL);
463 add_function_hook("__smatch_cur_slist", &match_cur_slist, NULL);
464 add_function_hook("__smatch_strlen", &match_strlen, NULL);
465 add_function_hook("__smatch_buf_size", &match_buf_size, NULL);
466 add_function_hook("__smatch_buf_size_rl", &match_buf_size_rl, NULL);
467 add_function_hook("__smatch_note", &match_note, NULL);
468 add_function_hook("__smatch_dump_related", &match_dump_related, NULL);
469 add_function_hook("__smatch_compare", &match_compare, NULL);
470 add_function_hook("__smatch_debug_on", &match_debug_on, NULL);
471 add_function_hook("__smatch_debug_off", &match_debug_off, NULL);
472 add_function_hook("__smatch_local_debug_on", &match_local_debug_on, NULL);
473 add_function_hook("__smatch_local_debug_off", &match_local_debug_off, NULL);
474 add_function_hook("__smatch_debug_implied_on", &match_debug_implied_on, NULL);
475 add_function_hook("__smatch_debug_implied_off", &match_debug_implied_off, NULL);