Skip to content

Commit

Permalink
ocrecord: Move gdb_asm2cfg to separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Weiss committed Jun 20, 2023
1 parent ff312a8 commit 7480912
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
Empty file added ocrecord/__init__.py
Empty file.
59 changes: 30 additions & 29 deletions ocgraph/gdb_asm2cfg.py → ocrecord/gdb_asm2cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
For further information see
https://sourceware.org/gdb/current/onlinedocs/gdb/Python.html#Python.
"""


import traceback

import gdb

from asm2cfg import asm2cfg
from ..ocgraph.interface.drawer import Drawer
from ..ocgraph.interface.analyzer import Analyzer


class SkipCalls(gdb.Parameter):
Expand All @@ -20,25 +19,25 @@ class SkipCalls(gdb.Parameter):
set skipcalls off
"""

set_doc = 'Set whether savecfg and viewcfg commands will skip function calls from splitting CFG blocks'
show_doc = 'Set whether savecfg and viewcfg commands will skip function calls from splitting CFG blocks'
set_doc = "Set whether savecfg and viewcfg commands will skip function calls from splitting CFG blocks"
show_doc = "Set whether savecfg and viewcfg commands will skip function calls from splitting CFG blocks"

def __init__(self):
super().__init__('skipcalls', gdb.COMMAND_DATA, gdb.PARAM_BOOLEAN)
super().__init__("skipcalls", gdb.COMMAND_DATA, gdb.PARAM_BOOLEAN)
self.value = False

def get_set_string(self):
return f'Commands savecfg and viewcfg will skip function calls \
from splitting CFG blocks: {self.value_to_string()}'
return f"Commands savecfg and viewcfg will skip function calls \
from splitting CFG blocks: {self.value_to_string()}"

def get_show_string(self, _):
return f'Commands savecfg and viewcfg will skip function calls \
from splitting CFG blocks: {self.value_to_string()}'
return f"Commands savecfg and viewcfg will skip function calls \
from splitting CFG blocks: {self.value_to_string()}"

def value_to_string(self):
if self.value:
return 'on'
return 'off'
return "on"
return "off"


class ViewCfg(gdb.Command): # pylint: disable=too-few-public-methods
Expand All @@ -50,23 +49,25 @@ class ViewCfg(gdb.Command): # pylint: disable=too-few-public-methods
"""

def __init__(self):
super().__init__('viewcfg', gdb.COMMAND_USER)
super().__init__("viewcfg", gdb.COMMAND_USER)

def invoke(self, _arg, _from_tty): # pylint: disable=bad-option-value,no-self-use
""" Called by GDB when viewcfg command is invoked """
"""Called by GDB when viewcfg command is invoked"""
try:
frame = gdb.selected_frame()
arch = frame.architecture().name()
if arch.startswith('i386'):
target_name = 'x86'
elif arch.startswith('arm'):
target_name = 'arm'
if arch.startswith("i386"):
target_name = "x86"
elif arch.startswith("arm"):
target_name = "arm"
elif arch.startswith("sparc"):
target_name = "sparc"
else:
raise RuntimeError(f'unknown platform: {arch}')
assembly_lines = gdb.execute('disassemble', from_tty=False, to_string=True).split('\n')
function_name, basic_blocks = asm2cfg.parse_lines(assembly_lines, gdb.parameter('skipcalls'),
target_name)
asm2cfg.draw_cfg(function_name, basic_blocks, view=True)
raise RuntimeError(f"unknown platform: {arch}")
assembly_lines = gdb.execute("disassemble", from_tty=False, to_string=True).split("\n")
analyzer = Analyzer(config=target_name + " GDB")
analyzer.parse_lines(assembly_lines)
Drawer(analyzer.configuration).view_cfg(analyzer.function_name, analyzer.basic_blocks)
# Catch error coming from GDB side before other errors.
except gdb.error as ex:
raise gdb.GdbError(ex)
Expand All @@ -83,15 +84,15 @@ class SaveCfg(gdb.Command): # pylint: disable=too-few-public-methods
"""

def __init__(self):
super().__init__('savecfg', gdb.COMMAND_USER)
super().__init__("savecfg", gdb.COMMAND_USER)

def invoke(self, _arg, _from_tty): # pylint: disable=no-self-use
""" Called by GDB when savecfg command is invoked """
"""Called by GDB when savecfg command is invoked"""
try:
assembly_lines = gdb.execute('disassemble', from_tty=False, to_string=True).split('\n')
function_name, basic_blocks = asm2cfg.parse_lines(assembly_lines, gdb.parameter('skipcalls'),
'x86')
asm2cfg.draw_cfg(function_name, basic_blocks, view=False)
assembly_lines = gdb.execute("disassemble", from_tty=False, to_string=True).split("\n")
analyzer = Analyzer(config="x86 GDB")
analyzer.parse_lines(assembly_lines)
Drawer(analyzer.configuration).view_cfg(analyzer.function_name, analyzer.basic_blocks)
# Catch error coming from GDB side before other errors.
except gdb.error as ex:
raise gdb.GdbError(ex)
Expand Down

0 comments on commit 7480912

Please sign in to comment.