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
|
"""
LLDB AppKit formatters
Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
See https://llvm.org/LICENSE.txt for license information.
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
"""
# example synthetic children and summary provider for CFString (and related NSString class)
# the real code is part of the LLDB core
import lldb
import lldb.runtime.objc.objc_runtime
import lldb.formatters.Logger
try:
unichr
except NameError:
unichr = chr
def CFString_SummaryProvider(valobj, dict):
logger = lldb.formatters.Logger.Logger()
provider = CFStringSynthProvider(valobj, dict)
if not provider.invalid:
try:
summary = provider.get_child_at_index(
provider.get_child_index("content"))
if isinstance(summary, lldb.SBValue):
summary = summary.GetSummary()
else:
summary = '"' + summary + '"'
except:
summary = None
if summary is None:
summary = '<variable is not NSString>'
return '@' + summary
return ''
def CFAttributedString_SummaryProvider(valobj, dict):
logger = lldb.formatters.Logger.Logger()
offset = valobj.GetTarget().GetProcess().GetAddressByteSize()
pointee = valobj.GetValueAsUnsigned(0)
summary = '<variable is not NSAttributedString>'
if pointee is not None and pointee != 0:
pointee = pointee + offset
child_ptr = valobj.CreateValueFromAddress(
"string_ptr", pointee, valobj.GetType())
child = child_ptr.CreateValueFromAddress(
"string_data",
child_ptr.GetValueAsUnsigned(),
valobj.GetType()).AddressOf()
provider = CFStringSynthProvider(child, dict)
if not provider.invalid:
try:
summary = provider.get_child_at_index(
provider.get_child_index("content")).GetSummary()
except:
summary = '<variable is not NSAttributedString>'
if summary is None:
summary = '<variable is not NSAttributedString>'
return '@' + summary
def __lldb_init_module(debugger, dict):
debugger.HandleCommand(
"type summary add -F CFString.CFString_SummaryProvider NSString CFStringRef CFMutableStringRef")
debugger.HandleCommand(
"type summary add -F CFString.CFAttributedString_SummaryProvider NSAttributedString")
class CFStringSynthProvider:
def __init__(self, valobj, dict):
logger = lldb.formatters.Logger.Logger()
self.valobj = valobj
self.update()
# children other than "content" are for debugging only and must not be
# used in production code
def num_children(self):
logger = lldb.formatters.Logger.Logger()
if self.invalid:
return 0
return 6
def read_unicode(self, pointer, max_len=2048):
logger = lldb.formatters.Logger.Logger()
process = self.valobj.GetTarget().GetProcess()
error = lldb.SBError()
pystr = u''
# cannot do the read at once because the length value has
# a weird encoding. better play it safe here
while max_len > 0:
content = process.ReadMemory(pointer, 2, error)
new_bytes = bytearray(content)
b0 = new_bytes[0]
b1 = new_bytes[1]
pointer = pointer + 2
if b0 == 0 and b1 == 0:
break
# rearrange bytes depending on endianness
# (do we really need this or is Cocoa going to
# use Windows-compatible little-endian even
# if the target is big endian?)
if self.is_little:
value = b1 * 256 + b0
else:
value = b0 * 256 + b1
pystr = pystr + unichr(value)
# read max_len unicode values, not max_len bytes
max_len = max_len - 1
return pystr
# handle the special case strings
# only use the custom code for the tested LP64 case
def handle_special(self):
logger = lldb.formatters.Logger.Logger()
if not self.is_64_bit:
# for 32bit targets, use safe ObjC code
return self.handle_unicode_string_safe()
offset = 12
pointer = self.valobj.GetValueAsUnsigned(0) + offset
pystr = self.read_unicode(pointer)
return self.valobj.CreateValueFromExpression(
"content", "(char*)\"" + pystr.encode('utf-8') + "\"")
# last resort call, use ObjC code to read; the final aim is to
# be able to strip this call away entirely and only do the read
# ourselves
def handle_unicode_string_safe(self):
return self.valobj.CreateValueFromExpression(
"content", "(char*)\"" + self.valobj.GetObjectDescription() + "\"")
def handle_unicode_string(self):
logger = lldb.formatters.Logger.Logger()
# step 1: find offset
if self.inline:
pointer = self.valobj.GetValueAsUnsigned(
0) + self.size_of_cfruntime_base()
if not self.explicit:
# untested, use the safe code path
return self.handle_unicode_string_safe()
else:
# a full pointer is skipped here before getting to the live
# data
pointer = pointer + self.pointer_size
else:
pointer = self.valobj.GetValueAsUnsigned(
0) + self.size_of_cfruntime_base()
# read 8 bytes here and make an address out of them
try:
char_type = self.valobj.GetType().GetBasicType(
lldb.eBasicTypeChar).GetPointerType()
vopointer = self.valobj.CreateValueFromAddress(
"dummy", pointer, char_type)
pointer = vopointer.GetValueAsUnsigned(0)
except:
return self.valobj.CreateValueFromExpression(
"content", '(char*)"@\"invalid NSString\""')
# step 2: read Unicode data at pointer
pystr = self.read_unicode(pointer)
# step 3: return it
return pystr.encode('utf-8')
def handle_inline_explicit(self):
logger = lldb.formatters.Logger.Logger()
offset = 3 * self.pointer_size
offset = offset + self.valobj.GetValueAsUnsigned(0)
return self.valobj.CreateValueFromExpression(
"content", "(char*)(" + str(offset) + ")")
def handle_mutable_string(self):
logger = lldb.formatters.Logger.Logger()
offset = 2 * self.pointer_size
data = self.valobj.CreateChildAtOffset(
"content", offset, self.valobj.GetType().GetBasicType(
lldb.eBasicTypeChar).GetPointerType())
data_value = data.GetValueAsUnsigned(0)
if self.explicit and self.unicode:
return self.read_unicode(data_value).encode('utf-8')
else:
data_value = data_value + 1
return self.valobj.CreateValueFromExpression(
"content", "(char*)(" + str(data_value) + ")")
def handle_UTF8_inline(self):
logger = lldb.formatters.Logger.Logger()
offset = self.valobj.GetValueAsUnsigned(
0) + self.size_of_cfruntime_base()
if not self.explicit:
offset = offset + 1
return self.valobj.CreateValueFromAddress(
"content", offset, self.valobj.GetType().GetBasicType(
lldb.eBasicTypeChar)).AddressOf()
def handle_UTF8_not_inline(self):
logger = lldb.formatters.Logger.Logger()
offset = self.size_of_cfruntime_base()
return self.valobj.CreateChildAtOffset(
"content", offset, self.valobj.GetType().GetBasicType(
lldb.eBasicTypeChar).GetPointerType())
def get_child_at_index(self, index):
logger = lldb.formatters.Logger.Logger()
logger >> "Querying for child [" + str(index) + "]"
if index == 0:
return self.valobj.CreateValueFromExpression(
"mutable", str(int(self.mutable)))
if index == 1:
return self.valobj.CreateValueFromExpression("inline",
str(int(self.inline)))
if index == 2:
return self.valobj.CreateValueFromExpression(
"explicit", str(int(self.explicit)))
if index == 3:
return self.valobj.CreateValueFromExpression(
"unicode", str(int(self.unicode)))
if index == 4:
return self.valobj.CreateValueFromExpression(
"special", str(int(self.special)))
if index == 5:
# we are handling the several possible combinations of flags.
# for each known combination we have a function that knows how to
# go fetch the data from memory instead of running code. if a string is not
# correctly displayed, one should start by finding a combination of flags that
# makes it different from these known cases, and provide a new reader function
# if this is not possible, a new flag might have to be made up (like the "special" flag
# below, which is not a real flag in CFString), or alternatively one might need to use
# the ObjC runtime helper to detect the new class and deal with it accordingly
# print 'mutable = ' + str(self.mutable)
# print 'inline = ' + str(self.inline)
# print 'explicit = ' + str(self.explicit)
# print 'unicode = ' + str(self.unicode)
# print 'special = ' + str(self.special)
if self.mutable:
return self.handle_mutable_string()
elif self.inline and self.explicit and \
self.unicode == False and self.special == False and \
self.mutable == False:
return self.handle_inline_explicit()
elif self.unicode:
return self.handle_unicode_string()
elif self.special:
return self.handle_special()
elif self.inline:
return self.handle_UTF8_inline()
else:
return self.handle_UTF8_not_inline()
def get_child_index(self, name):
logger = lldb.formatters.Logger.Logger()
logger >> "Querying for child ['" + str(name) + "']"
if name == "content":
return self.num_children() - 1
if name == "mutable":
return 0
if name == "inline":
return 1
if name == "explicit":
return 2
if name == "unicode":
return 3
if name == "special":
return 4
# CFRuntimeBase is defined as having an additional
# 4 bytes (padding?) on LP64 architectures
# to get its size we add up sizeof(pointer)+4
# and then add 4 more bytes if we are on a 64bit system
def size_of_cfruntime_base(self):
logger = lldb.formatters.Logger.Logger()
return self.pointer_size + 4 + (4 if self.is_64_bit else 0)
# the info bits are part of the CFRuntimeBase structure
# to get at them we have to skip a uintptr_t and then get
# at the least-significant byte of a 4 byte array. If we are
# on big-endian this means going to byte 3, if we are on
# little endian (OSX & iOS), this means reading byte 0
def offset_of_info_bits(self):
logger = lldb.formatters.Logger.Logger()
offset = self.pointer_size
if not self.is_little:
offset = offset + 3
return offset
def read_info_bits(self):
logger = lldb.formatters.Logger.Logger()
cfinfo = self.valobj.CreateChildAtOffset(
"cfinfo",
self.offset_of_info_bits(),
self.valobj.GetType().GetBasicType(
lldb.eBasicTypeChar))
cfinfo.SetFormat(11)
info = cfinfo.GetValue()
if info is not None:
self.invalid = False
return int(info, 0)
else:
self.invalid = True
return None
# calculating internal flag bits of the CFString object
# this stuff is defined and discussed in CFString.c
def is_mutable(self):
logger = lldb.formatters.Logger.Logger()
return (self.info_bits & 1) == 1
def is_inline(self):
logger = lldb.formatters.Logger.Logger()
return (self.info_bits & 0x60) == 0
# this flag's name is ambiguous, it turns out
# we must skip a length byte to get at the data
# when this flag is False
def has_explicit_length(self):
logger = lldb.formatters.Logger.Logger()
return (self.info_bits & (1 | 4)) != 4
# probably a subclass of NSString. obtained this from [str pathExtension]
# here info_bits = 0 and Unicode data at the start of the padding word
# in the long run using the isa value might be safer as a way to identify this
# instead of reading the info_bits
def is_special_case(self):
logger = lldb.formatters.Logger.Logger()
return self.info_bits == 0
def is_unicode(self):
logger = lldb.formatters.Logger.Logger()
return (self.info_bits & 0x10) == 0x10
# preparing ourselves to read into memory
# by adjusting architecture-specific info
def adjust_for_architecture(self):
logger = lldb.formatters.Logger.Logger()
self.pointer_size = self.valobj.GetTarget().GetProcess().GetAddressByteSize()
self.is_64_bit = self.pointer_size == 8
self.is_little = self.valobj.GetTarget().GetProcess(
).GetByteOrder() == lldb.eByteOrderLittle
# reading info bits out of the CFString and computing
# useful values to get at the real data
def compute_flags(self):
logger = lldb.formatters.Logger.Logger()
self.info_bits = self.read_info_bits()
if self.info_bits is None:
return
self.mutable = self.is_mutable()
self.inline = self.is_inline()
self.explicit = self.has_explicit_length()
self.unicode = self.is_unicode()
self.special = self.is_special_case()
def update(self):
logger = lldb.formatters.Logger.Logger()
self.adjust_for_architecture()
self.compute_flags()
|