def i_DSN(): global PC if not DisplayCPU.ison(): PC = (PC + 1) & WORDMASK Trace.itrace('DSN') return 1
def i_KSF(): global PC if Kbd.ready(): PC = (PC + 1) & WORDMASK Trace.itrace('KSF') return 1
def i_RSN(): global PC if not TtyIn.ready(): PC = (PC + 1) & WORDMASK Trace.itrace('RSN') return 1
def i_TSN(): global PC if not TtyOut.ready(): PC = (PC + 1) & WORDMASK Trace.itrace('TSN') return 1
def i_TSF(): global PC if TtyOut.ready(): PC = (PC + 1) & WORDMASK Trace.itrace('TSF') return 1
def i_SSF(): global PC if Display.ready(): # skip if 40Hz sync on PC = (PC + 1) & WORDMASK Trace.itrace('SSF') return 1
def i_SSN(): global PC if not Display.ready(): PC = (PC + 1) & WORDMASK Trace.itrace('SSN') return 1
def i_HSF(): global PC if Ptr.ready(): PC = (PC + 1) & WORDMASK Trace.itrace('HSF') return 1
def i_RSF(): global PC if TtyIn.ready(): PC = (PC + 1) & WORDMASK Trace.itrace('RSF') return 1
def doDEIMByte(byte): global DPC, DX, DY, DRSindex if byte & 0x80: # increment? prevDX = DX prevDY = DY if byte & 0x20: DX -= (byte & 0x18) >> 3 else: DX += (byte & 0x18) >> 3 if byte & 0x04: DY -= (byte & 0x03) else: DY += (byte & 0x03) # if byte & 0x40: # display.draw(0, prevDX, prevDY, DX, DY) else: # micro instructions if byte & 0x40: mode = MODE_NORMAL if byte & 0x20: # DRJM if DRSindex <= 0: Trace.comment('\nDRS stack underflow at display address %6.6o' % (DPC - 1)) illegal() DRSindex -= 1 DPC = DRS[DRSindex] if byte & 0x10: DX += 0x08 if byte & 0x08: DX &= 0xfff8 if byte & 0x02: DY += 0x10 if byte & 0x01: DY &= 0xfff0
def i_SAR1(indirect, address, instruction): global AC high_bit = AC & HIGHBITMASK AC = (AC >> 1) | high_bit Trace.itrace('SAR', False, 1) return 1
def i_LSN(): global PC if L != 0: PC = (PC + 1) & WORDMASK Trace.itrace('LSN') return 1
def i_ASN(): global PC if AC != 0: PC = (PC + 1) & WORDMASK Trace.itrace('ASN') return 1
def i_LSZ(): global PC if L == 0: PC = (PC + 1) & WORDMASK Trace.itrace('LSZ') return 1
def i_RRC(indirect, address, instruction): global AC AC |= TtyIn.read() TtyIn.clear() Trace.itrace('RRC') return 1
def i_PSF(indirect, address, instruction): global PC if Ptp.ready(): PC = (PC + 1) & WORDMASK Trace.itrace('PSF') return 1
def i_HSN(): global PC if not Ptr.ready(): PC = (PC + 1) & WORDMASK Trace.itrace('HSN') return 1
def i_KRC(indirect, address, instruction): global AC AC |= Kbd.read() Kbd.clear() Trace.itrace('KRC') return 1
def i_KSN(): global PC if not Kbd.ready(): PC = (PC + 1) & WORDMASK Trace.itrace('KSN') return 1
def i_ASM(): global PC if (AC & HIGHBITMASK): PC = (PC + 1) & WORDMASK Trace.itrace('ASM') return 1
def i_ASZ(): global PC if AC == 0: PC = (PC + 1) & WORDMASK Trace.itrace('ASZ') return 1
def page00(instruction): if instruction == 000000: # DHLT i_DHLT() elif instruction == 004000: # DNOP Trace.dtrace('DNOP') elif instruction == 004004: # DSTS 0 i_DSTS(0) elif instruction == 004005: # DSTS 1 i_DSTS(1) elif instruction == 004006: # DSTS 2 i_DSTS(2) elif instruction == 004007: # DSTS 3 i_DSTS(3) elif instruction == 004010: # DSTB 0 i_DSTB(0) elif instruction == 004011: # DSTB 1 i_DSTB(1) elif instruction == 004040: # DRJM i_DRJM() elif instruction == 004100: # DDYM i_DDYM() elif instruction == 004200: # DDXM i_DDXM() elif instruction == 004400: # DIYM i_DIYM() elif instruction == 005000: # DIXM i_DIXM() elif instruction == 006000: # DHVC i_DHVC() else: illegal(instruction) return 1
def i_SAL3(indirect, address, instruction): global AC high_bit = AC & HIGHBITMASK value = AC & 007777 AC = (value << 3) | high_bit Trace.itrace('SAL', False, 3) return 1
def illegal(instruction=None): if instruction: Trace.comment('Illegal display instruction (%6.6o) at address %6.6o' % (instruction, (DPC - 1))) else: Trace.comment('Illegal display instruction at address %6.6o' % (DPC - 1)) sys.exit(0)
def i_JMP(indirect, address, instruction): global PC jmpaddr = EFFADDR(address) if indirect: jmpaddr = Memory.get(jmpaddr, False) PC = jmpaddr & PCMASK Trace.itrace('JMP', indirect, address) return 3 if indirect else 2
def i_ISZ(indirect, address, instruction): global PC value = (Memory.get(address, indirect) + 1) & WORDMASK Memory.put(value, address, indirect) if value == 0: PC = (PC + 1) & WORDMASK Trace.itrace('ISZ', indirect, address) return 3 if indirect else 2
def init(run_address, tracefile, tstart, tend, boot_rom=None, corefile=None): global tracestart, traceend Memory.init(boot_rom, corefile) Trace.init(tracefile) tracestart = tstart traceend = tend DisplayCPU.init() MainCPU.init()
def i_RAL1(indirect, address, instruction): global AC, L newl = AC >> 15 newac = (AC << 1) | L L = newl AC = newac & WORDMASK Trace.itrace('RAL', False, 1) return 1
def i_SAM(indirect, address, instruction): global PC samaddr = EFFADDR(address) if indirect: samaddr = Memory.get(samaddr, False) if AC == Memory.get(samaddr, False): PC = (PC + 1) & PCMASK Trace.itrace('SAM', indirect, address) return 3 if indirect else 2
def i_RAR1(indirect, address, instruction): global AC, L newl = AC & 1 newac = (AC >> 1) | (L << 15) L = newl AC = newac & WORDMASK Trace.itrace('RAR', False, 1) return 1
def testLoading(self): for traceFile in [testTrace1, testTrace2]: trace = Trace.Trace() reader = BinaryCodec.Reader(trace, open(traceFile, "rb")) reader.load() for event in trace.events: for key, value in event.values.items(): assert not isinstance(value, Trace.UnknownPhrase)
def _handleEventToken(self, token): currentEvent = self.decoder.currentEvent self.decoder.currentEvent = Trace.Event() self.decoder.currentEvent.name = self.stringCache.get( cstr(self.decoder.getPhrase(token.nameId))) self.decoder.currentEvent.seq = token.seq self.decoder.currentEvent.time = token.time self.decoder.currentEvent.apiId = self.decoder.apiId return currentEvent
def readEvent(self,xml): label = self.readTerm(xml.find('label')) compromisetype = self.readText(xml.find('compromisetype')) follows = xml.findall('follows') bindinglist = [] for follow in follows: follaf = follow.find('after') if follaf == None: # Ignore follow definitions that do not contain after continue follev = (int(follaf.get('run')),int(follaf.get('index'))) # Parse and store the binding label blabel = self.readTerm(follow) if blabel != None: binding = (follev,blabel) bindinglist.append(binding) (etype,index) = (xml.get('type'),int(xml.get('index'))) if etype in ('send','read','recv'): fr = self.readTerm(xml.find('from')) to = self.readTerm(xml.find('to')) message = self.readTerm(xml.find('message')) if (etype == 'send'): return Trace.EventSend(index,label,fr,to,message,compromisetype=compromisetype,bindinglist=bindinglist) else: return Trace.EventRead(index,label,fr,to,message,compromisetype=compromisetype,bindinglist=bindinglist) elif xml.get('type') == 'claim': role = self.readTerm(xml.find('role')) etype = self.readTerm(xml.find('type')) argument = self.readTerm(xml.find('argument')) # Freshness claims are implemented as Empty claims with # (Fresh,Value) as arguments try: if etype == 'Empty' and argument[0] == 'Fresh': etype = Term.TermConstant('Fresh') argument = argument[1] elif etype == 'Empty' and argument[0] == 'Compromised': etype = Term.TermConstant('Compromised') argument = argument[1] except: pass return Trace.EventClaim(index,label,role,etype,argument,compromisetype=compromisetype,bindinglist=bindinglist) else: raise Trace.InvalidAction, "Invalid action in XML: %s" % (xml.get('type'))
def save_received_trace(self, trace, result, trace_filepath): try: self.logger.info('writing trace file %s' % trace_filepath) trace_file = open(trace_filepath, 'w') preamble = {} preamble['id'] = result['trace_id'] preamble['dport'] = result['server_address']['port'] packets = [] for timestamp, payload, footer in trace: packets.append({'timestamp': timestamp, 'length': len(payload), 'payload': payload, 'footer': footer}) Trace.write_v2_trace((preamble, packets), trace_file) except (IOError, ValueError) as e: self.logger.error('failed to write trace file %s (%s)' % (trace_filepath, e)) finally: if trace_file: trace_file.close()
def newTrace(self, traceName=None): """ Create a new empty trace file. @param traceName: Resulting trace name """ trace = Trace.Trace() traceName = traceName or "t%d" % len(self.analyzer.traces) self.analyzer.traces[traceName] = trace self.reportInfo("Created new trace as %s." % (traceName)) return trace
def readRoleDescr(self, xml): assert (xml.tag == 'role') run = Trace.Run() # We will need the last label later on to see if a # run is complete run.lastLabel = None run.role = xml.find('rolename').text for eventxml in xml.find('eventlist'): action = self.readEvent(eventxml) action.run = run run.eventList.append(action) run.lastLabel = action.label return run
def initialize_game_variables(self, game_mode): # Initialize the game board and the GameLogic object # Who is yellow? Who red? colour_p1 = 2 colour_p2 = 1 if game_mode == "TeachAgents" or game_mode=="Singleplayer": # Player 1 self.p1 = hp.HumanPlayer(colour_p1) self.p2 = ap.AgentPlayer(colour_p2, "qlearner", self.connect_mode, game_mode) self.p1.set_opponent(self.p2) self.trace = Trace.Trace(self.p2, self.p1) self.p1.set_trace(self.trace) self.p2.set_trace(self.trace) elif game_mode == "Multiplayer": self.p1 = hp.HumanPlayer(colour_p1) self.p2 = hp.HumanPlayer(colour_p2) elif game_mode == "Options": pass elif game_mode == "AgentsLearn": self.p1 = ap.AgentPlayer(colour_p1, "qlearner", self.connect_mode, "AgentsLearn") self.p2 = ap.AgentPlayer(colour_p2, "qlearner", self.connect_mode, "AgentsLearn") self.trace = Trace.Trace(self.p2, self.p1) self.p1.set_trace(self.trace) self.p2.set_trace(self.trace) self.p1.set_opponent(self.p2) self.p2.set_opponent(self.p1) self.game_board = Board.Board(cfg.BOARD_SIZE[0], cfg.BOARD_SIZE[1], game_mode) (self.board_rows, self.board_cols) = self.game_board.get_dimensions() self.game_logic = gl.GameLogic(self.game_board, self.connect_mode)
def loadTrace(self, traceFile): # Determine the trace file size try: traceFile.seek(0, 2) fileSize = traceFile.tell() traceFile.seek(0) except: fileSize = 0 task = Task.startTask("load", "Decoding STI data", steps=fileSize) #s = StringIO.StringIO() r = re.compile(r"\[TRACY:(.*?)\] ([0-9a-f]+)") l = 1 traces = {} #blockSize = None for line in traceFile: if "[TRACY:" in line: for match in r.finditer(line): #match = r.search(line) if not match: raise RuntimeError("Badly formatted line %d: %s" % l, line) proc = match.group(1) if not proc in traces: Log.notice("Found trace %s" % proc) traces[proc] = StringIO.StringIO() data = match.group(2) #if not blockSize: # blockSize = len(data) #elif len(data) != blockSize: # print "Line %d, new block size: %d, %s, %s" % (l, blockSize, line, data) # blockSize = len(data) traces[proc].write(binascii.unhexlify(data)) if fileSize: task.updateProgress(traceFile.tell()) elif (l & 0xff) == 0: task.step() l += 1 t = [] for proc, s in traces.items(): Log.notice("Decoding trace %s" % proc) s.seek(0) trace = Trace.Trace() reader = BinaryCodec.Reader(trace, s) try: reader.load() except Exception, e: Log.error("Trace %s decoding failed: %s" % (proc, e)) t.append(trace)
def _handleBeginObjectToken(self, token): className = self.stringCache.get( cstr(self.decoder.getPhrase(token.classNameId))) # Create the class if it's new if not className in self.decoder.classes: self.decoder.classes[className] = Class(className) cls = self.decoder.classes[className] obj = Trace.Object(token.handle, DEFAULT_NAMESPACE, cls) if not self.decoder.currentObject: self.decoder.currentObject = obj else: raise ValueError("Nested object specification.")
def load_trace(self, trace_name, trace_id): filepath = os.path.join(Config.TRACES_DIR, trace_name) self.logger.debug('reading trace file %s' % filepath) try: with open(filepath) as f: trace = Trace.read_rtp_trace(f) if trace[0]['id'] != trace_id: raise ValueError( 'trace id mismatch (%s instead of %s) for trace %s' % (trace_id, trace[0]['id'], trace_name)) return trace except (IOError, ValueError) as e: self.logger.error('failed to read trace file %s (%s)' % (filepath, e)) raise
def get_relation_probability(self, eye_obj, other_obj, relation_classfier_obj): #pdb.set_trace() sym_obj = Symbol() trace_list = [] total_points = [] #First get all the points together for i in xrange(len(eye_obj.symbol_list)): #trace_list.append(eye_obj.symbol_list[i]) total_points += eye_obj.symbol_list[i].original_points for i in xrange(len(other_obj.symbol_list)): total_points += other_obj.symbol_list[i].original_points #trace_list.append(other_obj.symbol_list[i]) trace_obj = Trace(points_float=total_points) trace_obj.normalization() trace_list.append(trace_obj) sym_obj.symbol_list = trace_list X = sym_obj.get_features() X = np.asarray(X) prob = np.max(relation_classfier_obj.predict_proba(X.reshape(1, -1))) #(1-prob) this is to get the minimum spanning tree. trace_list.remove(trace_obj) return (1 - prob)
def seek(self, index): BASE_TO_NM_CONVERSION_FACTOR=0.34#nm/bp self.context['path' ] =self.context['files'][index] self.context['index'] =index self.context['basename']=os.path.basename(self.context['path']) ##reads _trace file into scipy.array self.context['coordinates'] = scipy.genfromtxt( self.context['path' ], delimiter='\t') self.context['trace'] = Trace.Trace(self.context['coordinates']) self.context['scale'] = self.context['trace'].scale(self.settings['length']) self.context['resolution'] = self.context['trace']._ld.mean() * self.context['scale'] self.context['steadiness'] = scipy.sqrt(self.context['trace']._ld.var()) * self.context['scale'] self.context['pimgres'] = self.context['scale'] * BASE_TO_NM_CONVERSION_FACTOR self.context['domain'] = scipy.array(range(len(self.context['trace'])))
def __init__(self): self.broken = [] self.match = None self.initialKnowledge = [] self.inverseKeys = [] self.protocol = None self.semiTrace = Trace.SemiTrace() self.variables = [] self.protocoldescr = {} self.id = None self.knowledge = None self.untrusted = [] self.typeflaws = False self.commandline = '' self.scytherDot = None self.claim = None # refers to parent claim
def loadTrace(self, fileName, traceName=None, format=None): """ Open a trace file. @param fileName: Trace file to open @param traceName: Resulting trace name @param format: Force a specific format to be used instead of autodetection. """ trace = Trace.Trace() for importer in self.analyzer.importPlugins: if format is not None and importer.formatName == format: break if importer.recognizeTraceFile(fileName): break else: if format is not None: self.analyzer.fail( "No such format. Available formats: %s." % (", ".join( [i.formatName for i in self.analyzer.importPlugins]))) self.analyzer.fail("Trace file format not recognized.") try: f = open(fileName, "rb") traces = importer.loadTrace(f) f.close() # Some decoders can load multiple trace files from one source if not isinstance(traces, list): traces = [traces] for trace in traces: traceName = traceName or "t%d" % len(self.analyzer.traces) self.analyzer.traces[traceName] = trace self.analyzer.traceFiles[traceName] = fileName if not TraceOperations.verify(self, self.analyzer.project, trace): self.reportWarning( "The loaded project probably does not match the trace file." ) self.reportInfo("Loaded trace from '%s' as %s." % (fileName, traceName)) traceName = None return traces[0] except IOError, e: self.analyzer.fail(e)
def testing(self): code_fd = astor.to_source(self.ast) code_fc = self.funcCall(self.args) code = code_fd + code_fc #print(code) #print(code_fc) program = compile(code, '<string>', 'exec') global searcher searcher = Trace(self.args, self.k, self.id, self.dependency_chain, self.b) try: exec(program, globals()) except Exception as ex: self.result = searcher return self.result self.result = searcher return self.result
print "Header size:\t\t%d bytes" % ts._traceBlockOffset if __name__ == "__main__": parser = argparse.ArgumentParser( description='Convert Inspector 4 traceset into numpy array') parser.add_argument('-c', '--convertdata', action='store_true', help='convert data from byte array to uint64 chunks') parser.add_argument('filename', help='traceset file name without trs extension') args = parser.parse_args() ts = trs.TraceSet() ts.open(args.filename + ".trs") samplesDataType = determineTrsSampleCoding(ts) printTrsMetadata(ts, samplesDataType) # read out the traces print "Preallocating arrays" traces = np.empty(shape=(ts._numberOfTraces, ts._numberOfSamplesPerTrace), dtype=samplesDataType) data = np.empty(shape=(ts._numberOfTraces, ts._dataSpace), dtype="uint8") print "Populating arrays" for i in range(ts._numberOfTraces): t = ts.getTrace(i) traces[i, :] = np.array(t._samples, dtype=samplesDataType) data[i, :] = np.array(t._data, dtype="uint8")
def loadTrace(self, traceFile): trace = Trace.Trace() reader = BinaryCodec.Reader(trace, open(traceFile, "rb")) reader.load() return trace
def readAttack(self, xml): self.varlist = [] attack = Attack.Attack() attack.id = int(xml.get('id')) # A state contains 4 direct child nodes: # broken, system, variables and semitrace # optionally a fifth: dot for event in xml.getchildren(): if event.tag == 'broken': attack.broken.append((self.readTerm(event.find('claim')), self.readTerm(event.find('label')))) elif event.tag == 'system': attack.match = int(event.find('match').text) for term in event.find('commandline'): if term.text != None: if attack.commandline != '': attack.commandline += ' ' attack.commandline += term.text for term in event.find('untrusted').find('termlist'): attack.untrusted.append(str(self.readTerm(term))) for term in event.find('initialknowledge').find('termlist'): attack.initialKnowledge.append(self.readTerm(term)) for keypair in event.find('inversekeys'): inverse = [] for term in keypair: inverse.append(self.readTerm(term)) assert (len(inverse) == 0 or len(inverse) == 2) attack.inverseKeys.append(inverse) # TODO why is protocol name a term?? for protocolxml in event.findall('protocol'): protocol = str(self.readTerm(protocolxml.find('name'))) descr = Trace.ProtocolDescription(protocol) attack.protocoldescr[protocol] = descr for rolexml in protocolxml.findall('role'): roledescr = self.readRoleDescr(rolexml) descr.roledescr[roledescr.role] = roledescr elif event.tag == 'semitrace': for runxml in event: run = self.readRun(runxml) run.attack = attack attack.semiTrace.runs.append(run) elif event.tag == 'dot': # Apparently Scyther already generated dot output, # store attack.scytherDot = event.text elif event.tag == 'variables': # Read the variables one by one for varxml in event: if varxml.get('typeflaw') == 'true': attack.typeflaws = True var = self.readTerm(varxml.find('name').find('term')) var.types = self.readTypeList(varxml.find('name')) substxml = varxml.find('substitution') # Read substitution if present if substxml != None: subst = self.readTerm(substxml.find('term')) subst.types = self.readTypeList(substxml) newvar = Term.TermVariable(var.name, subst) newvar.types = var.types var = newvar attack.variables.append(var) # When all have been read set self.varlist so that when # we read terms in the attacks they can be filled in using # this list self.varlist = attack.variables else: print >> sys.stderr, "Warning unknown tag in attack: %s" % event.tag return attack
174: rt_sigaction_entry_debug_printer, 175: rt_sigprocmask_entry_debug_printer, 192: mmap2_entry_debug_printer, 195: stat64_entry_debug_printer, 196: lstat64_entry_debug_printer, 197: fstat64_entry_debug_printer, 221: fcntl64_entry_debug_printer, } # Open our trace (specified as either a command line argument with -t # or as specified in a replay config file. Then pass it to the # posix-omni-parser so that it can turned into a set of objects. if mutator: logging.debug('Mutating trace') trace = mutator.mutate_trace(trace) logging.debug('Mutated trace at: %s', trace) t = Trace.Trace(trace) tracereplay.system_calls = t.syscalls logging.info('Parsed trace with %s syscalls', len(t.syscalls)) logging.info('Entering syscall handling loop') # Loop until we are no longer receiving syscall notifications from our # ptrace session with the child process. while next_syscall(): # Get the system call id of the current system call. Convention in # our flavor of Linux is for this to be passed in the EAX # register (ORIG_EAX, in ptrace terms). Ptrace does not inform us # of whether the current system call action we have been notified # of is an entry or exit so we operate on the assumption that the # first notification we receive is an entry, the next is an exit, # the next is an entry etc. orig_eax = cint.peek_register(pid, cint.ORIG_EAX)
def Start(): global root root = Tk() root.attributes("-toolwindow", True) root.title("List Editor") root.geometry("640x480") Tracer.Trace("New Main Window [root] Created") Button_Frame = Frame(root) Button_Frame.pack() New_Item = Button(Button_Frame, text="Add New Item", bg="Green", fg="white", width=15, height=3, command=Add_New_Item_Front) New_Item_At_Index = Button(Button_Frame, text="New Item at Index", bg="Blue", fg="white", width=15, height=3, command=Add_At_Index_Front) Delete_Item = Button(Button_Frame, text="Delete Item", bg="Red", fg="white", width=15, height=3, command=Remove_From_End) Delete_Index_Item = Button(Button_Frame, text="Delete By Index", bg="blue", fg="white", width=15, height=3, command=Remove_By_Index) Clear = Button(Button_Frame, text="Clear All List", bg="Yellow", fg="Black", width=15, height=3, command=Clear_List) EndTrace = Trace._EndTrace() EndTrace._EndTrace(Button_Frame, 2, 3, 15, 3, "red", "white") New_Item.grid(row=1, column=1, padx=2, pady=3) New_Item_At_Index.grid(row=1, column=2, padx=2, pady=3) Delete_Item.grid(row=2, column=1, padx=2, pady=3) Delete_Index_Item.grid(row=2, column=2, padx=2, pady=3) Clear.grid(row=1, column=3, padx=2, pady=3) Tracer.Trace("All Buttons and Frames Created and Drawn") List_View() root.mainloop()
from tkinter import * import Trace Object_List = [] Last_Index = [] Tracer = Trace.Trace() def Start(): global root root = Tk() root.attributes("-toolwindow", True) root.title("List Editor") root.geometry("640x480") Tracer.Trace("New Main Window [root] Created") Button_Frame = Frame(root) Button_Frame.pack() New_Item = Button(Button_Frame, text="Add New Item", bg="Green", fg="white", width=15, height=3, command=Add_New_Item_Front) New_Item_At_Index = Button(Button_Frame, text="New Item at Index", bg="Blue", fg="white", width=15,
def calculateStatistics(project, trace): """ Calculate derived OpenVG statistics instrumentation sensor data and trace events. """ if not "code" in project.targets: raise ValueError("No code information in project file") task = Task.startTask("vg-stats", "Calculating OpenVG statistics", len(trace.events)) library = project.targets["code"].library constants = Collections.DictProxy(library.constants) # Create the derived sensors trace.sensors["average_path_size"] = Trace.InstrumentationSensor( "Average path size", isAverage=True) trace.sensors["image_uploads"] = Trace.InstrumentationSensor( "Image uploads") trace.sensors["render_calls"] = Trace.InstrumentationSensor( "Rendering calls") trace.sensors["draw_ratio"] = Trace.InstrumentationSensor("Draw ratio") trace.sensors["path_segments"] = Trace.InstrumentationSensor( "Path segments") trace.sensors["gradient_stops"] = Trace.InstrumentationSensor( "Number of gradient stops defined") prevRenderEvent = None for event in trace.events: task.step() func = library.functions[event.name] if func.isRenderCall: event.sensorData["render_calls"] = 1 if event.name in ["vgSetParameterfv", "vgSetParameteriv"]: if event.values[ "paramType"] == constants.VG_PAINT_COLOR_RAMP_STOPS: event.sensorData["gradient_stops"] = event.values["count"] / 5 fragments = event.sensorData.get("rasterized_pixels", 0) renderCalls = event.sensorData.get("render_calls", 0) if fragments and renderCalls: event.sensorData["average_path_size"] = fragments / float( renderCalls) if event.name == "vgImageSubData" and "width" in event.values and "height" in event.values: event.sensorData["image_uploads"] = int(event.values["width"] * event.values["height"]) if event.name == "vgAppendPathData": event.sensorData["path_segments"] = int( event.values["numSegments"]) width = event.sensorData.get("render_surface_width", 0) height = event.sensorData.get("render_surface_height", 0) if fragments and width and height: event.sensorData["draw_ratio"] = fragments / float(width * height)
#Number of samples to keep samplesToKeep = 4500 #1 AES round seems to be around 187us; 1 sample @ 24MHz is 0.0417us; 1 round is roughly 4500 samples #Ignore triggers within first ignoreTriggerSamples samples to #avoid capturing already started operations (e.g. first 1024 samples) ignoreTriggerSamples = 1024 triggerTooLate = capturedTraceLength - samplesToKeep #Length of input & output messages in bytes (this depends on crypto algorithm used!) #AES block length is 128 bit == 16 bytes for input and 16 bytes for output inputMessageLength = 16 outputMessageLength = 16 #Traceset init ts = trs.TraceSet() ts.new('output.trs', 0, trs.TraceSet.CodingFloat, inputMessageLength + outputMessageLength, samplesToKeep) #File with inputs&outputs inoutFile = open('inputs_outputs.bin', 'r') #########END OF INITIALIZATION VARIABLES############ print "Chomp chomp: trimming " + str(samplesToKeep) + " samples from " + str( numberOfTraces) + " traces" #Loop through all the traces, find a trigger, dump the traces into the traceset traceCount = 0 for i in xrange(numberOfTraces):
# ============================================================================== from __future__ import print_function version = '2.0.1' import sys import os import glob from optparse import OptionParser # ---------------------------------------------------------------------- # このスクリプトは ".../core/src/RunSwig" に置く # ScriptFileDir = os.sep.join(os.path.abspath(__file__).split(os.sep)[:-1]) prog = sys.argv[0].replace('/', os.sep).split(os.sep)[-1].split('.')[0] from Trace import * trace = Trace().flag(prog) if trace: print('ENTER: %s: %s' % (prog, sys.argv[1:])) sys.stdout.flush() SrcDir = '/'.join(ScriptFileDir.split(os.sep)[:-1]) SetupExists = os.path.exists('%s/setup.conf' % SrcDir) # ---------------------------------------------------------------------- # Springhead python library の導入 # libdir = '%s/RunSwig/pythonlib' % SrcDir sys.path.append(libdir) from SetupFile import * from TextFio import * from FileOp import *
def simplify(project, trace): """ Simplify a trace by removing redundant GLES commands from it. @param project: Tracy GLES project @param trace: Trace to examine @returns a simplified trace """ if not "code" in project.targets: raise ValueError("No code information in project file") state = TraceState.TraceState(project) newTrace = Trace.Trace() newTrace.sensors = trace.sensors task = Task.startTask("simplify", "Simplifying trace", len(trace.events)) state.beginCollectingEffectiveEvents() for event in trace.events: task.step() """ implicitStateValue = None if event.name == "glPushMatrix": try: depth = state.getValue(["ctx", "matrix_mode", "unit", "stack"]) if depth is None: depth = 0 except KeyError: depth = 0 implicitStateValue = depth + 1 elif event.name == "glPopMatrix": try: depth = state.getValue(["ctx", "matrix_mode", "unit", "stack"]) if depth is None: depth = 0 except KeyError: depth = 0 implicitStateValue = depth - 1 elif event.name == "glActiveTexture": implicitStateValue = event.values["texture"] """ #print ">>>", event.name #print state state.processEvent(event) # If the event doesn't access the state explicitly, it is considered to be a draw call if not state.isStateAccessingEvent(event): state.endCollectingEffectiveEvents() effectiveEvents = state.getEffectiveEvents() i = 0 while i < len(effectiveEvents): # Remove adjacent glPushMatrix()/glPopMatrix() pairs, since they are # an artifact of the state tracker if i < len(effectiveEvents) - 1: if effectiveEvents[i].name == "glPushMatrix" and effectiveEvents[i + 1].name == "glPopMatrix": i += 2 continue newTrace.events.append(effectiveEvents[i]) i += 1 newTrace.events.append(event) state.beginCollectingEffectiveEvents() continue #print state return newTrace
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. import sys import Trace import BinaryCodec try: trace = Trace.Trace() reader = BinaryCodec.Reader(trace, open(sys.argv[1], "rb")) writer = BinaryCodec.Writer(trace, open(sys.argv[2], "wb")) except IndexError: print "Usage: NormalizeTrace.py in.bin out.bin" sys.exit(1) reader.load() writer.save() print "%d events normalized." % len(trace.events)
def calculateStatistics(project, trace): """ Calculate derived OpenGL ES statistics instrumentation sensor data and trace events. """ if not "code" in project.targets: raise ValueError("No code information in project file") task = Task.startTask("gles-stats", "Calculating OpenGL ES statistics", len(trace.events)) library = project.targets["code"].library constants = Collections.DictProxy(library.constants) # Create the derived sensors trace.sensors["average_triangle_size"] = Trace.InstrumentationSensor("Average triangle size", isAverage = True) trace.sensors["texel_fetches_per_pixel"] = Trace.InstrumentationSensor("Texel fetches per pixel", isAverage = True) trace.sensors["texel_uploads"] = Trace.InstrumentationSensor("Texel uploads") trace.sensors["vertices_in"] = Trace.InstrumentationSensor("Vertices in") trace.sensors["triangles_in"] = Trace.InstrumentationSensor("Triangles in") trace.sensors["render_calls"] = Trace.InstrumentationSensor("Rendering calls") trace.sensors["rasterizer_discarded_pixels"] = Trace.InstrumentationSensor("Rasterizer discarded pixels") trace.sensors["draw_ratio"] = Trace.InstrumentationSensor("Draw ratio") prevRenderEvent = None depthMask = 1 for event in trace.events: task.step() func = library.functions[event.name] if func.isRenderCall: event.sensorData["render_calls"] = 1 if func.isRenderCall and "count" in event.values and "mode" in event.values: m = event.values["mode"] if m == constants.GL_TRIANGLES: event.sensorData["triangles_in"] = int(event.values["count"] / 3) elif m == constants.GL_TRIANGLE_STRIP: event.sensorData["triangles_in"] = int(event.values["count"] - 2) elif m == constants.GL_TRIANGLE_FAN: event.sensorData["triangles_in"] = int(event.values["count"] - 2) elif m == constants.GL_POINTS: event.sensorData["triangles_in"] = int(event.values["count"] * 2) elif m == constants.GL_LINES: event.sensorData["triangles_in"] = int(event.values["count"]) elif m == constants.GL_LINE_STRIP: event.sensorData["triangles_in"] = int(event.values["count"] * 2) elif m == constants.GL_LINE_LOOP: event.sensorData["triangles_in"] = int(event.values["count"] * 2 + 2) fragments = event.sensorData.get("rasterizer_pixels", 0) triangles = event.sensorData.get("triangles_in", 0) texFetches = event.sensorData.get("rasterizer_texel_fetches", 0) if triangles and fragments: event.sensorData["average_triangle_size"] = fragments / float(triangles) if fragments and texFetches: event.sensorData["texel_fetches_per_pixel"] = fragments / float(texFetches) if event.name in ["glTexImage2D", "glTexSubImage2D"]: event.sensorData["texel_uploads"] = int(event.values["width"] * event.values["height"]) if func.isRenderCall and "count" in event.values: event.sensorData["vertices_in"] = int(event.values["count"]) if event.name == "glDepthMask": depthMask = event.values["flag"] # If we have the depth buffers for this event and the previous draw event, see how many pixels # actually changed and use that value to estimate overdraw if func.isRenderCall and not "rasterizer_discarded_pixels" in event.sensorData: if depthMask and prevRenderEvent and "depth_stride" in event.sensorData and "depth_mask" in event.sensorData: f1 = player.Instrumentation.getBufferFileName(prevRenderEvent, "depth") f2 = player.Instrumentation.getBufferFileName(event, "depth") if f1 and f2: diff = Graphics.compareDepthBuffers(f1, f2, event.sensorData["depth_stride"], event.sensorData["depth_mask"]) event.sensorData["rasterizer_discarded_pixels"] = fragments - diff prevRenderEvent = event discFragments = event.sensorData.get("rasterizer_discarded_pixels", 0) width = event.sensorData.get("render_surface_width", 0) height = event.sensorData.get("render_surface_height", 0) if fragments and width and height: event.sensorData["draw_ratio"] = (fragments - discFragments) / float(width * height)
import h5py import numpy as np import Trace as trs # python interface to trs format ascad = h5py.File("ASCAD_data/ASCAD_databases/ATMega8515_raw_traces.h5", "r") # change this to your location of the ASCAD database traces = ascad['traces'] plaintexts = ascad['metadata']['plaintext'] ciphertexts = ascad['metadata']['ciphertext'] # default sample range used in ASCAD and in Benjamin Timon's paper for key byte 3 recovery min_range = 45400 max_range = 46100 newTs = trs.TraceSet() newTs.new("ASCAD.trs", 0, trs.TraceSet.CodingByte, len(np.hstack((plaintexts[0], ciphertexts[0]))), max_range - min_range) for i in range(len(traces)): newTs.addTrace( trs.Trace(b'', np.hstack((plaintexts[i], ciphertexts[i])), traces[i, min_range:max_range])) newTs.close() exit() # The ASCAD traceset corect key, for reference # key = [77, 251, 224, 242, 114, 33, 254, 16, 167, 141, 74, 220, 142, 73, 4, 105]
def loadInstrumentationData(analyzer, trace, instLog): """ Load precalculated instrumentation data for a trace from an instrumentation log file. @param trace: Trace that should be augmented with the instrumentation data @param instLog: Instrumentation log file name, see instrumentationLogFileName """ logFile = open(instLog) logDir = os.path.dirname(instLog) logFile.seek(0, 2) task = Task.startTask("load-instrumentation", "Loading instrumentation data", logFile.tell()) logFile.seek(0) valueTypeMap = {"int": int, "float": float, "str": str} # Register common sensors trace.sensors["render_surface_width"] = Trace.InstrumentationSensor( "Render surface width", isAverage=True) trace.sensors["render_surface_height"] = Trace.InstrumentationSensor( "Render surface height", isAverage=True) event = None events = dict([(event.seq, event) for event in trace.events]) for line in logFile.xreadlines(): msg, args = line.rstrip().split(" ", 1) if msg == "event": # Finish off the previous event if event: _postprocessEvent(event, logDir) eventName, seq = args.split(" ", 1) seq = int(seq) # Check that the data refers to a valid event if not seq in events: analyzer.fail("Bad event sequence number: %d" % seq) event = events[seq] if event.name != eventName: analyzer.fail("Event names do not match: %s != %s" % (event.name, eventName)) task.updateProgress(logFile.tell()) elif msg == "value": assert event # Convert and store the value try: valueType, valueName, value = args.split(" ", 2) event.sensorData[valueName] = valueTypeMap[valueType](value) except ValueError: analyzer.reportWarning("Badly formatted sensor value: %s" % line) elif msg == "desc": # Record the sensor description valueName, valueDesc = args.split(" ", 1) if not valueName in trace.sensors: isAverage = valueName in [ "render_surface_width", "render_surface_height", "red_mask", "green_mask", "blue_mask", "alpha_mask", "depth_mask", "stencil_mask", "color_stride", "is_linear", "is_premultiplied", "color_data_type", "depth_data_type", "stencil_data_type" ] trace.sensors[valueName] = Trace.InstrumentationSensor( valueDesc, isAverage=isAverage) else: analyzer.reportWarning( "Unknown message received from the player: %s" % line) if event: _postprocessEvent(event, logDir) task.finish() # Call every instrumentation post processor for plugin in analyzer.plugins: try: process = plugin.postProcessInstrumentationData except AttributeError: continue process(trace)
def main(args): os.makedirs(args.out, exist_ok=True) xbox = Xbox() xbox_helper = XboxHelper.XboxHelper(xbox) abort_flag = AbortFlag() def signal_handler(_signal, _frame): if not abort_flag.should_abort: print("Got first SIGINT! Aborting..") abort_flag.abort() else: print("Got second SIGINT! Forcing exit") sys.exit(0) signal.signal(signal.SIGINT, signal_handler) print("\n\nAwaiting stable PB state\n\n") dma_pull_addr, dma_push_addr = _wait_for_stable_push_buffer_state( xbox_helper, abort_flag, args.verbose) if not dma_pull_addr or not dma_push_addr or abort_flag.should_abort: if not abort_flag.should_abort: print("\n\nFailed to reach stable state.\n\n") return print("\n\nStepping through PB\n\n") # Start measuring time begin_time = time.monotonic() if _enable_experimental_disable_z_compression_and_tiling: # TODO: Enable after removing FIXME above. experimental_disable_z_compression_and_tiling(xbox) # Create a new trace object pixel_dumping = not args.no_pixel enable_texture_dumping = pixel_dumping and not args.no_texture enable_surface_dumping = pixel_dumping and not args.no_surface enable_raw_pixel_dumping = not args.no_raw_pixel enable_rdi = pixel_dumping and not args.no_rdi if args.alpha_mode == "both": alpha_mode = Trace.Tracer.ALPHA_MODE_BOTH elif args.alpha_mode == "keep": alpha_mode = Trace.Tracer.ALPHA_MODE_KEEP else: alpha_mode = Trace.Tracer.ALPHA_MODE_DROP trace = Trace.Tracer( dma_pull_addr, dma_push_addr, xbox, xbox_helper, abort_flag, output_dir=args.out, alpha_mode=alpha_mode, enable_texture_dumping=enable_texture_dumping, enable_surface_dumping=enable_surface_dumping, enable_raw_pixel_dumping=enable_raw_pixel_dumping, enable_rdi=enable_rdi, verbose=args.verbose, max_frames=args.max_flip, ) # Dump the initial state trace.command_count = -1 trace.dump_surfaces(xbox, None) trace.command_count = 0 trace.run() # Recover the real address xbox.write_u32(XboxHelper.DMA_PUSH_ADDR, trace.real_dma_push_addr) print("\n\nFinished PB\n\n") # We can continue the cache updates now. xbox_helper.resume_fifo_pusher() # Finish measuring time end_time = time.monotonic() duration = end_time - begin_time command_count = trace.recorded_command_count print( "Recorded %d flip stalls and %d PB commands (%.2f commands / second)" % (trace.recorded_flip_stall_count, command_count, command_count / duration))