Skip to content

Instantly share code, notes, and snippets.

@Dragorn421
Last active July 14, 2021 16:22
Show Gist options
  • Save Dragorn421/d5017fc0f8df4d4caee2fe31c9e86d6f to your computer and use it in GitHub Desktop.
Save Dragorn421/d5017fc0f8df4d4caee2fe31c9e86d6f to your computer and use it in GitHub Desktop.
stackTraceStr = """
8022cf50
8022cca8
8022cb40
80030a40
800bdae8
800bf14c
800c4054
800c5808
800c5ea0
"""
overlaysStr = """
8022c860-8022d500 TestActor
"""
# This is free and unencumbered software released into the public domain.
# For more information, please refer to <http://unlicense.org/>
import os, subprocess
def sym_info(address):
symInfoPyPath = 'sym_info.py'
for i in range(2):
if not os.path.isfile(symInfoPyPath):
symInfoPyPath = '../' + symInfoPyPath
return subprocess.run(['python3', symInfoPyPath, hex(address)], stdout=subprocess.PIPE).stdout.decode().rstrip('\n')
stackTrace = [int(l, 16) for l in stackTraceStr.splitlines() if l]
overlays = []
for l in overlaysStr.splitlines():
if not l:
continue
dashIdx = l.index('-')
spaceIdx = l.index(' ')
if dashIdx < 0:
print(f'malformatted line in overlaysStr: {l} (expected "<startHex>-<endHex> <name>", eg "8022c860-8022d500 TestActor"')
continue
overlays.append((int(l[:dashIdx], 16), int(l[dashIdx+1:spaceIdx], 16), l[spaceIdx+1:]))
for stackTraceAddr in stackTrace:
symInfoRes = sym_info(stackTraceAddr)
print(symInfoRes)
inOverlays = [ovl for ovl in overlays if ovl[0] <= stackTraceAddr and stackTraceAddr < ovl[1]]
if len(inOverlays) > 1:
print('\tWeird, found several overlays where this address belongs:')
print(f'\t{inOverlays}')
if 'gSystemHeap' in symInfoRes:
if not inOverlays:
print('\tThis address does not belong in the listed overlays')
else:
inOvl = inOverlays[0]
mapPath = 'build/z64.map'
for i in range(2):
if not os.path.isfile(mapPath):
mapPath = '../' + mapPath
searchSymbol = f'_ovl_{inOvl[2]}SegmentStart'
with open(mapPath) as mapF:
matches = [l for l in mapF if searchSymbol in l]
if len(matches) != 1:
print(f'Found not exactly 1 match when searching {searchSymbol} in the map:')
print(matches)
if matches:
m = matches[0]
hexAddrStartIdx = m.index('0x')
hexAddrEndIdx = m.index(' ', hexAddrStartIdx)
ovlVaddr = int(m[hexAddrStartIdx+2:hexAddrEndIdx], 16)
symInfoRes2 = sym_info(stackTraceAddr - inOvl[0] + ovlVaddr)
print(f'\t{symInfoRes2}')
elif inOverlays:
print('\tWeird, this address is not in gSystemHeap but belongs in the listed overlays:')
print(f'\t{inOverlays}')
@Dragorn421
Copy link
Author

Python script for use when debugging a decomp-based hack

Runs sym_info.py on all stack trace addresses, mapping them to virtual addresses when needed (for addresses in overlays)

From the crash debugger:
crash debugger stack trace
crash debugger overlays loaded
(the stack trace is also printed to logs)

Update the two variables at the start of the script:

stackTraceStr = """
8022cf50
8022cca8
8022cb40
80030a40
800bdae8
800bf14c
800c4054
800c5808
800c5ea0
"""
overlaysStr = """
8022c860-8022d500 TestActor
"""

Run:

$ python3 ./runner/tools/syminfotrace.py
0x8022cf50 is at 0x1CBF0 bytes inside gSystemHeap (RAM 0x80210360, ROM 0xC698A0, build/src/buffers/heaps.o)
        0x80baa760 is at 0x1D0 bytes inside TestActor_UpdateEnabled (RAM 0x80BAA590, ROM 0xF58AF0, build/runner/actors/test_actor/test_actor.o)
0x8022cca8 is at 0x1C948 bytes inside gSystemHeap (RAM 0x80210360, ROM 0xC698A0, build/src/buffers/heaps.o)
        0x80baa4b8 is at 0x158 bytes inside TestActor_UpdateImpl (RAM 0x80BAA360, ROM 0xF588C0, build/runner/actors/test_actor/test_actor.o)
0x8022cb40 is at 0x1C7E0 bytes inside gSystemHeap (RAM 0x80210360, ROM 0xC698A0, build/src/buffers/heaps.o)
        0x80baa350 is at 0x10 bytes inside TestActor_Update (RAM 0x80BAA340, ROM 0xF588A0, build/runner/actors/test_actor/test_actor.o)
0x80030a40 is at 0x3D0 bytes inside Actor_UpdateAll (RAM 0x80030670, ROM 0xAA77E0, build/src/code/z_actor.o)
0x800bdae8 is at 0x1284 bytes inside Gameplay_Update (RAM 0x800BC864, ROM 0xB339D4, build/src/code/z_play.o)
0x800bf14c is at 0x118 bytes inside Gameplay_Main (RAM 0x800BF034, ROM 0xB361A4, build/src/code/z_play.o)
0x800c4054 is at 0x2C bytes inside GameState_Update (RAM 0x800C4028, ROM 0xB3B198, build/src/code/game.o)
0x800c5808 is at 0xD0 bytes inside Graph_Update (RAM 0x800C5738, ROM 0xB3C8A8, build/src/code/graph.o)
0x800c5ea0 is at 0xFC bytes inside Graph_ThreadEntry (RAM 0x800C5DA4, ROM 0xB3CF14, build/src/code/graph.o)

@Dragorn421
Copy link
Author

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

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 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.

For more information, please refer to http://unlicense.org/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment