From 9cad361b9f6898d231cf94be2e59b0cf550d77f4 Mon Sep 17 00:00:00 2001 From: doronz Date: Mon, 29 Jan 2024 16:21:27 +0200 Subject: [PATCH] hilda_client: add `std::string` formatting --- hilda/hilda_client.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/hilda/hilda_client.py b/hilda/hilda_client.py index 0d1ac89..091179c 100644 --- a/hilda/hilda_client.py +++ b/hilda/hilda_client.py @@ -7,6 +7,7 @@ import logging import os import pickle +import struct import time import typing from collections import namedtuple @@ -16,8 +17,8 @@ from pathlib import Path from typing import Any, List, Optional, Union -import hexdump import IPython +import hexdump import lldb from humanfriendly import prompts from humanfriendly.terminal.html import html_to_ansi @@ -333,6 +334,7 @@ def file_symbol(self, address: int, module_name: Optional[str] = None) -> Symbol module = self.target else: module = self.target.FindModule(lldb.SBFileSpec(module_name)) + return self.symbol(module.ResolveFileAddress(address).GetLoadAddress(self.target)) def get_register(self, name) -> Symbol: @@ -402,6 +404,8 @@ def monitor(self, address, condition: str = None, **options) -> lldb.SBBreakpoin s: string cf: use CFCopyDescription() to get more informative description of the object po: use LLDB po command + std::string: for std::string + User defined function, will be called like `format_function(hilda_client, value)`. For example: @@ -1089,6 +1093,13 @@ def _ipython_run_cell_hook(self, info): symbol if symbol.type_ != lldb.eSymbolTypeObjCMetaClass else self.objc_get_class(node.id) ) + @staticmethod + def _std_string(value): + if struct.unpack("b", (value + 23).peek(1))[0] >= 0: + return value.peek_str() + else: + return value[0].peek_str() + def _monitor_format_value(self, fmt, value): if callable(fmt): return fmt(self, value) @@ -1097,6 +1108,7 @@ def _monitor_format_value(self, fmt, value): 's': lambda val: val.peek_str(), 'cf': lambda val: val.cf_description, 'po': lambda val: val.po(), + 'std::string': self._std_string } if fmt in formatters: return formatters[fmt](value)