-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathvr_IOMatch_clr.c
More file actions
1227 lines (1057 loc) · 41 KB
/
Copy pathvr_IOMatch_clr.c
File metadata and controls
1227 lines (1057 loc) · 41 KB
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
/*--------------------------------------------------------------------*/
/*--- Verrou: a FPU instrumentation tool. ---*/
/*--- This file contains code allowing to apply client request ---*/
/*--- from an expect script. ---*/
/*--- vr_IOMatch_clr.c ---*/
/*--------------------------------------------------------------------*/
/*
This file is part of Verrou, a FPU instrumentation tool.
Copyright (C) 2014-2023
B. Lathuilière <bruno.lathuiliere@edf.fr>
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 3 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
The GNU General Public License is contained in the file COPYING.
*/
#include "vr_main.h"
#include "vr_IOMatch_clr.h"
#include "pub_tool_seqmatch.h"
#include "pub_tool_libcfile.h"
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
static VgFile* vr_IOmatchCLRFileLog;
static VgFile* vr_IOmatchCLRFileStdout;
static VgFile* vr_IOmatchCLRFileFilteredStdout;
#define LINE_SIZEMAX 40000
#define FILTER_SIZEMAX 512
static const HChar vr_defaultKeyStr[]="default: ";
static const SizeT vr_defaultKeyStrSize=sizeof(vr_defaultKeyStr)-1;
static const HChar vr_initKeyStr[]="init: ";
static const SizeT vr_initKeyStrSize=sizeof(vr_initKeyStr)-1;
static const HChar vr_postinitKeyStr[]="post-init: ";
static const SizeT vr_postinitKeyStrSize=sizeof(vr_postinitKeyStr)-1;
static const HChar vr_filterLineExecKeyStr[]="filter_line_exec: ";
static const SizeT vr_filterLineExecKeyStrSize=sizeof(vr_filterLineExecKeyStr)-1 ;
static const HChar vr_cmatchKeyStr[]= "cmatch: ";
static const SizeT vr_cmatchKeyStrSize=sizeof(vr_cmatchKeyStr)-1;
static const HChar vr_bmatchKeyStr[]= "bmatch: ";
static const SizeT vr_bmatchKeyStrSize=sizeof(vr_bmatchKeyStr)-1;
static const HChar vr_applyKeyStr[]= "apply: ";
static const SizeT vr_applyKeyStrSize=sizeof(vr_applyKeyStr)-1;
static const HChar vr_postApplyKeyStr[]= "post-apply: ";
static const SizeT vr_postApplyKeyStrSize=sizeof(vr_postApplyKeyStr)-1;
static const HChar vr_ignoreEmptyLineKeyStr[]= "ignore-empty-line: ";
static const SizeT vr_ignoreEmptyLineKeyStrSize=sizeof(vr_ignoreEmptyLineKeyStr)-1;
static Bool ignoreEmptyLine=True;
static const HChar vr_verboseKeyStr[]= "verbose: ";
static const SizeT vr_verboseKeyStrSize=sizeof(vr_verboseKeyStr)-1;
static const HChar vr_logLevelKeyStr[]= "log-level: ";
static const SizeT vr_logLevelKeyStrSize=sizeof(vr_logLevelKeyStr)-1;
static const HChar vr_nbMatchMaxKeyStr[]= "nb-match-max: ";
static const SizeT vr_nbMatchMaxKeyStrSize=sizeof(vr_nbMatchMaxKeyStr)-1;
static const HChar vr_permutableBmatchIndexesKeyStr[]= "permutable-bmatch-indexes: ";
static const SizeT vr_permutableBmatchIndexesKeyStrSize=sizeof(vr_permutableBmatchIndexesKeyStr)-1;
static const HChar vr_dumpStdoutKeyStr[]= "dump-stdout:";//no space : no param
static const SizeT vr_dumpStdoutKeyStrSize=sizeof(vr_dumpStdoutKeyStr)-1;
static Bool vr_dumpStdout=False;
static const HChar vr_dumpFilteredStdoutKeyStr[]= "dump-filtered-stdout:";//no space : no param
static const SizeT vr_dumpFilteredStdoutKeyStrSize=sizeof(vr_dumpFilteredStdoutKeyStr)-1;
static Bool vr_dumpFilteredStdout=False;
#define DEFAULT_MAX 10
#define DEFAULT_SIZE_MAX 30
static HChar vr_applyDefault[DEFAULT_MAX][DEFAULT_SIZE_MAX];
static SizeT vr_nbDefault=0;
static HChar vr_applyInit[DEFAULT_MAX][DEFAULT_SIZE_MAX];
static SizeT vr_nbInit=0;
static HChar vr_applypostInit[DEFAULT_MAX][DEFAULT_SIZE_MAX];
static SizeT vr_nbpostInit=0;
static SizeT vr_countPostInit=0;
static SizeT vr_nb_match_max=100; // not const
#define APPLY_PER_MATCH_MAX 5
#define POST_APPLY_PER_MATCH_MAX 2
#define MATCH_SIZE_MAX 30
typedef struct vr_match_data {
HChar* match_pattern;
Bool is_break_match_pattern;
Bool is_exact_match_pattern;
SizeT count_match;
HChar apply_match[APPLY_PER_MATCH_MAX][MATCH_SIZE_MAX];
SizeT nb_apply_match;
HChar post_apply_match[APPLY_PER_MATCH_MAX][MATCH_SIZE_MAX];
SizeT nb_post_apply_match;
} vr_match_data_t;
static vr_match_data_t* vr_match_tab=NULL;
static SizeT vr_nbMatch=0;
static SizeT vr_reorgBegin=1;
static SizeT vr_reorgEnd=0;
static SizeT vr_lastReorgMatch=0;
static inline void vr_allocate_match_if_needed(void){
if(vr_match_tab==NULL){
vr_match_tab=VG_(malloc)("vr_allocate_match_if_needed", vr_nb_match_max*sizeof(vr_match_data_t));
if(vr_match_tab==NULL){
VG_(tool_panic)("vr_IOmatch_clr : allocation failed");
}
}
}
static Int previousMatchIndex=-1;
static HChar* vr_IOmatch_CmdLine =NULL;
static HChar* vr_writeLineBuff =NULL;
static HChar* vr_writeLineBuffCurrent =NULL;
static Bool vr_filter=False;
static HChar vr_filter_cmd[FILTER_SIZEMAX];
static Int filter_fdin[2];
static Int filter_fdout[2];
static Int filter_pid;
static HChar* vr_filtered_buff;
#define ARGV_FILTER_MAX 10
static const HChar *argvFiltered[ARGV_FILTER_MAX];
static const HChar nopStr[]="nop";
static const HChar emptyStr[]="";
static const HChar defaultStr[]="default";
static const HChar initStr[]="init";
static const HChar postinitStr[]="post-init";
static const HChar stopStr[]="stop";
static const HChar startStr[]="start";
static const HChar stopSoftStr[]="stop_soft";
static const HChar startSoftStr[]="start_soft";
static const HChar displayCounterStr[]="display_counter";
static const HChar nbInstrStr[]="nb_instr";
static const HChar resetCounterStr[]="reset_counter";
static const HChar dumpCoverStr[]="dump_cover";
static const HChar panicStr[]="panic";
static const HChar exitStr[]="exit";
static const HChar denormCounterStr[]="print_denorm_counter";
static const HChar resetDenormCounterStr[]="reset_denorm_counter";
static const HChar backtraceStr[]="backtrace";
typedef enum {nopKey=0, emptyKey, defaultKey, initKey, postinitKey, stopKey, startKey, stopSoftKey, startSoftKey,displayCounterKey, nbInstrKey, resetCounterKey, dumpCoverKey, panicKey, exitKey, denormCounterKey,resetDenormCounterKey,backtraceKey} Vr_applyKey;
static const SizeT actionNumber=18;
static const HChar* actionStrTab[]={nopStr, emptyStr, defaultStr, initStr, postinitStr, stopStr, startStr, stopSoftStr, startSoftStr, displayCounterStr, nbInstrStr, resetCounterStr, dumpCoverStr, panicStr, exitStr, denormCounterStr, resetDenormCounterStr,backtraceStr};
static SizeT actionSizeTab[]={sizeof(nopStr), sizeof(emptyStr),sizeof(defaultStr), sizeof(initStr), sizeof(postinitStr), sizeof(stopStr), sizeof(startStr),sizeof(stopSoftStr), sizeof(startSoftStr), sizeof(displayCounterStr), sizeof(nbInstrStr), sizeof(resetCounterStr), sizeof(dumpCoverStr),sizeof(panicStr),sizeof(exitStr), sizeof(denormCounterStr), sizeof(resetDenormCounterStr),sizeof(backtraceStr)};
//Bool actionRequireCacheCleanTab[]={False, False, False, False, False, True, True, False, False, False, False, False, False };
static UInt IOMatch_verbose=1;
static UInt IOMatch_log_level=1;
static Vr_applyKey vr_CmdToEnum(const HChar* cmd){
for(SizeT i=0 ; i< actionNumber; i++){
if(VG_(strncmp)(cmd, actionStrTab[i],actionSizeTab[i])==0){
return i;
}
}
VG_(umsg)("vr_CmdToEnum unknown cmd : |%s|\n", cmd);
VG_(tool_panic)("invalid IOMatch script");
}
static
Bool vr_valid_apply_cmd(const HChar* cmd){
/*Function to known if a cmd is valid : useful to get fast error with cmd error for default*/
Bool res=False;
for(SizeT i=0 ; i< actionNumber; i++){
res= res || (VG_(strncmp)(cmd, actionStrTab[i],actionSizeTab[i])==0);
}
return res;
}
/*to avoid side effect we duplicate the code of get_char and get_line*/
#define my_assert(expr) \
((void) (LIKELY(expr) ? 0 : \
(VG_(assert_fail) (/*isCore*/True, #expr, \
__FILE__, __LINE__, __PRETTY_FUNCTION__, \
""), \
0)))
#define my_assert2(expr, format, args...) \
((void) (LIKELY(expr) ? 0 : \
(VG_(assert_fail) (/*isCore*/True, #expr, \
__FILE__, __LINE__, __PRETTY_FUNCTION__, \
format, ##args), \
0)))
/*Remarks : get_char and VG_(get_line) are reimplemented because of a side-effect
of VG_(get_line) which implies to fully read a file before to read an other. Moreover VG_(get_line)
has specificities for suppression file
*/
static Int my_get_char ( Int fd, HChar* out_buf )
{
Int r;
static HChar buf[256];
static Int buf_size = 0;
static Int buf_used = 0;
my_assert(buf_size >= 0 && buf_size <= sizeof buf);
my_assert(buf_used >= 0 && buf_used <= buf_size);
if (buf_used == buf_size) {
r = VG_(read)(fd, buf, sizeof buf);
if (r < 0) return r; /* read failed */
my_assert(r >= 0 && r <= sizeof buf);
buf_size = r;
buf_used = 0;
}
if (buf_size == 0)
return 0; /* eof */
my_assert(buf_size >= 0 && buf_size <= sizeof buf);
my_assert(buf_used >= 0 && buf_used < buf_size);
*out_buf = buf[buf_used];
buf_used++;
return 1;
}
// Get a non blank non comment line.
// Returns True if eof.
static Bool get_fullnc_line ( Int fd, HChar** bufpp)
{
// VG_(get_line) adapted
HChar* buf = *bufpp;
//Loop over the line until valid line
while (True) {
HChar ch=0;
SizeT n; //error of my_get_char
SizeT i; // index for characters buffer
Bool isBlank=True;
/* Now, read the line into buf. */
i = 0;
//Loop over the char until end of line
while (True) {
n = my_get_char(fd, &ch);
if (n <= 0){
if(i==0 || buf[0]=='#' || isBlank){
return True;
}else{
buf[i] = 0;
return False;
}
}
if (i > 0 && i == LINE_SIZEMAX-1) {
VG_(umsg)("too large line\n");
VG_(exit)(1);
}
if (ch == '\n'){
buf[i]=0;
break;
}else{
buf[i] = ch;
buf[i+1] = 0;
}
if( (ch!=' ') && ((ch!='\t'))){
isBlank=False;
}
i++;
}
/* Ok, we have a line. If a non-comment line, return.
If a comment line, start all over again. */
if ( (buf[0] != '#') && (isBlank==False) ){
return False;
}
}
}
inline static Bool vr_containWildCard(const HChar* matchString){
int i=0;
while(matchString[i]!=(HChar)0){
if(matchString[i]=='*' || matchString[i]=='?'){
return True;
}
i++;
}
return False;
}
inline static Bool vr_string_match(SizeT matchIndex, const HChar* filtered_line){
if(vr_match_tab[matchIndex].is_exact_match_pattern){
return (VG_(strcmp)(vr_match_tab[matchIndex].match_pattern,filtered_line)==0);
}else{
return VG_(string_match)(vr_match_tab[matchIndex].match_pattern,filtered_line);
}
}
inline static void vr_close_filter(void){
char msgEnd[]="";
VG_(write)(filter_fdin[1], msgEnd, 1);
VG_(close)(filter_fdout[0]);
VG_(close)(filter_fdin[1]);
VG_(waitpid)(filter_pid, NULL, 0);
VG_(free)(vr_filtered_buff);
}
static void vr_applyCmd(Vr_applyKey key, const HChar* cmd, Bool noIntrusiveOnly){
if(IOMatch_verbose>2){
VG_(umsg)("vr_applyCmd : %s\n", cmd);
}
switch(key){
case nopKey:
return;
case emptyKey:
return;
case defaultKey:
VG_(tool_panic)("default treated before");
return;
case initKey:
VG_(tool_panic)("init treated before");
return;
case postinitKey:
VG_(tool_panic)("postinit treated before");
return;
case stopKey:
if(noIntrusiveOnly){
vr.instrument_hard = False;
}else{
vr_set_instrument_state ("IO Match CLR", False, True);
}
if( IOMatch_log_level>0 ){
VG_(fprintf)(vr_IOmatchCLRFileLog,"apply: stop\n");
}
return;
case startKey:
if(noIntrusiveOnly){
vr.instrument_hard = True;
}else{
vr_set_instrument_state ("IO Match CLR", True, True);
}
if( IOMatch_log_level>0 ){
VG_(fprintf)(vr_IOmatchCLRFileLog,"apply: start\n");
}
return;
case stopSoftKey:
vr_set_instrument_state ("IO Match CLR", False, False);
if( IOMatch_log_level>0 ){
VG_(fprintf)(vr_IOmatchCLRFileLog,"apply: stop soft\n");
}
return;
case startSoftKey:
vr_set_instrument_state ("IO Match CLR", True, False);
if( IOMatch_log_level>0 ){
VG_(fprintf)(vr_IOmatchCLRFileLog,"apply: start soft\n");
}
return;
case displayCounterKey:
vr_ppOpCount();
if( IOMatch_log_level>0 ){
VG_(fprintf)(vr_IOmatchCLRFileLog,"apply: display_counter\n");
}
return;
case nbInstrKey:
{
UInt nbInstr=vr_count_fp_instrumented();
if( IOMatch_log_level>0 ){
VG_(fprintf)(vr_IOmatchCLRFileLog,"fp_instr: %u\n", nbInstr );
}
return;
}
case resetCounterKey:
vr_resetCount();
return;
case dumpCoverKey:
{
SizeT ret;
ret=vr_traceBB_dumpCov();
if( IOMatch_log_level>0 ){
VG_(fprintf)(vr_IOmatchCLRFileLog,"apply: dump_cover : %lu\n", ret);
}
return;
}
case denormCounterKey:
vr_print_denorm_counter();
return;
case resetDenormCounterKey:
vr_reset_denorm_counter();
return;
case backtraceKey:
VG_(get_and_pp_StackTrace)(VG_(get_running_tid)(), VG_(clo_backtrace_size));
return;
case panicKey:
VG_(fprintf)(vr_IOmatchCLRFileLog, "apply: panic\n");
VG_(tool_panic)("apply: panic");
case exitKey:
if( IOMatch_log_level>0 ){
VG_(fprintf)(vr_IOmatchCLRFileLog, "apply: exit\n");
}
if(vr_filter){
vr_close_filter();
}
VG_(exit)(1);
}
VG_(umsg)("vr_applyCmd : unknown cmd : |%s|\n", cmd);
VG_(exit)(1);
}
static
void vr_IOmatch_apply_clr(const HChar* cmd, Bool noIntrusiveOnly){
if(IOMatch_verbose>2){
VG_(umsg)("vr_IOmatch_apply_clr: %s\n",cmd);
}
Vr_applyKey key=vr_CmdToEnum(cmd);
if(key==initKey){
for(SizeT i=0; i< vr_nbInit;i++){
Vr_applyKey keyInit=vr_CmdToEnum(vr_applyInit[i]);
vr_applyCmd(keyInit,vr_applyInit[i], noIntrusiveOnly);
}
return;
}
if(key==postinitKey){
for(SizeT i=0; i< vr_nbpostInit;i++){
Vr_applyKey keypostInit=vr_CmdToEnum(vr_applypostInit[i]);
vr_applyCmd(keypostInit,vr_applypostInit[i],noIntrusiveOnly);
}
return;
}
if(key==defaultKey){
for(SizeT i=0; i< vr_nbDefault;i++){
Vr_applyKey keyDefault=vr_CmdToEnum(vr_applyDefault[i]);
vr_applyCmd(keyDefault,vr_applyDefault[i],noIntrusiveOnly);
}
return;
}
vr_applyCmd(key,cmd,noIntrusiveOnly);
}
static VgFile* openOutputIOMatchFile(const HChar * fileName, const HChar * fileNameIOMatch, const HChar * strPost);
static VgFile* openOutputIOMatchFile(const HChar * fileName, const HChar * fileNameIOMatch, const HChar * strPost){
/*Open output File*/
HChar strFilename[512];
if( VG_(strncmp)(fileName, "",512)==0){
if(vr.outputIOMatchRep==NULL){
VG_(sprintf)(strFilename, "%s%s",fileNameIOMatch,strPost);
}else{
VG_(sprintf)(strFilename, "%s/%s%s",vr.outputIOMatchRep,"IOMatch",strPost);
}
}else{
if(vr.outputIOMatchRep==NULL){
VG_(sprintf)(strFilename, "%s",fileName);
}else{
if(fileName[0]=='/'){
VG_(umsg)("incompatible option --output-expect-rep with abspath : %s", fileName);
VG_(tool_panic)("incompatible option --output-expect-rep with abspath");
}
VG_(sprintf)(strFilename, "%s/%s", vr.outputIOMatchRep, fileName);
}
}
const HChar * strFilenameExpanded= VG_(expand_file_name)(strPost, strFilename);
VG_(umsg)("Open IOMatchClr output file : `%s'... \n", strFilenameExpanded);
VgFile* vr_IOmatchCLRFile = VG_(fopen)(strFilenameExpanded,
VKI_O_WRONLY | VKI_O_CREAT | VKI_O_TRUNC,
VKI_S_IRUSR|VKI_S_IWUSR|VKI_S_IRGRP|VKI_S_IROTH);
if(vr_IOmatchCLRFile==NULL){
VG_(umsg)("ERROR (fopen) %s\n",strFilenameExpanded);
VG_(tool_panic)("vr_IOmatch_clr : missing write file");
}
return vr_IOmatchCLRFile;
};
static HChar* stripSpace(HChar* str);
static HChar* stripSpace(HChar* str){
if(str[0]==0) return str;
HChar* res=str;
while(res[0]==' ') res+=1;
HChar* end=res;
while(*end!=0) end+=1;
end--;
while(*end==' '){
*end=0;
end-=1;
}
return res;
}
static Bool vrIO_Parse_filterLineExec(void);
static Bool vrIO_Parse_filterLineExec(void){
//Treat filter line exec key
if( VG_(strncmp)(vr_IOmatch_CmdLine, vr_filterLineExecKeyStr, vr_filterLineExecKeyStrSize)==0 ){
const HChar* filterExecCmd=stripSpace(vr_IOmatch_CmdLine+vr_filterLineExecKeyStrSize);
if(IOMatch_verbose>3){
VG_(umsg)("filter line exec str : %s\n", filterExecCmd);
}
if(vr_filter==True){
VG_(tool_panic)("vr_IOmatch_clr : only one filter_line_exec cmd is accepted");
}
vr_filter=True;
VG_(strncpy)(vr_filter_cmd,filterExecCmd,FILTER_SIZEMAX);
Bool beginWord=True;
Int indexWord=0;
Int indexShift=0;
Bool escaped=False;
for(Int indexStr=0; indexStr<FILTER_SIZEMAX; indexStr++){
HChar currentChar=vr_filter_cmd[indexStr];
if(currentChar== '\\'){
indexShift++;
escaped=True;
continue;
}
if(currentChar==0){
vr_filter_cmd[indexStr-indexShift]=0;
argvFiltered[indexWord+1]=NULL;
break;
}
if(currentChar==' ' && (!escaped)){
vr_filter_cmd[indexStr-indexShift]=0;
indexWord++;
if(indexWord==(ARGV_FILTER_MAX-1) ){
VG_(tool_panic)("too many args for filter_line_exec:");
}
beginWord=True;
escaped=False;
continue;
}
if(beginWord && ((currentChar!=' ')|| escaped) ){
argvFiltered[indexWord] = vr_filter_cmd+indexStr-indexShift;
beginWord=False;
}
if(beginWord==False && ((currentChar!=' ')|| escaped)){
vr_filter_cmd[indexStr-indexShift]=vr_filter_cmd[indexStr];
}
escaped=False;
}
if(IOMatch_verbose>1){
VG_(umsg)("vr_filter_cmd: %s\n", argvFiltered[0]);
Int indexArg=1;
while(True){
if(argvFiltered[indexArg]==NULL){
break;
}
VG_(umsg)("\tARGV[%d]: %s\n", indexArg,argvFiltered[indexArg]);
indexArg+=1;
}
}
// Run the filterCMD
if( VG_(pipe)(filter_fdin)!=0){
VG_(umsg)("vr_IOmatchCLR: problem with PIPE fdin\n");
VG_(tool_panic)("Pipe error");
}
if( VG_(pipe)(filter_fdout)!=0){
VG_(umsg)("vr_IOmatchCLR: problem with PIPE fdout\n");
VG_(tool_panic)("Pipe error");
}
filter_pid= VG_(fork)();
if(filter_pid == 0){
VG_(close)(filter_fdin[1]);
SysRes res = VG_(dup2)(filter_fdin[0], STDIN_FILENO);
if (sr_Res(res) < 0){
VG_(umsg)("vr_IOmatchCLR: problem with DUP2 in pipe 0\n");
VG_(exit)(1);
}
VG_(close)(filter_fdout[0]);
SysRes res2 = VG_(dup2)(filter_fdout[1], STDOUT_FILENO );
if (sr_Res(res2) < 0){
VG_(umsg)("vr_IOmatchCLR: problem with DUP2 out pipe 1\n");
VG_(exit)(1);
}
VG_(execv)(argvFiltered[0], argvFiltered);
/* If we are still alive here, execv failed. */
VG_(umsg)("vr_IOmatchCLR: problem with EXECV\n");
VG_(umsg)("ARGV[0] %s\n", argvFiltered[0]);
Int indexArg=1;
while(True){
if(argvFiltered[indexArg]==NULL){
break;
}
VG_(umsg)("ARGV[%d] %s\n", indexArg,argvFiltered[indexArg]);
indexArg+=1;
}
VG_(tool_panic)("EXECV filter failed");
}
if (filter_pid < 0) {
VG_(umsg)("vr_IOmatchCLR: problem with fork\n");
}
VG_(close)(filter_fdin[0]);
VG_(close)(filter_fdout[1]);
//initialize vr_filtered_buff
vr_filtered_buff=VG_(malloc)("vr_filtered_buff.1", LINE_SIZEMAX*sizeof(HChar));
return True;
}
return False;
}
static Bool vrIO_Parse_default(void);
static Bool vrIO_Parse_default(void){
//Treat default key
if( VG_(strncmp)(vr_IOmatch_CmdLine, vr_defaultKeyStr, vr_defaultKeyStrSize)==0 ){
const HChar* defaultAction=stripSpace(vr_IOmatch_CmdLine+vr_defaultKeyStrSize);
if(IOMatch_verbose>2){
VG_(umsg)("default action str : %s\n", defaultAction);
}
if (vr_valid_apply_cmd(defaultAction)){
if(IOMatch_log_level>0){
VG_(fprintf)(vr_IOmatchCLRFileLog, "default action [%lu] : %s\n",vr_nbDefault ,defaultAction);
}
VG_(strncpy)(vr_applyDefault[vr_nbDefault],defaultAction, DEFAULT_SIZE_MAX);
vr_nbDefault++;
}else{
VG_(umsg)("default action %s is not valid", defaultAction);
VG_(tool_panic)("vr_IOmatch_clr : invalid action");
}
return True;
}
return False;
}
static Bool vrIO_Parse_emptyLine(void);
static Bool vrIO_Parse_emptyLine(void){
//Treat ignoreEmptyLine key
if( VG_(strncmp)(vr_IOmatch_CmdLine, vr_ignoreEmptyLineKeyStr, vr_ignoreEmptyLineKeyStrSize)==0 ){
const HChar* boolStr=stripSpace(vr_IOmatch_CmdLine+vr_ignoreEmptyLineKeyStrSize);
const HChar trueStr[]="true";
const HChar falseStr[]="false";
if( VG_(strncmp)(boolStr,trueStr, sizeof(trueStr))==0){
ignoreEmptyLine=True;
VG_(umsg)("ignoreEmptyLine=True\n");
}else{
if( VG_(strncmp)(boolStr,falseStr, sizeof(falseStr))==0){
ignoreEmptyLine=False;
VG_(umsg)("ignoreEmptyLine=False\n");
}else{
VG_(umsg)("ignore-empty-line %s is not valid", boolStr);
VG_(tool_panic)("ignore-empty-line %s is not valid");
}
}
if(IOMatch_verbose>2){
VG_(umsg)("ignore-empty-line: %s\n", boolStr);
}
return True;
}
return False;
}
static Bool vrIO_Parse_init(void);
static Bool vrIO_Parse_init(void){
//Treat init key
if( VG_(strncmp)(vr_IOmatch_CmdLine, vr_initKeyStr, vr_initKeyStrSize)==0 ){
const HChar* initAction=stripSpace(vr_IOmatch_CmdLine+vr_initKeyStrSize);
if(IOMatch_verbose>2){
VG_(umsg)("init action str : %s\n", initAction);
}
if (vr_valid_apply_cmd(initAction)){
if( IOMatch_log_level>0){
VG_(fprintf)(vr_IOmatchCLRFileLog, "init action [%lu] : %s\n",vr_nbInit , initAction);
}
VG_(strncpy)(vr_applyInit[vr_nbInit],initAction, DEFAULT_SIZE_MAX);
vr_nbInit++;
}else{
VG_(umsg)("init action %s is not valid", initAction);
VG_(tool_panic)("vr_IOmatch_clr : invalid action");
}
return True;
}
return False;
}
static Bool vrIO_Parse_postinit(void);
static Bool vrIO_Parse_postinit(void){
//Treat postinit key
if( VG_(strncmp)(vr_IOmatch_CmdLine, vr_postinitKeyStr, vr_postinitKeyStrSize)==0 ){
const HChar* postinitAction=stripSpace(vr_IOmatch_CmdLine+vr_postinitKeyStrSize);
if(IOMatch_verbose>2){
VG_(umsg)("post-init action str : %s\n", postinitAction);
}
if (vr_valid_apply_cmd(postinitAction)){
if( IOMatch_log_level>0){
VG_(fprintf)(vr_IOmatchCLRFileLog, "postinit action [%lu] : %s\n",vr_nbpostInit , postinitAction);
}
VG_(strncpy)(vr_applypostInit[vr_nbpostInit], postinitAction, DEFAULT_SIZE_MAX);
vr_nbpostInit++;
}else{
VG_(umsg)("post init action %s is not valid", postinitAction);
VG_(tool_panic)("vr_IOmatch_clr : invalid action");
}
return True;
}
return False;
}
static Bool vrIO_Parse_bmatch(void);
static Bool vrIO_Parse_bmatch(void){
//Treat bmatch key
if( VG_(strncmp)(vr_IOmatch_CmdLine, vr_bmatchKeyStr, vr_bmatchKeyStrSize)==0 ){
vr_allocate_match_if_needed();
if(vr_nbMatch> vr_nb_match_max){
VG_(tool_panic)("vr_IOmatch_clr : to many match");
}
const HChar* matchPattern=vr_IOmatch_CmdLine+vr_bmatchKeyStrSize;
if( IOMatch_log_level>0){
VG_(fprintf)(vr_IOmatchCLRFileLog, "bmatch pattern [%lu] : %s\n",vr_nbMatch ,matchPattern);
}
vr_match_tab[vr_nbMatch].match_pattern=VG_(strdup)("bmatch.patterndup", matchPattern);
vr_match_tab[vr_nbMatch].is_break_match_pattern=True;
vr_match_tab[vr_nbMatch].is_exact_match_pattern=!(vr_containWildCard(matchPattern));
vr_match_tab[vr_nbMatch].count_match=0;
vr_match_tab[vr_nbMatch].nb_apply_match=0;
vr_match_tab[vr_nbMatch].nb_post_apply_match=0;
vr_nbMatch++;
return True;
}
return False;
}
static Bool vrIO_Parse_cmatch(void);
static Bool vrIO_Parse_cmatch(void){
if( VG_(strncmp)(vr_IOmatch_CmdLine, vr_cmatchKeyStr, vr_cmatchKeyStrSize)==0 ){
vr_allocate_match_if_needed();
if(vr_nbMatch> vr_nb_match_max){
VG_(tool_panic)("vr_IOmatch_clr : to many match");
}
const HChar* matchPattern=vr_IOmatch_CmdLine+vr_cmatchKeyStrSize;
if( IOMatch_log_level>0){
VG_(fprintf)(vr_IOmatchCLRFileLog, "cmatch pattern [%lu] : %s\n",vr_nbMatch ,matchPattern);
}
vr_match_tab[vr_nbMatch].match_pattern=VG_(strdup)("cmatch.patterndup", matchPattern);
vr_match_tab[vr_nbMatch].is_break_match_pattern=False;
vr_match_tab[vr_nbMatch].is_exact_match_pattern=!(vr_containWildCard(matchPattern));
vr_match_tab[vr_nbMatch].count_match=0;
vr_match_tab[vr_nbMatch].nb_apply_match=0;
vr_match_tab[vr_nbMatch].nb_post_apply_match=0;
vr_nbMatch++;
return True;
}
return False;
}
static Bool vrIO_Parse_apply(void);
static Bool vrIO_Parse_apply(void){
if( VG_(strncmp)(vr_IOmatch_CmdLine, vr_applyKeyStr, vr_applyKeyStrSize)==0 ){
const HChar* applyCmd=stripSpace(vr_IOmatch_CmdLine+vr_applyKeyStrSize);
if(IOMatch_verbose>2){
VG_(umsg)("apply : %s\n", applyCmd);
}
if(vr_nbMatch<=0){
VG_(tool_panic)("vr_IOmatch_clr : match expected before apply ");
}
vr_match_data_t* last_match_data=&(vr_match_tab[vr_nbMatch-1]);
(last_match_data->nb_apply_match)++;
if( (last_match_data->nb_apply_match) >= APPLY_PER_MATCH_MAX ){
VG_(tool_panic)("vr_IOmatch_clr : too many apply per match ");
}
VG_(strncpy)( last_match_data->apply_match[last_match_data->nb_apply_match -1], applyCmd, MATCH_SIZE_MAX);
return True;
}
return False;
}
static Bool vrIO_Parse_postapply(void);
static Bool vrIO_Parse_postapply(void){
if( VG_(strncmp)(vr_IOmatch_CmdLine, vr_postApplyKeyStr, vr_postApplyKeyStrSize)==0 ){
const HChar* applyCmd=stripSpace(vr_IOmatch_CmdLine+vr_postApplyKeyStrSize);
if(IOMatch_verbose>2){
VG_(umsg)("post_apply : %s\n", applyCmd);
}
if(vr_nbMatch<0){
VG_(tool_panic)("vr_IOmatch_clr : match expected before post-apply ");
}
vr_match_data_t* last_match_data=&(vr_match_tab[vr_nbMatch-1]);
if(!(last_match_data->is_break_match_pattern)){
VG_(tool_panic)("cmatch and post_apply are incompatible ");
}
(last_match_data->nb_post_apply_match)++;
if((last_match_data->nb_post_apply_match)>=POST_APPLY_PER_MATCH_MAX ){
VG_(tool_panic)("vr_IOmatch_clr : too many post-apply per match ");
}
VG_(strncpy)( last_match_data->post_apply_match[last_match_data->nb_post_apply_match -1], applyCmd, MATCH_SIZE_MAX);
return True;
}
return False;
}
static Bool vrIO_Parse_verbose(void);
static Bool vrIO_Parse_verbose(void){
if( VG_(strncmp)(vr_IOmatch_CmdLine, vr_verboseKeyStr, vr_verboseKeyStrSize)==0 ){
const HChar* verboseStr=stripSpace(vr_IOmatch_CmdLine+vr_verboseKeyStrSize);
IOMatch_verbose=VG_(strtoull10)(verboseStr, NULL);
return True;
}
return False;
}
static Bool vrIO_Parse_logLevel(void);
static Bool vrIO_Parse_logLevel(void){
if( VG_(strncmp)(vr_IOmatch_CmdLine, vr_logLevelKeyStr, vr_logLevelKeyStrSize)==0 ){
const HChar* logLevelStr=stripSpace(vr_IOmatch_CmdLine+vr_logLevelKeyStrSize);
IOMatch_log_level=VG_(strtoull10)(logLevelStr, NULL);
return True;
}
return False;
}
static Bool vrIO_Parse_matchMax(void);
static Bool vrIO_Parse_matchMax(void){
if( VG_(strncmp)(vr_IOmatch_CmdLine, vr_nbMatchMaxKeyStr, vr_nbMatchMaxKeyStrSize)==0 ){
if(vr_nbMatch>0){
VG_(tool_panic)("vr_IOmatch_clr : nb-match-max is invalid after bmatch of cmatch");
}
const HChar* nbMatchMaxStr=stripSpace(vr_IOmatch_CmdLine+vr_nbMatchMaxKeyStrSize);
vr_nb_match_max=VG_(strtoull10)(nbMatchMaxStr, NULL);
return True;
}
return False;
}
static Bool vrIO_Parse_permutableBmatchIndexes(void);
static Bool vrIO_Parse_permutableBmatchIndexes(void){
if( VG_(strncmp)(vr_IOmatch_CmdLine, vr_permutableBmatchIndexesKeyStr, vr_permutableBmatchIndexesKeyStrSize)==0 ){
HChar* indexesStr=stripSpace(vr_IOmatch_CmdLine+vr_permutableBmatchIndexesKeyStrSize);
HChar* next;
vr_reorgBegin =VG_(strtoull10)(indexesStr, &next );
vr_reorgEnd =VG_(strtoull10)(next, NULL );
if(vr_reorgEnd <=vr_reorgBegin){
VG_(tool_panic)("vr_IOmatch_clr : vr_reorgEnd < vr_reorgBegin");
}
if(vr_reorgEnd >=vr_nb_match_max){
VG_(tool_panic)("vr_IOmatch_clr : vr_reorgEnd >= vr_nb_match_max");
}
return True;
}
return False;
}
static Bool vrIO_Parse_dumpStdout(const HChar * fileName);
static Bool vrIO_Parse_dumpStdout(const HChar * fileName){
if( VG_(strncmp)(vr_IOmatch_CmdLine, vr_dumpStdoutKeyStr, vr_dumpStdoutKeyStrSize)==0 ){
const HChar* dumpStr=stripSpace(vr_IOmatch_CmdLine+vr_dumpStdoutKeyStrSize);
vr_dumpStdout=True;
vr_IOmatchCLRFileStdout=openOutputIOMatchFile(dumpStr,fileName,".stdout-%p");
return True;
}
return False;
}
static Bool vrIO_Parse_dumpFilteredStdout(const HChar * fileName);
static Bool vrIO_Parse_dumpFilteredStdout(const HChar * fileName){
if( VG_(strncmp)(vr_IOmatch_CmdLine, vr_dumpFilteredStdoutKeyStr, vr_dumpFilteredStdoutKeyStrSize)==0 ){
const HChar* dumpStr=stripSpace(vr_IOmatch_CmdLine+vr_dumpFilteredStdoutKeyStrSize);
vr_dumpFilteredStdout=True;
vr_IOmatchCLRFileFilteredStdout=openOutputIOMatchFile(dumpStr,fileName,".filtered.stdout-%p");
return True;
}
return False;
}
void vr_IOmatch_clr_init (const HChar * fileName) {
/*Open input File*/
VG_(umsg)("Open IOMatchClr file : `%s'... \n", fileName);
vr.IOMatchCLRFileInput= VG_(fd_open)(fileName, VKI_O_RDONLY,0);
if (vr.IOMatchCLRFileInput == -1) {
VG_(umsg)("ERROR (open)\n");
VG_(exit)(1);
}
/*Open output File*/
vr_IOmatchCLRFileLog=openOutputIOMatchFile("",fileName, ".log-%p");
/*Allocate requiered Buffer*/
vr_IOmatch_CmdLine = VG_(malloc)("vr_IOmatch_cl_init.1", LINE_SIZEMAX*sizeof(HChar));
vr_writeLineBuff = VG_(malloc)("vr_IOmatch_cl_init.2", LINE_SIZEMAX*sizeof(HChar));
vr_writeLineBuff[0]=0;
vr_writeLineBuffCurrent= vr_writeLineBuff;
/*Loop over input line until first expect key*/
while (! get_fullnc_line(vr.IOMatchCLRFileInput, &vr_IOmatch_CmdLine)) {
if(IOMatch_verbose>3){
VG_(umsg)("debug line : %s\n", vr_IOmatch_CmdLine);
}
if( vrIO_Parse_default()){ continue ;}
if( vrIO_Parse_emptyLine()){ continue ;}
if( vrIO_Parse_init()){ continue ;}
if( vrIO_Parse_postinit()){ continue ;}
if( vrIO_Parse_bmatch()){ continue ;}
if( vrIO_Parse_cmatch()){ continue ;}
if( vrIO_Parse_apply()){ continue ;}
if( vrIO_Parse_postapply()){ continue ;}
if( vrIO_Parse_verbose()){ continue ;}
if( vrIO_Parse_logLevel()){ continue ;}
if( vrIO_Parse_matchMax()){ continue ;}
if( vrIO_Parse_permutableBmatchIndexes()){ continue ;}
if( vrIO_Parse_dumpStdout(fileName)){ continue ;}
if( vrIO_Parse_dumpFilteredStdout(fileName)){ continue ;}
if( vrIO_Parse_filterLineExec()){ continue ;}
VG_(umsg)("Unused line : %s\n", vr_IOmatch_CmdLine);
VG_(tool_panic)("vr_IOmatchCLR: bad IOMatch script\n");
}//End while loop over line
VG_(free)(vr_IOmatch_CmdLine);
VG_(close)(vr.IOMatchCLRFileInput);
vr_IOmatch_apply_clr("init", True);
}
static int readlineCharByChar(int fd, char* msgRead,int sizeMax);
static int readlineCharByChar(int fd, char* msgRead,int sizeMax){
int totalSize=0;
while(totalSize<sizeMax){
char buffer[1];
int size=VG_(read)(fd,buffer, 1);
if(size==1){
msgRead[totalSize]=buffer[0];
if(buffer[0]=='\n' || buffer[0]==0){
msgRead[totalSize]=0;
return totalSize+1;
}else{