-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathFileIO.java
More file actions
2997 lines (2667 loc) · 112 KB
/
FileIO.java
File metadata and controls
2997 lines (2667 loc) · 112 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
package daikon;
import static daikon.PptRelation.PptRelationType;
import static daikon.PptTopLevel.PptFlags;
import static daikon.PptTopLevel.PptType;
import static daikon.VarInfo.LangFlags;
import static daikon.VarInfo.RefType;
import static daikon.VarInfo.VarFlags;
import static daikon.VarInfo.VarKind;
import static daikon.tools.nullness.NullnessUtils.castNonNullDeep;
import daikon.config.Configuration;
import daikon.derive.ValueAndModified;
import daikon.diff.InvMap;
import daikon.inv.Invariant;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.*;
import plume.*;
/*>>>
import org.checkerframework.checker.interning.qual.*;
import org.checkerframework.checker.nullness.qual.*;
import org.checkerframework.dataflow.qual.*;
*/
public final class FileIO {
/** Nobody should ever instantiate a FileIO. **/
private FileIO() {
throw new Error();
}
/// Constants
static final String declaration_header = "DECLARE";
// Program point name tags
public static final String ppt_tag_separator = ":::";
public static final String enter_suffix = "ENTER";
public static final String enter_tag = ppt_tag_separator + enter_suffix;
// EXIT does not necessarily appear at the end of the program point name;
// a number may follow it.
public static final String exit_suffix = "EXIT";
public static final String exit_tag = ppt_tag_separator + exit_suffix;
public static final String throw_suffix = "THROW";
public static final String throw_tag = ppt_tag_separator + throw_suffix;
public static final String throws_suffix = "THROWS";
public static final String throws_tag = ppt_tag_separator + throws_suffix;
public static final String exception_suffix = "THROWSCOMBINED";
public static final String exception_tag = ppt_tag_separator + exception_suffix;
public static final String object_suffix = "OBJECT";
public static final String object_tag = ppt_tag_separator + object_suffix;
public static final String class_static_suffix = "CLASS";
public static final String class_static_tag = ppt_tag_separator + class_static_suffix;
public static final String global_suffix = "GLOBAL";
private static final String lineSep = Global.lineSep;
/// Settings
// Variables starting with dkconfig_ should only be set via the
// daikon.config.Configuration interface.
/**
* When true, just ignore exit ppts that don't have a matching enter
* ppt rather than exiting with an error. Unmatched exits can occur
* if only a portion of a dtrace file is processed.
*/
public static boolean dkconfig_ignore_missing_enter = false;
/**
* Boolean. When false, set modbits to 1 iff the printed
* representation has changed. When true, set modbits to 1 if the
* printed representation has changed; leave other modbits as is.
**/
public static boolean dkconfig_add_changed = true;
/**
* Integer. Maximum number of lines to read from the dtrace file. If
* 0, reads the entire file.
*/
public static int dkconfig_max_line_number = 0;
/**
* Boolean. When false, don't count the number of lines in the dtrace file
* before reading. This will disable the percentage progress printout.
*/
public static boolean dkconfig_count_lines = true;
/**
* Boolean. When true, only read the samples, but don't process them.
* Used to gather timing information.
*/
public static boolean dkconfig_read_samples_only = false;
/** Boolean. When true, don't print a warning about unmatched procedure
* entries, which are ignored by Daikon (unless the --nohierarchy switch
* is provided).
**/
public static boolean dkconfig_unmatched_procedure_entries_quiet = false;
/**
* Boolean. If true, prints the unmatched procedure entries
* verbosely.
**/
public static boolean dkconfig_verbose_unmatched_procedure_entries = false;
/**
* Boolean. When true, suppress exceptions related to file reading.
* This permits Daikon to continue even if there is a malformed trace
* file. Use this with care: in general, it is better to fix the
* problem that caused a bad trace file, rather than to suppress the
* exception.
**/
public static boolean dkconfig_continue_after_file_exception = false;
/**
* Long integer. If non-zero, this value will be used as the number
* of lines in (each) dtrace file input for the purposes of the
* progress display, and the counting of the lines in the file will
* be suppressed.
*/
public static long dkconfig_dtrace_line_count = 0;
/** True if declaration records are in the new format -- that is, decl-version 2.0. **/
// Set by read_decl_version; by read_data_trace_record if the file is non-empty;
// by read_serialized_pptmap; and by InvMap.readObject.
public static /*@MonotonicNonNull*/ Boolean new_decl_format = null;
/**
* Do not use this routine unless you know what you are doing.
* This routine breaks the representation invariant that new_decl_format,
* once set, is never reset to null.
* This routine should be used only if you can guarantee that
* new_decl_format will be once again set to a non-null value before any
* code runs that depends on the fact that new_decl_format is non-null.
*/
@SuppressWarnings("nullness") // reinitialization
public static void resetNewDeclFormat() {
FileIO.new_decl_format = null;
}
/**
* If true, modified all ppt names to remove duplicate routine
* names within the ppt name. This is used when a stack trace
* (of active methods) is used as the ppt name. The routine names
* must be separated by vertical bars (|)
*/
public static boolean dkconfig_rm_stack_dups = false;
/// Variables
// This hashmap maps every program point to an array, which contains the
// old values of all variables in scope the last time the program point
// was executed. This enables us to determine whether the values have been
// modified since this program point was last executed.
static HashMap<PptTopLevel, String[]> ppt_to_value_reps = new HashMap<PptTopLevel, String[]>();
// For debugging purposes: printing out a modified trace file with
// changed modbits.
private static boolean to_write_nonce = false;
private static final String NONCE_HEADER = "this_invocation_nonce";
private static String nonce_value = "no nonce (yet)";
// (This implementation as a public static variable is a bit unclean.)
// Number of ignored declarations.
public static int omitted_declarations = 0;
// Logging Categories
/**
* If true, then print the variable name each time the variable's value
* is first missing/nonsensical.
**/
public static boolean debug_missing = false;
/** Debug tracer for reading. **/
public static final Logger debugRead = Logger.getLogger("daikon.FileIO.read");
/** Debug tracer for printing. **/
public static final Logger debugPrint = Logger.getLogger("daikon.FileIO.printDtrace");
/** Debug tracer for printing variable values. **/
public static final Logger debugVars = Logger.getLogger("daikon.FileIO.vars");
public static final SimpleLog debug_decl = new SimpleLog(false);
// Are these supposed to be printed like Daikon.TerminationMessage, or
// intended to indicate internal Daikon errors, or both? There is no
// documentation of this. The latter ("both") would be bad design.
/** Errors while processing ppt declarations */
public static class DeclError extends IOException {
static final long serialVersionUID = 20060518L;
public DeclError(String msg) {
super(msg);
}
public DeclError(String msg, Throwable cause) {
super(msg, cause);
}
@SuppressWarnings("formatter") // acts as format method wrapper
public static DeclError detail(ParseState state, String format, /*@Nullable*/ Object... args) {
String msg = String.format(format, args) + state.line_file_message();
return new DeclError(msg);
}
public static DeclError detail(ParseState state, Throwable cause) {
String msg = cause.getMessage() + state.line_file_message();
if (msg.startsWith("null at")) {
msg = msg.substring(5);
}
return new DeclError(msg, cause);
}
@SuppressWarnings("formatter") // acts as format method wrapper
public static DeclError detail(
ParseState state, Throwable cause, String format, /*@Nullable*/ Object... args) {
String msg = String.format(format, args) + state.line_file_message();
return new DeclError(msg, cause);
}
}
/**
* Parents in the ppt/variable hierarchy for a particular program point
*/
public static final class ParentRelation implements java.io.Serializable {
static final long serialVersionUID = 20060622L;
public PptRelationType rel_type;
public /*@Interned*/ String parent_ppt_name;
public int id;
public ParentRelation(PptRelationType rel_type, /*@Interned*/ String parent_ppt_name, int id) {
this.rel_type = rel_type;
this.parent_ppt_name = parent_ppt_name;
this.id = id;
}
/*@SideEffectFree*/ public String toString() {
return parent_ppt_name + "[" + id + "] " + rel_type;
};
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
if (parent_ppt_name != null) parent_ppt_name.intern();
}
}
// Utilities
/*@EnsuresNonNullIf(result=true, expression="#1")*/
/*@Pure*/ public static final boolean isComment(/*@Nullable*/ String s) {
return s != null && (s.startsWith("//") || s.startsWith("#"));
}
// Nullness-checking of read_data_trace_record(ParseState) works even
// without these two lines, since StringBuilderDelimited accepts null values.
@SuppressWarnings(
"nullness:contracts.conditional.postcondition.not.satisfied") // readLine() assertion is ensured by call to reset()
// can't do this since readLine is not deterministic /*@EnsuresNonNullIf(result=true, expression="#1.readLine()")*/
public static final boolean nextLineIsComment(BufferedReader reader) {
boolean result = false;
try {
reader.mark(10000);
String nextline = reader.readLine();
result = isComment(nextline);
} catch (IOException e) {
result = false;
} finally {
try {
reader.reset();
} catch (IOException e) {
throw new Error(e);
}
}
return result;
}
///////////////////////////////////////////////////////////////////////////
/// Declaration files
///
/**
* @param files files to be read (java.io.File)
* @return a new PptMap containing declarations read from the files
* listed in the argument; connection information (controlling
* variables and entry ppts) is set correctly upon return.
**/
public static PptMap read_declaration_files(Collection<File> files) throws IOException {
PptMap all_ppts = new PptMap();
// Read all decls, creating PptTopLevels and VarInfos
for (File file : files) {
Daikon.progress = "Reading " + file;
if (!Daikon.dkconfig_quiet) {
System.out.print("."); // show progress
}
read_declaration_file(file, all_ppts);
}
return all_ppts;
}
/** Read one decls file; add it to all_ppts. **/
public static void read_declaration_file(File filename, PptMap all_ppts) throws IOException {
if (Daikon.using_DaikonSimple) {
Processor processor = new DaikonSimple.SimpleProcessor();
read_data_trace_file(filename.toString(), all_ppts, processor, true, false);
} else {
Processor processor = new Processor();
read_data_trace_file(filename.toString(), all_ppts, processor, true, true);
}
}
// Read a declaration in the Version 2 format. For Version 1, see
// read_declaration.
/**
* Reads one ppt declaration. The next line should be the ppt record.
* After completion, the file pointer will be pointing at the next
* record (ie, the blank line at the end of the ppt declaration will
* have been read in).
* Returns null if the ppt is excluded/omitted from this execution of Daikon.
*/
private static /*@Nullable*/ PptTopLevel read_ppt_decl(ParseState state, String top_line)
throws IOException {
// process the ppt record
String line = top_line;
Scanner scanner = new Scanner(line);
/*@Interned*/ String record_name = need(state, scanner, "'ppt'");
if (record_name != "ppt") { // interned
decl_error(state, "found '%s' where 'ppt' expected", record_name);
}
String ppt_name = need(state, scanner, "ppt name");
ppt_name = user_mod_ppt_name(ppt_name);
// Information that will populate the new program point
Map<String, VarDefinition> varmap = new LinkedHashMap<String, VarDefinition>();
// The VarDefinition we are in the middle of reading, or null if we are not.
VarDefinition vardef = null;
List<ParentRelation> ppt_parents = new ArrayList<ParentRelation>();
EnumSet<PptFlags> ppt_flags = EnumSet.noneOf(PptFlags.class);
PptType ppt_type = PptType.POINT;
// Read the records that define this program point
while ((line = state.reader.readLine()) != null) {
debug_decl.log("read line %s%n", line);
line = line.trim();
if (line.length() == 0) break;
scanner = new Scanner(line);
/*@Interned*/ String record = scanner.next().intern();
if (vardef == null) {
if (record == "parent") { // interned
ppt_parents.add(parse_ppt_parent(state, scanner));
} else if (record == "flags") { // interned
parse_ppt_flags(state, scanner, ppt_flags);
} else if (record == "variable") { // interned
vardef = new VarDefinition(state, scanner);
// There is no need to check "varmap.containsKey(vardef.name)"
// because this is the first variable.
assert varmap.isEmpty();
if (var_included(vardef.name)) varmap.put(vardef.name, vardef);
} else if (record == "ppt-type") { // interned
ppt_type = parse_ppt_type(state, scanner);
} else {
decl_error(state, "record '%s' found where %s expected", record, "'parent', 'flags'");
}
} else { // there must be a current variable
if (record == "var-kind") { // interned
vardef.parse_var_kind(scanner);
} else if (record == "enclosing-var") { // interned
vardef.parse_enclosing_var(scanner);
} else if (record == "reference-type") { // interned
vardef.parse_reference_type(scanner);
} else if (record == "array") { // interned
vardef.parse_array(scanner);
} else if (record == "function-args") { // interned
vardef.parse_function_args(scanner);
} else if (record == "rep-type") { // interned
vardef.parse_rep_type(scanner);
} else if (record == "dec-type") { // interned
vardef.parse_dec_type(scanner);
} else if (record == "flags") { // interned
vardef.parse_flags(scanner);
} else if (record == "lang-flags") { // interned
vardef.parse_lang_flags(scanner);
} else if (record == "parent") { // interned
vardef.parse_parent(scanner, ppt_parents);
} else if (record == "comparability") { // interned
vardef.parse_comparability(scanner);
} else if (record == "constant") { // interned
vardef.parse_constant(scanner);
} else if (record == "variable") { // interned
try {
vardef.checkRep(); // make sure the previous variable is ok
} catch (AssertionError e) {
decl_error(state, e);
}
vardef = new VarDefinition(state, scanner);
if (varmap.containsKey(vardef.name)) {
decl_error(state, "var %s declared twice", vardef.name);
}
if (var_included(vardef.name)) varmap.put(vardef.name, vardef);
} else if (record == "min-value") { // interned
vardef.parse_min_value(scanner);
} else if (record == "max-value") { // interned
vardef.parse_max_value(scanner);
} else if (record == "min-length") { // interned
vardef.parse_min_length(scanner);
} else if (record == "max-length") { // interned
vardef.parse_max_length(scanner);
} else if (record == "valid-values") { // interned
vardef.parse_valid_values(scanner);
} else {
decl_error(state, "Unexpected variable item '%s' found", record);
}
}
}
if (vardef != null) {
try {
vardef.checkRep();
} catch (AssertionError e) {
decl_error(state, e);
}
}
// If we are excluding this ppt, just read the data and throw it away
if (!ppt_included(ppt_name)) {
omitted_declarations++;
return null;
}
// Build the var infos from the var definitions.
List<VarInfo> vi_list = new ArrayList<VarInfo>(varmap.size());
for (VarDefinition vd : varmap.values()) {
vi_list.add(new VarInfo(vd));
}
VarInfo[] vi_array = vi_list.toArray(new VarInfo[vi_list.size()]);
// Check to see if the program point is new
if (state.all_ppts.containsName(ppt_name)) {
PptTopLevel existing_ppt = state.all_ppts.get(ppt_name);
assert existing_ppt != null : "state.all_ppts.containsName(" + ppt_name + ")";
assert existing_ppt != null
: "@AssumeAssertion(nullness): bug: containsName() is annotated as @EnsuresNonNullIf(result=true, expression='get(#0)')";
if (state.ppts_may_be_new) {
check_decl_match(state, existing_ppt, vi_array);
} else { // ppts are already in the map
if (VarInfo.assertionsEnabled()) {
for (VarInfo vi : vi_array) {
vi.checkRep();
}
}
return existing_ppt;
}
}
// Build the program point
PptTopLevel newppt = new PptTopLevel(ppt_name, ppt_type, ppt_parents, ppt_flags, vi_array);
return newppt;
}
/** Parses a ppt parent hierarchy record and returns it. **/
private static ParentRelation parse_ppt_parent(ParseState state, Scanner scanner)
throws DeclError {
PptRelationType rel_type =
parse_enum_val(state, scanner, PptRelationType.class, "relation type");
String parent_ppt_name = need(state, scanner, "ppt name");
int id = Integer.parseInt(need(state, scanner, "relation id"));
ParentRelation pr = new ParentRelation(rel_type, parent_ppt_name, id);
need_eol(state, scanner);
return pr;
}
/**
* Parses a program point flag record. Adds any specified flags to
* to flags.
*/
private static void parse_ppt_flags(ParseState state, Scanner scanner, EnumSet<PptFlags> flags)
throws DeclError {
flags.add(parse_enum_val(state, scanner, PptFlags.class, "ppt flags"));
while (scanner.hasNext())
flags.add(parse_enum_val(state, scanner, PptFlags.class, "ppt flags"));
}
/** Parses a ppt-type record and returns the type **/
private static PptType parse_ppt_type(ParseState state, Scanner scanner) throws DeclError {
PptType ppt_type = parse_enum_val(state, scanner, PptType.class, "ppt type");
need_eol(state, scanner);
return ppt_type;
}
// Read a declaration in the Version 1 format. For version 2, see
// read_ppt_decl.
// The "DECLARE" line has already been read.
private static /*@Nullable*/ PptTopLevel read_declaration(ParseState state) throws IOException {
// We have just read the "DECLARE" line.
String ppt_name = state.reader.readLine();
if (ppt_name == null) {
throw new Daikon.TerminationMessage(
"File ends with \"DECLARE\" with no following program point name", state);
}
ppt_name = user_mod_ppt_name(ppt_name);
ppt_name = ppt_name.intern();
VarInfo[] vi_array = read_VarInfos(state, ppt_name);
// System.out.printf ("Ppt %s with %d variables\n", ppt_name,
// vi_array.length);
// This program point name has already been encountered.
if (state.all_ppts.containsName(ppt_name)) {
PptTopLevel existing_ppt = state.all_ppts.get(ppt_name);
assert existing_ppt != null : "state.all_ppts.containsName(" + ppt_name + ")";
assert existing_ppt != null
: "@AssumeAssertion(nullness): bug: containsName() is annotated as @EnsuresNonNullIf(result=true, expression='get(#0)')";
if (state.ppts_may_be_new) {
check_decl_match(state, existing_ppt, vi_array);
} else { // ppts are already in the map
return existing_ppt;
}
}
// If we are excluding this ppt, just throw it away
if (!ppt_included(ppt_name)) {
omitted_declarations++;
return null;
}
// taking care of visibility information
// the information is needed in the variable hierarchy because private methods
// should not be linked under the object program point
// the ppt name is truncated before putting it in the pptMap because the visibility
// information is only present in the decls file and not the dtrace file
// if (ppt_name.startsWith("public")) {
// int position = ppt_name.indexOf("public");
// ppt_name = ppt_name.substring(7);
// PptTopLevel newppt = new PptTopLevel(ppt_name, vi_array);
// newppt.ppt_name.setVisibility("public");
// return newppt;
// }
// if (ppt_name.startsWith("private")) {
// int position = ppt_name.indexOf("private");
// ppt_name = ppt_name.substring(8);
// PptTopLevel newppt = new PptTopLevel(ppt_name, vi_array);
// newppt.ppt_name.setVisibility("private");
// return newppt;
// }
// if (ppt_name.startsWith("protected")) {
// int position = ppt_name.indexOf("protected");
// ppt_name = ppt_name.substring(10);
// PptTopLevel newppt = new PptTopLevel(ppt_name, vi_array);
// newppt.ppt_name.setVisibility("protected");
// return newppt;
// }
//TODO: add a new config variable to turn this accessibility flag processing on?
PptTopLevel newppt = new PptTopLevel(ppt_name, vi_array);
// newppt.ppt_name.setVisibility("package-protected");
return newppt;
// return new PptTopLevel(ppt_name, vi_array);
}
private static VarInfo[] read_VarInfos(ParseState state, String ppt_name) throws IOException {
// The var_infos that will populate the new program point
List<VarInfo> var_infos = new ArrayList<VarInfo>();
// Each iteration reads a variable name, type, and comparability.
// Possibly abstract this out into a separate function??
VarInfo vi;
while ((vi = read_VarInfo(state, ppt_name)) != null) {
for (VarInfo vi2 : var_infos) {
if (vi.name() == vi2.name()) {
throw new Daikon.TerminationMessage("Duplicate variable name " + vi.name(), state);
}
}
// Can't do this test in read_VarInfo, it seems, because of the test
// against null above.
if (!var_included(vi.name())) {
continue;
}
var_infos.add(vi);
}
VarInfo[] result = var_infos.toArray(new VarInfo[var_infos.size()]);
return result;
}
// So that warning message below is only printed once
private static boolean seen_string_rep_type = false;
/**
* Read a variable name, type, and comparability; construct a VarInfo.
* Return null after reading the last variable in this program point
* declaration.
**/
private static /*@Nullable*/ VarInfo read_VarInfo(ParseState state, String ppt_name)
throws IOException {
LineNumberReader file = state.reader;
int varcomp_format = state.varcomp_format;
String filename = state.filename;
String line = file.readLine();
if ((line == null) || (line.equals(""))) {
return null;
}
String varname = line;
String proglang_type_string_and_aux = file.readLine();
String file_rep_type_string = file.readLine();
String comparability_string = file.readLine();
if ( // (varname == null) || // just check varname above
(proglang_type_string_and_aux == null)
|| (file_rep_type_string == null)
|| (comparability_string == null))
throw new Daikon.TerminationMessage(
"End of file "
+ filename
+ " while reading variable "
+ varname
+ " in declaration of program point "
+ ppt_name);
int equals_index = file_rep_type_string.indexOf(" = ");
String static_constant_value_string = null;
/*@Interned*/ Object static_constant_value = null;
boolean is_static_constant = false;
if (equals_index != -1) {
is_static_constant = true;
static_constant_value_string = file_rep_type_string.substring(equals_index + 3);
file_rep_type_string = file_rep_type_string.substring(0, equals_index);
}
// XXX temporary, for compatibility with older .dtrace files. 12/20/2001
if ("String".equals(file_rep_type_string)) {
file_rep_type_string = "java.lang.String";
if (!seen_string_rep_type) {
seen_string_rep_type = true;
System.err.println(
"Warning: Malformed trace file. Representation type 'String' should be "
+ "'java.lang.String' instead on line "
+ (file.getLineNumber() - 1)
+ " of "
+ filename);
}
}
// This is for people who were confused by the above temporary
// workaround when it didn't have a warning. But this has never
// worked, so it's fatal.
else if ("String[]".equals(file_rep_type_string)) {
throw new Daikon.TerminationMessage(
"Representation type 'String[]' should be "
+ "'java.lang.String[]' instead for variable "
+ varname,
file,
filename);
}
/// XXX
int hash_position = proglang_type_string_and_aux.indexOf('#');
String aux_string = "";
if (hash_position == -1) {
hash_position = proglang_type_string_and_aux.length();
} else {
aux_string =
proglang_type_string_and_aux.substring(
hash_position + 1, proglang_type_string_and_aux.length());
}
String proglang_type_string = proglang_type_string_and_aux.substring(0, hash_position).trim();
ProglangType prog_type;
ProglangType file_rep_type;
ProglangType rep_type;
VarInfoAux aux;
try {
prog_type = ProglangType.parse(proglang_type_string);
file_rep_type = ProglangType.rep_parse(file_rep_type_string);
rep_type = file_rep_type.fileTypeToRepType();
aux = VarInfoAux.parse(aux_string);
} catch (IOException e) {
throw new Daikon.TerminationMessage(e, file, filename);
}
if (static_constant_value_string != null) {
static_constant_value = rep_type.parse_value(static_constant_value_string, file, filename);
// Why can't the value be null?
assert static_constant_value != null;
}
VarComparability comparability = null;
try {
comparability = VarComparability.parse(varcomp_format, comparability_string, prog_type);
} catch (Exception e) {
throw new Daikon.TerminationMessage(
String.format(
"Error parsing comparability (%s) at line %d " + "in file %s",
e,
file.getLineNumber(),
filename));
}
if (!VarInfo.legalFileRepType(file_rep_type)) {
throw new Daikon.TerminationMessage(
"Unsupported representation type "
+ file_rep_type.format()
+ " (parsed as "
+ rep_type
+ ")"
+ " for variable "
+ varname,
file,
filename);
}
if (!VarInfo.legalRepType(rep_type)) {
throw new Daikon.TerminationMessage(
"Unsupported (converted) representation type "
+ file_rep_type.format()
+ " for variable "
+ varname,
file,
filename);
}
// COMPARABILITY TEST
if (!(comparability.alwaysComparable()
|| ((VarComparabilityImplicit) comparability).dimensions == file_rep_type.dimensions())) {
System.err.println();
throw new Daikon.TerminationMessage(
"Rep type "
+ file_rep_type.format()
+ " has "
+ file_rep_type.dimensions()
+ " dimensions,"
+ " but comparability "
+ comparability
+ " has "
+ ((VarComparabilityImplicit) comparability).dimensions
+ " dimensions,"
+ " for variable "
+ varname,
file,
filename);
}
return new VarInfo(
varname,
prog_type,
file_rep_type,
comparability,
is_static_constant,
static_constant_value,
aux);
}
/*@RequiresNonNull("FileIO.new_decl_format")*/
private static int read_var_comparability(ParseState state, String line) throws IOException {
// System.out.printf("read_var_comparability, line = '%s' %b%n", line,
// new_decl_format);
String comp_str = null;
if (new_decl_format) {
Scanner scanner = new Scanner(line);
scanner.next();
comp_str = need(state, scanner, "comparability");
need_eol(state, scanner);
} else { // old format
comp_str = state.reader.readLine();
if (comp_str == null) {
throw new Daikon.TerminationMessage("Found end of file, expected comparability", state);
}
}
if (comp_str.equals("none")) {
return VarComparability.NONE;
} else if (comp_str.equals("implicit")) {
return VarComparability.IMPLICIT;
} else {
throw new Daikon.TerminationMessage(
"Unrecognized VarComparability '" + comp_str + "'", state);
}
}
private static /*@Interned*/ String read_input_language(ParseState state, String line)
throws IOException {
Scanner scanner = new Scanner(line);
scanner.next();
/*@Interned*/ String input_lang = need(state, scanner, "input language");
need_eol(state, scanner);
return input_lang;
}
/*@EnsuresNonNull("FileIO.new_decl_format")*/
private static void read_decl_version(ParseState state, String line) throws IOException {
Scanner scanner = new Scanner(line);
scanner.next();
/*@Interned*/ String version = need(state, scanner, "declaration version number");
need_eol(state, scanner);
boolean new_df;
if (version == "2.0") // interned
new_df = true;
else if (version == "1.0") // interned
new_df = false;
else {
decl_error(state, "'%s' found where 1.0 or 2.0 expected", version);
throw new Error("Can't get here"); // help out definite assignment analysis
}
// Make sure that if a format was specified previously, it is the same
if ((new_decl_format != null) && (new_df != new_decl_format.booleanValue())) {
decl_error(state, "decl format '%s' does not match previous setting", version);
}
// System.out.println("setting new_decl_format = " + new_df);
new_decl_format = Boolean.valueOf(new_df);
}
// Each line following is the name (in JVM form) of a class that
// implements java.util.List. All those lines (including interspersed
// comments) are returned.
private static String read_list_implementors(LineNumberReader reader) throws IOException {
StringBuilderDelimited result = new StringBuilderDelimited(lineSep);
for (; ; ) {
String line = reader.readLine();
if (line == null || line.equals("")) break;
result.append(line);
if (isComment(line)) continue;
ProglangType.list_implementors.add(line.intern());
}
return result.toString();
}
///////////////////////////////////////////////////////////////////////////
/// invocation tracking for dtrace files entry/exit grouping
///
static final class Invocation implements Comparable<Invocation> {
PptTopLevel ppt; // used in printing and in suppressing duplicates
// Rather than a valuetuple, place its elements here.
/*@Nullable*/ Object[] vals;
int[] mods;
static Object canonical_hashcode = new Object();
Invocation(PptTopLevel ppt, /*@Nullable*/ Object[] vals, int[] mods) {
this.ppt = ppt;
this.vals = vals;
this.mods = mods;
}
// Print the Invocation on two lines, indented by two spaces
// The receiver Invocation may be canonicalized or not.
String format() {
return format(true);
}
// Print the Invocation on one or two lines, indented by two spaces.
// The receiver Invocation may be canonicalized or not.
String format(boolean show_values) {
if (!show_values) {
return " " + ppt.ppt_name.getNameWithoutPoint();
}
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.println(" " + ppt.ppt_name.getNameWithoutPoint());
pw.print(" ");
// [adonovan] is this sound? Let me know if not (sorry).
//assert ppt.var_infos.length == vals.length;
for (int j = 0; j < vals.length; j++) {
if (j != 0) pw.print(", ");
pw.print(ppt.var_infos[j].name() + "=");
Object val = vals[j];
if (canonical_hashcode.equals(
val)) // succeeds only for canonicalized Invocations. Can be an == test, but there is little point. val can be null, so it cannot be the receiver.
pw.print("<hashcode>");
else if (val instanceof int[]) pw.print(ArraysMDE.toString((int[]) val));
else if (val instanceof String) pw.print(UtilMDE.escapeNonASCII((String) val));
else pw.print(val);
}
pw.println();
return sw.toString();
}
/** Change uses of hashcodes to canonical_hashcode. **/
public /*@Interned*/ Invocation canonicalize() {
/*@Nullable*/ Object[] new_vals = new /*@Nullable*/ Object[vals.length];
System.arraycopy(vals, 0, new_vals, 0, vals.length);
VarInfo[] vis = ppt.var_infos;
// Warning: abstraction violation!
for (VarInfo vi : vis) {
if ((vi.value_index != -1) && (vi.file_rep_type == ProglangType.HASHCODE)) {
new_vals[vi.value_index] = canonical_hashcode;
}
}
return new /*@Interned*/ Invocation(ppt, new_vals, mods);
}
// Return true if the invocations print the same
/*@EnsuresNonNullIf(result=true, expression="#1")*/
/*@Pure*/ public boolean equals(/*@Nullable*/ Object other) {
if (other instanceof FileIO.Invocation) {
return this.format().equals(((FileIO.Invocation) other).format());
} else {
return false;
}
}
/*@Pure*/ public int compareTo(Invocation other) {
return ppt.name().compareTo(other.ppt.name());
}
/*@Pure*/ public int hashCode() {
return this.format().hashCode();
}
}
// call_hashmap is for procedures with a (global, not per-procedure)
// nonce that indicates which returns are associated with which entries.
// call_stack is for functions without nonces.
// I could save some Object overhead by using two parallel stacks
// instead of Invocation objects; but that's not worth it.
static Stack<Invocation> call_stack = new Stack<Invocation>();
static HashMap<Integer, Invocation> call_hashmap = new HashMap<Integer, Invocation>();
/**
* Reads data from .dtrace files.
* For each record in the files, calls the appropriate callback in the processor.
* @see #read_data_trace_files(Collection,PptMap,Processor,boolean)
* @see #read_data_trace_file(String,PptMap,Processor,boolean,boolean)
**/
public static void read_data_trace_files(Collection<String> files, PptMap all_ppts)
throws IOException {
Processor processor = new Processor();
read_data_trace_files(files, all_ppts, processor, true);
}
/**
* Reads data from .dtrace files.
* Calls
* {@link #read_data_trace_file(String,PptMap,Processor,boolean,boolean)}
* for each element of filenames.
*
* @param ppts_may_be_new true if declarations of ppts read from the data
* trace file are new (and thus are not in all_ppts).
* false if the ppts may already be there.
*
* @see #read_data_trace_file(String,PptMap,Processor,boolean,boolean)
**/
public static void read_data_trace_files(
Collection<String> files, PptMap all_ppts, Processor processor, boolean ppts_may_be_new)
throws IOException {
for (String filename : files) {
// System.out.printf ("processing filename %s%n", filename);
try {
read_data_trace_file(filename, all_ppts, processor, false, ppts_may_be_new);
} catch (Daikon.TerminationMessage e) {
throw e;
} catch (Throwable e) {
String message = e.getMessage();
if (message != null && message.equals("Corrupt GZIP trailer")) {
System.out.println(
filename + " has a corrupt gzip trailer. " + "All possible data was recovered.");
} else if (dkconfig_continue_after_file_exception) {
System.out.println();
System.out.println(
"WARNING: Error while processing " + "trace file - remaining records ignored");
System.out.print("Ignored backtrace:");
e.printStackTrace(System.out);
System.out.println();
} else {
throw new Error(e);
}
}
}
if (Daikon.server_dir != null) {
// Yoav: server mode
while (true) {
@SuppressWarnings(
"nullness") // server_dir is a directory; this was checked when the variable was set
String /*@NonNull*/ [] dir_files = Daikon.server_dir.list();
Arrays.sort(dir_files);
boolean hasEnd = false;
for (String f : dir_files) {
if (f.endsWith(".end")) hasEnd = true;
if (f.endsWith(".end") || f.endsWith(".start")) continue;