-
Notifications
You must be signed in to change notification settings - Fork 14
/
check_debug.c
1028 lines (839 loc) · 27.1 KB
/
check_debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2009 Dan Carpenter.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
#include <string.h>
#include "smatch.h"
#include "smatch_slist.h"
#include "smatch_extra.h"
void show_sname_alloc(void);
void show_data_range_alloc(void);
void show_ptrlist_alloc(void);
void show_rl_ptrlist_alloc(void);
void show_sm_state_alloc(void);
int local_debug;
static int my_id;
char *trace_variable;
static int cur_state_cnt;
static bool print_state_cnt;
static void match_all_values(const char *fn, struct expression *expr, void *info)
{
struct stree *stree;
stree = get_all_states_stree(SMATCH_EXTRA);
__print_stree(stree);
free_stree(&stree);
}
static void match_cur_stree(const char *fn, struct expression *expr, void *info)
{
__print_cur_stree();
}
static void match_state(const char *fn, struct expression *expr, void *info)
{
struct expression *check_arg, *state_arg;
struct sm_state *sm;
int found = 0;
check_arg = get_argument_from_call_expr(expr->args, 0);
if (check_arg->type != EXPR_STRING) {
sm_error("the check_name argument to %s is supposed to be a string literal", fn);
return;
}
state_arg = get_argument_from_call_expr(expr->args, 1);
if (!state_arg || state_arg->type != EXPR_STRING) {
sm_error("the state_name argument to %s is supposed to be a string literal", fn);
return;
}
FOR_EACH_SM(__get_cur_stree(), sm) {
if (strcmp(check_name(sm->owner), check_arg->string->data) != 0)
continue;
if (strcmp(sm->name, state_arg->string->data) != 0)
continue;
sm_msg("'%s' = '%s'", sm->name, sm->state->name);
found = 1;
} END_FOR_EACH_SM(sm);
if (!found)
sm_msg("%s '%s' not found", check_arg->string->data, state_arg->string->data);
}
static void match_states(const char *fn, struct expression *expr, void *info)
{
struct expression *check_arg;
check_arg = get_argument_from_call_expr(expr->args, 0);
if (check_arg->type != EXPR_STRING) {
sm_error("the check_name argument to %s is supposed to be a string literal", fn);
return;
}
if (__print_states(check_arg->string->data))
return;
if (!id_from_name(check_arg->string->data))
sm_msg("invalid check name '%s'", check_arg->string->data);
else
sm_msg("%s: no states", check_arg->string->data);
}
static void match_print_value(const char *fn, struct expression *expr, void *info)
{
struct stree *stree;
struct sm_state *tmp;
struct expression *arg_expr;
arg_expr = get_argument_from_call_expr(expr->args, 0);
if (arg_expr->type != EXPR_STRING) {
sm_error("the argument to %s is supposed to be a string literal", fn);
return;
}
stree = __get_cur_stree();
FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
if (!strcmp(tmp->name, arg_expr->string->data))
sm_msg("%s = %s", tmp->name, tmp->state->name);
} END_FOR_EACH_SM(tmp);
}
static void match_print_known(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
struct range_list *rl = NULL;
char *name;
int known = 0;
sval_t sval;
arg = get_argument_from_call_expr(expr->args, 0);
if (get_value(arg, &sval))
known = 1;
get_implied_rl(arg, &rl);
name = expr_to_str(arg);
sm_msg("known: '%s' = '%s'. implied = '%s'", name, known ? sval_to_str(sval) : "<unknown>", show_rl(rl));
free_string(name);
}
static void match_print_implied(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
struct range_list *rl = NULL;
char *name;
arg = get_argument_from_call_expr(expr->args, 0);
get_implied_rl(arg, &rl);
name = expr_to_str(arg);
sm_msg("implied: %s = '%s'", name, show_rl(rl));
free_string(name);
}
static void match_real_absolute(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
struct range_list *rl = NULL;
char *name;
arg = get_argument_from_call_expr(expr->args, 0);
get_real_absolute_rl(arg, &rl);
name = expr_to_str(arg);
sm_msg("real absolute: %s = '%s'", name, show_rl(rl));
free_string(name);
}
static void match_print_implied_min(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
sval_t sval;
char *name;
arg = get_argument_from_call_expr(expr->args, 0);
name = expr_to_str(arg);
if (get_implied_min(arg, &sval))
sm_msg("implied min: %s = %s", name, sval_to_str(sval));
else
sm_msg("implied min: %s = <unknown>", name);
free_string(name);
}
static void match_print_implied_max(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
sval_t sval;
char *name;
arg = get_argument_from_call_expr(expr->args, 0);
name = expr_to_str(arg);
if (get_implied_max(arg, &sval))
sm_msg("implied max: %s = %s", name, sval_to_str(sval));
else
sm_msg("implied max: %s = <unknown>", name);
free_string(name);
}
static void match_user_rl(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
struct range_list *rl = NULL;
bool capped = false;
char *name;
if (option_project != PROJ_KERNEL)
sm_msg("no user data for project = '%s'", option_project_str);
arg = get_argument_from_call_expr(expr->args, 0);
name = expr_to_str(arg);
get_user_rl(arg, &rl);
if (rl)
capped = user_rl_capped(arg);
sm_msg("user rl: '%s' = '%s'%s", name, show_rl(rl), capped ? " (capped)" : "");
free_string(name);
}
static void match_host_rl(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
struct range_list *rl = NULL;
struct sm_state *sm;
bool capped = false;
bool new = false;
int host_id;
char *name;
host_id = id_from_name("register_kernel_host_data");
if (!host_id) {
sm_msg("no host id");
return;
}
arg = get_argument_from_call_expr(expr->args, 0);
name = expr_to_str(arg);
get_host_rl(arg, &rl);
if (rl)
capped = host_rl_capped(arg);
sm = get_sm_state_expr(host_id, arg);
if (sm && estate_new(sm->state))
new = true;
sm_msg("host rl: '%s' = '%s'%s %s sm='%s'", name, show_rl(rl),
capped ? " (capped)" : "",
new ? "(new)" : "(old)",
show_sm(sm));
free_string(name);
}
static void match_capped(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
char *name;
arg = get_argument_from_call_expr(expr->args, 0);
name = expr_to_str(arg);
sm_msg("'%s' = '%s'", name, is_capped(arg) ? "capped" : "not capped");
free_string(name);
}
static void match_print_hard_max(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
sval_t sval;
char *name;
arg = get_argument_from_call_expr(expr->args, 0);
name = expr_to_str(arg);
if (get_hard_max(arg, &sval))
sm_msg("hard max: %s = %s", name, sval_to_str(sval));
else
sm_msg("hard max: %s = <unknown>", name);
free_string(name);
}
static void match_print_fuzzy_max(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
sval_t sval;
char *name;
arg = get_argument_from_call_expr(expr->args, 0);
name = expr_to_str(arg);
if (get_fuzzy_max(arg, &sval))
sm_msg("fuzzy max: %s = %s", name, sval_to_str(sval));
else
sm_msg("fuzzy max: %s = <unknown>", name);
free_string(name);
}
static void match_print_absolute(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
struct range_list *rl;
char *name;
arg = get_argument_from_call_expr(expr->args, 0);
name = expr_to_str(arg);
get_absolute_rl(arg, &rl);
sm_msg("absolute: %s = %s", name, show_rl(rl));
free_string(name);
}
static void match_print_absolute_min(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
sval_t sval;
char *name;
arg = get_argument_from_call_expr(expr->args, 0);
name = expr_to_str(arg);
if (get_absolute_min(arg, &sval))
sm_msg("absolute min: %s = %s", name, sval_to_str(sval));
else
sm_msg("absolute min: %s = <unknown>", name);
free_string(name);
}
static void match_print_absolute_max(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
sval_t sval;
char *name;
arg = get_argument_from_call_expr(expr->args, 0);
get_absolute_max(arg, &sval);
name = expr_to_str(arg);
sm_msg("absolute max: %s = %s", name, sval_to_str(sval));
free_string(name);
}
static void match_sval_info(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
sval_t sval;
char *name;
arg = get_argument_from_call_expr(expr->args, 0);
name = expr_to_str(arg);
if (!get_implied_value(arg, &sval)) {
sm_msg("no sval for '%s'", name);
goto free;
}
sm_msg("implied: %s %c%d ->value = %llx", name, sval_unsigned(sval) ? 'u' : 's', sval_bits(sval), sval.value);
free:
free_string(name);
}
static void match_member_name(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
char *name, *member_name;
arg = get_argument_from_call_expr(expr->args, 0);
name = expr_to_str(arg);
member_name = get_member_name(arg);
sm_msg("member name: '%s => %s'", name, member_name);
free_string(name);
}
static void print_possible(struct sm_state *sm)
{
struct sm_state *tmp;
sm_msg("Possible values for %s", sm->name);
FOR_EACH_PTR(sm->possible, tmp) {
printf("%s\n", tmp->state->name);
} END_FOR_EACH_PTR(tmp);
sm_msg("===");
}
static void match_possible(const char *fn, struct expression *expr, void *info)
{
struct stree *stree;
struct sm_state *tmp;
struct expression *arg_expr;
arg_expr = get_argument_from_call_expr(expr->args, 0);
if (arg_expr->type != EXPR_STRING) {
sm_error("the argument to %s is supposed to be a string literal", fn);
return;
}
stree = __get_cur_stree();
FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
if (!strcmp(tmp->name, arg_expr->string->data))
print_possible(tmp);
} END_FOR_EACH_SM(tmp);
}
static void match_strlen(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
struct range_list *rl = NULL;
char *name;
arg = get_argument_from_call_expr(expr->args, 0);
get_implied_strlen(arg, &rl);
name = expr_to_str(arg);
sm_msg("strlen: '%s' %s characters", name, show_rl(rl));
free_string(name);
}
static void match_buf_size(const char *fn, struct expression *expr, void *info)
{
struct expression *arg, *comp;
struct range_list *rl;
int elements, bytes;
char *name;
char buf[256] = "";
int limit_type;
int n;
sval_t sval;
arg = get_argument_from_call_expr(expr->args, 0);
elements = get_array_size(arg);
bytes = get_array_size_bytes_max(arg);
rl = get_array_size_bytes_rl(arg);
comp = get_size_variable(arg, &limit_type);
name = expr_to_str(arg);
n = snprintf(buf, sizeof(buf), "buf size: '%s' %d elements, %d bytes", name, elements, bytes);
free_string(name);
if (!rl_to_sval(rl, &sval))
n += snprintf(buf + n, sizeof(buf) - n, " (rl = %s)", show_rl(rl));
if (comp) {
name = expr_to_str(comp);
snprintf(buf + n, sizeof(buf) - n, "[size_var=%s %s]", limit_type_str(limit_type), name);
free_string(name);
}
sm_msg("%s", buf);
}
static void match_note(const char *fn, struct expression *expr, void *info)
{
struct expression *arg_expr;
arg_expr = get_argument_from_call_expr(expr->args, 0);
if (arg_expr->type != EXPR_STRING) {
sm_error("the argument to %s is supposed to be a string literal", fn);
return;
}
sm_msg("%s", arg_expr->string->data);
}
static void print_related(struct sm_state *sm)
{
struct relation *rel;
if (!estate_related(sm->state))
return;
sm_prefix();
sm_printf("%s: ", sm->name);
FOR_EACH_PTR(estate_related(sm->state), rel) {
sm_printf("%s ", rel->name);
} END_FOR_EACH_PTR(rel);
sm_printf("\n");
}
static void match_dump_related(const char *fn, struct expression *expr, void *info)
{
struct stree *stree;
struct sm_state *tmp;
stree = __get_cur_stree();
FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
print_related(tmp);
} END_FOR_EACH_SM(tmp);
}
static void match_compare(const char *fn, struct expression *expr, void *info)
{
struct expression *one, *two;
char *one_name, *two_name;
int comparison;
char buf[16];
one = get_argument_from_call_expr(expr->args, 0);
two = get_argument_from_call_expr(expr->args, 1);
comparison = get_comparison(one, two);
if (!comparison)
snprintf(buf, sizeof(buf), "<none>");
else
snprintf(buf, sizeof(buf), "%s", show_special(comparison));
one_name = expr_to_str(one);
two_name = expr_to_str(two);
sm_msg("%s %s %s", one_name, buf, two_name);
free_string(one_name);
free_string(two_name);
}
static void match_debug_on(const char *fn, struct expression *expr, void *info)
{
option_debug = 1;
}
static void match_debug_check(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_check = arg->string->data;
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;
cur_state_cnt = 0;
print_state_cnt = false;
}
static void match_start_skip(const char *fn, struct expression *expr, void *info)
{
__debug_skip = true;
}
static void match_local_debug_on(const char *fn, struct expression *expr, void *info)
{
local_debug = 1;
option_print_names++;
}
static void match_local_debug_off(const char *fn, struct expression *expr, void *info)
{
option_print_names--;
local_debug = 0;
}
static void match_debug_db_on(const char *fn, struct expression *expr, void *info)
{
debug_db = 1;
}
static void match_debug_db_off(const char *fn, struct expression *expr, void *info)
{
debug_db = 0;
}
static void match_debug_implied_on(const char *fn, struct expression *expr, void *info)
{
implied_debug = true;
}
static void match_debug_implied_off(const char *fn, struct expression *expr, void *info)
{
implied_debug = false;
}
static void mtag_info(struct expression *expr)
{
mtag_t tag = 0;
int offset = 0;
struct range_list *rl = NULL;
expr_to_mtag_offset(expr, &tag, &offset);
get_mtag_rl(expr, &rl);
sm_msg("mtag = %llu offset = %d rl = '%s'", tag, offset, show_rl(rl));
}
static void match_about(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
struct range_list *rl;
struct sm_state *sm;
char *name;
int len;
sm_msg("---- about ----");
match_print_implied(fn, expr, NULL);
match_buf_size(fn, expr, NULL);
match_strlen(fn, expr, NULL);
match_real_absolute(fn, expr, NULL);
mtag_info(expr);
arg = get_argument_from_call_expr(expr->args, 0);
name = expr_to_str(arg);
if (!name) {
sm_msg("info: not a straight forward variable.");
return;
}
if (get_user_rl(arg, &rl))
sm_msg("user_rl = '%s'", show_rl(rl));
if (points_to_user_data(arg))
sm_msg("points to user data");
len = strlen(name);
FOR_EACH_SM(__get_cur_stree(), sm) {
if (strcmp(sm->name, name) == 0)
goto print;
if (strncmp(sm->name, name, len) == 0 &&
sm->name[len] == ':')
goto print;
continue;
print:
sm_msg("%s", show_sm(sm));
} END_FOR_EACH_SM(sm);
}
static void match_intersection(const char *fn, struct expression *expr, void *info)
{
struct expression *one, *two;
struct range_list *one_rl, *two_rl;
struct range_list *res;
one = get_argument_from_call_expr(expr->args, 0);
two = get_argument_from_call_expr(expr->args, 1);
get_absolute_rl(one, &one_rl);
get_absolute_rl(two, &two_rl);
res = rl_intersection(one_rl, two_rl);
sm_msg("'%s' intersect '%s' is '%s'", show_rl(one_rl), show_rl(two_rl), show_rl(res));
}
static void match_type(const char *fn, struct expression *expr, void *info)
{
struct expression *one;
struct symbol *type;
char *name;
one = get_argument_from_call_expr(expr->args, 0);
type = get_type(one);
name = expr_to_str(one);
sm_msg("type of '%s' is: '%s'", name, type_to_str(type));
free_string(name);
}
static int match_type_rl_return(struct expression *call, void *unused, struct range_list **rl)
{
struct expression *one, *two;
struct symbol *type;
one = get_argument_from_call_expr(call->args, 0);
type = get_type(one);
two = get_argument_from_call_expr(call->args, 1);
if (!two || two->type != EXPR_STRING) {
sm_msg("expected: __smatch_type_rl(type, \"string\")");
return 0;
}
call_results_to_rl(call, type, two->string->data, rl);
return 1;
}
static void print_left_right(struct sm_state *sm)
{
if (!sm)
return;
if (!sm->left && !sm->right)
return;
sm_printf("[ ");
if (sm->left)
sm_printf("(%d: %s->'%s')", get_stree_id(sm->left->pool), sm->left->name, sm->left->state->name);
else
sm_printf(" - ");
print_left_right(sm->left);
if (sm->right)
sm_printf("(%d: %s->'%s')", get_stree_id(sm->right->pool), sm->right->name, sm->right->state->name);
else
sm_printf(" - ");
print_left_right(sm->right);
}
static void match_print_merge_tree(const char *fn, struct expression *expr, void *info)
{
struct sm_state *sm;
struct expression *arg;
char *name;
arg = get_argument_from_call_expr(expr->args, 0);
name = expr_to_str(arg);
sm = get_sm_state_expr(SMATCH_EXTRA, arg);
if (!sm) {
sm_msg("no sm state for '%s'", name);
goto free;
}
sm_prefix();
sm_printf("merge tree: %s -> %s", name, sm->state->name);
print_left_right(sm);
sm_printf("\n");
free:
free_string(name);
}
static void match_print_stree_id(const char *fn, struct expression *expr, void *info)
{
sm_msg("stree_id %d", __stree_id);
}
static void match_bits(const char *fn, struct expression *expr, void *_unused)
{
struct expression *arg;
struct bit_info *info;
char *name;
arg = get_argument_from_call_expr(expr->args, 0);
name = expr_to_str(arg);
info = get_bit_info(arg);
sm_msg("bit info '%s': definitely set 0x%llx. possibly set 0x%llx.",
name, info->set, info->possible);
}
static void match_units(const char *fn, struct expression *expr, void *info)
{
static int units_id;
struct expression *arg;
struct sm_state *sm;
char *name;
if (!units_id)
units_id = id_from_name("register_units");
arg = get_argument_from_call_expr(expr->args, 0);
sm = get_sm_state_expr(units_id, arg);
name = expr_to_str(arg);
sm_msg("units: '%s' '%s'", name, sm ? show_sm(sm) : get_unit_str(arg));
free_string(name);
}
static void match_mtag(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
char *name;
mtag_t tag = 0;
int offset = 0;
arg = get_argument_from_call_expr(expr->args, 0);
name = expr_to_str(arg);
expr_to_mtag_offset(arg, &tag, &offset);
sm_msg("mtag: '%s' => tag: %llu %d", name, tag, offset);
free_string(name);
}
static void match_mtag_data_offset(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
char *name;
mtag_t tag = 0;
int offset = -1;
arg = get_argument_from_call_expr(expr->args, 0);
name = expr_to_str(arg);
expr_to_mtag_offset(arg, &tag, &offset);
sm_msg("mtag: '%s' => tag: %lld, offset: %d", name, tag, offset);
free_string(name);
}
static void match_container(const char *fn, struct expression *expr, void *info)
{
struct expression *container, *x;
char *cont, *name, *str;
container = get_argument_from_call_expr(expr->args, 0);
x = get_argument_from_call_expr(expr->args, 1);
str = get_container_name(container, x);
cont = expr_to_str(container);
name = expr_to_str(x);
sm_msg("container: '%s' vs '%s' --> '%s'", cont, name, str);
free_string(cont);
free_string(name);
}
static void match_param_key(const char *fn, struct expression *expr, void *info)
{
struct expression *arg;
const char *key = NULL;
char *name;
int param;
arg = get_argument_from_call_expr(expr->args, 0);
param = get_param_key_from_expr(arg, NULL, &key);
name = expr_to_str(expr);
sm_msg("expr='%s' param=%d key='%s'", name, param, key);
free_string(name);
}
static struct timeval debug_timer;
static void match_timer_start(const char *fn, struct expression *expr, void *info)
{
gettimeofday(&debug_timer, NULL);
}
static void match_timer_stop(const char *fn, struct expression *expr, void *info)
{
struct timeval stop;
long sec, usec, msec;
gettimeofday(&stop, NULL);
sec = stop.tv_sec - debug_timer.tv_sec;
usec = stop.tv_usec - debug_timer.tv_usec;
msec = sec * 1000 + usec / 1000;
sm_msg("timer: %ld msec", msec);
gettimeofday(&debug_timer, NULL);
}
static void match_debug_state_cnt(const char *fn, struct expression *expr, void *info)
{
struct stree *stree = __get_cur_stree();
if (stree)
cur_state_cnt = stree->count;
print_state_cnt = true;
}
static void print_state_count(struct statement *stmt)
{
struct stree *stree = __get_cur_stree();
int new_cnt;
if (!print_state_cnt &&
(!option_state_cnt || !get_function() || strcmp(get_function(), option_state_cnt) != 0))
return;
new_cnt = 0;
if (stree)
new_cnt = stree->count;
if (cur_state_cnt && new_cnt > cur_state_cnt)
sm_msg("new states = '%d' total=%d", new_cnt - cur_state_cnt, new_cnt);
if (new_cnt > cur_state_cnt)
cur_state_cnt = new_cnt;
}
static void match_expr(const char *fn, struct expression *expr, void *info)
{
struct expression *arg, *str, *new;
char *name, *new_name;
str = get_argument_from_call_expr(expr->args, 0);
arg = get_argument_from_call_expr(expr->args, 1);
if (!arg || !str)
return;
if (str->type != EXPR_STRING)
return;
new = gen_expression_from_key(arg, str->string->data);
name = expr_to_str(arg);
new_name = expr_to_str(new);
sm_msg("str = '%s', arg = '%s' expr = '%s'", str->string->data, name, new_name);
free_string(new_name);
free_string(name);
}
static void match_state_count(const char *fn, struct expression *expr, void *info)
{
struct stree *stree = __get_cur_stree();
int count = 0;
if (stree)
count = stree->count;
sm_msg("state_count = %d\n", count);
}
static void match_mem(const char *fn, struct expression *expr, void *info)
{
show_sname_alloc();
show_data_range_alloc();
show_rl_ptrlist_alloc();
show_ptrlist_alloc();
sm_msg("%lu pools", get_pool_count());
sm_msg("%d strees", unfree_stree);
show_smatch_state_alloc();
show_sm_state_alloc();
}
static void match_exit(const char *fn, struct expression *expr, void *info)
{
exit(0);
}
static struct stree *old_stree;
static void trace_var(struct statement *stmt)
{
struct sm_state *sm, *old;
int printed = 0;
if (!trace_variable)
return;
if (__inline_fn)
return;
FOR_EACH_SM(__get_cur_stree(), sm) {
if (strcmp(sm->name, trace_variable) != 0)
continue;
old = get_sm_state_stree(old_stree, sm->owner, sm->name, sm->sym);
if (old && old->state == sm->state)
continue;
sm_msg("[%d] %s '%s': '%s' => '%s'", stmt->type,
check_name(sm->owner),
sm->name, old ? old->state->name : "<none>", sm->state->name);
printed = 1;
} END_FOR_EACH_SM(sm);
if (printed) {
free_stree(&old_stree);
old_stree = clone_stree(__get_cur_stree());
}
}
static void free_old_stree(struct symbol *sym)
{
free_stree(&old_stree);
}
void check_debug(int id)
{
my_id = id;
add_function_hook("__smatch_about", &match_about, NULL);
add_function_hook("__smatch_all_values", &match_all_values, NULL);
add_function_hook("__smatch_state", &match_state, NULL);
add_function_hook("__smatch_states", &match_states, NULL);
add_function_hook("__smatch_value", &match_print_value, NULL);
add_function_hook("__smatch_known", &match_print_known, NULL);
add_function_hook("__smatch_implied", &match_print_implied, NULL);
add_function_hook("__smatch_implied_min", &match_print_implied_min, NULL);
add_function_hook("__smatch_implied_max", &match_print_implied_max, NULL);
add_function_hook("__smatch_user_rl", &match_user_rl, NULL);
add_function_hook("__smatch_host_rl", &match_host_rl, NULL);
add_function_hook("__smatch_capped", &match_capped, NULL);
add_function_hook("__smatch_hard_max", &match_print_hard_max, NULL);
add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max, NULL);
add_function_hook("__smatch_absolute", &match_print_absolute, NULL);
add_function_hook("__smatch_absolute_min", &match_print_absolute_min, NULL);
add_function_hook("__smatch_absolute_max", &match_print_absolute_max, NULL);
add_function_hook("__smatch_real_absolute", &match_real_absolute, NULL);
add_function_hook("__smatch_sval_info", &match_sval_info, NULL);
add_function_hook("__smatch_member_name", &match_member_name, NULL);
add_function_hook("__smatch_possible", &match_possible, NULL);
add_function_hook("__smatch_cur_stree", &match_cur_stree, NULL);
add_function_hook("__smatch_strlen", &match_strlen, NULL);
add_function_hook("__smatch_buf_size", &match_buf_size, NULL);
add_function_hook("__smatch_note", &match_note, NULL);
add_function_hook("__smatch_dump_related", &match_dump_related, NULL);
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_start_skip", &match_start_skip, 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);