-
Notifications
You must be signed in to change notification settings - Fork 7
/
libcas.py
executable file
·2299 lines (2006 loc) · 117 KB
/
libcas.py
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
"""
Module contains set of useful libetrace binding functions.
"""
from typing import List, Dict, Set, Tuple, Optional, Iterable
import fnmatch
import multiprocessing
import json
import sys
import os
import time
import shlex
import re
import subprocess
import base64
from functools import lru_cache
import shutil
import libetrace
from bas import gcc
from bas import clang
from bas import exec_worker
try:
# If tqdm is available use it for nice progressbar
from tqdm import tqdm as progressbar
except ModuleNotFoundError:
# Fallback if tqdm is not installed
class progressbar:
n = 0
x = None
def __init__(self, data:Optional[Iterable]=None, x=None, total=0, disable=None):
self.data = data
self.x = x
self.total = total
if disable is None:
self.disable = not sys.stdout.isatty()
def __iter__(self):
return iter(self.data) if self.data else iter([])
def refresh(self):
if not self.disable:
print(f'\r\033[KProcessing {self.n} / {self.total}', end='')
def close(self):
pass
class DepsParam:
"""
Extended parameter used in deps generation. Enables use per-path excludes and direct switch.
:param file: path to process
:type file: str
:param direct: return only direct dependencies of this file
:type direct: bool
:param exclude_cmd: list of commands to exclude while generating dependencies of this file
:type exclude_cmd: List[str]
:param exclude_pattern: list of file patterns to exclude while generating dependencies of this file
:type exclude_pattern: List[str]
:param negate_pattern: reversed pattern - exclude is included
:type negate_pattern: bool
"""
def __init__(self, file: str, direct: Optional[bool] = None, exclude_cmd: Optional[List[str]] = None,
exclude_pattern: Optional[List[str]] = None, negate_pattern: bool = False):
self.file = file
self.direct = direct
self.negate_pattern = negate_pattern
if isinstance(exclude_cmd, list):
self.exclude_cmd = exclude_cmd
elif isinstance(exclude_cmd, str):
self.exclude_cmd = [exclude_cmd]
else:
self.exclude_cmd = []
if isinstance(exclude_pattern, list):
self.exclude_pattern = exclude_pattern
elif isinstance(exclude_pattern, str):
self.exclude_pattern = [exclude_pattern]
else:
self.exclude_pattern = []
class CASConfig:
"""
CAS configuration handler. Loads config from filesystem, and generates excludes.
Specifies compilers and linkers matching patterns, dependency excludes patterns and more.
:param config_file: config file path
:type config_file: str
"""
def __init__(self, config_file):
self.config_info = {}
self.config_file = config_file
self.linker_spec = []
self.ld_spec = []
self.ar_spec = []
self.gcc_spec = []
self.gpp_spec = []
self.clang_spec = []
self.clangpp_spec = []
self.armcc_spec = []
self.clang_tailopts = []
self.dependency_exclude_patterns = []
self.additional_module_exclude_patterns = []
self.module_dependencies_with_pipes:List[str] = []
self.module_dependencies_exclude_with_pipes:List[str] = []
self.additional_module_exclude_pattern_variants = {}
self.exclude_command_variants = {}
self.exclude_command_variants_index = {}
self.string_table = []
self.integrated_clang_compilers = []
self.rbm_wrapping_binaries = []
self.paths = {}
with open(self.config_file, "r", encoding=sys.getfilesystemencoding()) as fil:
self.config_info = json.load(fil)
for key, val in self.config_info.items():
setattr(self, key, val)
# Recompute `exclude_command_variants_index` based on `exclude_command_variants`
pattern_map = {}
for ptr in [v for cv in self.exclude_command_variants.values() for v in cv]:
if ptr not in pattern_map:
pattern_map[ptr] = len(pattern_map)
for wildc, pattern_list in self.exclude_command_variants.items():
self.exclude_command_variants_index[wildc] = [pattern_map[x] for x in pattern_list]
self.string_table = [u[0] for u in sorted(pattern_map.items(), key=lambda x: x[1])]
def apply_source_root(self, source_root=""):
# If `integrated_clang_compilers` contains relative paths to compilers, normalize it to absolute paths
for i, icc_path in enumerate(self.integrated_clang_compilers):
if not os.path.isabs(icc_path):
self.integrated_clang_compilers[i] = os.path.realpath(os.path.join(source_root, self.integrated_clang_compilers[i]))
def gen_excludes_for_path(self, path) -> Tuple[List, List, List]:
excl_patterns = [x for x in self.dependency_exclude_patterns]
excl_commands = []
excl_commands_index = []
for pattern_variant, pattern_val in self.additional_module_exclude_pattern_variants.items():
if fnmatch.fnmatch(path, pattern_variant):
excl_patterns += pattern_val
for command_variant, command_val in self.exclude_command_variants_index.items():
if fnmatch.fnmatch(path, command_variant):
excl_commands_index += command_val
return excl_patterns, excl_commands, excl_commands_index
def get_use_pipe_for_path(self, path:str, global_pipe_opt:bool) -> bool:
"""Function returns proper use-pipe argument if overwrite was defined in config
:param path: File path
:type path: str
:param global_pipe_opt: global use-pipe argument
:type global_pipe_opt: bool
:return: True if pipes should be used otherwise False
:rtype: bool
"""
if global_pipe_opt:
for path_wildcard in self.module_dependencies_exclude_with_pipes:
if fnmatch.fnmatch(path, path_wildcard):
return False
return True
else:
for path_wildcard in self.module_dependencies_with_pipes:
if fnmatch.fnmatch(path, path_wildcard):
return True
return False
class CASDatabase:
"""
Wrapping object for libetrace.nfsdb interface.
Used for most database interaction.
"""
db: libetrace.nfsdb
"""Main database object - direct wrapper to `libetrace.nfsdb`"""
source_root: str
"""Directory where tracing process started execution"""
db_loaded: bool
"""Database load status"""
cache_db_loaded: bool
"""Cache database load status"""
config: CASConfig
"""CASConfig object"""
def __init__(self) -> None:
self.db = libetrace.nfsdb()
self.db_loaded = False
self.cache_db_loaded = False
self.config = None
self.db_path = None
def set_config(self, config: CASConfig):
"""
Function used to assign CASConfig to database
:param config: config object
:type config: CASConfig
"""
self.config = config
def load_db(self, db_file:str, debug: bool=False, quiet: bool=True) -> bool:
"""
Function uses libetrace.load to load database and applies config.
:param db_file: Database file
:type db_file: str
:param debug: print debug information, defaults to False
:type debug: bool, optional
:param quiet: suppress verbose prints, defaults to True
:type quiet: bool, optional
:return: True if load succed otherwise False
:rtype: bool
"""
self.db_loaded = self.db.load(db_file, debug=debug, quiet=quiet)
self.source_root = self.db.source_root
assert self.config is not None, "Please set config first. Use CASDatabase.set_config()"
self.config.apply_source_root(self.source_root)
self.db_path = db_file
return self.db_loaded
@staticmethod
def get_db_version(db_file: str) -> str:
"""
Function returns version of given nfsdb file
:param db_file: Database file
:type db_file: str
"""
return libetrace.database_version(db_file)
@staticmethod
def get_img_version(db_file: str) -> str:
"""
Function returns version of given image file
:param db_file: Database file
:type db_file: str
"""
return libetrace.image_version(db_file)
def load_deps_db(self, db_file:str, debug: bool=False, quiet: bool=True, mp_safe: bool=True, no_map_memory: bool = False) -> bool:
"""
Function uses libetrace.load_deps to load database from given filename
:param cache_filename: database file path
:type db_file: str
:param debug: print debug information, defaults to False
:type debug: bool, optional
:param quiet: suppress verbose prints, defaults to True
:type quiet: bool, optional
:param mp_safe: load database in read-only mode slower but safer when using multiprocessing, defaults to True
:type mp_safe: bool, optional
:param no_map_memory: prevents memory mapping, defaults to False
:type no_map_memory: bool, optional
:return: True if load succed otherwise False
:rtype: bool
"""
self.cache_db_loaded = self.db.load_deps(db_file, debug=debug, quiet=quiet, mp_safe=mp_safe, no_map_memory=no_map_memory)
return self.cache_db_loaded
@lru_cache(maxsize=1)
def linked_module_paths(self) -> List[str]:
"""
Function returns linked modules paths.
This function uses cache.
:return: List of module paths
:rtype: List[str]
"""
return list({x[0] for x in self.db.linked_module_paths()})
@lru_cache(maxsize=1)
def object_files_paths(self) -> List[str]:
"""
Function returns objects file paths.
This function uses cache.
:return: List of object files paths
:rtype: List[str]
"""
return list({ obj_p
for x in self.db.filtered_execs(has_comp_info=True)
for obj_p in x.compilation_info.object_paths })
@lru_cache(maxsize=1)
def linked_modules(self) -> List[libetrace.nfsdbEntryOpenfile]:
"""
Function returns linked modules as libetrace.nfsdbEntryOpenfile objects.
This function uses cache.
:return: List of opens objects that are modules
:rtype: List[libetrace.nfsdbEntryOpenfile]
"""
return list({x[0] for x in self.db.linked_modules()})
def get_entries_with_pid(self, pid: int) -> List[libetrace.nfsdbEntry]:
"""
Function return list of execs with given pid.
:param pid: process pid
:type pid: int
:return: list of execs object
:rtype: List[libetrace.nfsdbEntry]
"""
return self.db[tuple([pid])]
def get_entries_with_pids(self, pidlist: "List[Tuple[int, int]]| List[Tuple[int,]]") -> List[libetrace.nfsdbEntry]:
"""
Function return list of execs with given list of pid,idx tuples.
:param pidlist: process pid and optional index tuple
:type pidlist: List[Tuple[int, Optional[int]]]
:return: list of execs object
:rtype: List[libetrace.nfsdbEntry]
"""
return self.db[pidlist]
def get_exec(self, pid: int, index: int) -> libetrace.nfsdbEntry:
"""
Function returns exec with given pid and index.
:param pid: process pid
:type pid: int
:param index: process index
:type index: int
:return: exec object
:rtype: libetrace.nfsdbEntry
"""
return self.db[(pid, index)][0]
def get_exec_at_pos(self, ptr: int) -> libetrace.nfsdbEntry:
"""
Function returns exec with given pointer value.
:param pos: pointer to exec `libetrace.nfsdbEntry.ptr`
:type pos: int
:return: exec object
:rtype: libetrace.nfsdbEntry
"""
return self.db[ptr]
def get_eid(self, eid: "Tuple[int, int] | Tuple[int,]") -> List[libetrace.nfsdbEntry]:
"""
Function returns execs with given eid value.
:param pos: list of execs matching given eid value
:type pos: `Tuple[int, int] | Tuple[int,]`
:return: list of exec objects
:rtype: `List[libetrace.nfsdbEntry]`
"""
return self.db[eid]
def get_eids(self, eids: "List[Tuple[int, int]] | List[Tuple[int,]]"):
"""
Function returns execs with given eid values.
:param pos: list of execs matching given eid values
:type pos: `"List[Tuple[int, int]] | List[Tuple[int,]]"`
:return: list of exec objects
:rtype: `List[libetrace.nfsdbEntry]`
"""
return self.db[eids]
def opens_list(self) -> List[libetrace.nfsdbEntryOpenfile]:
"""
Function returns list of all opens objects.
:return: list of opens
:rtype: List[libetrace.nfsdbEntryOpenfile]
"""
return self.db.opens()
def opens_iter(self) -> libetrace.nfsdbOpensIter:
"""
Function returns iterator to all opens objects.
:return: opens iterator
:rtype: libetrace.nfsdbOpensIter
"""
return self.db.opens_iter()
def opens_paths(self) -> List[str]:
"""
Function returns list of unique opens paths.
:return: list of open paths
:rtype: List[str]
"""
return self.db.opens_paths()
def filtered_paths(self, file_filter:Optional[List]=None, path: Optional[List[str]] = None,
has_path: Optional[str] = None, wc: Optional[str] = None, re: Optional[str] = None,
compiled: Optional[bool] = None, linked: Optional[bool] = None, linked_static: Optional[bool] = None,
linked_shared: Optional[bool] = None, linked_exe: Optional[bool] = None, plain: Optional[bool] = None,
compiler: Optional[bool] = None, linker: Optional[bool] = None, binary: Optional[bool] = None,
symlink: Optional[bool] = None, no_symlink: Optional[bool] = None,
file_exists: Optional[bool] = None, file_not_exists: Optional[bool] = None, dir_exists: Optional[bool] = None,
has_access: Optional[int] = None, negate: Optional[bool] = None,
at_source_root: Optional[bool] = None, not_at_source_root: Optional[bool] = None,
source_type: Optional[int] = None) -> List[str]:
return self.db.filtered_paths(file_filter=file_filter, path=path, has_path=has_path, wc=wc, re=re, compiled=compiled,
linked=linked, linked_static=linked_static, linked_shared=linked_shared,
linked_exe=linked_exe, plain=plain, compiler=compiler,linker=linker, binary=binary, symlink=symlink,
no_symlink=no_symlink, file_exists=file_exists, file_not_exists=file_not_exists, dir_exists=dir_exists,
has_access=has_access, negate=negate, at_source_root=at_source_root, not_at_source_root=not_at_source_root, source_type=source_type)
def filtered_paths_iter(self, file_filter:Optional[List]=None, path: Optional[List[str]] = None,
has_path: Optional[str] = None, wc: Optional[str] = None, re: Optional[str] = None,
compiled: Optional[bool] = None, linked: Optional[bool] = None, linked_static: Optional[bool] = None,
linked_shared: Optional[bool] = None, linked_exe: Optional[bool] = None, plain: Optional[bool] = None,
compiler: Optional[bool] = None, linker: Optional[bool] = None, binary: Optional[bool] = None,
symlink: Optional[bool] = None, no_symlink: Optional[bool] = None,
file_exists: Optional[bool] = None, file_not_exists: Optional[bool] = None, dir_exists: Optional[bool] = None,
has_access: Optional[int] = None, negate: Optional[bool] = None,
at_source_root: Optional[bool] = None, not_at_source_root: Optional[bool] = None,
source_type: Optional[int] = None) -> libetrace.nfsdbFilteredOpensPathsIter:
return self.db.filtered_paths_iter(file_filter=file_filter, path=path, has_path=has_path, wc=wc, re=re, compiled=compiled,
linked=linked, linked_static=linked_static, linked_shared=linked_shared,
linked_exe=linked_exe, plain=plain, compiler=compiler,linker=linker, binary=binary, symlink=symlink,
no_symlink=no_symlink, file_exists=file_exists, file_not_exists=file_not_exists, dir_exists=dir_exists,
has_access=has_access, negate=negate, at_source_root=at_source_root, not_at_source_root=not_at_source_root, source_type=source_type)
def filtered_opens(self, file_filter:Optional[List]=None, path: Optional[List[str]] = None,
has_path: Optional[str] = None, wc: Optional[str] = None, re: Optional[str] = None,
compiled: Optional[bool] = None, linked: Optional[bool] = None, linked_static: Optional[bool] = None,
linked_shared: Optional[bool] = None, linked_exe: Optional[bool] = None, plain: Optional[bool] = None,
compiler: Optional[bool] = None, linker: Optional[bool] = None, binary: Optional[bool] = None,
symlink: Optional[bool] = None, no_symlink: Optional[bool] = None,
file_exists: Optional[bool] = None, file_not_exists: Optional[bool] = None, dir_exists: Optional[bool] = None,
has_access: Optional[int] = None, negate: Optional[bool] = None,
at_source_root: Optional[bool] = None, not_at_source_root: Optional[bool] = None,
source_type: Optional[int] = None) -> List[libetrace.nfsdbEntryOpenfile]:
"""
Function filters opens with given `file_filter` and set of global filter parameters and returns list of opens objects.
Global filter switches are used before `file_filter`.
:param file_filter: file filter object, defaults to None
:type file_filter: Optional[List], optional
:param path: match opens with given paths, defaults to None
:type path: Optional[List[str]], optional
:param has_path: _description_, defaults to None
:type has_path: Optional[str], optional
:param wc: match opens with wildcard, defaults to None
:type wc: Optional[str], optional
:param re: match opens with regex, defaults to None
:type re: Optional[str], optional
:param compiled: returns opens that has been use in compilation, defaults to None
:type compiled: Optional[bool], optional
:param linked: returns opens that has been use in linking, defaults to None
:type linked: Optional[bool], optional
:param linked_static: returns opens that has been use in linking static module, defaults to None
:type linked_static: Optional[bool], optional
:param linked_shared: returns opens that has been use in linking shared module, defaults to None
:type linked_shared: Optional[bool], optional
:param linked_exe: returns opens that has been use in linking executable module, defaults to None
:type linked_exe: Optional[bool], optional
:param plain: returns opens that wasn't used in any linking or compilation, defaults to None
:type plain: Optional[bool], optional
:param compiler: returns opens which path is compilers, defaults to None
:type compiler: Optional[bool], optional
:param linker: returns opens which path is linkers, defaults to None
:type linker: Optional[bool], optional
:param binary: returns opens which path is binary file, defaults to None
:type binary: Optional[bool], optional
:param symlink: returns opens which path is symlink, defaults to None
:type symlink: Optional[bool], optional
:param no_symlink: returns opens which path is not symlink, defaults to None
:type no_symlink: Optional[bool], optional
:param file_exists: returns opens which path exists while building database, defaults to None
:type file_exists: Optional[bool], optional
:param file_not_exists: returns opens which path did not exists while building database, defaults to None
:type file_not_exists: Optional[bool], optional
:param dir_exists: returns opens which path is dir and exists while building database, defaults to None
:type dir_exists: Optional[bool], optional
:param has_access: returns opens which where opened with given access type, defaults to None
:type has_access: Optional[int], optional
:param negate: negate global switches, defaults to None
:type negate: Optional[bool], optional
:param at_source_root: returns opens with source root in begining of path, defaults to None
:type at_source_root: Optional[bool], optional
:param not_at_source_root: returns opens without source root in begining of path, defaults to None
:type not_at_source_root: Optional[bool], optional
:param source_type: returns opens with path that was compiled and matching given source type, defaults to None
:type source_type: Optional[int], optional
:return: list of opens objects
:rtype: List[libetrace.nfsdbEntryOpenfile]
"""
return self.db.filtered_opens(file_filter=file_filter, path=path, has_path=has_path, wc=wc, re=re, compiled=compiled,
linked=linked, linked_static=linked_static, linked_shared=linked_shared,
linked_exe=linked_exe, plain=plain, compiler=compiler,linker=linker, binary=binary, symlink=symlink,
no_symlink=no_symlink, file_exists=file_exists, file_not_exists=file_not_exists, dir_exists=dir_exists,
has_access=has_access, negate=negate, at_source_root=at_source_root, not_at_source_root=not_at_source_root, source_type=source_type)
def filtered_opens_iter(self, file_filter:Optional[List]=None, path: Optional[List[str]] = None,
has_path: Optional[str] = None, wc: Optional[str] = None, re: Optional[str] = None,
compiled: Optional[bool] = None, linked: Optional[bool] = None, linked_static: Optional[bool] = None,
linked_shared: Optional[bool] = None, linked_exe: Optional[bool] = None, plain: Optional[bool] = None,
compiler: Optional[bool] = None, linker: Optional[bool] = None, binary: Optional[bool] = None,
symlink: Optional[bool] = None, no_symlink: Optional[bool] = None,
file_exists: Optional[bool] = None, file_not_exists: Optional[bool] = None, dir_exists: Optional[bool] = None,
has_access: Optional[int] = None, negate: Optional[bool] = None,
at_source_root: Optional[bool] = None, not_at_source_root: Optional[bool] = None,
source_type: Optional[int] = None) -> libetrace.nfsdbFilteredOpensIter:
"""
Function filters opens with given `file_filter` and set of global filter parameters and returns opens objects iterator.
Global filter switches are used before `file_filter`.
:param file_filter: file filter object, defaults to None
:type file_filter: Optional[List], optional
:param path: match opens with given paths, defaults to None
:type path: Optional[List[str]], optional
:param has_path: _description_, defaults to None
:type has_path: Optional[str], optional
:param wc: match opens with wildcard, defaults to None
:type wc: Optional[str], optional
:param re: match opens with regex, defaults to None
:type re: Optional[str], optional
:param compiled: returns opens that has been use in compilation, defaults to None
:type compiled: Optional[bool], optional
:param linked: returns opens that has been use in linking, defaults to None
:type linked: Optional[bool], optional
:param linked_static: returns opens that has been use in linking static module, defaults to None
:type linked_static: Optional[bool], optional
:param linked_shared: returns opens that has been use in linking shared module, defaults to None
:type linked_shared: Optional[bool], optional
:param linked_exe: returns opens that has been use in linking executable module, defaults to None
:type linked_exe: Optional[bool], optional
:param plain: returns opens that wasn't used in any linking or compilation, defaults to None
:type plain: Optional[bool], optional
:param compiler: returns opens which path is compilers, defaults to None
:type compiler: Optional[bool], optional
:param linker: returns opens which path is linkers, defaults to None
:type linker: Optional[bool], optional
:param binary: returns opens which path is binary file, defaults to None
:type binary: Optional[bool], optional
:param symlink: returns opens which path is symlink, defaults to None
:type symlink: Optional[bool], optional
:param no_symlink: returns opens which path is not symlink, defaults to None
:type no_symlink: Optional[bool], optional
:param file_exists: returns opens which path exists while building database, defaults to None
:type file_exists: Optional[bool], optional
:param file_not_exists: returns opens which path did not exists while building database, defaults to None
:type file_not_exists: Optional[bool], optional
:param dir_exists: returns opens which path is dir and exists while building database, defaults to None
:type dir_exists: Optional[bool], optional
:param has_access: returns opens which where opened with given access type, defaults to None
:type has_access: Optional[int], optional
:param negate: negate global switches, defaults to None
:type negate: Optional[bool], optional
:param at_source_root: returns opens with source root in begining of path, defaults to None
:type at_source_root: Optional[bool], optional
:param not_at_source_root: returns opens without source root in begining of path, defaults to None
:type not_at_source_root: Optional[bool], optional
:param source_type: returns opens with path that was compiled and matching given source type, defaults to None
:type source_type: Optional[int], optional
:return: open objects iterator
:rtype: libetrace.nfsdbFilteredOpensIter
"""
return self.db.filtered_opens_iter(file_filter=file_filter, path=path, has_path=has_path, wc=wc, re=re, compiled=compiled,
linked=linked, linked_static=linked_static, linked_shared=linked_shared,
linked_exe=linked_exe, plain=plain, compiler=compiler,linker=linker, binary=binary, symlink=symlink,
no_symlink=no_symlink, file_exists=file_exists, file_not_exists=file_not_exists, dir_exists=dir_exists,
has_access=has_access, negate=negate, at_source_root=at_source_root, not_at_source_root=not_at_source_root, source_type=source_type)
def filtered_execs(self, exec_filter: Optional[List]=None, bins: Optional[List[str]] = None, pids: Optional[List[int]] = None,
cwd_has_str: Optional[str] = None, cwd_wc: Optional[str] = None, cwd_re: Optional[str] = None,
cmd_has_str: Optional[str] = None, cmd_wc: Optional[str] = None, cmd_re: Optional[str] = None,
bin_has_str: Optional[str] = None, bin_wc: Optional[str] = None, bin_re: Optional[str] = None,
has_ppid: Optional[int] = None, has_command: Optional[bool] = None, has_comp_info: Optional[bool] = None,
has_linked_file:Optional[bool] = None, negate: Optional[bool] = None,
bin_at_source_root: Optional[bool] = None, bin_not_at_source_root: Optional[bool] = None,
cwd_at_source_root: Optional[bool] = None, cwd_not_at_source_root: Optional[bool] = None) -> List[libetrace.nfsdbEntry]:
"""
Function filters execs with given `exec_filter` and set of global filter parameters and returns list of execs objects.
Global filter switches are used before `exec_filter`.
:param exec_filter: exec filter object, defaults to None
:type exec_filter: Optional[List], optional
:param bins: return execs with given bins, defaults to None
:type bins: Optional[List[str]], optional
:param pids: return execs with given pids, defaults to None
:type pids: Optional[List[int]], optional
:param cwd_has_str: return execs which cwd contains given path, defaults to None
:type cwd_has_str: Optional[str], optional
:param cwd_wc: return execs which cwd matches given wildcard, defaults to None
:type cwd_wc: Optional[str], optional
:param cwd_re: return execs which cwd matches given regex, defaults to None
:type cwd_re: Optional[str], optional
:param cmd_has_str: return execs which cmd contains given path, defaults to None
:type cmd_has_str: Optional[str], optional
:param cmd_wc: return execs which cmd matches given wildcard, defaults to None
:type cmd_wc: Optional[str], optional
:param cmd_re: return execs which cmd matches given regex, defaults to None
:type cmd_re: Optional[str], optional
:param bin_has_str: return execs which bin contains given path, defaults to None
:type bin_has_str: Optional[str], optional
:param bin_wc: return execs which bin matches given wildcard, defaults to None
:type bin_wc: Optional[str], optional
:param bin_re: return execs which bin matches given regex, defaults to None
:type bin_re: Optional[str], optional
:param has_ppid: return execs with given parent pids, defaults to None
:type has_ppid: Optional[int], optional
:param has_command: return execs that are generic commands, defaults to None
:type has_command: Optional[bool], optional
:param has_comp_info: return execs that are compilations, defaults to None
:type has_comp_info: Optional[bool], optional
:param has_linked_file: return execs that are linkers, defaults to None
:type has_linked_file: Optional[bool], optional
:param negate: negate global switches, defaults to None
:type negate: Optional[bool], optional
:param bin_at_source_root: return execs which bin starts with source root, defaults to None
:type bin_at_source_root: Optional[bool], optional
:param bin_not_at_source_root: return execs which bin does not starts with source root, defaults to None
:type bin_not_at_source_root: Optional[bool], optional
:param cwd_at_source_root: return execs which cwd starts with source root, defaults to None
:type cwd_at_source_root: Optional[bool], optional
:param cwd_not_at_source_root: return execs which cwd does not starts with source root, defaults to None
:type cwd_not_at_source_root: Optional[bool], optional
:return: list of execs objects
:rtype: List[libetrace.nfsdbEntry]
"""
return self.db.filtered_execs(exec_filter=exec_filter, bins=bins, pids=pids, cwd_has_str=cwd_has_str, cwd_wc=cwd_wc, cwd_re=cwd_re,
cmd_has_str=cmd_has_str, cmd_wc=cmd_wc, cmd_re=cmd_re, bin_has_str=bin_has_str, bin_wc=bin_wc, bin_re=bin_re,
has_ppid=has_ppid, has_command=has_command, has_comp_info=has_comp_info, has_linked_file=has_linked_file,negate=negate,
bin_at_source_root=bin_at_source_root,bin_not_at_source_root=bin_not_at_source_root,
cwd_at_source_root=cwd_at_source_root,cwd_not_at_source_root=cwd_not_at_source_root)
def filtered_execs_iter(self, exec_filter:Optional[List]=None, bins: Optional[List[str]] = None, pids: Optional[List[int]] = None,
cwd_has_str: Optional[str] = None, cwd_wc: Optional[str] = None, cwd_re: Optional[str] = None,
cmd_has_str: Optional[str] = None, cmd_wc: Optional[str] = None, cmd_re: Optional[str] = None,
bin_has_str: Optional[str] = None, bin_wc: Optional[str] = None, bin_re: Optional[str] = None,
has_ppid: Optional[int] = None, has_command: Optional[bool] = None, has_comp_info: Optional[bool] = None,
has_linked_file:Optional[bool] = None, negate: Optional[bool] = None,
bin_at_source_root: Optional[bool] = None, bin_not_at_source_root: Optional[bool] = None,
cwd_at_source_root: Optional[bool] = None, cwd_not_at_source_root: Optional[bool] = None) -> libetrace.nfsdbFilteredCommandsIter:
"""
Function filters execs with given `exec_filter` and set of global filter parameters and returns execs objects iterator.
Global filter switches are used before `exec_filter`.
:param exec_filter: exec filter object, defaults to None
:type exec_filter: Optional[List], optional
:param bins: return execs with given bins, defaults to None
:type bins: Optional[List[str]], optional
:param pids: return execs with given pids, defaults to None
:type pids: Optional[List[int]], optional
:param cwd_has_str: return execs which cwd contains given path, defaults to None
:type cwd_has_str: Optional[str], optional
:param cwd_wc: return execs which cwd matches given wildcard, defaults to None
:type cwd_wc: Optional[str], optional
:param cwd_re: return execs which cwd matches given regex, defaults to None
:type cwd_re: Optional[str], optional
:param cmd_has_str: return execs which cmd contains given path, defaults to None
:type cmd_has_str: Optional[str], optional
:param cmd_wc: return execs which cmd matches given wildcard, defaults to None
:type cmd_wc: Optional[str], optional
:param cmd_re: return execs which cmd matches given regex, defaults to None
:type cmd_re: Optional[str], optional
:param bin_has_str: return execs which bin contains given path, defaults to None
:type bin_has_str: Optional[str], optional
:param bin_wc: return execs which bin matches given wildcard, defaults to None
:type bin_wc: Optional[str], optional
:param bin_re: return execs which bin matches given regex, defaults to None
:type bin_re: Optional[str], optional
:param has_ppid: return execs with given parent pids, defaults to None
:type has_ppid: Optional[int], optional
:param has_command: return execs that are generic commands, defaults to None
:type has_command: Optional[bool], optional
:param has_comp_info: return execs that are compilations, defaults to None
:type has_comp_info: Optional[bool], optional
:param has_linked_file: return execs that are linkers, defaults to None
:type has_linked_file: Optional[bool], optional
:param negate: negate global switches, defaults to None
:type negate: Optional[bool], optional
:param bin_at_source_root: return execs which bin starts with source root, defaults to None
:type bin_at_source_root: Optional[bool], optional
:param bin_not_at_source_root: return execs which bin does not starts with source root, defaults to None
:type bin_not_at_source_root: Optional[bool], optional
:param cwd_at_source_root: return execs which cwd starts with source root, defaults to None
:type cwd_at_source_root: Optional[bool], optional
:param cwd_not_at_source_root: return execs which cwd does not starts with source root, defaults to None
:type cwd_not_at_source_root: Optional[bool], optional
:return: execs objects iterator
:rtype: libetrace.nfsdbFilteredCommandsIter
"""
return self.db.filtered_execs_iter(exec_filter=exec_filter, bins=bins, pids=pids, cwd_has_str=cwd_has_str, cwd_wc=cwd_wc, cwd_re=cwd_re,
cmd_has_str=cmd_has_str, cmd_wc=cmd_wc, cmd_re=cmd_re, bin_has_str=bin_has_str, bin_wc=bin_wc, bin_re=bin_re,
has_ppid=has_ppid, has_command=has_command, has_comp_info=has_comp_info, has_linked_file=has_linked_file,negate=negate,
bin_at_source_root=bin_at_source_root,bin_not_at_source_root=bin_not_at_source_root,
cwd_at_source_root=cwd_at_source_root,cwd_not_at_source_root=cwd_not_at_source_root)
def opens_num(self) -> int:
"""
Function returns opens count.
:return: opens count
:rtype: int
"""
return len(self.db.opens_iter())
def execs_num(self):
"""
Function returns execs count.
:return: execs count
:rtype: int
"""
return len(self.db)
def get_version(self) -> str:
"""
Function returns version string (set in cache creation)
:return: version string
:rtype: str
"""
return self.db.dbversion
def get_opens_of_path(self, file_path: str) -> List[libetrace.nfsdbEntryOpenfile]:
"""
Function returns opens objects with given path.
:param file_path: file name
:type file_path: str
:return: list of opens objects
:rtype: List[libetrace.nfsdbEntryOpenfile]
"""
return self.db.filemap[file_path]
def get_execs_using_binary(self, binary: str) -> List[libetrace.nfsdbEntry]:
"""
Function returns execs objects with given binary path.
:param binary: binary path
:type binary: str
:return: list of execs objects
:rtype: List[libetrace.nfsdbEntry]
"""
ret = []
try:
ret = self.db[binary]
except Exception:
pass
return ret
def get_compilations(self) -> Set[libetrace.nfsdbEntry]:
"""
Function returns compiler execs.
:return: compiler execs objects
:rtype: Set[libetrace.nfsdbEntry]
"""
return set(self.db.filtered_execs_iter(has_comp_info=True))
def get_compiled_files(self) -> Set[libetrace.nfsdbEntryOpenfile]:
"""
Function returns compiled opens.
:return: compiled opens objects
:rtype: Set[libetrace.nfsdbEntryOpenfile]
"""
return { cfile
for ent in self.get_compilations()
for cfile in ent.compilation_info.files }
@lru_cache(maxsize=1)
def get_compiled_file_paths(self) -> Set[str]:
"""
Function returns unique compiled file paths.
:return: compiled paths
:rtype: Set[str]
"""
return { cfile
for ent in self.get_compilations()
for cfile in ent.compilation_info.file_paths }
def get_linkers(self) -> Set[libetrace.nfsdbEntry]:
"""
Function returns linker execs.
:return: linker execs objects
:rtype: Set[libetrace.nfsdbEntry]
"""
return set(self.db.filtered_execs_iter(has_linked_file=True))
def get_linked_files(self) -> Set[libetrace.nfsdbEntryOpenfile]:
"""
Function returns linked opens.
:return: linked opens objects
:rtype: Set[libetrace.nfsdbEntryOpenfile]
"""
return { ent.linked_file for ent in self.get_linkers() }
@lru_cache(maxsize=1)
def get_linked_file_paths(self) -> Set[str]:
"""
Function returns unique linked file paths.
:return: linked paths
:rtype: Set[str]
"""
return { ent.linked_path for ent in self.get_linkers() }
def get_relative_path(self, file_path: str) -> str:
"""
Function removes source path from given file path.
:param file_path: file path
:type file_path: str
:return: file path without source root
:rtype: str
"""
return file_path.replace(self.source_root, "")
def get_open_path(self, opn: libetrace.nfsdbEntryOpenfile, relative_path=False, original_path=False) -> str:
"""
Function returns desired path from nfsdbEntryOpenfile object depending on original_path and relative_path parameters.
:param opn: nfsdbEntryOpenfile object
:type opn: libetrace.nfsdbEntryOpenfile
:param relative_path: get only relative path
:type relative_path: bool
:param original_path: get original path
:type original_path: bool
:return: Desired file path
:rtype: str
"""
if relative_path:
return self.get_relative_path(opn.original_path if original_path else opn.path)
else:
return opn.original_path if original_path else opn.path
def get_reverse_dependencies(self, file_paths: "str | List[str]", recursive=False) -> List[str]:
"""
Function returns reverse dependencies of given file paths.
:param file_paths: file paths
:type file_paths: str | List[str]
:param recursive: enable recursive processing, defaults to False
:type recursive: bool, optional
:return: reverse dependencies
:rtype: List[str]
"""
return list(self.db.rdeps(file_paths, recursive=recursive))
def get_module_dependencies(self, module_paths:"List[DepsParam| str]", direct:bool=False) -> List[libetrace.nfsdbEntryOpenfile]:
"""
Function gets precomputed module dependencies for given module path.
:param module_path: extended path or simple string path
:type module_path: DepsParam | str
:param direct: direct flag
:type direct: bool
"""
paths:List[str] = []
for m in module_paths:
if isinstance(m, DepsParam):
paths.append(m.file)
else:
paths.append(m)
return self.db.mdeps(paths, direct=direct)
def get_deps(self, epath: "DepsParam | str", direct_global=False, dep_graph=False, debug=False, debug_fd=False, use_pipes=False,
wrap_deps=False, all_modules: Optional[List[str]] = None) -> Tuple[List[int], List[str], List[libetrace.nfsdbEntryOpenfile], Dict]:
"""
Function calculates dependencies of given file.
:param epath: extended path or simple string path
:type epath: DepsParam | str
:param direct_global: global direct flag
:type direct_global: bool
:param dep_graph: generate dependency graph
:type dep_graph: bool
:param debug: enable debug info about dependency generation arguments
:type debug: bool
:param debug_fd: enable debug info about process of dependency generation
:type debug_fd: bool
:param use_pipes: relation between process pipe will be respected
:type use_pipes: bool
:param wrap_deps: process wrappers (like bash) will be respected
:type wrap_deps: bool
:param all_modules: list of all modules - needed in direct deps generation
:type all_modules: List[str]
:return: Tuple with process id, list of paths, list of opens objects and optionaly dependency graph
:rtype: Tuple[List[int],List[str],List[nfsdbEntryOpenfile],Dict]
"""
direct = direct_global
if direct and all_modules is None:
all_modules = self.linked_module_paths()
if isinstance(epath, str): # simple path - no excludes
excl_patterns, excl_commands, excl_commands_index = self.config.gen_excludes_for_path(epath)
use_pipe_for_path = self.config.get_use_pipe_for_path(epath, use_pipes)
if all_modules is not None:
return self.db.fdeps(epath, debug=debug, debug_fd=debug_fd, use_pipes=use_pipe_for_path,
wrap_deps=wrap_deps, direct=direct, dep_graph=dep_graph, exclude_patterns=excl_patterns,
exclude_commands=excl_commands, exclude_commands_index=excl_commands_index, all_modules=all_modules)
else:
return self.db.fdeps(epath, debug=debug, debug_fd=debug_fd, use_pipes=use_pipe_for_path,
wrap_deps=wrap_deps, direct=direct, dep_graph=dep_graph, exclude_patterns=excl_patterns,
exclude_commands=excl_commands, exclude_commands_index=excl_commands_index)
else:
if epath.direct is not None: # If extended path has direct it will overwrite global direct args
direct = epath.direct
if direct and all_modules is None:
all_modules = self.linked_module_paths()
excl_patterns, excl_commands, excl_commands_index = self.config.gen_excludes_for_path(epath.file)
use_pipe_for_path = self.config.get_use_pipe_for_path(epath.file, use_pipes)
for e_c in epath.exclude_cmd:
excl_commands.append(e_c)
for e_p in epath.exclude_pattern:
excl_patterns.append(e_p)
if all_modules is not None:
return self.db.fdeps(epath.file, debug=debug, debug_fd=debug_fd, use_pipes=use_pipe_for_path,
wrap_deps=wrap_deps, direct=direct, dep_graph=dep_graph, exclude_patterns=excl_patterns,
exclude_commands=excl_commands, negate_pattern=epath.negate_pattern,
exclude_commands_index=excl_commands_index, all_modules=all_modules)
else:
return self.db.fdeps(epath.file, debug=debug, debug_fd=debug_fd, use_pipes=use_pipe_for_path,
wrap_deps=wrap_deps, direct=direct, dep_graph=dep_graph, exclude_patterns=excl_patterns,
exclude_commands=excl_commands, negate_pattern=epath.negate_pattern,
exclude_commands_index=excl_commands_index)
def get_multi_deps_cached(self, epaths:"List[DepsParam|str]", direct_global=False) -> List[libetrace.nfsdbEntryOpenfile]:
"""
Function returns cached list of open files that are dependencies of given file(s).
:param epaths: extended path or simple string path
:type epaths: List[DepsParam] | List[str]
:param direct_global: global direct flag, defaults to False
:type direct_global: bool, optional
:return: list of dependency opens
:rtype: List[List[libetrace.nfsdbEntryOpenfile]]
"""
return self.get_module_dependencies(epaths, direct=direct_global)
def get_multi_deps(self, epaths:"List[DepsParam|str]", direct_global:bool=False, dep_graph:bool=False,
debug:bool=False, debug_fd:bool=False, use_pipes:bool=False, wrap_deps:bool=False) -> List[libetrace.nfsdbEntryOpenfile]:
"""
Function returns list of open files that are dependencies of given file path(s).
:param epaths: extended path or simple string path
:type epaths: List[DepsParam] | List[str]
:param direct_global: global direct flag, defaults to False
:type direct_global: bool, optional
:param dep_graph: generate dependency graph, defaults to False
:type dep_graph: bool, optional
:param debug: enable debug info about dependency generation arguments
:type debug: bool
:param debug_fd: enable debug info about process of dependency generation
:type debug_fd: bool
:param use_pipes: relation between process pipe will be respected
:type use_pipes: bool
:param wrap_deps: process wrappers (like bash) will be respected
:type wrap_deps: bool
:return: list of dependency opens
:rtype: List[List[libetrace.nfsdbEntryOpenfile]]
"""
ret = []
for epath in epaths:
ret += self.get_deps(epath, direct_global=direct_global, dep_graph=dep_graph,
debug=debug, debug_fd=debug_fd, use_pipes=use_pipes, wrap_deps=wrap_deps)[2]
return ret
def get_dependency_graph(self, epaths:"List[DepsParam|str]", direct_global=False,
debug=False, debug_fd=False, use_pipes=False, wrap_deps=False) -> List[Tuple]:
"""
Function returns list of dependency graph of given file path(s).
:param epaths: extended path or simple string path
:type epaths: List[DepsParam|str]
:param direct_global: global direct flag, defaults to False
:type direct_global: bool, optional