aboutsummaryrefslogtreecommitdiff
path: root/python/weenix/debug_userland.py
blob: 07e8cc30fff48aec2182a0034e5ecc5d7878e3bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from elftools.elf.elffile import ELFFile
from os import path

class DebugUserland(gdb.Command):
    def __init__(self):
      super(DebugUserland, self).__init__("debug-userland", gdb.COMMAND_USER)

    def invoke(self, arg, from_tty):
        directory = 'user/usr/bin/'
        filename = directory + arg + '.exec'
        if not path.exists(filename):
            filename = 'user/bin/' + arg + '.exec'
        if arg == 'init':
            filename = 'user/sbin/init.exec'
        elf = ELFFile(open(filename, 'rb'))
        text_section = elf.get_section_by_name('.text')
        entry = text_section.header.sh_addr
        gdb.execute(f"add-symbol-file {filename} {entry}")

        symtab = elf.get_section_by_name('.symtab')
        main = symtab.get_symbol_by_name('main')[0]
        main_addr = main.entry.st_value
        gdb.execute(f"break *{main_addr}")
        
DebugUserland()