Skip to content

Commit 97d0c28

Browse files
authored
Merge pull request #1294 from JDevlieghere/raise-runtime-error
[lldb/Bindings] Raise exception when using properties that rely on lldb.target
2 parents f8c380e + 6e4e583 commit 97d0c28

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

lldb/bindings/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
file(GLOB SWIG_INTERFACES interfaces/*.i)
1+
file(GLOB SWIG_INTERFACES interface/*.i)
22
file(GLOB_RECURSE SWIG_SOURCES *.swig)
33
file(GLOB SWIG_HEADERS
44
${LLDB_SOURCE_DIR}/include/lldb/API/*.h

lldb/bindings/interface/SBAddress.i

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,27 +144,34 @@ public:
144144

145145
#ifdef SWIGPYTHON
146146
%pythoncode %{
147+
__runtime_error_str = 'This resolves the SBAddress using the SBTarget from lldb.target so this property can ONLY be used in the interactive script interpreter (i.e. under the lldb script command). For things like Python based commands and breakpoint callbacks use GetLoadAddress instead.'
148+
147149
def __get_load_addr_property__ (self):
148-
'''Get the load address for a lldb.SBAddress using the current target.'''
150+
'''Get the load address for a lldb.SBAddress using the current target. This resolves the SBAddress using the SBTarget from lldb.target so this property can ONLY be used in the interactive script interpreter (i.e. under the lldb script command). For things like Python based commands and breakpoint callbacks use GetLoadAddress instead.'''
151+
if not target:
152+
raise RuntimeError(self.__runtime_error_str)
149153
return self.GetLoadAddress (target)
150154

151155
def __set_load_addr_property__ (self, load_addr):
152-
'''Set the load address for a lldb.SBAddress using the current target.'''
156+
'''Set the load address for a lldb.SBAddress using the current target. This resolves the SBAddress using the SBTarget from lldb.target so this property can ONLY be used in the interactive script interpreter (i.e. under the lldb script command). For things like Python based commands and breakpoint callbacks use GetLoadAddress instead.'''
157+
if not target:
158+
raise RuntimeError(self.__runtime_error_str)
153159
return self.SetLoadAddress (load_addr, target)
154160

155161
def __int__(self):
156-
'''Convert an address to a load address if there is a process and that process is alive, or to a file address otherwise.'''
157-
if process and process.is_alive:
162+
'''Convert an address to a load address if there is a process and that process is alive, or to a file address otherwise. This resolves the SBAddress using the SBTarget from lldb.target so this property can ONLY be used in the interactive script interpreter (i.e. under the lldb script command). For things like Python based commands and breakpoint callbacks use GetLoadAddress instead.'''
163+
if not process or not target:
164+
raise RuntimeError(self.__runtime_error_str)
165+
if process.is_alive:
158166
return self.GetLoadAddress (target)
159-
else:
160-
return self.GetFileAddress ()
167+
return self.GetFileAddress ()
161168

162169
def __oct__(self):
163-
'''Convert the address to an octal string'''
170+
'''Convert the address to an octal string. This resolves the SBAddress using the SBTarget from lldb.target so this property can ONLY be used in the interactive script interpreter (i.e. under the lldb script command). For things like Python based commands and breakpoint callbacks use GetLoadAddress instead.'''
164171
return '%o' % int(self)
165172

166173
def __hex__(self):
167-
'''Convert the address to an hex string'''
174+
'''Convert the address to an hex string. This resolves the SBAddress using the SBTarget from lldb.target so this property can ONLY be used in the interactive script interpreter (i.e. under the lldb script command). For things like Python based commands and breakpoint callbacks use GetLoadAddress instead.'''
168175
return '0x%x' % int(self)
169176

170177
module = property(GetModule, None, doc='''A read only property that returns an lldb object that represents the module (lldb.SBModule) that this address resides within.''')
@@ -176,7 +183,7 @@ public:
176183
offset = property(GetOffset, None, doc='''A read only property that returns the section offset in bytes as an integer.''')
177184
section = property(GetSection, None, doc='''A read only property that returns an lldb object that represents the section (lldb.SBSection) that this address resides within.''')
178185
file_addr = property(GetFileAddress, None, doc='''A read only property that returns file address for the section as an integer. This is the address that represents the address as it is found in the object file that defines it.''')
179-
load_addr = property(__get_load_addr_property__, __set_load_addr_property__, doc='''A read/write property that gets/sets the SBAddress using load address. The setter resolves SBAddress using the SBTarget from lldb.target so this property can ONLY be used in the interactive script interpreter (i.e. under the lldb script command) and not in Python based commands, or breakpoint commands.''')
186+
load_addr = property(__get_load_addr_property__, __set_load_addr_property__, doc='''A read/write property that gets/sets the SBAddress using load address. This resolves the SBAddress using the SBTarget from lldb.target so this property can ONLY be used in the interactive script interpreter (i.e. under the lldb script command). For things like Python based commands and breakpoint callbacks use GetLoadAddress instead.''')
180187
%}
181188
#endif
182189

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import lldb
2+
3+
def test(debugger, command, result, internal_dict):
4+
return int(lldb.SBAddress())
5+
6+
def __lldb_init_module(debugger, internal_dict):
7+
debugger.HandleCommand('command script add -f sbaddress.test test')
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# REQUIRES: python
2+
# UNSUPPORTED: lldb-repro
3+
#
4+
# Test that the SBAddress properties throw an exception when used outside of
5+
# the interactive script interpreter.
6+
#
7+
# RUN: %lldb --script-language python -o 'command script import %S/Inputs/sbaddress.py' -o 'test' 2>&1 | FileCheck %s
8+
# CHECK: RuntimeError: This resolves the SBAddress using the SBTarget from lldb.target so this property can ONLY be used in the interactive script interpreter (i.e. under the lldb script command). For things like Python based commands and breakpoint callbacks use GetLoadAddress instead.

0 commit comments

Comments
 (0)