don't click here

Introducing My Annotated Trace Generator

Discussion in 'Engineering & Reverse Engineering' started by Cokie, Feb 28, 2022.

  1. Cokie

    Cokie

    C. Okie Member
    57
    7
    8
    My script ( hopefully ) works on any disassembly that uses ASW. I could find annotated disassembler feature in an Emulator that works with ASW (AS.exe) . So I wrote a Lua script that takes two inputs. A trace file you made with gens-re-record and the games list file generated by ASW. It outputs to a new trace file, each line from the trace file with the correlating lines of code from this list file with labels symbols below the trace line . Followed by a empty new line. And continues doing this for every line in the trace file. Here is an example output from Sonic 3 and Knuckles:

    Please let me know if a annotated disassembler that works with asw list files exists or if a similar parsing script / program, like this one exists .

    057FCECC-5B06-4863-8B2E-8FB6EE49C838.jpeg

    The script works but was an over night . Will upload it soon!
     
  2. Cokie

    Cokie

    C. Okie Member
    57
    7
    8
    Here is the code in its early stage . You put trace in the scripts directory and name it trace.log. Put list file also in same directory and rename it list.lst

    Code (Text):
    1.  
    2. TraceFileHandle= assert(io.open("trace.log", "r"))
    3. local TraceLines = TraceFileHandle:lines();
    4. local Output =  assert(io.open("Output.txt", "w"))
    5.  
    6. local CurrentTraceLineAddress = nil
    7. local CurrentListLineAddress = nil
    8.  
    9.  
    10.  
    11. for CurrentTraceLine in TraceLines do
    12.  
    13.  
    14.     -- if the Gens trace files line started with nn:nnnn - the instructions address
    15.     if string.find(CurrentTraceLine,"^%x%x:%x%x%x%x") ~= nil then
    16.  
    17.         -- get the address the instruction, formatting it to asw list files format for the instructions address
    18.         -- removing the : and any possible 0 in front of address
    19.         CurrentTraceLineAddress  = string.sub(CurrentTraceLine,string.find(CurrentTraceLine,"^%x%x:%x%x%x%x"))
    20.    
    21.         -- remove the :
    22.         CurrentTraceLineAddress = string.sub(CurrentTraceLineAddress,1,2) .. string.sub(CurrentTraceLineAddress,4,7)
    23.  
    24.  
    25.         -- do we need to format it, removing "0" in front of address
    26.         if CurrentTraceLineAddress ~= "000000" and string.find(CurrentTraceLineAddress,"^0+") ~= nil then
    27.         local  __ , zeroTrailEnd = string.find(CurrentTraceLineAddress,"^0+")
    28.             CurrentTraceLineAddress = string.sub(CurrentTraceLineAddress,zeroTrailEnd+1,string.len(CurrentTraceLineAddress))
    29.         end
    30.  
    31.  
    32.         -- if address of the current line of the trace acde file is RAM, add 10 "F"
    33.         -- to front to make the RAM address follow the asw list format of the address
    34.         if tonumber(CurrentTraceLineAddress,16) >= 0xFF0000 then
    35.             CurrentTraceLineAddress = "FFFFFFFFFF" .. CurrentTraceLineAddress
    36.         end
    37.  
    38.    
    39.  
    40.         -- Append THIS TRACE FILE LINE TO OUTPUT FILE
    41.         Output:write("\n" .. CurrentTraceLine .."\n")
    42.    
    43.  
    44.         local ListFileHandle = assert(io.open("list.lst", "r"));
    45.         local ListLines = ListFileHandle:lines();
    46.  
    47.         for CurrentListLine in ListLines do
    48.  
    49.             -- if the line in the list file has an adress for instruction then
    50.             -- Parse and get the Address of the instruction in the listing file
    51.             -- and check if it is the same address as the line in the trace file
    52.             if string.find(CurrentListLine,"/%s*%x+%s:") then
    53.            
    54.            
    55.                 --    40952/   1EE94 : D640                    add.w    d0,d3
    56.                 CurrentListLineAddress = string.sub(CurrentListLine,string.find(CurrentListLine,"/%s*%x+%s:"))
    57.    
    58.          
    59.                 local CurrentListLineAddressStart = string.find(CurrentListLineAddress,"%x")
    60.                 local CurrentListLineAddressEnd = string.len(CurrentListLineAddress) - 2
    61.  
    62.                 CurrentListLineAddress = string.sub(CurrentListLineAddress,CurrentListLineAddressStart,CurrentListLineAddressEnd)
    63.            
    64.    
    65.                 -- if the trace and list file are the same address for the instruction
    66.                -- print(CurrentListLineAddress)
    67.                -- print(string.len(CurrentListLineAddress))
    68.                -- print(string.len(CurrentTraceLineAddress))
    69.    
    70.                 if CurrentListLineAddress  == CurrentTraceLineAddress then
    71.                
    72.              
    73.                
    74.                     -- Then append this line in TRACE FILE TO OUTPUT FILE
    75.                     Output:write(CurrentListLine.."\n")
    76.                 end
    77.             end
    78.  
    79.  
    80.         end
    81.  
    82.        
    83.         ListFileHandle:close()
    84.     end
    85.  
    86.  
    87.  
    88.        
    89.     LINENUMBA = LINENUMBA + 1
    90.  
    91. end
    92.  
    93. TraceFileHandle:close()
    94. Output:close()
    95.  
    Would very much appreciate all speed optimization advice to make run even faster.
     
    Last edited: Mar 1, 2022
  3. Cokie

    Cokie

    C. Okie Member
    57
    7
    8
    My code currently doesn’t work 100 percent of the time because the code section of the list file has variation to the format. For instance some lines depth for include (n) at the beginning of the line. Is it easily possible with all the variations of the asw compiler to correctly finds all the code section of the list code correlating.

    Also is there annotated disassembler for sonic or any asw assembled rom using list file? Or any other method of seeing labels and such whether runtime in emulator or in trace ?

    Also, I have optimized the code to run many many times faster . My test ran in couple seconds for my revision vs minutes of above code. Will post it soon.
     
    Last edited: Mar 10, 2022