[lldb-dev] Various questions about lldbinit and python scripts.

Enrico Granata egranata at apple.com
Mon Oct 7 18:10:30 PDT 2013


On Oct 7, 2013, at 5:19 PM, Jean-Yves Avenard <jyavenard at gmail.com> wrote:

> Hello
> 
> Thanks for answering!... very much appreciated
> 
> Thanks for the heads up... I'll have a look.
> 
> I already got QString to be displayed properly by changing the summary
> format with:
> {(const char *)$VAR.toUtf8().constData()}:s
> 
> not sure if you're familiar with xcode summary format, but that may be
> an easier approach than messing with python scripts. Is there a way to
> do loops in there (so I could display the content of loop and map
> container)..
> 

Python is not all that complicated :)
Here’s an example script that does formatting for arbitrary (pointer,length) pairs.

Since I was lazy I just decided my string type was going to be a std::pair<CharType*,LengthType> as in:

	std::pair<const char *,unsigned long> Sutf8 {utf8_buf,utf8_size};
	std::pair<const char16_t *,unsigned long> Sutf16 {utf16_buf,utf16_size};
	std::pair<const char32_t *,unsigned long> Sutf32 {utf32_buf,utf32_size};
It should be easy to adjust the scripts to any type that exposes the same information through different member variable names.

import lldb
def utf8_summary(value,unused):
	pointer = value.GetChildMemberWithName("first").GetValueAsUnsigned(0)
	length = value.GetChildMemberWithName("second").GetValueAsUnsigned(0)
	if pointer == 0:
		return False
	if length == 0:
		return '""'
	error = lldb.SBError()
	string_data = value.process.ReadMemory(pointer, length, error)
	return '"%s"' % (string_data) # utf8 is safe to emit as-is on OSX

def utf16_summary(value,unused):
	pointer = value.GetChildMemberWithName("first").GetValueAsUnsigned(0)
	length = value.GetChildMemberWithName("second").GetValueAsUnsigned(0)
	# assume length is in bytes - if in UTF16 chars, just multiply by 2
	if pointer == 0:
		return False
	if length == 0:
		return '""'
	error = lldb.SBError()
	string_data = value.process.ReadMemory(pointer, length, error)
	return '"%s"' % (string_data.decode('utf-16').encode('utf-8')) # utf8 is safe to emit as-is on OSX

def utf32_summary(value,unused):
	pointer = value.GetChildMemberWithName("first").GetValueAsUnsigned(0)
	length = value.GetChildMemberWithName("second").GetValueAsUnsigned(0)
	# assume length is in bytes - if in UTF32 chars, just multiply by 4
	if pointer == 0:
		return False
	if length == 0:
		return '""'
	error = lldb.SBError()
	string_data = value.process.ReadMemory(pointer, length, error)
	return '"%s"' % (string_data.decode('utf-32').encode('utf-8')) # utf8 is safe to emit as-is on OSX

def __lldb_init_module(debugger,*args):
	debugger.HandleCommand('type summary add --python-function utf81632.utf8_summary "std::__1::pair<const char *, unsigned long>"')
	debugger.HandleCommand('type summary add --python-function utf81632.utf16_summary "std::__1::pair<const char16_t *, unsigned long>"')
	debugger.HandleCommand('type summary add --python-function utf81632.utf32_summary "std::__1::pair<const char32_t *, unsigned long>"')


In the “type summary add” part, you will bind the functions to the type name depending on the string encoding in use (8, 16 or 32)
The functions itself know to use a pointer,length pair to read the correct number of bytes, and do the encoding/decoding that is necessary (on OSX, outputting UTF8 is just safe and will do the right thing, so we are porting any other encoding to UTF8 for display)
This is what the output looks like:

(std::__1::pair<const char *, unsigned long>) Sutf8 = "blah blah blah けほ"
(std::__1::pair<const char16_t *, unsigned long>) Sutf16 = "blah blah blah けほ"
(std::__1::pair<const char32_t *, unsigned long>) Sutf32 = "blah blah blah けほ"

In case you do not know the “bytes per character” for your type, you should simply be asking the pointer’s type for its bye size, and switch on that to pick the right encoding. Or maybe your string type will have a flag telling you the encoding to use. Either way, it all boils down to one of these three functions.
Feel free to ping back if you have any further questions!

Enrico Granata
📩 egranata@.com
☎️ 27683


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/lldb-dev/attachments/20131007/a52e4964/attachment.html>


More information about the lldb-dev mailing list