common_functions.py 2.83 KB
Newer Older
German Leon's avatar
German Leon committed
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
import os
import pickle
import re
import sys
import common_parameters as cp

if sys.version_info >= (3, 0):
    import configparser  # python 3
else:
    import ConfigParser  # python 2

"""
Support function to execute a command
and return the output.
If the command contains NEWLINE character
it will result in a list.
"""


def execute_command(gdb, to_execute):
    ret = gdb.execute(to_execute, to_string=True)
    return ret.splitlines()


"""
Serialize a dictionary into a
file path using pickle.
"""


def save_file(file_path, data):
    with open(file_path, "wb") as f_out:
        pickle.dump(data, f_out)
        f_out.close()


"""
Serialize a dictionary into a
file path using pickle.
"""


def append_file(file_path, data):
    with open(file_path, "ab") as f_out:
        pickle.dump(data, f_out)
        f_out.close()


"""
Load a dictionary from a file path using pickle.
return a dictionary
"""


def load_file(file_path):
    with open(file_path, "rb") as f_in:
        data = pickle.load(f_in)
        return data


"""
Read configuration file
"""


def load_config_file(flip_config_file):
    # Read configuration file
    if sys.version_info >= (3, 0):
        conf = configparser.ConfigParser()
    else:
        conf = ConfigParser.ConfigParser()

    conf.read(flip_config_file)
    return conf


"""
Kill all remaining processes
"""


def kill_all(kill_string, logging=None):
    for cmd in kill_string.split(";"):
        os.system(cmd + " > /dev/null 2>&1")
        if logging:
            logging.debug("kill cmd: {}".format(cmd))


"""
GDB python cannot find common_functions.py, so I added this directory to PYTHONPATH
"""


def set_python_env():
    current_path = os.path.dirname(os.path.realpath(__file__))
    os.environ['PYTHONPATH'] = "$PYTHONPATH:" + current_path + ":" + current_path + "/classes"
    os.environ['OMP_NUM_THREADS'] = '1'
    return current_path


"""
Remove all useless information produced by CUDA-GDB on the output files
before they got to the SDC check script
"""


def remove_useless_information_from_output(output_file_path):
    ok_output_lines = []
    with open(output_file_path, 'r') as ifp:
        lines = ifp.readlines()
        for line in lines:
            is_line_addable = True
            for pattern in cp.POSSIBLE_USELESS_GDB_OUTPUT_PATTERNS:
                # It is addable or not
                search_result = re.search(pattern=pattern, string=line)
                if search_result:
                    is_line_addable = False
            if is_line_addable:
                ok_output_lines.append(line)

    # Overwrite the output file
    with open(output_file_path, 'w') as ofp:
        ofp.writelines(ok_output_lines)

"""
Show output function
to allow pretty printing
"""


def printf(*args):
    string_to_print = ""  # ""\r"
    for i in args:
        string_to_print += "{0} ".format(i)

    print(string_to_print)