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

"""
Handler attached to exit event
"""


def exit_handler(event):
    global global_logging
15
      os.system ("kill -s SIGRTMIN " + str(pid))
German Leon's avatar
German Leon committed
16
17
    global_logging.info(str("event type: exit"))
    print ("llego el final")
18
  
German Leon's avatar
German Leon committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    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
34
    global global_logging, was_hit, bit_flip,bp,t
German Leon's avatar
German Leon committed
35
36
37
38
39
40
41
    if (isinstance(event, gdb.BreakpointEvent)):
      global_logging.info("Before breakpoint"+ str(time.clock()-t))
      global_logging.info ("Enviado senal a "+ str(pid))
      os.system ("kill -s USR1 " + str(pid))
      bp.enabled=False
      gdb.execute('c')
     #      #os.system ("killall -2 python3")
German Leon's avatar
German Leon committed
42
    elif (isinstance(event, gdb.SignalEvent)):
German Leon's avatar
German Leon committed
43
      try:
German Leon's avatar
German Leon committed
44
        
German Leon's avatar
German Leon committed
45
46
47
48
        # 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
49
50
        #global_logging.info ("Enviado senal a "+ str(pid))
        #os.system ("kill -s USR1 " + str(pid))    
German Leon's avatar
German Leon committed
51
52
53
54

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

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


def main():
German Leon's avatar
German Leon committed
62
    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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

    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)

    # Get variables values from environment
    # Firsn parse line
leon@uji.es's avatar
leon@uji.es committed
80
    [kernel,pid,max_regs,bits_to_flip, fault_model, flip_log_file,
German Leon's avatar
German Leon committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
     gdb_init_strings, injection_site] = arg0.split('|')

    # 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
97
    bit_flip = BitFlip(bits_to_flip=bits_to_flip, fault_model=fault_model,max_regs=max_regs,
German Leon's avatar
German Leon committed
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
                       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()