-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathvr_IOMatch_clr.c
998 lines (837 loc) · 30.9 KB
/
vr_IOMatch_clr.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
/*--------------------------------------------------------------------*/
/*--- 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 <[email protected]>
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, 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
VgFile* vr_IOmatchCLRFileLog;
VgFile* vr_IOmatchCLRFileStdout;
VgFile* vr_IOmatchCLRFileFilteredStdout;
#define LINE_SIZEMAX 40000
#define FILTER_SIZEMAX 512
const HChar vr_defaultKeyStr[]="default: ";
SizeT vr_defaultKeyStrSize=sizeof(vr_defaultKeyStr)-1;
const HChar vr_initKeyStr[]="init: ";
SizeT vr_initKeyStrSize=sizeof(vr_initKeyStr)-1;
const HChar vr_postinitKeyStr[]="post-init: ";
SizeT vr_postinitKeyStrSize=sizeof(vr_postinitKeyStr)-1;
const HChar vr_filterLineExecKeyStr[]="filter_line_exec: ";
SizeT vr_filterLineExecKeyStrSize=sizeof(vr_filterLineExecKeyStr)-1 ;
const HChar vr_cmatchKeyStr[]= "cmatch: ";
SizeT vr_cmatchKeyStrSize=sizeof(vr_cmatchKeyStr)-1;
const HChar vr_bmatchKeyStr[]= "bmatch: ";
SizeT vr_bmatchKeyStrSize=sizeof(vr_bmatchKeyStr)-1;
const HChar vr_applyKeyStr[]= "apply: ";
SizeT vr_applyKeyStrSize=sizeof(vr_applyKeyStr)-1;
const HChar vr_postApplyKeyStr[]= "post-apply: ";
SizeT vr_postApplyKeyStrSize=sizeof(vr_postApplyKeyStr)-1;
const HChar vr_ignoreEmptyLineKeyStr[]= "ignore-empty-line: ";
SizeT vr_ignoreEmptyLineKeyStrSize=sizeof(vr_ignoreEmptyLineKeyStr)-1;
Bool ignoreEmptyLine=True;
const HChar vr_verboseKeyStr[]= "verbose: ";
SizeT vr_verboseKeyStrSize=sizeof(vr_verboseKeyStr)-1;
const HChar vr_dumpStdoutKeyStr[]= "dump-stdout:";//no space : no param
SizeT vr_dumpStdoutKeyStrSize=sizeof(vr_dumpStdoutKeyStr)-1;
Bool vr_dumpStdout=False;
const HChar vr_dumpFilteredStdoutKeyStr[]= "dump-filtered-stdout:";//no space : no param
SizeT vr_dumpFilteredStdoutKeyStrSize=sizeof(vr_dumpFilteredStdoutKeyStr)-1;
Bool vr_dumpFilteredStdout=False;
#define DEFAULT_MAX 10
#define DEFAULT_SIZE_MAX 30
HChar vr_applyDefault[DEFAULT_MAX][DEFAULT_SIZE_MAX];
SizeT vr_nbDefault=0;
HChar vr_applyInit[DEFAULT_MAX][DEFAULT_SIZE_MAX];
SizeT vr_nbInit=0;
HChar vr_applypostInit[DEFAULT_MAX][DEFAULT_SIZE_MAX];
SizeT vr_nbpostInit=0;
SizeT vr_countPostInit=0;
#define MATCH_MAX 2000
#define APPLY_PER_MATCH_MAX 5
#define POST_APPLY_PER_MATCH_MAX 2
#define MATCH_SIZE_MAX 30
HChar vr_applyMatch[MATCH_MAX][APPLY_PER_MATCH_MAX][MATCH_SIZE_MAX];
SizeT vr_nbApplyMatch[MATCH_MAX];
HChar vr_postApplyMatch[MATCH_MAX][APPLY_PER_MATCH_MAX][MATCH_SIZE_MAX];
SizeT vr_nbPostApplyMatch[MATCH_MAX];
HChar* vr_matchPattern[MATCH_MAX];
Bool vr_BreakMatchPattern[MATCH_MAX];
SizeT vr_countMatchPattern[MATCH_MAX];
SizeT vr_nbMatch=0;
Int previousMatchIndex=-1;
HChar* vr_IOmatch_CmdLine =NULL;
HChar* vr_writeLineBuff =NULL;
HChar* vr_writeLineBuffCurrent =NULL;
//SizeT vr_last_expect_lineNo=0;
Bool vr_filter=False;
HChar vr_filter_cmd[FILTER_SIZEMAX];
Int filter_fdin[2];
Int filter_fdout[2];
Int filter_pid;
HChar* vr_filtered_buff;
#define ARGV_FILTER_MAX 10
const HChar *argvFiltered[ARGV_FILTER_MAX];
const HChar nopStr[]="nop";
const HChar emptyStr[]="";
const HChar defaultStr[]="default";
const HChar initStr[]="init";
const HChar postinitStr[]="post-init";
const HChar stopStr[]="stop";
const HChar startStr[]="start";
const HChar stopSoftStr[]="stop_soft";
const HChar startSoftStr[]="start_soft";
const HChar displayCounterStr[]="display_counter";
const HChar nbInstrStr[]="nb_instr";
const HChar resetCounterStr[]="reset_counter";
const HChar dumpCoverStr[]="dump_cover";
const HChar panicStr[]="panic";
const HChar exitStr[]="exit";
typedef enum {nopKey=0, emptyKey, defaultKey, initKey, postinitKey, stopKey, startKey, stopSoftKey, startSoftKey,displayCounterKey, nbInstrKey, resetCounterKey, dumpCoverKey, panicKey, exitKey} Vr_applyKey;
static const SizeT actionNumber=15;
const HChar* actionStrTab[]={nopStr, emptyStr, defaultStr, initStr, postinitStr, stopStr, startStr, stopSoftStr, startSoftStr, displayCounterStr, nbInstrStr, resetCounterStr, dumpCoverStr, panicStr, exitStr};
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)};
//Bool actionRequireCacheCleanTab[]={False, False, False, False, False, True, True, False, False, False, False, False, False };
UInt IOMatch_verbose=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;
}
}
}
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);
}
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);
}
VG_(fprintf)(vr_IOmatchCLRFileLog,"apply: start\n");
return;
case stopSoftKey:
vr_set_instrument_state ("IO Match CLR", False, False);
VG_(fprintf)(vr_IOmatchCLRFileLog,"apply: stop soft\n");
return;
case startSoftKey:
vr_set_instrument_state ("IO Match CLR", True, False);
VG_(fprintf)(vr_IOmatchCLRFileLog,"apply: start soft\n");
return;
case displayCounterKey:
vr_ppOpCount();
VG_(fprintf)(vr_IOmatchCLRFileLog,"apply: display_counter\n");
return;
case nbInstrKey:
{
UInt nbInstr=vr_count_fp_instrumented();
VG_(fprintf)(vr_IOmatchCLRFileLog,"fp_instr: %u\n", nbInstr );
return;
}
case resetCounterKey:
{
vr_resetCount();
return;
}
case dumpCoverKey:
{
SizeT ret;
ret=vr_traceBB_dumpCov();
VG_(fprintf)(vr_IOmatchCLRFileLog,"apply: dump_cover : %lu\n", ret);
return;
}
case panicKey:
{
VG_(fprintf)(vr_IOmatchCLRFileLog, "apply: panic\n");
VG_(tool_panic)("apply: panic");
}
case exitKey:
{
VG_(fprintf)(vr_IOmatchCLRFileLog, "apply: exit\n");
if(vr_filter){
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);
}
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);
}
VgFile* openOutputIOMatchFile(const HChar * fileName, const HChar * fileNameIOMatch, const HChar * strPost);
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)("incomptatible option --output-expect-rep with abspath : %s", fileName);
VG_(tool_panic)("incomptatible 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;
};
HChar* stripSpace(HChar* str);
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;
}
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);
}
//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)){
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");
}
continue;
}
//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);
}
continue;
}
//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)){
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");
}
continue;
}
//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)){
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");
}
continue;
}
//Treat bmatch key
if( VG_(strncmp)(vr_IOmatch_CmdLine, vr_bmatchKeyStr, vr_bmatchKeyStrSize)==0 ){
if(vr_nbMatch> MATCH_MAX){
VG_(tool_panic)("vr_IOmatch_clr : to many match");
}
const HChar* matchPattern=vr_IOmatch_CmdLine+vr_bmatchKeyStrSize;
VG_(fprintf)(vr_IOmatchCLRFileLog, "bmatch pattern [%lu] : %s\n",vr_nbMatch ,matchPattern);
vr_matchPattern[vr_nbMatch]=VG_(strdup)("bmath.patterndup", matchPattern);
vr_nbApplyMatch[vr_nbMatch]=0;
vr_countMatchPattern[vr_nbMatch]=0;
vr_BreakMatchPattern[vr_nbMatch]=True;
vr_nbMatch++;
continue;
}
if( VG_(strncmp)(vr_IOmatch_CmdLine, vr_cmatchKeyStr, vr_cmatchKeyStrSize)==0 ){
if(vr_nbMatch> MATCH_MAX){
VG_(tool_panic)("vr_IOmatch_clr : to many match");
}
const HChar* matchPattern=vr_IOmatch_CmdLine+vr_cmatchKeyStrSize;
VG_(fprintf)(vr_IOmatchCLRFileLog, "cmatch pattern [%lu] : %s\n",vr_nbMatch ,matchPattern);
vr_matchPattern[vr_nbMatch]=VG_(strdup)("cmath.patterndup", matchPattern);
vr_nbApplyMatch[vr_nbMatch]=0;
vr_countMatchPattern[vr_nbMatch]=0;
vr_BreakMatchPattern[vr_nbMatch]=False;
vr_nbMatch++;
continue;
}
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_nbApplyMatch<0){
VG_(tool_panic)("vr_IOmatch_clr : match expected before apply ");
}
vr_nbApplyMatch[vr_nbMatch-1]++;
if(vr_nbApplyMatch[vr_nbMatch-1]>=APPLY_PER_MATCH_MAX ){
VG_(tool_panic)("vr_IOmatch_clr : too many apply per match ");
}
VG_(strncpy)(vr_applyMatch[vr_nbMatch-1][vr_nbApplyMatch[vr_nbMatch-1]-1],applyCmd, MATCH_SIZE_MAX);
continue;
}
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_nbApplyMatch<0){
VG_(tool_panic)("vr_IOmatch_clr : match expected before post-apply ");
}
if(!(vr_BreakMatchPattern[vr_nbMatch-1])){
VG_(tool_panic)("cmatch and post_apply are incompatible ");
}
vr_nbPostApplyMatch[vr_nbMatch-1]++;
if(vr_nbPostApplyMatch[vr_nbMatch-1]>=POST_APPLY_PER_MATCH_MAX ){
VG_(tool_panic)("vr_IOmatch_clr : too many post-apply per match ");
}
VG_(strncpy)(vr_postApplyMatch[vr_nbMatch-1][vr_nbPostApplyMatch[vr_nbMatch-1]-1],applyCmd, MATCH_SIZE_MAX);
continue;
}
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);
continue;
}
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");
continue;
}
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");
continue;
}
//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));
continue;
}
if(IOMatch_verbose>0){
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);
// VG_(umsg)("expectCLR init done\n");
}
int readlineCharByChar(int fd, char* msgRead,int sizeMax);
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{
totalSize+=1;
}
continue;
}
}
return -1;
};
void vr_IOmatch_clr_checkmatch(const HChar* writeLine,SizeT size){
/*As the syscall to not give always a full line we need to create a buffer and to treat the buffer only we detect the end of line*/
static Bool first=True;
SizeT totalSize= (vr_writeLineBuffCurrent - vr_writeLineBuff) + size;
if(totalSize >= LINE_SIZEMAX){
VG_(umsg)("sizemax excedeed\n");
VG_(exit(1));
}
VG_(strncat)(vr_writeLineBuffCurrent , writeLine, size);
vr_writeLineBuff[totalSize]=0;
if(IOMatch_verbose>4){
VG_(umsg)("vr_writeLineBuff: |%s|\n",vr_writeLineBuff);
}
vr_writeLineBuffCurrent=vr_writeLineBuff;
//Search end of file
for( SizeT i =0; i<totalSize ; i++){
if(vr_writeLineBuff[i]=='\n'){
vr_writeLineBuff[i]=0;
HChar* filteredBuf=vr_writeLineBuffCurrent;
if(vr_dumpStdout){
VG_(fprintf)(vr_IOmatchCLRFileStdout,"%s\n", vr_writeLineBuffCurrent);
}
if(vr_filter){
// apply filter
vr_writeLineBuff[i]='\n';
VG_(write)(filter_fdin[1],vr_writeLineBuffCurrent, i+1 - (vr_writeLineBuffCurrent -vr_writeLineBuff ));
vr_writeLineBuff[i]=0;
Int sizeRead=readlineCharByChar(filter_fdout[0], vr_filtered_buff, LINE_SIZEMAX);
if(sizeRead <0){
VG_(umsg)("vr_IOmatchCLR: read error\n");
}
if(sizeRead >=LINE_SIZEMAX){
VG_(umsg)("vr_IOmatchCLR: read error sizemax\n");
}
filteredBuf=vr_filtered_buff;
if(vr_dumpFilteredStdout){
VG_(fprintf)(vr_IOmatchCLRFileFilteredStdout,"%s\n", vr_filtered_buff);
}
}
if(first){
if( !ignoreEmptyLine || (filteredBuf[0]!=0) ){
VG_(fprintf)(vr_IOmatchCLRFileLog,"post-init:\n");
vr_IOmatch_apply_clr("post-init", False);
vr_countPostInit+=1;
first=False;
}
}
if(previousMatchIndex!=-1 ){
if((ignoreEmptyLine) && (filteredBuf[0]==0 )){
if(IOMatch_verbose >0){
VG_(umsg)("empty Line ignored: \n");
}
}else{
if(IOMatch_verbose >0){
VG_(umsg)("post apply:\n");
}
for(SizeT postApplyIndex=0 ; postApplyIndex< vr_nbPostApplyMatch[previousMatchIndex]; postApplyIndex++){
vr_IOmatch_apply_clr(vr_postApplyMatch[previousMatchIndex][postApplyIndex], False);
}
previousMatchIndex=-1;
}
}
if(previousMatchIndex==-1){
Bool matchFound=False;
for(SizeT matchIndex=0; matchIndex< vr_nbMatch; matchIndex++){
if( VG_(string_match)(vr_matchPattern[matchIndex],filteredBuf)){
matchFound=True;
//The line match the expect pattern
if(IOMatch_verbose>0){
VG_(umsg)("match [%lu]: |%s|\n",matchIndex ,vr_writeLineBuffCurrent);
}
vr_countMatchPattern[matchIndex]++;
VG_(fprintf)(vr_IOmatchCLRFileLog,"match [%lu]: %s\n",matchIndex ,vr_writeLineBuffCurrent);
if(vr_filter){
if(IOMatch_verbose>0){
VG_(umsg)("match(filtered): |%s|\n", filteredBuf);
}
VG_(fprintf)(vr_IOmatchCLRFileLog,"match(filtered): %s\n", filteredBuf);
}
//Loop apply to DO
if(vr_nbApplyMatch[matchIndex]==0){
vr_IOmatch_apply_clr("default", False);
}
for(SizeT applyIndex=0 ; applyIndex< vr_nbApplyMatch[matchIndex]; applyIndex++){
vr_IOmatch_apply_clr(vr_applyMatch[matchIndex][applyIndex], False);
}
if(vr_nbPostApplyMatch[matchIndex]!=0){
previousMatchIndex=matchIndex;
}
if(vr_BreakMatchPattern[matchIndex]){
break; //match only once
}
}
}
if( matchFound==False){
VG_(fprintf)(vr_IOmatchCLRFileLog,"line unmatch : |%s|\n", vr_writeLineBuffCurrent);
VG_(fprintf)(vr_IOmatchCLRFileLog,"line(filtered) unmatch: |%s|\n", filteredBuf);
if(IOMatch_verbose>1){
VG_(umsg)("line unmatch : |%s|\n", vr_writeLineBuffCurrent);
VG_(umsg)("line(filtered) unmatch: |%s|\n", filteredBuf);
}
}
}
vr_writeLineBuffCurrent= vr_writeLineBuff+i+1;
}
}
//Move the end of buffer at the begin
SizeT nbRemain=vr_writeLineBuff+totalSize-vr_writeLineBuffCurrent;
for(SizeT i=0 ; i< nbRemain; i++){
vr_writeLineBuff[i]=vr_writeLineBuffCurrent[i];
}
vr_writeLineBuff[nbRemain]=0;
vr_writeLineBuffCurrent=vr_writeLineBuff+nbRemain;
}
void vr_IOmatch_clr_finalize (void){
if(vr_countPostInit==0 && vr_nbpostInit!=0){
VG_(fprintf)(vr_IOmatchCLRFileLog,"post-init:\n");
vr_IOmatch_apply_clr("post-init", True);
}
// If post_apply need to be applied
if(previousMatchIndex!=-1){
for(SizeT postApplyIndex=0 ; postApplyIndex< vr_nbPostApplyMatch[previousMatchIndex]; postApplyIndex++){
vr_IOmatch_apply_clr(vr_postApplyMatch[previousMatchIndex][postApplyIndex], True);
}
previousMatchIndex=-1;
}
VG_(fprintf)(vr_IOmatchCLRFileLog,"match pattern count\n");
VG_(umsg)("match pattern count\n");
for(SizeT matchIndex=0; matchIndex< vr_nbMatch; matchIndex++){
SizeT count=vr_countMatchPattern[matchIndex];
VG_(fprintf)(vr_IOmatchCLRFileLog,"\t%lu : %s\n", count,vr_matchPattern[matchIndex]);
VG_(umsg)("\t%lu : %s\n", count,vr_matchPattern[matchIndex]);
}
//free and close evrything
VG_(free)(vr_writeLineBuff);
if(vr_filter){
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);
}
for(SizeT i=0; i< vr_nbMatch; i++){
VG_(free)(vr_matchPattern[i]);
}
VG_(fclose)(vr_IOmatchCLRFileLog);
if(vr_dumpStdout){
VG_(fclose)(vr_IOmatchCLRFileStdout);
}
if(vr_IOmatchCLRFileFilteredStdout!=NULL){
VG_(fclose)(vr_IOmatchCLRFileFilteredStdout);
}
}