xref: /aoo42x/main/solenv/inc/lldb4aoo.py (revision 84d1acfe)
1*84d1acfeSHerbert Dürr# Licensed to the Apache Software Foundation (ASF) under one
2*84d1acfeSHerbert Dürr# or more contributor license agreements.  See the NOTICE file
3*84d1acfeSHerbert Dürr# distributed with this work for additional information
4*84d1acfeSHerbert Dürr# regarding copyright ownership.  The ASF licenses this file
5*84d1acfeSHerbert Dürr# to you under the Apache License, Version 2.0 (the
6*84d1acfeSHerbert Dürr# "License"); you may not use this file except in compliance
7*84d1acfeSHerbert Dürr# with the License.  You may obtain a copy of the License at
8*84d1acfeSHerbert Dürr#
9*84d1acfeSHerbert Dürr#   http://www.apache.org/licenses/LICENSE-2.0
10*84d1acfeSHerbert Dürr#
11*84d1acfeSHerbert Dürr# Unless required by applicable law or agreed to in writing,
12*84d1acfeSHerbert Dürr# software distributed under the License is distributed on an
13*84d1acfeSHerbert Dürr# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14*84d1acfeSHerbert Dürr# KIND, either express or implied.  See the License for the
15*84d1acfeSHerbert Dürr# specific language governing permissions and limitations
16*84d1acfeSHerbert Dürr# under the License.
17*84d1acfeSHerbert Dürr
1881712d15SHerbert Dürr# to activate the AOO-LLDB helper script type the line below into LLDB
19aec0f987SHerbert Dürr#	command script import path-to-script/lldb4aoo.py
20aec0f987SHerbert Dürr# or activate it automatically by adding the line to ~/.lldbinit
21f174ccd1SHerbert Dürr
22f174ccd1SHerbert Dürrdef __lldb_init_module( dbg, dict):
23f174ccd1SHerbert Dürr	# the list of AOO specific types
24aec0f987SHerbert Dürr	aoo_types = ['rtl_String', 'rtl_uString', '_ByteStringData', '_UniStringData']
25aec0f987SHerbert Dürr	pimpl_types = ['rtl::OString', 'rtl::OUString', 'ByteString', 'UniString']
26aec0f987SHerbert Dürr	# register a helper function for each non-trivial type
27f174ccd1SHerbert Dürr	for t in aoo_types:
28f174ccd1SHerbert Dürr		f = 'getinfo_for_' + t.replace( '::', '_')
29f174ccd1SHerbert Dürr		if f in globals():
30aec0f987SHerbert Dürr			dbg.HandleCommand( 'type summary add %s -v -C yes -F %s.%s' % (t,__name__,f))
31f174ccd1SHerbert Dürr		else:
32aec0f987SHerbert Dürr			print( 'AOO-LLDB helper function "%s" is not yet defined: '
33aec0f987SHerbert Dürr			    '"%s" types cannot be displayed properly!' % (f,t))
34aec0f987SHerbert Dürr	# register a generic helper function for pimpl types
35aec0f987SHerbert Dürr	dbg.HandleCommand( 'type summary add -F %s.%s -v -C yes -n PIMPL %s' % ( __name__,'get_pimpl_info', ' '.join(pimpl_types)))
36f174ccd1SHerbert Dürr
376f79f796SHerbert Dürr	# add info about specific helper methods
386f79f796SHerbert Dürr	# assume functions with docstrings are available for general consumption
396f79f796SHerbert Dürr	helper_funcs = [v for (k,v) in globals().iteritems() if( not k.startswith('_') and callable(v) and v.__doc__)]
406f79f796SHerbert Dürr	if helper_funcs:
416f79f796SHerbert Dürr		print( 'Available AOO-specific helper functions:')
426f79f796SHerbert Dürr		for hfunc in helper_funcs:
436f79f796SHerbert Dürr			shortdesc = hfunc.__doc__.splitlines()[0]
446f79f796SHerbert Dürr			print( '\t%s\t# "%s"' %(hfunc.__name__, shortdesc))
456f79f796SHerbert Dürr		print( 'Run them with:')
466f79f796SHerbert Dürr		for hfunc in helper_funcs[:4]:
476f79f796SHerbert Dürr			print( '\tscript %s.%s()' %(__name__, hfunc.__name__))
486f79f796SHerbert Dürr
496f79f796SHerbert Dürr# some helpers for use from interactive LLDB sessions
506f79f796SHerbert Dürr
516f79f796SHerbert Dürrimport lldb
526f79f796SHerbert Dürr
536f79f796SHerbert Dürrdef add_breakpoints():
546f79f796SHerbert Dürr	'Setup breakpoints useful for AOO debugging'
556f79f796SHerbert Dürr	dbg = lldb.debugger
566f79f796SHerbert Dürr	if dbg.GetNumTargets() == 0:
576f79f796SHerbert Dürr		return
586f79f796SHerbert Dürr	# the list of interesting function breakpoints
596f79f796SHerbert Dürr	aoo_breakfn = ['main', '__cxa_call_unexpected', 'objc_exception_throw']
606f79f796SHerbert Dürr	aoo_breakfn += ['__cxa_throw']
616f79f796SHerbert Dürr	# register breakpoints for function basenames
626f79f796SHerbert Dürr	for b in aoo_breakfn:
636f79f796SHerbert Dürr		dbg.HandleCommand( 'breakpoint set -b ' + b)
646f79f796SHerbert Dürr
6581712d15SHerbert Dürr
66aec0f987SHerbert Dürr# local functions for use by the AOO-type summary providers
67f174ccd1SHerbert Dürr
6881712d15SHerbert Dürrdef walk_ptrchain( v):
6981712d15SHerbert Dürr	info = ''
70aec0f987SHerbert Dürr	while v.TypeIsPointerType():
71aec0f987SHerbert Dürr		n = v.GetValueAsUnsigned()
72aec0f987SHerbert Dürr		if n == 0:
73aec0f987SHerbert Dürr			info += 'NULL'
74aec0f987SHerbert Dürr			return (None, info)
7581712d15SHerbert Dürr		info += '0x%04X-> ' % (n)
7681712d15SHerbert Dürr		v = v.Dereference()
77aec0f987SHerbert Dürr	return (v, info)
78f174ccd1SHerbert Dürr
7980e5b736SHerbert Dürrdef ret_strdata_info( v, refvar, lenvar, aryvar):
8081712d15SHerbert Dürr	(v, info) = walk_ptrchain( v)
81aec0f987SHerbert Dürr	if not v:
82aec0f987SHerbert Dürr		return info
8380e5b736SHerbert Dürr	r = v.GetChildMemberWithName( refvar).GetValueAsSigned()
8480e5b736SHerbert Dürr	l = v.GetChildMemberWithName( lenvar).GetValueAsSigned()
8580e5b736SHerbert Dürr	c = v.GetChildMemberWithName( aryvar)
86f4289c07SHerbert Dürr	if (r < 0) or (l < 0):
87f4289c07SHerbert Dürr		info += 'CORRUPT_STR={refs=%d, len=%d}' % (r,l)
88f4289c07SHerbert Dürr		return info
89aec0f987SHerbert Dürr	L = min(l,128)
90aec0f987SHerbert Dürr	d = c.AddressOf().GetPointeeData( 0, L)
9180e5b736SHerbert Dürr	if c.GetByteSize() == 1: # assume UTF-8
9280e5b736SHerbert Dürr		s = ''.join([chr(x) for x in d.uint8s])
93a54092f6SHerbert Dürr	else: # assume UTF-16
9480e5b736SHerbert Dürr		s = (u''.join([unichr(x) for x in d.uint16s])).encode('utf-8')
95aec0f987SHerbert Dürr	info += ('{refs=%d, len=%d, str="%s"%s}' % (r, l, s.encode('string_escape'), '...'if(l!=L)else''))
9680e5b736SHerbert Dürr	return info
9780e5b736SHerbert Dürr
98aec0f987SHerbert Dürr# definitions for our individual LLDB type summary providers
99aec0f987SHerbert Dürr
100aec0f987SHerbert Dürrdef get_pimpl_info( valobj, dict):
10181712d15SHerbert Dürr	(v, info) = walk_ptrchain( valobj)
102aec0f987SHerbert Dürr	p = v.GetChildAtIndex(0)
10381712d15SHerbert Dürr	pname = p.GetName()
10481712d15SHerbert Dürr	n = p.GetValueAsUnsigned()
10581712d15SHerbert Dürr	if n == 0:
10681712d15SHerbert Dürr		return '%s(%s==NULL)' % (info, pname)
10781712d15SHerbert Dürr	info = '%s(%s=0x%04X)-> ' % (info, pname, n)
108aec0f987SHerbert Dürr	return info + p.Dereference().GetSummary()
10980e5b736SHerbert Dürr
110a54092f6SHerbert Dürr
111f174ccd1SHerbert Dürrdef getinfo_for_rtl_String( valobj, dict):
11280e5b736SHerbert Dürr	return ret_strdata_info( valobj, 'refCount', 'length', 'buffer')
113f174ccd1SHerbert Dürr
114f174ccd1SHerbert Dürrdef getinfo_for_rtl_uString( valobj, dict):
11580e5b736SHerbert Dürr	return ret_strdata_info( valobj, 'refCount', 'length', 'buffer')
116f174ccd1SHerbert Dürr
1170d72dfe8SHerbert Dürrdef getinfo_for__ByteStringData( valobj, dict):
11880e5b736SHerbert Dürr	return ret_strdata_info( valobj, 'mnRefCount', 'mnLen', 'maStr')
119f174ccd1SHerbert Dürr
1200d72dfe8SHerbert Dürrdef getinfo_for__UniStringData( valobj, dict):
12180e5b736SHerbert Dürr	return ret_strdata_info( valobj, 'mnRefCount', 'mnLen', 'maStr')
122f174ccd1SHerbert Dürr
123