1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
#!/usr/bin/python
#----------------------------------------------------------------------
# Be sure to add the python path that points to the LLDB shared library.
#
# # To use this in the embedded python interpreter using "lldb" just
# import it with the full path using the "command script import"
# command
# (lldb) command script import /path/to/cmdtemplate.py
#----------------------------------------------------------------------
from __future__ import print_function
import platform
import os
import re
import signal
import sys
import subprocess
try:
# Just try for LLDB in case PYTHONPATH is already correctly setup
import lldb
except ImportError:
lldb_python_dirs = list()
# lldb is not in the PYTHONPATH, try some defaults for the current platform
platform_system = platform.system()
if platform_system == 'Darwin':
# On Darwin, try the currently selected Xcode directory
xcode_dir = subprocess.check_output("xcode-select --print-path", shell=True)
if xcode_dir:
lldb_python_dirs.append(
os.path.realpath(
xcode_dir +
'/../SharedFrameworks/LLDB.framework/Resources/Python'))
lldb_python_dirs.append(
xcode_dir + '/Library/PrivateFrameworks/LLDB.framework/Resources/Python')
lldb_python_dirs.append(
'/System/Library/PrivateFrameworks/LLDB.framework/Resources/Python')
success = False
for lldb_python_dir in lldb_python_dirs:
if os.path.exists(lldb_python_dir):
if not (sys.path.__contains__(lldb_python_dir)):
sys.path.append(lldb_python_dir)
try:
import lldb
except ImportError:
pass
else:
print('imported lldb from: "%s"' % (lldb_python_dir))
success = True
break
if not success:
print("error: couldn't locate the 'lldb' module, please set PYTHONPATH correctly")
sys.exit(1)
import optparse
import shlex
import time
def regex_option_callback(option, opt_str, value, parser):
if opt_str == "--std":
value = '^std::'
regex = re.compile(value)
parser.values.skip_type_regexes.append(regex)
def create_types_options(for_lldb_command):
if for_lldb_command:
usage = "usage: %prog [options]"
description = '''This command will help check for padding in between
base classes and members in structs and classes. It will summarize the types
and how much padding was found. If no types are specified with the --types TYPENAME
option, all structure and class types will be verified. If no modules are
specified with the --module option, only the target's main executable will be
searched.
'''
else:
usage = "usage: %prog [options] EXEPATH [EXEPATH ...]"
description = '''This command will help check for padding in between
base classes and members in structures and classes. It will summarize the types
and how much padding was found. One or more paths to executable files must be
specified and targets will be created with these modules. If no types are
specified with the --types TYPENAME option, all structure and class types will
be verified in all specified modules.
'''
parser = optparse.OptionParser(
description=description,
prog='framestats',
usage=usage)
if not for_lldb_command:
parser.add_option(
'-a',
'--arch',
type='string',
dest='arch',
help='The architecture to use when creating the debug target.',
default=None)
parser.add_option(
'-p',
'--platform',
type='string',
metavar='platform',
dest='platform',
help='Specify the platform to use when creating the debug target. Valid values include "localhost", "darwin-kernel", "ios-simulator", "remote-freebsd", "remote-macosx", "remote-ios", "remote-linux".')
parser.add_option(
'-m',
'--module',
action='append',
type='string',
metavar='MODULE',
dest='modules',
help='Specify one or more modules which will be used to verify the types.',
default=[])
parser.add_option(
'-d',
'--debug',
action='store_true',
dest='debug',
help='Pause 10 seconds to wait for a debugger to attach.',
default=False)
parser.add_option(
'-t',
'--type',
action='append',
type='string',
metavar='TYPENAME',
dest='typenames',
help='Specify one or more type names which should be verified. If no type names are specified, all class and struct types will be verified.',
default=[])
parser.add_option(
'-v',
'--verbose',
action='store_true',
dest='verbose',
help='Enable verbose logging and information.',
default=False)
parser.add_option(
'-s',
'--skip-type-regex',
action="callback",
callback=regex_option_callback,
type='string',
metavar='REGEX',
dest='skip_type_regexes',
help='Regular expressions that, if they match the current member typename, will cause the type to no be recursively displayed.',
default=[])
parser.add_option(
'--std',
action="callback",
callback=regex_option_callback,
metavar='REGEX',
dest='skip_type_regexes',
help="Don't' recurse into types in the std namespace.",
default=[])
return parser
def verify_type(target, options, type):
print(type)
typename = type.GetName()
# print 'type: %s' % (typename)
(end_offset, padding) = verify_type_recursive(
target, options, type, None, 0, 0, 0)
byte_size = type.GetByteSize()
# if end_offset < byte_size:
# last_member_padding = byte_size - end_offset
# print '%+4u <%u> padding' % (end_offset, last_member_padding)
# padding += last_member_padding
print('Total byte size: %u' % (byte_size))
print('Total pad bytes: %u' % (padding))
if padding > 0:
print('Padding percentage: %2.2f %%' % ((float(padding) / float(byte_size)) * 100.0))
print()
def verify_type_recursive(
target,
options,
type,
member_name,
depth,
base_offset,
padding):
prev_end_offset = base_offset
typename = type.GetName()
byte_size = type.GetByteSize()
if member_name and member_name != typename:
print('%+4u <%3u> %s%s %s;' % (base_offset, byte_size, ' ' * depth, typename, member_name))
else:
print('%+4u {%3u} %s%s' % (base_offset, byte_size, ' ' * depth, typename))
for type_regex in options.skip_type_regexes:
match = type_regex.match(typename)
if match:
return (base_offset + byte_size, padding)
members = type.members
if members:
for member_idx, member in enumerate(members):
member_type = member.GetType()
member_canonical_type = member_type.GetCanonicalType()
member_type_class = member_canonical_type.GetTypeClass()
member_name = member.GetName()
member_offset = member.GetOffsetInBytes()
member_total_offset = member_offset + base_offset
member_byte_size = member_type.GetByteSize()
member_is_class_or_struct = False
if member_type_class == lldb.eTypeClassStruct or member_type_class == lldb.eTypeClassClass:
member_is_class_or_struct = True
if member_idx == 0 and member_offset == target.GetAddressByteSize(
) and type.IsPolymorphicClass():
ptr_size = target.GetAddressByteSize()
print('%+4u <%3u> %s__vtbl_ptr_type * _vptr;' % (prev_end_offset, ptr_size, ' ' * (depth + 1)))
prev_end_offset = ptr_size
else:
if prev_end_offset < member_total_offset:
member_padding = member_total_offset - prev_end_offset
padding = padding + member_padding
print('%+4u <%3u> %s<PADDING>' % (prev_end_offset, member_padding, ' ' * (depth + 1)))
if member_is_class_or_struct:
(prev_end_offset,
padding) = verify_type_recursive(target,
options,
member_canonical_type,
member_name,
depth + 1,
member_total_offset,
padding)
else:
prev_end_offset = member_total_offset + member_byte_size
member_typename = member_type.GetName()
if member.IsBitfield():
print('%+4u <%3u> %s%s:%u %s;' % (member_total_offset, member_byte_size, ' ' * (depth + 1), member_typename, member.GetBitfieldSizeInBits(), member_name))
else:
print('%+4u <%3u> %s%s %s;' % (member_total_offset, member_byte_size, ' ' * (depth + 1), member_typename, member_name))
if prev_end_offset < byte_size:
last_member_padding = byte_size - prev_end_offset
print('%+4u <%3u> %s<PADDING>' % (prev_end_offset, last_member_padding, ' ' * (depth + 1)))
padding += last_member_padding
else:
if type.IsPolymorphicClass():
ptr_size = target.GetAddressByteSize()
print('%+4u <%3u> %s__vtbl_ptr_type * _vptr;' % (prev_end_offset, ptr_size, ' ' * (depth + 1)))
prev_end_offset = ptr_size
prev_end_offset = base_offset + byte_size
return (prev_end_offset, padding)
def check_padding_command(debugger, command, result, dict):
# Use the Shell Lexer to properly parse up command options just like a
# shell would
command_args = shlex.split(command)
parser = create_types_options(True)
try:
(options, args) = parser.parse_args(command_args)
except:
# if you don't handle exceptions, passing an incorrect argument to the OptionParser will cause LLDB to exit
# (courtesy of OptParse dealing with argument errors by throwing SystemExit)
result.SetStatus(lldb.eReturnStatusFailed)
# returning a string is the same as returning an error whose
# description is the string
return "option parsing failed"
verify_types(debugger.GetSelectedTarget(), options)
@lldb.command("parse_all_struct_class_types")
def parse_all_struct_class_types(debugger, command, result, dict):
command_args = shlex.split(command)
for f in command_args:
error = lldb.SBError()
target = debugger.CreateTarget(f, None, None, False, error)
module = target.GetModuleAtIndex(0)
print("Parsing all types in '%s'" % (module))
types = module.GetTypes(lldb.eTypeClassClass | lldb.eTypeClassStruct)
for t in types:
print(t)
print("")
def verify_types(target, options):
if not target:
print('error: invalid target')
return
modules = list()
if len(options.modules) == 0:
# Append just the main executable if nothing was specified
module = target.modules[0]
if module:
modules.append(module)
else:
for module_name in options.modules:
module = lldb.target.module[module_name]
if module:
modules.append(module)
if modules:
for module in modules:
print('module: %s' % (module.file))
if options.typenames:
for typename in options.typenames:
types = module.FindTypes(typename)
if types.GetSize():
print('Found %u types matching "%s" in "%s"' % (len(types), typename, module.file))
for type in types:
verify_type(target, options, type)
else:
print('error: no type matches "%s" in "%s"' % (typename, module.file))
else:
types = module.GetTypes(
lldb.eTypeClassClass | lldb.eTypeClassStruct)
print('Found %u types in "%s"' % (len(types), module.file))
for type in types:
verify_type(target, options, type)
else:
print('error: no modules')
if __name__ == '__main__':
debugger = lldb.SBDebugger.Create()
parser = create_types_options(False)
# try:
(options, args) = parser.parse_args(sys.argv[1:])
# except:
# print "error: option parsing failed"
# sys.exit(1)
if options.debug:
print("Waiting for debugger to attach to process %d" % os.getpid())
os.kill(os.getpid(), signal.SIGSTOP)
for path in args:
# in a command - the lldb.* convenience variables are not to be used
# and their values (if any) are undefined
# this is the best practice to access those objects from within a
# command
error = lldb.SBError()
target = debugger.CreateTarget(path,
options.arch,
options.platform,
True,
error)
if error.Fail():
print(error.GetCString())
continue
verify_types(target, options)
elif getattr(lldb, 'debugger', None):
lldb.debugger.HandleCommand(
'command script add -f types.check_padding_command check_padding')
print('"check_padding" command installed, use the "--help" option for detailed help')
|