2 * Copyright (C) 2009 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 #include "smatch_slist.h"
20 #include "smatch_extra.h"
25 static void match_all_values(const char *fn
, struct expression
*expr
, void *info
)
29 stree
= get_all_states_stree(SMATCH_EXTRA
);
34 static void match_cur_slist(const char *fn
, struct expression
*expr
, void *info
)
39 static void match_state(const char *fn
, struct expression
*expr
, void *info
)
41 struct expression
*check_arg
, *state_arg
;
45 check_arg
= get_argument_from_call_expr(expr
->args
, 0);
46 if (check_arg
->type
!= EXPR_STRING
) {
47 sm_msg("error: the check_name argument to %s is supposed to be a string literal", fn
);
50 state_arg
= get_argument_from_call_expr(expr
->args
, 1);
51 if (!state_arg
|| state_arg
->type
!= EXPR_STRING
) {
52 sm_msg("error: the state_name argument to %s is supposed to be a string literal", fn
);
56 FOR_EACH_PTR(__get_cur_slist(), sm
) {
57 if (strcmp(check_name(sm
->owner
), check_arg
->string
->data
) != 0)
59 if (strcmp(sm
->name
, state_arg
->string
->data
) != 0)
61 sm_msg("'%s' = '%s'", sm
->name
, sm
->state
->name
);
63 } END_FOR_EACH_PTR(sm
);
66 sm_msg("%s '%s' not found", check_arg
->string
->data
, state_arg
->string
->data
);
69 static void match_states(const char *fn
, struct expression
*expr
, void *info
)
71 struct expression
*check_arg
;
75 check_arg
= get_argument_from_call_expr(expr
->args
, 0);
76 if (check_arg
->type
!= EXPR_STRING
) {
77 sm_msg("error: the check_name argument to %s is supposed to be a string literal", fn
);
81 FOR_EACH_PTR(__get_cur_slist(), sm
) {
82 if (strcmp(check_name(sm
->owner
), check_arg
->string
->data
) != 0)
84 sm_msg("'%s' = '%s'", sm
->name
, sm
->state
->name
);
86 } END_FOR_EACH_PTR(sm
);
89 sm_msg("%s: no states", check_arg
->string
->data
);
92 static void match_print_value(const char *fn
, struct expression
*expr
, void *info
)
96 struct expression
*arg_expr
;
98 arg_expr
= get_argument_from_call_expr(expr
->args
, 0);
99 if (arg_expr
->type
!= EXPR_STRING
) {
100 sm_msg("error: the argument to %s is supposed to be a string literal", fn
);
104 stree
= get_all_states_stree(SMATCH_EXTRA
);
105 FOR_EACH_SM(stree
, tmp
) {
106 if (!strcmp(tmp
->name
, arg_expr
->string
->data
))
107 sm_msg("%s = %s", tmp
->name
, tmp
->state
->name
);
108 } END_FOR_EACH_SM(tmp
);
112 static void match_print_implied(const char *fn
, struct expression
*expr
, void *info
)
114 struct expression
*arg
;
115 struct range_list
*rl
= NULL
;
118 arg
= get_argument_from_call_expr(expr
->args
, 0);
119 get_implied_rl(arg
, &rl
);
121 name
= expr_to_str(arg
);
122 sm_msg("implied: %s = '%s'", name
, show_rl(rl
));
126 static void match_print_implied_min(const char *fn
, struct expression
*expr
, void *info
)
128 struct expression
*arg
;
132 arg
= get_argument_from_call_expr(expr
->args
, 0);
133 name
= expr_to_str(arg
);
135 if (get_implied_min(arg
, &sval
))
136 sm_msg("implied min: %s = %s", name
, sval_to_str(sval
));
138 sm_msg("implied min: %s = <unknown>", name
);
143 static void match_print_implied_max(const char *fn
, struct expression
*expr
, void *info
)
145 struct expression
*arg
;
149 arg
= get_argument_from_call_expr(expr
->args
, 0);
150 name
= expr_to_str(arg
);
152 if (get_implied_max(arg
, &sval
))
153 sm_msg("implied max: %s = %s", name
, sval_to_str(sval
));
155 sm_msg("implied max: %s = <unknown>", name
);
160 static void match_print_hard_max(const char *fn
, struct expression
*expr
, void *info
)
162 struct expression
*arg
;
166 arg
= get_argument_from_call_expr(expr
->args
, 0);
167 name
= expr_to_str(arg
);
169 if (get_hard_max(arg
, &sval
))
170 sm_msg("hard max: %s = %s", name
, sval_to_str(sval
));
172 sm_msg("hard max: %s = <unknown>", name
);
177 static void match_print_fuzzy_max(const char *fn
, struct expression
*expr
, void *info
)
179 struct expression
*arg
;
183 arg
= get_argument_from_call_expr(expr
->args
, 0);
184 name
= expr_to_str(arg
);
186 if (get_fuzzy_max(arg
, &sval
))
187 sm_msg("fuzzy max: %s = %s", name
, sval_to_str(sval
));
189 sm_msg("fuzzy max: %s = <unknown>", name
);
194 static void match_print_absolute_min(const char *fn
, struct expression
*expr
, void *info
)
196 struct expression
*arg
;
200 arg
= get_argument_from_call_expr(expr
->args
, 0);
201 name
= expr_to_str(arg
);
203 if (get_absolute_min(arg
, &sval
))
204 sm_msg("absolute min: %s = %s", name
, sval_to_str(sval
));
206 sm_msg("absolute min: %s = <unknown>", name
);
211 static void match_print_absolute_max(const char *fn
, struct expression
*expr
, void *info
)
213 struct expression
*arg
;
217 arg
= get_argument_from_call_expr(expr
->args
, 0);
218 get_absolute_max(arg
, &sval
);
220 name
= expr_to_str(arg
);
221 sm_msg("absolute max: %s = %s", name
, sval_to_str(sval
));
225 static void match_sval_info(const char *fn
, struct expression
*expr
, void *info
)
227 struct expression
*arg
;
231 arg
= get_argument_from_call_expr(expr
->args
, 0);
232 name
= expr_to_str(arg
);
234 if (!get_implied_value(arg
, &sval
)) {
235 sm_msg("no sval for '%s'", name
);
239 sm_msg("implied: %s %c%d ->value = %llx", name
, sval_unsigned(sval
) ? 'u' : 's', sval_bits(sval
), sval
.value
);
244 static void match_member_name(const char *fn
, struct expression
*expr
, void *info
)
246 struct expression
*arg
;
247 char *name
, *member_name
;
249 arg
= get_argument_from_call_expr(expr
->args
, 0);
250 name
= expr_to_str(arg
);
251 member_name
= get_member_name(arg
);
252 sm_msg("member name: '%s => %s'", name
, member_name
);
253 free_string(member_name
);
257 static void print_possible(struct sm_state
*sm
)
259 struct sm_state
*tmp
;
261 sm_msg("Possible values for %s", sm
->name
);
262 FOR_EACH_PTR(sm
->possible
, tmp
) {
263 printf("%s\n", tmp
->state
->name
);
264 } END_FOR_EACH_PTR(tmp
);
268 static void match_possible(const char *fn
, struct expression
*expr
, void *info
)
271 struct sm_state
*tmp
;
272 struct expression
*arg_expr
;
274 arg_expr
= get_argument_from_call_expr(expr
->args
, 0);
275 if (arg_expr
->type
!= EXPR_STRING
) {
276 sm_msg("error: the argument to %s is supposed to be a string literal", fn
);
280 stree
= get_all_states_stree(SMATCH_EXTRA
);
281 FOR_EACH_SM(stree
, tmp
) {
282 if (!strcmp(tmp
->name
, arg_expr
->string
->data
))
284 } END_FOR_EACH_SM(tmp
);
288 static void match_strlen(const char *fn
, struct expression
*expr
, void *info
)
290 struct expression
*arg
;
291 struct range_list
*rl
;
294 arg
= get_argument_from_call_expr(expr
->args
, 0);
295 get_implied_strlen(arg
, &rl
);
297 name
= expr_to_str(arg
);
298 sm_msg("strlen: '%s' %s characters", name
, show_rl(rl
));
302 static void match_buf_size(const char *fn
, struct expression
*expr
, void *info
)
304 struct expression
*arg
;
308 arg
= get_argument_from_call_expr(expr
->args
, 0);
309 elements
= get_array_size(arg
);
310 bytes
= get_array_size_bytes(arg
);
312 name
= expr_to_str(arg
);
313 sm_msg("buf size: '%s' %d elements, %d bytes", name
, elements
, bytes
);
317 static void match_buf_size_rl(const char *fn
, struct expression
*expr
, void *info
)
319 struct expression
*arg
;
320 struct range_list
*rl
;
324 arg
= get_argument_from_call_expr(expr
->args
, 0);
325 rl
= get_array_size_bytes_rl(arg
);
326 elements
= get_array_size(arg
);
327 bytes
= get_array_size_bytes(arg
);
329 name
= expr_to_str(arg
);
330 sm_msg("buf size: '%s' %s %d elements, %d bytes", name
, show_rl(rl
), elements
, bytes
);
334 static void match_note(const char *fn
, struct expression
*expr
, void *info
)
336 struct expression
*arg_expr
;
338 arg_expr
= get_argument_from_call_expr(expr
->args
, 0);
339 if (arg_expr
->type
!= EXPR_STRING
) {
340 sm_msg("error: the argument to %s is supposed to be a string literal", fn
);
343 sm_msg("%s", arg_expr
->string
->data
);
346 static void print_related(struct sm_state
*sm
)
348 struct relation
*rel
;
350 if (!estate_related(sm
->state
))
354 sm_printf("%s: ", sm
->name
);
355 FOR_EACH_PTR(estate_related(sm
->state
), rel
) {
356 sm_printf("%s ", rel
->name
);
357 } END_FOR_EACH_PTR(rel
);
361 static void match_dump_related(const char *fn
, struct expression
*expr
, void *info
)
364 struct sm_state
*tmp
;
366 stree
= get_all_states_stree(SMATCH_EXTRA
);
367 FOR_EACH_SM(stree
, tmp
) {
369 } END_FOR_EACH_SM(tmp
);
373 static void match_compare(const char *fn
, struct expression
*expr
, void *info
)
375 struct expression
*one
, *two
;
376 char *one_name
, *two_name
;
380 one
= get_argument_from_call_expr(expr
->args
, 0);
381 two
= get_argument_from_call_expr(expr
->args
, 1);
383 comparison
= get_comparison(one
, two
);
385 snprintf(buf
, sizeof(buf
), "<none>");
387 snprintf(buf
, sizeof(buf
), "%s", show_special(comparison
));
389 one_name
= expr_to_str(one
);
390 two_name
= expr_to_str(two
);
392 sm_msg("%s %s %s", one_name
, buf
, two_name
);
394 free_string(one_name
);
395 free_string(two_name
);
398 static void match_debug_on(const char *fn
, struct expression
*expr
, void *info
)
403 static void match_debug_off(const char *fn
, struct expression
*expr
, void *info
)
408 static void match_local_debug_on(const char *fn
, struct expression
*expr
, void *info
)
413 static void match_local_debug_off(const char *fn
, struct expression
*expr
, void *info
)
418 static void match_debug_implied_on(const char *fn
, struct expression
*expr
, void *info
)
420 option_debug_implied
= 1;
423 static void match_debug_implied_off(const char *fn
, struct expression
*expr
, void *info
)
425 option_debug_implied
= 0;
428 static void match_about(const char *fn
, struct expression
*expr
, void *info
)
430 struct expression
*arg
;
434 sm_msg("---- about ----");
435 match_print_implied(fn
, expr
, NULL
);
436 match_buf_size(fn
, expr
, NULL
);
437 match_strlen(fn
, expr
, NULL
);
439 arg
= get_argument_from_call_expr(expr
->args
, 0);
440 name
= expr_to_str(arg
);
442 sm_msg("info: not a straight forward variable.");
446 FOR_EACH_PTR(__get_cur_slist(), sm
) {
447 if (strcmp(sm
->name
, name
) != 0)
449 sm_msg("%s '%s' = '%s'", check_name(sm
->owner
), sm
->name
, sm
->state
->name
);
450 } END_FOR_EACH_PTR(sm
);
453 void check_debug(int id
)
456 add_function_hook("__smatch_about", &match_about
, NULL
);
457 add_function_hook("__smatch_all_values", &match_all_values
, NULL
);
458 add_function_hook("__smatch_state", &match_state
, NULL
);
459 add_function_hook("__smatch_states", &match_states
, NULL
);
460 add_function_hook("__smatch_value", &match_print_value
, NULL
);
461 add_function_hook("__smatch_implied", &match_print_implied
, NULL
);
462 add_function_hook("__smatch_implied_min", &match_print_implied_min
, NULL
);
463 add_function_hook("__smatch_implied_max", &match_print_implied_max
, NULL
);
464 add_function_hook("__smatch_hard_max", &match_print_hard_max
, NULL
);
465 add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max
, NULL
);
466 add_function_hook("__smatch_absolute_min", &match_print_absolute_min
, NULL
);
467 add_function_hook("__smatch_absolute_max", &match_print_absolute_max
, NULL
);
468 add_function_hook("__smatch_sval_info", &match_sval_info
, NULL
);
469 add_function_hook("__smatch_member_name", &match_member_name
, NULL
);
470 add_function_hook("__smatch_possible", &match_possible
, NULL
);
471 add_function_hook("__smatch_cur_slist", &match_cur_slist
, NULL
);
472 add_function_hook("__smatch_strlen", &match_strlen
, NULL
);
473 add_function_hook("__smatch_buf_size", &match_buf_size
, NULL
);
474 add_function_hook("__smatch_buf_size_rl", &match_buf_size_rl
, NULL
);
475 add_function_hook("__smatch_note", &match_note
, NULL
);
476 add_function_hook("__smatch_dump_related", &match_dump_related
, NULL
);
477 add_function_hook("__smatch_compare", &match_compare
, NULL
);
478 add_function_hook("__smatch_debug_on", &match_debug_on
, NULL
);
479 add_function_hook("__smatch_debug_off", &match_debug_off
, NULL
);
480 add_function_hook("__smatch_local_debug_on", &match_local_debug_on
, NULL
);
481 add_function_hook("__smatch_local_debug_off", &match_local_debug_off
, NULL
);
482 add_function_hook("__smatch_debug_implied_on", &match_debug_implied_on
, NULL
);
483 add_function_hook("__smatch_debug_implied_off", &match_debug_implied_off
, NULL
);