flip_value.py 3.94 KB
Newer Older
1
import os,signal
German Leon's avatar
German Leon committed
2
3
4
5
6
7
8
9
10
import gdb
import time
from classes.BitFlip import BitFlip
from classes.Logging import Logging
import common_parameters as cp

"""
Handler attached to exit event
"""
German Leon's avatar
German Leon committed
11
12
def sendsignal (signal):
   os.kill(int(pid),signal)
German Leon's avatar
German Leon committed
13
14
def exit_handler(event):
    global global_logging
German Leon's avatar
German Leon committed
15
    sendsignal(cp.SIGNAL_EXIT)
German Leon's avatar
German Leon committed
16
    global_logging.info(str("event type: exit"))
17
  
German Leon's avatar
German Leon committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    try:
        global_logging.info("exit code: {}".format(str(event.exit_code)))
    except Exception as err:
        err_str = "ERROR: {}".format(str(err))
        global_logging.exception(err_str)


"""
Handler that will put a breakpoint on the kernel after
signal
"""


def set_event(event):
    # Accessing global vars
German Leon's avatar
German Leon committed
33
    global global_logging, was_hit, bit_flip,bp,t
German Leon's avatar
German Leon committed
34
35
36
    if (isinstance(event, gdb.BreakpointEvent)):
      global_logging.info("Before breakpoint"+ str(time.clock()-t))
      global_logging.info ("Enviado senal a "+ str(pid))
German Leon's avatar
German Leon committed
37
      sendsignal(cp.SIGNAL_STOP)
German Leon's avatar
German Leon committed
38
39
40
      bp.enabled=False
      gdb.execute('c')
     #      #os.system ("killall -2 python3")
German Leon's avatar
German Leon committed
41
    elif (isinstance(event, gdb.SignalEvent)):
German Leon's avatar
German Leon committed
42
      try:
German Leon's avatar
German Leon committed
43
        
German Leon's avatar
German Leon committed
44
45
46
47
        # Just checking if it was hit
        if bit_flip.fault_injected is False:
            bit_flip.single_event()
            global_logging.info("BIT FLIP SET ON SIGNAL {}".format(event.stop_signal))
German Leon's avatar
German Leon committed
48
        #global_logging.info ("Enviado senal a "+ str(pid))
German Leon's avatar
German Leon committed
49
        sendsignal(cp.SIGNAL_STOP)    
German Leon's avatar
German Leon committed
50
51
52
53

      except Exception as err:
        global_logging.exception("EVENT DIFFERENT FROM STOP SIGNAL: {}".format(str(err)))

German Leon's avatar
German Leon committed
54
#De esta forma, si llega un event por nexti (event.stop), no realiza nada.
German Leon's avatar
German Leon committed
55
56
57
58
59
60
"""
Main function
"""


def main():
German Leon's avatar
German Leon committed
61
    global global_logging, register, injection_site, bits_to_flip, fault_model, was_hit, bit_flip, arg0,t,kernel,pid,bp
German Leon's avatar
German Leon committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75

    was_hit = False

    # Initialize GDB to run the app
    gdb.execute("set confirm off")
    gdb.execute("set pagination off")
    gdb.execute("set target-async off")
    gdb.execute("set non-stop off")

    # Connecting to a exit handler event
    gdb.events.exited.connect(exit_handler)

    # Connecting to a stop signal event
    gdb.events.stop.connect(set_event)
German Leon's avatar
German Leon committed
76
                                                   
German Leon's avatar
German Leon committed
77
78
    # Get variables values from environment
    # Firsn parse line
leon@uji.es's avatar
leon@uji.es committed
79
    [kernel,pid,max_regs,bits_to_flip, fault_model, flip_log_file,
German Leon's avatar
German Leon committed
80
     gdb_init_strings, injection_site] = arg0.split('|')
German Leon's avatar
German Leon committed
81
82
83
    
   
    
German Leon's avatar
German Leon committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    # Logging
    global_logging = Logging(log_file=flip_log_file)
    global_logging.info("Starting flip_value script "+" called by " + str(pid) + " for stop kernel " + str(kernel));
    try:
        for init_str in gdb_init_strings.split(";"):
            gdb.execute(init_str)
            global_logging.info("initializing setup: " + str(init_str))

    except gdb.error as err:
        global_logging.exception("ERROR on initializing setup: {}".format(str(err)))

    # Set Breakpoint attributes to be use
    bits_to_flip = [i for i in bits_to_flip.split(",")]
    fault_model = int(fault_model)
leon@uji.es's avatar
leon@uji.es committed
98
    bit_flip = BitFlip(bits_to_flip=bits_to_flip, fault_model=fault_model,max_regs=max_regs,
German Leon's avatar
German Leon committed
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
                       logging=global_logging, injection_site=cp.INJECTION_SITES[injection_site])

    # Start app execution
    t=time.clock();
   
    #gdb.execute("break "+kernel)
    bp=gdb.Breakpoint(kernel)
    global_logging.info("Put Break "+ str(time.clock()-t))
    gdb.execute("r")

  
    i = 0
    try:
        while 'The program' not in gdb.execute('c', to_string=True):
            i += 1
    except Exception as err:
        global_logging.info("CONTINUED {} times".format(i))
        err_str = str(err).rstrip()
        global_logging.exception("IGNORED CONTINUE ERROR: {}".format(err_str))

        # Make sure that it is going to finish
        if 'Failed' in err_str:
            gdb.execute('quit')
            global_logging.exception("QUIT REQUIRED")


# Call main execution
global_logging = None
register = None
bits_to_flip = None
fault_model = None
was_hit = False
injection_site = None
bit_flip = None

main()
German Leon's avatar
German Leon committed
135
136
137
138
139
140