]>
Commit | Line | Data |
---|---|---|
39236c6e A |
1 | from xnu import * |
2 | from utils import * | |
3 | from process import * | |
4 | from pmap import * | |
fe8ab488 | 5 | import struct |
39236c6e A |
6 | |
7 | def GetBinaryNameForPC(pc_val, user_lib_info = None): | |
8 | """ find the binary in user_lib_info that the passed pc_val falls in range of. | |
9 | params: | |
fe8ab488 | 10 | pc_val : int - integer form of the pc address |
39236c6e A |
11 | user_lib_info: [] of [] which hold start, end, binary name |
12 | returns: | |
13 | str - Name of binary or "unknown" if not found. | |
14 | """ | |
15 | retval = "unknown" | |
16 | if not user_lib_info: | |
17 | return retval | |
18 | matches = [] | |
19 | for info in user_lib_info: | |
20 | if pc_val >= info[0] and pc_val <= info[1]: | |
21 | matches.append((pc_val - info[0], info[2])) | |
22 | matches.sort() | |
23 | if matches: | |
24 | retval = matches[0][1] | |
25 | return retval | |
26 | ||
27 | def ShowX86UserStack(thread, user_lib_info = None): | |
28 | """ Display user space stack frame and pc addresses. | |
29 | params: | |
30 | thread: obj referencing thread value | |
31 | returns: | |
32 | Nothing | |
33 | """ | |
34 | iss = Cast(thread.machine.iss, 'x86_saved_state_t *') | |
35 | abi = int(iss.flavor) | |
36 | user_ip = 0 | |
37 | user_frame = 0 | |
38 | user_abi_ret_offset = 0 | |
39 | if abi == 0xf: | |
40 | debuglog("User process is 64 bit") | |
41 | user_ip = iss.uss.ss_64.isf.rip | |
42 | user_frame = iss.uss.ss_64.rbp | |
43 | user_abi_ret_offset = 8 | |
fe8ab488 | 44 | user_abi_type = "uint64_t" |
39236c6e A |
45 | else: |
46 | debuglog("user process is 32 bit") | |
47 | user_ip = iss.uss.ss_32.eip | |
48 | user_frame = iss.uss.ss_32.ebp | |
49 | user_abi_ret_offset = 4 | |
fe8ab488 A |
50 | user_abi_type = "uint32_t" |
51 | ||
39236c6e A |
52 | if user_ip == 0: |
53 | print "This activation does not appear to have a valid user context." | |
54 | return False | |
55 | ||
56 | cur_ip = user_ip | |
57 | cur_frame = user_frame | |
58 | debuglog("ip= 0x%x , fr = 0x%x " % (cur_ip, cur_frame)) | |
39236c6e A |
59 | |
60 | frameformat = "{0:d} FP: 0x{1:x} PC: 0x{2:x}" | |
61 | if user_lib_info is not None: | |
62 | frameformat = "{0:d} {3: <30s} 0x{2:x}" | |
63 | print frameformat.format(0, cur_frame, cur_ip, GetBinaryNameForPC(cur_ip, user_lib_info)) | |
64 | ||
65 | print kern.Symbolicate(cur_ip) | |
fe8ab488 A |
66 | |
67 | frameno = 0 | |
68 | while True: | |
69 | frameno = frameno + 1 | |
70 | frame = GetUserDataAsString(thread.task, unsigned(cur_frame), user_abi_ret_offset*2) | |
71 | cur_ip = _ExtractDataFromString(frame, user_abi_ret_offset, user_abi_type) | |
72 | cur_frame = _ExtractDataFromString(frame, 0, user_abi_type) | |
73 | if not cur_frame or cur_frame == 0x0000000800000008: | |
74 | break | |
75 | print frameformat.format(frameno, cur_frame, cur_ip, GetBinaryNameForPC(cur_ip, user_lib_info)) | |
76 | print kern.Symbolicate(cur_ip) | |
39236c6e A |
77 | return |
78 | ||
79 | def _PrintARMUserStack(task, cur_pc, cur_fp, framesize, frametype, frameformat, user_lib_info=None): | |
cb323159 | 80 | cur_pc = kern.StripUserPAC(cur_pc) |
39236c6e A |
81 | if cur_pc == 0: |
82 | "No valid user context for this activation." | |
83 | return | |
84 | frameno = 0 | |
85 | print frameformat.format(frameno, cur_fp, cur_pc, GetBinaryNameForPC(cur_pc, user_lib_info)) | |
86 | while True: | |
87 | frameno = frameno + 1 | |
88 | frame = GetUserDataAsString(task, cur_fp, framesize) | |
89 | cur_fp = _ExtractDataFromString(frame, 0, frametype) | |
90 | cur_pc = _ExtractDataFromString(frame, (framesize / 2), frametype) | |
cb323159 | 91 | cur_pc = kern.StripUserPAC(cur_pc) |
39236c6e A |
92 | if not cur_fp: |
93 | break | |
94 | print frameformat.format(frameno, cur_fp, cur_pc, GetBinaryNameForPC(cur_pc, user_lib_info)) | |
95 | ||
96 | def ShowARMUserStack(thread, user_lib_info = None): | |
97 | cur_pc = unsigned(thread.machine.PcbData.pc) | |
98 | cur_fp = unsigned(thread.machine.PcbData.r[7]) | |
99 | frameformat = "{0:>2d} FP: 0x{1:x} PC: 0x{2:x}" | |
100 | if user_lib_info is not None: | |
101 | frameformat = "{0:>2d} {3: <30s} 0x{2:0>8x}" | |
102 | framesize = 8 | |
103 | frametype = "uint32_t" | |
104 | _PrintARMUserStack(thread.task, cur_pc, cur_fp, framesize, frametype, frameformat, user_lib_info=user_lib_info) | |
105 | ||
fe8ab488 A |
106 | def ShowARM64UserStack(thread, user_lib_info = None): |
107 | SAVED_STATE_FLAVOR_ARM=20 | |
108 | SAVED_STATE_FLAVOR_ARM64=21 | |
109 | upcb = thread.machine.upcb | |
110 | flavor = upcb.ash.flavor | |
111 | frameformat = "{0:>2d} FP: 0x{1:x} PC: 0x{2:x}" | |
112 | if flavor == SAVED_STATE_FLAVOR_ARM64: | |
113 | cur_pc = unsigned(upcb.uss.ss_64.pc) | |
114 | cur_fp = unsigned(upcb.uss.ss_64.fp) | |
115 | if user_lib_info is not None: | |
116 | frameformat = "{0:>2d} {3: <30s} 0x{2:x}" | |
117 | framesize = 16 | |
118 | frametype = "uint64_t" | |
119 | elif flavor == SAVED_STATE_FLAVOR_ARM: | |
120 | cur_pc = unsigned(upcb.uss.ss_32.pc) | |
121 | cur_fp = unsigned(upcb.uss.ss_32.r[7]) | |
122 | if user_lib_info is not None: | |
123 | frameformat = "{0:>2d}: {3: <30s} 0x{2:x}" | |
124 | framesize = 8 | |
125 | frametype = "uint32_t" | |
126 | else: | |
127 | raise RuntimeError("Thread {0} has an invalid flavor {1}".format(unsigned(thread), flavor)) | |
128 | ||
129 | _PrintARMUserStack(thread.task, cur_pc, cur_fp, framesize, frametype, frameformat, user_lib_info=user_lib_info) | |
130 | ||
39236c6e A |
131 | |
132 | @lldb_command('showthreaduserstack') | |
133 | def ShowThreadUserStack(cmd_args=None): | |
134 | """ Show user stack for a given thread. | |
135 | Syntax: (lldb) showthreaduserstack <thread_ptr> | |
136 | """ | |
137 | if not cmd_args: | |
138 | raise ArgumentError("Insufficient arguments") | |
139 | ||
140 | thread = kern.GetValueFromAddress(ArgumentStringToInt(cmd_args[0]), 'thread *') | |
141 | if kern.arch == "x86_64": | |
142 | ShowX86UserStack(thread) | |
143 | elif kern.arch == "arm": | |
144 | ShowARMUserStack(thread) | |
5ba3f43e | 145 | elif kern.arch.startswith("arm64"): |
fe8ab488 A |
146 | ShowARM64UserStack(thread) |
147 | return True | |
148 | ||
3e170ce0 | 149 | @lldb_command('printuserdata','XO:') |
fe8ab488 A |
150 | def PrintUserspaceData(cmd_args=None, cmd_options={}): |
151 | """ Read userspace data for given task and print based on format provided. | |
152 | Syntax: (lldb) printuserdata <task_t> <uspace_address> <format_specifier> | |
153 | params: | |
154 | <task_t> : pointer to task | |
155 | <uspace_address> : address to user space memory | |
156 | <format_specifier> : String representation for processing the data and printing it. | |
3e170ce0 A |
157 | e.g Q -> unsigned long long, q -> long long, I -> unsigned int, i -> int |
158 | 10i -> 10 ints, 20s -> 20 character string, s -> null terminated string | |
159 | See: https://docs.python.org/2/library/struct.html#format-characters | |
fe8ab488 A |
160 | options: |
161 | -X : print all values in hex. | |
3e170ce0 | 162 | -O <file path>: Save data to file |
fe8ab488 A |
163 | """ |
164 | ||
165 | if not cmd_args or len(cmd_args) < 3: | |
166 | raise ArgumentError("Insufficient arguments") | |
167 | task = kern.GetValueFromAddress(cmd_args[0], 'task *') | |
168 | uspace_addr = ArgumentStringToInt(cmd_args[1]) | |
169 | format_specifier_str = cmd_args[2] | |
170 | user_data_len = 0 | |
3e170ce0 A |
171 | if format_specifier_str == "s": |
172 | print "0x%x: " % uspace_addr + GetUserspaceString(task, uspace_addr) | |
173 | return True | |
174 | ||
fe8ab488 A |
175 | try: |
176 | user_data_len = struct.calcsize(format_specifier_str) | |
177 | except Exception, e: | |
178 | raise ArgumentError("Invalid format specifier provided.") | |
179 | ||
180 | user_data_string = GetUserDataAsString(task, uspace_addr, user_data_len) | |
181 | if not user_data_string: | |
182 | print "Could not read any data from userspace address." | |
183 | return False | |
3e170ce0 A |
184 | if "-O" in cmd_options: |
185 | fh = open(cmd_options["-O"],"w") | |
186 | fh.write(user_data_string) | |
187 | fh.close() | |
188 | print "Written %d bytes to %s." % (user_data_len, cmd_options['-O']) | |
189 | return True | |
fe8ab488 | 190 | upacked_data = struct.unpack(format_specifier_str, user_data_string) |
3e170ce0 | 191 | element_size = user_data_len / len(upacked_data) |
fe8ab488 A |
192 | for i in range(len(upacked_data)): |
193 | if "-X" in cmd_options: | |
3e170ce0 | 194 | print "0x%x: " % (uspace_addr + i*element_size) + hex(upacked_data[i]) |
fe8ab488 | 195 | else: |
3e170ce0 | 196 | print "0x%x: " % (uspace_addr + i*element_size) + str(upacked_data[i]) |
fe8ab488 | 197 | |
39236c6e A |
198 | return True |
199 | ||
3e170ce0 A |
200 | @lldb_command('showtaskuserargs') |
201 | def ShowTaskUserArgs(cmd_args=None, cmd_options={}): | |
202 | """ Read the process argv, env, and apple strings from the user stack | |
203 | Syntax: (lldb) showtaskuserargs <task_t> | |
204 | params: | |
205 | <task_t> : pointer to task | |
206 | """ | |
207 | if not cmd_args or len(cmd_args) != 1: | |
208 | raise ArgumentError("Insufficient arguments") | |
209 | ||
210 | task = kern.GetValueFromAddress(cmd_args[0], 'task *') | |
211 | proc = Cast(task.bsd_info, 'proc *') | |
d9a64523 | 212 | ptrsize = 8 if int(task.t_flags) & 0x1 else 4 |
3e170ce0 | 213 | |
d9a64523 | 214 | format_string = "Q" if ptrsize == 8 else "I" |
3e170ce0 A |
215 | |
216 | string_area_size = proc.p_argslen | |
217 | string_area_addr = proc.user_stack - string_area_size | |
218 | ||
219 | string_area = GetUserDataAsString(task, string_area_addr, string_area_size) | |
220 | if not string_area: | |
221 | print "Could not read any data from userspace address." | |
222 | return False | |
223 | ||
224 | i = 0 | |
d9a64523 | 225 | pos = string_area_addr - ptrsize |
3e170ce0 A |
226 | |
227 | for name in ["apple", "env", "argv"] : | |
228 | while True: | |
229 | if name == "argv" : | |
230 | if i == proc.p_argc: | |
231 | break | |
232 | i += 1 | |
233 | ||
d9a64523 | 234 | pos -= ptrsize |
3e170ce0 | 235 | |
d9a64523 | 236 | user_data_string = GetUserDataAsString(task, pos, ptrsize) |
3e170ce0 A |
237 | ptr = struct.unpack(format_string, user_data_string)[0] |
238 | ||
239 | if ptr == 0: | |
240 | break | |
241 | ||
242 | if string_area_addr <= ptr and ptr < string_area_addr+string_area_size : | |
243 | string_offset = ptr - string_area_addr | |
244 | string = string_area[string_offset:]; | |
245 | else: | |
246 | string = GetUserspaceString(task, ptr) | |
247 | ||
248 | print name + "[]: " + string | |
249 | ||
250 | return True | |
251 | ||
d190cdc3 | 252 | def ShowTaskUserStacks(task): |
39236c6e A |
253 | #print GetTaskSummary.header + " " + GetProcSummary.header |
254 | pval = Cast(task.bsd_info, 'proc *') | |
255 | #print GetTaskSummary(task) + " " + GetProcSummary(pval) + "\n \n" | |
256 | crash_report_format_string = """\ | |
d190cdc3 | 257 | Process: {pname:s} [{pid:d}] |
39236c6e | 258 | Path: {path: <50s} |
fe8ab488 | 259 | Identifier: {pname: <30s} |
39236c6e A |
260 | Version: ??? (???) |
261 | Code Type: {parch: <20s} | |
d190cdc3 | 262 | Parent Process: {ppname:s} [{ppid:d}] |
39236c6e A |
263 | |
264 | Date/Time: {timest:s}.000 -0800 | |
265 | OS Version: {osversion: <20s} | |
266 | Report Version: 8 | |
267 | ||
268 | Exception Type: n/a | |
269 | Exception Codes: n/a | |
270 | Crashed Thread: 0 | |
271 | ||
272 | Application Specific Information: | |
273 | Synthetic crash log generated from Kernel userstacks | |
274 | ||
275 | """ | |
276 | user_lib_rex = re.compile("([0-9a-fx]+)\s-\s([0-9a-fx]+)\s+(.*?)\s", re.IGNORECASE|re.MULTILINE) | |
277 | from datetime import datetime | |
5ba3f43e A |
278 | if pval: |
279 | ts = datetime.fromtimestamp(int(pval.p_start.tv_sec)) | |
280 | date_string = ts.strftime('%Y-%m-%d %H:%M:%S') | |
281 | else: | |
282 | date_string = "none" | |
283 | is_64 = True | |
284 | if pval and (pval.p_flag & 0x4) == 0 : | |
285 | is_64 = False | |
fe8ab488 | 286 | |
39236c6e A |
287 | parch_s = "" |
288 | if kern.arch == "x86_64" or kern.arch == "i386": | |
289 | osversion = "Mac OS X 10.8" | |
290 | parch_s = "I386 (32 bit)" | |
291 | if is_64: | |
292 | parch_s = "X86-64 (Native)" | |
293 | else: | |
294 | parch_s = kern.arch | |
295 | osversion = "iOS" | |
296 | osversion += " ({:s})".format(kern.globals.osversion) | |
5ba3f43e A |
297 | if pval: |
298 | pid = pval.p_pid | |
299 | pname = pval.p_comm | |
300 | path = pval.p_comm | |
301 | ppid = pval.p_ppid | |
302 | else: | |
303 | pid = 0 | |
304 | pname = "unknown" | |
305 | path = "unknown" | |
306 | ppid = 0 | |
307 | ||
308 | print crash_report_format_string.format(pid = pid, | |
309 | pname = pname, | |
310 | path = path, | |
311 | ppid = ppid, | |
312 | ppname = GetProcNameForPid(ppid), | |
39236c6e A |
313 | timest = date_string, |
314 | parch = parch_s, | |
315 | osversion = osversion | |
39236c6e A |
316 | ) |
317 | print "Binary Images:" | |
318 | ShowTaskUserLibraries([hex(task)]) | |
319 | usertask_lib_info = [] # will host [startaddr, endaddr, lib_name] entries | |
320 | for entry in ShowTaskUserLibraries.found_images: | |
321 | #print "processing line %s" % line | |
322 | arr = user_lib_rex.findall(entry[3]) | |
323 | #print "%r" % arr | |
324 | if len(arr) == 0 : | |
325 | continue | |
326 | usertask_lib_info.append([int(arr[0][0],16), int(arr[0][1],16), str(arr[0][2]).strip()]) | |
327 | ||
328 | printthread_user_stack_ptr = ShowX86UserStack | |
329 | if kern.arch == "arm": | |
330 | printthread_user_stack_ptr = ShowARMUserStack | |
5ba3f43e | 331 | elif kern.arch.startswith("arm64"): |
fe8ab488 A |
332 | printthread_user_stack_ptr = ShowARM64UserStack |
333 | ||
39236c6e A |
334 | counter = 0 |
335 | for thval in IterateQueue(task.threads, 'thread *', 'task_threads'): | |
336 | print "\nThread {0:d} name:0x{1:x}\nThread {0:d}:".format(counter, thval) | |
337 | counter += 1 | |
338 | try: | |
339 | printthread_user_stack_ptr(thval, usertask_lib_info) | |
340 | except Exception as exc_err: | |
341 | print "Failed to show user stack for thread 0x{0:x}".format(thval) | |
342 | if config['debug']: | |
343 | raise exc_err | |
344 | else: | |
345 | print "Enable debugging ('(lldb) xnudebug debug') to see detailed trace." | |
346 | return | |
347 | ||
d190cdc3 A |
348 | @lldb_command('showtaskuserstacks', "P:F:") |
349 | def ShowTaskUserStacksCmdHelper(cmd_args=None, cmd_options={}): | |
350 | """ Print out the user stack for each thread in a task, followed by the user libraries. | |
351 | Syntax: (lldb) showtaskuserstacks <task_t> | |
352 | or: (lldb) showtaskuserstacks -P <pid> | |
353 | or: (lldb) showtaskuserstacks -F <task_name> | |
354 | The format is compatible with CrashTracer. You can also use the speedtracer plugin as follows | |
355 | (lldb) showtaskuserstacks <task_t> -p speedtracer | |
356 | ||
357 | Note: the address ranges are approximations. Also the list may not be completely accurate. This command expects memory read failures | |
358 | and hence will skip a library if unable to read information. Please use your good judgement and not take the output as accurate | |
359 | """ | |
360 | task_list = [] | |
361 | if "-F" in cmd_options: | |
362 | task_list = FindTasksByName(cmd_options["-F"]) | |
363 | elif "-P" in cmd_options: | |
364 | pidval = ArgumentStringToInt(cmd_options["-P"]) | |
365 | for t in kern.tasks: | |
366 | pval = Cast(t.bsd_info, 'proc *') | |
367 | if pval and pval.p_pid == pidval: | |
368 | task_list.append(t) | |
369 | break | |
370 | elif cmd_args: | |
371 | t = kern.GetValueFromAddress(cmd_args[0], 'task *') | |
372 | task_list.append(t) | |
373 | else: | |
374 | raise ArgumentError("Insufficient arguments") | |
375 | ||
376 | for task in task_list: | |
377 | ShowTaskUserStacks(task) | |
39236c6e A |
378 | |
379 | def GetUserDataAsString(task, addr, size): | |
380 | """ Get data from task's address space as a string of bytes | |
381 | params: | |
382 | task: task object from which to extract information | |
383 | addr: int - start address to get data from. | |
384 | size: int - no of bytes to read. | |
385 | returns: | |
386 | str - a stream of bytes. Empty string if read fails. | |
387 | """ | |
388 | err = lldb.SBError() | |
389 | if GetConnectionProtocol() == "kdp": | |
390 | kdp_pmap_addr = unsigned(addressof(kern.globals.kdp_pmap)) | |
391 | if not WriteInt64ToMemoryAddress(unsigned(task.map.pmap), kdp_pmap_addr): | |
392 | debuglog("Failed to write in kdp_pmap from GetUserDataAsString.") | |
393 | return "" | |
394 | content = LazyTarget.GetProcess().ReadMemory(addr, size, err) | |
395 | if not err.Success(): | |
396 | debuglog("Failed to read process memory. Error: " + err.description) | |
397 | return "" | |
398 | if not WriteInt64ToMemoryAddress(0, kdp_pmap_addr): | |
399 | debuglog("Failed to reset in kdp_pmap from GetUserDataAsString.") | |
400 | return "" | |
5ba3f43e | 401 | elif (kern.arch == 'x86_64' or kern.arch.startswith('arm')) and (long(size) < (2 * kern.globals.page_size)): |
39236c6e A |
402 | # Without the benefit of a KDP stub on the target, try to |
403 | # find the user task's physical mapping and memcpy the data. | |
404 | # If it straddles a page boundary, copy in two passes | |
405 | range1_addr = long(addr) | |
406 | range1_size = long(size) | |
407 | if kern.StraddlesPage(range1_addr, range1_size): | |
408 | range2_addr = long(kern.TruncPage(range1_addr + range1_size)) | |
409 | range2_size = long(range1_addr + range1_size - range2_addr) | |
410 | range1_size = long(range2_addr - range1_addr) | |
411 | else: | |
412 | range2_addr = 0 | |
413 | range2_size = 0 | |
414 | range2_in_kva = 0 | |
415 | ||
416 | paddr_range1 = PmapWalk(task.map.pmap, range1_addr, vSILENT) | |
417 | if not paddr_range1: | |
418 | debuglog("Not mapped task 0x{:x} address 0x{:x}".format(task, addr)) | |
419 | return "" | |
420 | ||
421 | range1_in_kva = kern.PhysToKernelVirt(paddr_range1) | |
422 | content = LazyTarget.GetProcess().ReadMemory(range1_in_kva, range1_size, err) | |
423 | if not err.Success(): | |
424 | raise RuntimeError("Failed to read process memory. Error: " + err.description) | |
425 | ||
426 | if range2_addr: | |
427 | paddr_range2 = PmapWalk(task.map.pmap, range2_addr, vSILENT) | |
428 | if not paddr_range2: | |
429 | debuglog("Not mapped task 0x{:x} address 0x{:x}".format(task, addr)) | |
430 | return "" | |
431 | range2_in_kva = kern.PhysToKernelVirt(paddr_range2) | |
fe8ab488 | 432 | content += LazyTarget.GetProcess().ReadMemory(range2_in_kva, range2_size, err) |
39236c6e A |
433 | if not err.Success(): |
434 | raise RuntimeError("Failed to read process memory. Error: " + err.description) | |
435 | else: | |
436 | raise NotImplementedError("GetUserDataAsString does not support this configuration") | |
437 | ||
438 | return content | |
439 | ||
440 | def _ExtractDataFromString(strdata, offset, data_type, length=0): | |
441 | """ Extract specific data from string buffer | |
442 | params: | |
443 | strdata: str - string data give from GetUserDataAsString | |
444 | offset: int - 0 based offset into the data. | |
445 | data_type: str - defines what type to be read as. Supported values are: | |
446 | 'uint64_t', 'uint32_t', 'string' | |
447 | length: int - used when data_type=='string' | |
448 | returns | |
449 | None - if extraction failed. | |
450 | obj - based on what is requested in data_type | |
451 | """ | |
452 | unpack_str = "s" | |
453 | if data_type == 'uint64_t': | |
454 | length = 8 | |
455 | unpack_str = "Q" | |
456 | elif data_type == "uint32_t": | |
457 | length = 4 | |
458 | unpack_str = "I" | |
459 | else: | |
460 | unpack_str= "%ds" % length | |
461 | ||
462 | data_len = len(strdata) | |
463 | if offset > data_len or (offset + length) > data_len or offset < 0: | |
464 | debuglog("Invalid arguments to _ExtractDataFromString.") | |
465 | return 0 | |
466 | return struct.unpack(unpack_str, strdata[offset:(offset + length)])[0] | |
467 | ||
3e170ce0 | 468 | def GetUserspaceString(task, string_address): |
fe8ab488 | 469 | """ Maps 32 bytes at a time and packs as string |
39236c6e A |
470 | params: |
471 | task: obj - referencing task to read data from | |
3e170ce0 | 472 | string_address: int - address where the image path is stored |
39236c6e A |
473 | returns: |
474 | str - string path of the file. "" if failed to read. | |
475 | """ | |
476 | done = False | |
477 | retval = "" | |
478 | ||
3e170ce0 | 479 | if string_address == 0: |
39236c6e A |
480 | done = True |
481 | ||
482 | while not done: | |
3e170ce0 A |
483 | str_data = GetUserDataAsString(task, string_address, 32) |
484 | if len(str_data) == 0: | |
39236c6e A |
485 | break |
486 | i = 0 | |
487 | while i < 32: | |
3e170ce0 A |
488 | if ord(str_data[i]): |
489 | retval += str_data[i] | |
39236c6e A |
490 | else: |
491 | break | |
492 | i += 1 | |
493 | if i < 32: | |
494 | done = True | |
495 | else: | |
3e170ce0 | 496 | string_address += 32 |
39236c6e A |
497 | return retval |
498 | ||
499 | def GetImageInfo(task, mh_image_address, mh_path_address, approx_end_address=None): | |
500 | """ Print user library informaiton. | |
501 | params: | |
502 | task : obj referencing the task for which Image info printed | |
503 | mh_image_address : int - address which has image info | |
504 | mh_path_address : int - address which holds path name string | |
505 | approx_end_address: int - address which lldbmacros think is end address. | |
506 | returns: | |
507 | str - string representing image info. "" if failure to read data. | |
508 | """ | |
509 | if approx_end_address: | |
510 | image_end_load_address = int(approx_end_address) -1 | |
511 | else: | |
512 | image_end_load_address = int(mh_image_address) + 0xffffffff | |
513 | ||
514 | print_format = "0x{0:x} - 0x{1:x} {2: <50s} (??? - ???) <{3: <36s}> {4: <50s}" | |
515 | # 32 bytes enough for mach_header/mach_header_64 | |
516 | mh_data = GetUserDataAsString(task, mh_image_address, 32) | |
517 | if len(mh_data) == 0: | |
518 | debuglog("unable to get userdata for task 0x{:x} img_addr 0x{:x} path_address 0x{:x}".format( | |
519 | task, mh_image_address, mh_path_address)) | |
520 | return "" | |
521 | mh_magic = _ExtractDataFromString(mh_data, (4 * 0), "uint32_t") | |
522 | mh_cputype = _ExtractDataFromString(mh_data,(4 * 1), "uint32_t") | |
523 | mh_cpusubtype = _ExtractDataFromString(mh_data,(4 * 2), "uint32_t") | |
524 | mh_filetype = _ExtractDataFromString(mh_data,(4 * 3), "uint32_t") | |
525 | mh_ncmds = _ExtractDataFromString(mh_data,(4 * 4), "uint32_t") | |
526 | mh_sizeofcmds = _ExtractDataFromString(mh_data,(4 * 5), "uint32_t") | |
527 | mh_flags = _ExtractDataFromString(mh_data,(4 * 6), "uint32_t") | |
528 | ||
529 | if mh_magic == 0xfeedfacf: | |
530 | mh_64 = True | |
531 | lc_address = mh_image_address + 32 | |
532 | else: | |
533 | mh_64 = False | |
534 | lc_address = mh_image_address + 28 | |
535 | ||
536 | lc_idx = 0 | |
537 | uuid_data = 0 | |
538 | found_uuid_data = False | |
539 | retval = None | |
540 | while lc_idx < mh_ncmds: | |
541 | # 24 bytes is the size of uuid_command | |
fe8ab488 | 542 | lcmd_data = GetUserDataAsString(task, lc_address, 24) |
39236c6e A |
543 | lc_cmd = _ExtractDataFromString(lcmd_data, 4 * 0, "uint32_t") |
544 | lc_cmd_size = _ExtractDataFromString(lcmd_data, 4 * 1, "uint32_t") | |
545 | lc_data = _ExtractDataFromString(lcmd_data, 4*2, "string", 16) | |
546 | ||
547 | uuid_out_string = "" | |
548 | path_out_string = "" | |
549 | ||
550 | if lc_cmd == 0x1b: | |
551 | # need to print the uuid now. | |
552 | uuid_data = [ord(x) for x in lc_data] | |
553 | found_uuid_data = True | |
fe8ab488 | 554 | uuid_out_string = "{a[0]:02X}{a[1]:02X}{a[2]:02X}{a[3]:02X}-{a[4]:02X}{a[5]:02X}-{a[6]:02X}{a[7]:02X}-{a[8]:02X}{a[9]:02X}-{a[10]:02X}{a[11]:02X}{a[12]:02X}{a[13]:02X}{a[14]:02X}{a[15]:02X}".format(a=uuid_data) |
39236c6e | 555 | #also print image path |
3e170ce0 | 556 | path_out_string = GetUserspaceString(task, mh_path_address) |
39236c6e A |
557 | path_base_name = path_out_string.split("/")[-1] |
558 | retval = print_format.format(mh_image_address, image_end_load_address, path_base_name, uuid_out_string, path_out_string) | |
559 | elif lc_cmd == 0xe: | |
560 | ShowTaskUserLibraries.exec_load_path = lc_address + _ExtractDataFromString(lcmd_data, 4*2, "uint32_t") | |
fe8ab488 | 561 | debuglog("Found load command to be 0xe for address %s" % hex(ShowTaskUserLibraries.exec_load_path)) |
39236c6e A |
562 | lc_address = lc_address + lc_cmd_size |
563 | lc_idx += 1 | |
564 | ||
565 | if not found_uuid_data: | |
3e170ce0 | 566 | path_out_string = GetUserspaceString(task, mh_path_address) |
39236c6e A |
567 | path_base_name = path_out_string.split("/")[-1] |
568 | uuid_out_string = "" | |
569 | ||
570 | retval = print_format.format(mh_image_address, image_end_load_address, path_base_name, uuid_out_string, path_out_string) | |
571 | return retval | |
572 | ||
573 | @static_var("found_images", []) # holds entries of format (startaddr, endaddr, image_path_addr, infostring) | |
574 | @static_var("exec_load_path", 0) | |
575 | @lldb_command("showtaskuserlibraries") | |
576 | def ShowTaskUserLibraries(cmd_args=None): | |
577 | """ Show binary images known by dyld in target task | |
578 | For a given user task, inspect the dyld shared library state and print information about all Mach-O images. | |
579 | Syntax: (lldb)showtaskuserlibraries <task_t> | |
580 | Note: the address ranges are approximations. Also the list may not be completely accurate. This command expects memory read failures | |
581 | and hence will skip a library if unable to read information. Please use your good judgement and not take the output as accurate | |
582 | """ | |
583 | if not cmd_args: | |
584 | raise ArgumentError("Insufficient arguments") | |
585 | ||
586 | #reset the found_images array | |
587 | ShowTaskUserLibraries.found_images = [] | |
588 | ||
589 | task = kern.GetValueFromAddress(cmd_args[0], 'task_t') | |
590 | is_task_64 = int(task.t_flags) & 0x1 | |
591 | dyld_all_image_infos_address = unsigned(task.all_image_info_addr) | |
fe8ab488 A |
592 | debuglog("dyld_all_image_infos_address = %s" % hex(dyld_all_image_infos_address)) |
593 | ||
39236c6e A |
594 | cur_data_offset = 0 |
595 | if dyld_all_image_infos_address == 0: | |
596 | print "No dyld shared library information available for task" | |
597 | return False | |
fe8ab488 A |
598 | |
599 | debuglog("Extracting version information.") | |
39236c6e A |
600 | vers_info_data = GetUserDataAsString(task, dyld_all_image_infos_address, 112) |
601 | version = _ExtractDataFromString(vers_info_data, cur_data_offset, "uint32_t") | |
602 | cur_data_offset += 4 | |
fe8ab488 | 603 | if version > 14: |
39236c6e A |
604 | print "Unknown dyld all_image_infos version number %d" % version |
605 | image_info_count = _ExtractDataFromString(vers_info_data, cur_data_offset, "uint32_t") | |
fe8ab488 A |
606 | debuglog("version = %d count = %d is_task_64 = %s" % (version, image_info_count, repr(is_task_64))) |
607 | ||
39236c6e A |
608 | ShowTaskUserLibraries.exec_load_path = 0 |
609 | if is_task_64: | |
610 | image_info_size = 24 | |
611 | image_info_array_address = _ExtractDataFromString(vers_info_data, 8, "uint64_t") | |
612 | dyld_load_address = _ExtractDataFromString(vers_info_data, 8*4, "uint64_t") | |
613 | dyld_all_image_infos_address_from_struct = _ExtractDataFromString(vers_info_data, 8*13, "uint64_t") | |
614 | else: | |
615 | image_info_size = 12 | |
616 | image_info_array_address = _ExtractDataFromString(vers_info_data, 4*2, "uint32_t") | |
617 | dyld_load_address = _ExtractDataFromString(vers_info_data, 4*5, "uint32_t") | |
618 | dyld_all_image_infos_address_from_struct = _ExtractDataFromString(vers_info_data, 4*14, "uint32_t") | |
619 | # Account for ASLR slide before dyld can fix the structure | |
620 | dyld_load_address = dyld_load_address + (dyld_all_image_infos_address - dyld_all_image_infos_address_from_struct) | |
621 | ||
622 | i = 0 | |
623 | image_info_list = [] | |
624 | while i < image_info_count: | |
625 | image_info_address = image_info_array_address + i * image_info_size | |
fe8ab488 | 626 | debuglog("i = %d, image_info_address = %s, image_info_size = %d" % (i, hex(image_info_address), image_info_size)) |
39236c6e A |
627 | n_im_info_addr = None |
628 | img_data = "" | |
629 | try: | |
630 | img_data = GetUserDataAsString(task, image_info_address, image_info_size) | |
631 | except Exception, e: | |
632 | debuglog("Failed to read user data for task 0x{:x} addr 0x{:x}, exception {:s}".format(task, image_info_address, str(e))) | |
633 | pass | |
fe8ab488 | 634 | |
39236c6e A |
635 | if is_task_64: |
636 | image_info_addr = _ExtractDataFromString(img_data, 0, "uint64_t") | |
637 | image_info_path = _ExtractDataFromString(img_data, 8, "uint64_t") | |
638 | else: | |
639 | image_info_addr = _ExtractDataFromString(img_data, 0, "uint32_t") | |
640 | image_info_path = _ExtractDataFromString(img_data, 4, "uint32_t") | |
fe8ab488 | 641 | |
39236c6e | 642 | if image_info_addr : |
fe8ab488 | 643 | debuglog("Found image: image_info_addr = %s, image_info_path= %s" % (hex(image_info_addr), hex(image_info_path))) |
39236c6e A |
644 | image_info_list.append((image_info_addr, image_info_path)) |
645 | i += 1 | |
fe8ab488 | 646 | |
39236c6e | 647 | image_info_list.sort() |
fe8ab488 A |
648 | num_images_found = len(image_info_list) |
649 | ||
39236c6e A |
650 | for ii in range(num_images_found): |
651 | n_im_info_addr = dyld_load_address | |
652 | if ii + 1 < num_images_found: | |
653 | n_im_info_addr = image_info_list[ii+1][0] | |
654 | ||
655 | image_info_addr = image_info_list[ii][0] | |
656 | image_info_path = image_info_list[ii][1] | |
657 | try: | |
658 | image_print_s = GetImageInfo(task, image_info_addr, image_info_path, approx_end_address=n_im_info_addr) | |
659 | if len(image_print_s) > 0: | |
660 | print image_print_s | |
661 | ShowTaskUserLibraries.found_images.append((image_info_addr, n_im_info_addr, image_info_path, image_print_s)) | |
662 | else: | |
663 | debuglog("Failed to print image info for task 0x{:x} image_info 0x{:x}".format(task, image_info_addr)) | |
664 | except Exception,e: | |
665 | if config['debug']: | |
666 | raise e | |
fe8ab488 A |
667 | |
668 | # load_path might get set when the main executable is processed. | |
669 | if ShowTaskUserLibraries.exec_load_path != 0: | |
670 | debuglog("main executable load_path is set.") | |
39236c6e A |
671 | image_print_s = GetImageInfo(task, dyld_load_address, ShowTaskUserLibraries.exec_load_path) |
672 | if len(image_print_s) > 0: | |
673 | print image_print_s | |
674 | ShowTaskUserLibraries.found_images.append((dyld_load_address, dyld_load_address + 0xffffffff, | |
675 | ShowTaskUserLibraries.exec_load_path, image_print_s)) | |
676 | else: | |
677 | debuglog("Failed to print image for main executable for task 0x{:x} dyld_load_addr 0x{:x}".format(task, dyld_load_address)) | |
fe8ab488 A |
678 | else: |
679 | debuglog("Falling back to vm entry method for finding executable load address") | |
680 | print "# NOTE: Failed to find executable using all_image_infos. Using fuzzy match to find best possible load address for executable." | |
681 | ShowTaskLoadInfo([cmd_args[0]]) | |
39236c6e A |
682 | return |
683 | ||
fe8ab488 A |
684 | @lldb_command("showtaskuserdyldinfo") |
685 | def ShowTaskUserDyldInfo(cmd_args=None): | |
686 | """ Inspect the dyld global info for the given user task & print out all fields including error messages | |
687 | Syntax: (lldb)showtaskuserdyldinfo <task_t> | |
688 | """ | |
689 | if cmd_args == None or len(cmd_args) < 1: | |
690 | print "No arguments passed" | |
691 | print ShowTaskUserDyldInfo.__doc__.strip() | |
692 | return | |
693 | ||
694 | out_str = "" | |
695 | task = kern.GetValueFromAddress(cmd_args[0], 'task_t') | |
696 | is_task_64 = int(task.t_flags) & 0x1 | |
697 | dyld_all_image_infos_address = unsigned(task.all_image_info_addr) | |
698 | if dyld_all_image_infos_address == 0: | |
699 | print "No dyld shared library information available for task" | |
700 | return False | |
701 | vers_info_data = GetUserDataAsString(task, dyld_all_image_infos_address, 112) | |
702 | dyld_all_image_infos_version = _ExtractDataFromString(vers_info_data, 0, "uint32_t") | |
703 | if dyld_all_image_infos_version > 14: | |
704 | out_str += "Unknown dyld all_image_infos version number %d" % dyld_all_image_infos_version | |
705 | ||
706 | # Find fields by byte offset. We assume at least version 9 is supported | |
707 | if is_task_64: | |
708 | dyld_all_image_infos_infoArrayCount = _ExtractDataFromString(vers_info_data, 4, "uint32_t") | |
709 | dyld_all_image_infos_infoArray = _ExtractDataFromString(vers_info_data, 8, "uint64_t") | |
710 | dyld_all_image_infos_notification = _ExtractDataFromString(vers_info_data, 16, "uint64_t") | |
711 | dyld_all_image_infos_processDetachedFromSharedRegion = _ExtractDataFromString(vers_info_data, 24, "string") | |
712 | dyld_all_image_infos_libSystemInitialized = _ExtractDataFromString(vers_info_data, 25, "string") | |
713 | dyld_all_image_infos_dyldImageLoadAddress = _ExtractDataFromString(vers_info_data, 32, "uint64_t") | |
714 | dyld_all_image_infos_jitInfo = _ExtractDataFromString(vers_info_data, 40, "uint64_t") | |
715 | dyld_all_image_infos_dyldVersion = _ExtractDataFromString(vers_info_data, 48, "uint64_t") | |
716 | dyld_all_image_infos_errorMessage = _ExtractDataFromString(vers_info_data, 56, "uint64_t") | |
717 | dyld_all_image_infos_terminationFlags = _ExtractDataFromString(vers_info_data, 64, "uint64_t") | |
718 | dyld_all_image_infos_coreSymbolicationShmPage = _ExtractDataFromString(vers_info_data, 72, "uint64_t") | |
719 | dyld_all_image_infos_systemOrderFlag = _ExtractDataFromString(vers_info_data, 80, "uint64_t") | |
720 | dyld_all_image_infos_uuidArrayCount = _ExtractDataFromString(vers_info_data, 88, "uint64_t") | |
721 | dyld_all_image_infos_uuidArray = _ExtractDataFromString(vers_info_data, 96, "uint64_t") | |
722 | dyld_all_image_infos_dyldAllImageInfosAddress = _ExtractDataFromString(vers_info_data, 104, "uint64_t") | |
723 | else: | |
724 | dyld_all_image_infos_infoArrayCount = _ExtractDataFromString(vers_info_data, 4, "uint32_t") | |
725 | dyld_all_image_infos_infoArray = _ExtractDataFromString(vers_info_data, 8, "uint32_t") | |
726 | dyld_all_image_infos_notification = _ExtractDataFromString(vers_info_data, 12, "uint32_t") | |
727 | dyld_all_image_infos_processDetachedFromSharedRegion = _ExtractDataFromString(vers_info_data, 16, "string") | |
728 | dyld_all_image_infos_libSystemInitialized = _ExtractDataFromString(vers_info_data, 17, "string") | |
729 | dyld_all_image_infos_dyldImageLoadAddress = _ExtractDataFromString(vers_info_data, 20, "uint32_t") | |
730 | dyld_all_image_infos_jitInfo = _ExtractDataFromString(vers_info_data, 24, "uint32_t") | |
731 | dyld_all_image_infos_dyldVersion = _ExtractDataFromString(vers_info_data, 28, "uint32_t") | |
732 | dyld_all_image_infos_errorMessage = _ExtractDataFromString(vers_info_data, 32, "uint32_t") | |
733 | dyld_all_image_infos_terminationFlags = _ExtractDataFromString(vers_info_data, 36, "uint32_t") | |
734 | dyld_all_image_infos_coreSymbolicationShmPage = _ExtractDataFromString(vers_info_data, 40, "uint32_t") | |
735 | dyld_all_image_infos_systemOrderFlag = _ExtractDataFromString(vers_info_data, 44, "uint32_t") | |
736 | dyld_all_image_infos_uuidArrayCount = _ExtractDataFromString(vers_info_data, 48, "uint32_t") | |
737 | dyld_all_image_infos_uuidArray = _ExtractDataFromString(vers_info_data, 52, "uint32_t") | |
738 | dyld_all_image_infos_dyldAllImageInfosAddress = _ExtractDataFromString(vers_info_data, 56, "uint32_t") | |
739 | ||
740 | dyld_all_imfo_infos_slide = (dyld_all_image_infos_address - dyld_all_image_infos_dyldAllImageInfosAddress) | |
741 | dyld_all_image_infos_dyldVersion_postslide = (dyld_all_image_infos_dyldVersion + dyld_all_imfo_infos_slide) | |
742 | ||
3e170ce0 | 743 | path_out = GetUserspaceString(task, dyld_all_image_infos_dyldVersion_postslide) |
fe8ab488 A |
744 | out_str += "[dyld-{:s}]\n".format(path_out) |
745 | out_str += "version \t\t\t\t: {:d}\n".format(dyld_all_image_infos_version) | |
746 | out_str += "infoArrayCount \t\t\t\t: {:d}\n".format(dyld_all_image_infos_infoArrayCount) | |
747 | out_str += "infoArray \t\t\t\t: {:#x}\n".format(dyld_all_image_infos_infoArray) | |
748 | out_str += "notification \t\t\t\t: {:#x}\n".format(dyld_all_image_infos_notification) | |
749 | ||
750 | out_str += "processDetachedFromSharedRegion \t: " | |
751 | if dyld_all_image_infos_processDetachedFromSharedRegion != "": | |
752 | out_str += "TRUE\n".format(dyld_all_image_infos_processDetachedFromSharedRegion) | |
753 | else: | |
754 | out_str += "FALSE\n" | |
755 | ||
756 | out_str += "libSystemInitialized \t\t\t: " | |
757 | if dyld_all_image_infos_libSystemInitialized != "": | |
758 | out_str += "TRUE\n".format(dyld_all_image_infos_libSystemInitialized) | |
759 | else: | |
760 | out_str += "FALSE\n" | |
761 | ||
762 | out_str += "dyldImageLoadAddress \t\t\t: {:#x}\n".format(dyld_all_image_infos_dyldImageLoadAddress) | |
763 | out_str += "jitInfo \t\t\t\t: {:#x}\n".format(dyld_all_image_infos_jitInfo) | |
764 | out_str += "\ndyldVersion \t\t\t\t: {:#x}".format(dyld_all_image_infos_dyldVersion) | |
765 | if (dyld_all_imfo_infos_slide != 0): | |
766 | out_str += " (currently {:#x})\n".format(dyld_all_image_infos_dyldVersion_postslide) | |
767 | else: | |
768 | out_str += "\n" | |
769 | ||
770 | out_str += "errorMessage \t\t\t\t: {:#x}\n".format(dyld_all_image_infos_errorMessage) | |
771 | if dyld_all_image_infos_errorMessage != 0: | |
3e170ce0 | 772 | out_str += GetUserspaceString(task, dyld_all_image_infos_errorMessage) |
fe8ab488 A |
773 | |
774 | out_str += "terminationFlags \t\t\t: {:#x}\n".format(dyld_all_image_infos_terminationFlags) | |
775 | out_str += "coreSymbolicationShmPage \t\t: {:#x}\n".format(dyld_all_image_infos_coreSymbolicationShmPage) | |
776 | out_str += "systemOrderFlag \t\t\t: {:#x}\n".format(dyld_all_image_infos_systemOrderFlag) | |
777 | out_str += "uuidArrayCount \t\t\t\t: {:#x}\n".format(dyld_all_image_infos_uuidArrayCount) | |
778 | out_str += "uuidArray \t\t\t\t: {:#x}\n".format(dyld_all_image_infos_uuidArray) | |
779 | out_str += "dyldAllImageInfosAddress \t\t: {:#x}".format(dyld_all_image_infos_dyldAllImageInfosAddress) | |
780 | if (dyld_all_imfo_infos_slide != 0): | |
781 | out_str += " (currently {:#x})\n".format(dyld_all_image_infos_address) | |
782 | else: | |
783 | out_str += "\n" | |
784 | ||
785 | if is_task_64: | |
786 | dyld_all_image_infos_address = dyld_all_image_infos_address + 112 | |
787 | dyld_all_image_infos_v10 = GetUserDataAsString(task, dyld_all_image_infos_address, 64) | |
788 | dyld_all_image_infos_initialImageCount = _ExtractDataFromString(dyld_all_image_infos_v10, 112-112, "uint64_t") | |
789 | dyld_all_image_infos_errorKind = _ExtractDataFromString(dyld_all_image_infos_v10, 120-112, "uint64_t") | |
790 | dyld_all_image_infos_errorClientOfDylibPath = _ExtractDataFromString(dyld_all_image_infos_v10, 128-112, "uint64_t") | |
791 | dyld_all_image_infos_errorTargetDylibPath = _ExtractDataFromString(dyld_all_image_infos_v10, 136-112, "uint64_t") | |
792 | dyld_all_image_infos_errorSymbol = _ExtractDataFromString(dyld_all_image_infos_v10, 144-112, "uint64_t") | |
793 | dyld_all_image_infos_sharedCacheSlide = _ExtractDataFromString(dyld_all_image_infos_v10, 152-112, "uint64_t") | |
794 | dyld_all_image_infos_sharedCacheUUID = _ExtractDataFromString(dyld_all_image_infos_v10, 160-112, "string") | |
795 | else: | |
796 | dyld_all_image_infos_address = dyld_all_image_infos_address + 60 | |
797 | dyld_all_image_infos_v10 = GetUserDataAsString(task, dyld_all_image_infos_address, 40) | |
798 | dyld_all_image_infos_initialImageCount = _ExtractDataFromString(dyld_all_image_infos_v10, 60-60, "uint32_t") | |
799 | dyld_all_image_infos_errorKind = _ExtractDataFromString(dyld_all_image_infos_v10, 64-60, "uint32_t") | |
800 | dyld_all_image_infos_errorClientOfDylibPath = _ExtractDataFromString(dyld_all_image_infos_v10, 68-60, "uint32_t") | |
801 | dyld_all_image_infos_errorTargetDylibPath = _ExtractDataFromString(dyld_all_image_infos_v10, 72-60, "uint32_t") | |
802 | dyld_all_image_infos_errorSymbol = _ExtractDataFromString(dyld_all_image_infos_v10, 76-60, "uint32_t") | |
803 | dyld_all_image_infos_sharedCacheSlide = _ExtractDataFromString(dyld_all_image_infos_v10, 80-60, "uint32_t") | |
804 | dyld_all_image_infos_sharedCacheUUID = _ExtractDataFromString(dyld_all_image_infos_v10, 84-60, "string") | |
805 | ||
806 | if dyld_all_image_infos_version >= 10: | |
807 | out_str += "\ninitialImageCount \t\t\t: {:#x}\n".format(dyld_all_image_infos_initialImageCount) | |
808 | ||
809 | if dyld_all_image_infos_version >= 11: | |
810 | out_str += "errorKind \t\t\t\t: {:#x}\n".format(dyld_all_image_infos_errorKind) | |
811 | out_str += "errorClientOfDylibPath \t\t\t: {:#x}\n".format(dyld_all_image_infos_errorClientOfDylibPath) | |
812 | if dyld_all_image_infos_errorClientOfDylibPath != 0: | |
813 | out_str += "\t\t\t\t" | |
3e170ce0 | 814 | out_str += GetUserspaceString(task, dyld_all_image_infos_errorClientOfDylibPath) |
fe8ab488 A |
815 | out_str += "\n" |
816 | out_str += "errorTargetDylibPath \t\t\t: {:#x}\n".format(dyld_all_image_infos_errorTargetDylibPath) | |
817 | if dyld_all_image_infos_errorTargetDylibPath != 0: | |
818 | out_str += "\t\t\t\t" | |
3e170ce0 | 819 | out_str += GetUserspaceString(task, dyld_all_image_infos_errorTargetDylibPath) |
fe8ab488 A |
820 | out_str += "\n" |
821 | out_str += "errorSymbol \t\t\t\t: {:#x}\n".format(dyld_all_image_infos_errorSymbol) | |
822 | if dyld_all_image_infos_errorSymbol != 0: | |
823 | out_str += "\t\t\t\t" | |
3e170ce0 | 824 | out_str += GetUserspaceString(task, dyld_all_image_infos_errorSymbol) |
fe8ab488 A |
825 | out_str += "\n" |
826 | ||
827 | if dyld_all_image_infos_version >= 12: | |
828 | out_str += "sharedCacheSlide \t\t\t: {:#x}\n".format(dyld_all_image_infos_sharedCacheSlide) | |
829 | if dyld_all_image_infos_version >= 13 and dyld_all_image_infos_sharedCacheUUID != "": | |
830 | out_str += "sharedCacheUUID \t\t\t: {:s}\n".format(dyld_all_image_infos_sharedCacheUUID) | |
831 | else: | |
832 | out_str += "No dyld information available for task\n" | |
833 | print out_str | |
834 | ||
835 | # Macro: showosmalloc | |
836 | @lldb_type_summary(['OSMallocTag']) | |
837 | @header("{0: <20s} {1: >5s} {2: ^16s} {3: <5s} {4: <40s}".format("TAG", "COUNT", "STATE", "ATTR", "NAME")) | |
838 | def GetOSMallocTagSummary(malloc_tag): | |
839 | """ Summarize the given OSMalloc tag. | |
840 | params: | |
841 | malloc_tag : value - value representing a _OSMallocTag_ * in kernel | |
842 | returns: | |
843 | out_str - string summary of the OSMalloc tag. | |
844 | """ | |
845 | if not malloc_tag: | |
846 | return "Invalid malloc tag value: 0x0" | |
847 | ||
848 | out_str = "{: <#20x} {: >5d} {: ^#16x} {: <5d} {: <40s}\n".format(malloc_tag, | |
849 | malloc_tag.OSMT_refcnt, malloc_tag.OSMT_state, malloc_tag.OSMT_attr, malloc_tag.OSMT_name) | |
850 | return out_str | |
851 | ||
852 | @lldb_command('showosmalloc') | |
853 | def ShowOSMalloc(cmd_args=None): | |
854 | """ Print the outstanding allocation count of OSMalloc tags | |
855 | Usage: showosmalloc | |
856 | """ | |
857 | summary_str = "" | |
858 | tag_headp = Cast(addressof(kern.globals.OSMalloc_tag_list), 'struct _OSMallocTag_ *') | |
859 | tagp = Cast(tag_headp.OSMT_link.next, 'struct _OSMallocTag_ *') | |
860 | summary_str += GetOSMallocTagSummary.header + "\n" | |
861 | while tagp != tag_headp: | |
862 | summary_str += GetOSMallocTagSummary(tagp) | |
863 | tagp = Cast(tagp.OSMT_link.next, 'struct _OSMallocTag_ *') | |
864 | ||
865 | print summary_str | |
866 | ||
867 | # EndMacro: showosmalloc | |
868 | ||
cb323159 A |
869 | def SaveDataToFile(start_addr, length, outputfile, task=None,): |
870 | """ Save the data at the specified address (of the specified length) to the file. | |
871 | params: start_addr : start address of the region of memory to save | |
872 | length : length of the region of memory to save | |
873 | outputfile : file to save the data in | |
874 | task (optional) : task containing the memory region (if from user data) | |
875 | returns: True if we saved the requested data, False otherwise | |
876 | """ | |
877 | if task: | |
878 | memory_data = GetUserDataAsString(task, start_addr, length) | |
879 | else: | |
880 | data_ptr = kern.GetValueFromAddress(start_addr, 'uint8_t *') | |
881 | if data_ptr == 0: | |
882 | print "invalid kernel start address specified" | |
883 | return False | |
884 | memory_data = [] | |
885 | for i in range(length): | |
886 | memory_data.append(chr(data_ptr[i])) | |
887 | if i % 50000 == 0: | |
888 | print "%d of %d \r" % (i, length), | |
889 | memory_data = ''.join(memory_data) | |
890 | ||
891 | if len(memory_data) != length: | |
892 | print "Failed to read {:d} bytes from address {: <#020x}".format(length, start_addr) | |
893 | return False | |
894 | ||
895 | fh = open(outputfile, 'w') | |
896 | fh.write(memory_data) | |
897 | fh.close() | |
898 | print "Saved {:d} bytes to file {:s}".format(length, outputfile) | |
899 | return True | |
900 | ||
fe8ab488 | 901 | |
3e170ce0 A |
902 | @lldb_command('savekcdata', 'T:O:') |
903 | def SaveKCDataToFile(cmd_args=None, cmd_options={}): | |
904 | """ Save the data referred by the kcdata_descriptor structure. | |
905 | options: | |
906 | -T: <task_t> pointer to task if memory referenced is in userstask. | |
907 | -O: <output file path> path to file to save data. default: /tmp/kcdata.<timestamp>.bin | |
908 | Usage: (lldb) savekcdata <kcdata_descriptor_t> -T <task_t> -O /path/to/outputfile.bin | |
909 | """ | |
910 | if not cmd_args: | |
911 | raise ArgumentError('Please provide the kcdata descriptor.') | |
912 | ||
913 | kcdata = kern.GetValueFromAddress(cmd_args[0], 'kcdata_descriptor_t') | |
914 | ||
915 | outputfile = '/tmp/kcdata.{:s}.bin'.format(str(time.time())) | |
916 | task = None | |
917 | if '-O' in cmd_options: | |
918 | outputfile = cmd_options['-O'] | |
919 | if '-T' in cmd_options: | |
920 | task = kern.GetValueFromAddress(cmd_options['-T'], 'task_t') | |
921 | ||
922 | memory_begin_address = unsigned(kcdata.kcd_addr_begin) | |
923 | memory_size = 16 + unsigned(kcdata.kcd_addr_end) - memory_begin_address | |
924 | flags_copyout = unsigned(kcdata.kcd_flags) | |
925 | if flags_copyout: | |
926 | if not task: | |
927 | raise ArgumentError('Invalid task pointer provided.') | |
cb323159 | 928 | return SaveDataToFile(memory_begin_address, memory_size, outputfile, task) |
3e170ce0 | 929 | else: |
cb323159 | 930 | return SaveDataToFile(memory_begin_address, memory_size, outputfile, None) |