1from xnu import * 2from utils import * 3from process import * 4from misc import * 5from memory import * 6from ipc import * 7 8# TODO: write scheduler related macros here 9 10# Macro: showallprocrunqcount 11 12@lldb_command('showallprocrunqcount') 13def ShowAllProcRunQCount(cmd_args=None): 14 """ Prints out the runq count for all processors 15 """ 16 out_str = "Processor\t# Runnable\n" 17 processor_itr = kern.globals.processor_list 18 while processor_itr: 19 out_str += "{:d}\t\t{:d}\n".format(processor_itr.cpu_id, processor_itr.runq.count) 20 processor_itr = processor_itr.processor_list 21 # out_str += "RT:\t\t{:d}\n".format(kern.globals.rt_runq.count) 22 print out_str 23 24# EndMacro: showallprocrunqcount 25 26# Macro: showinterrupts 27 28@lldb_command('showinterrupts') 29def ShowInterrupts(cmd_args=None): 30 """ Prints IRQ, IPI and TMR counts for each CPU 31 """ 32 33 if not kern.arch.startswith('arm'): 34 print "showinterrupts is only supported on arm/arm64" 35 return 36 37 base_address = kern.GetLoadAddressForSymbol('CpuDataEntries') 38 struct_size = 16 39 x = 0 40 y = 0 41 while x < unsigned(kern.globals.machine_info.physical_cpu): 42 element = kern.GetValueFromAddress(base_address + (y * struct_size), 'uintptr_t *')[1] 43 if element: 44 cpu_data_entry = Cast(element, 'cpu_data_t *') 45 print "CPU {} IRQ: {:d}\n".format(y, cpu_data_entry.cpu_stat.irq_ex_cnt) 46 print "CPU {} IPI: {:d}\n".format(y, cpu_data_entry.cpu_stat.ipi_cnt) 47 print "CPU {} PMI: {:d}\n".format(y, cpu_data_entry.cpu_monotonic.mtc_npmis) 48 print "CPU {} TMR: {:d}\n".format(y, cpu_data_entry.cpu_stat.timer_cnt) 49 x = x + 1 50 y = y + 1 51 52# EndMacro: showinterrupts 53 54# Macro: showactiveinterrupts 55 56@lldb_command('showactiveinterrupts') 57def ShowActiveInterrupts(cmd_args=None): 58 """ Prints the interrupts that are unmasked & active with the Interrupt Controller 59 Usage: showactiveinterrupts <address of Interrupt Controller object> 60 """ 61 if not cmd_args: 62 print "No arguments passed" 63 print ShowActiveInterrupts.__doc__ 64 return False 65 aic = kern.GetValueFromAddress(cmd_args[0], 'AppleInterruptController *') 66 if not aic: 67 print "unknown arguments:", str(cmd_args) 68 return False 69 70 aic_base = unsigned(aic._aicBaseAddress) 71 current_interrupt = 0 72 aic_imc_base = aic_base + 0x4180 73 aic_him_offset = 0x80 74 current_pointer = aic_imc_base 75 unmasked = dereference(kern.GetValueFromAddress(current_pointer, 'uintptr_t *')) 76 active = dereference(kern.GetValueFromAddress(current_pointer + aic_him_offset, 'uintptr_t *')) 77 group_count = 0 78 mask = 1 79 while current_interrupt < 192: 80 if (((unmasked & mask) == 0) and (active & mask)): 81 print "Interrupt {:d} unmasked and active\n".format(current_interrupt) 82 current_interrupt = current_interrupt + 1 83 if (current_interrupt % 32 == 0): 84 mask = 1 85 group_count = group_count + 1 86 unmasked = dereference(kern.GetValueFromAddress(current_pointer + (4 * group_count), 'uintptr_t *')) 87 active = dereference(kern.GetValueFromAddress((current_pointer + aic_him_offset) + (4 * group_count), 'uintptr_t *')) 88 else: 89 mask = mask << 1 90# EndMacro: showactiveinterrupts 91 92# Macro: showirqbyipitimerratio 93@lldb_command('showirqbyipitimerratio') 94def ShowIrqByIpiTimerRatio(cmd_args=None): 95 """ Prints the ratio of IRQ by sum of IPI & TMR counts for each CPU 96 """ 97 if kern.arch == "x86_64": 98 print "This macro is not supported on x86_64 architecture" 99 return 100 101 out_str = "IRQ-IT Ratio: " 102 base_address = kern.GetLoadAddressForSymbol('CpuDataEntries') 103 struct_size = 16 104 x = 0 105 y = 0 106 while x < unsigned(kern.globals.machine_info.physical_cpu): 107 element = kern.GetValueFromAddress(base_address + (y * struct_size), 'uintptr_t *')[1] 108 if element: 109 cpu_data_entry = Cast(element, 'cpu_data_t *') 110 out_str += " CPU {} [{:.2f}]".format(y, float(cpu_data_entry.cpu_stat.irq_ex_cnt)/(cpu_data_entry.cpu_stat.ipi_cnt + cpu_data_entry.cpu_stat.timer_cnt)) 111 x = x + 1 112 y = y + 1 113 print out_str 114 115# EndMacro: showirqbyipitimerratio 116 117#Macro: showinterruptsourceinfo 118@lldb_command('showinterruptsourceinfo') 119def showinterruptsourceinfo(cmd_args = None): 120 """ Extract information of interrupt source causing interrupt storms. 121 """ 122 if not cmd_args: 123 print "No arguments passed" 124 return False 125 #Dump IOInterruptVector object 126 print "--- Dumping IOInterruptVector object ---\n" 127 object_info = lldb_run_command("dumpobject {:s} IOInterruptVector".format(cmd_args[0])) 128 print object_info 129 print "--- Dumping IOFilterInterruptEventSource object ---\n" 130 #Dump the IOFilterInterruptEventSource object. 131 target_info=re.search('target =\s+(.*)',object_info) 132 target= target_info.group() 133 target= target.split() 134 #Dump the Object pointer of the source who is triggering the Interrupts. 135 vector_info=lldb_run_command("dumpobject {:s} ".format(target[2])) 136 print vector_info 137 owner_info= re.search('owner =\s+(.*)',vector_info) 138 owner= owner_info.group() 139 owner= owner.split() 140 print "\n\n" 141 out=lldb_run_command(" dumpobject {:s}".format(owner[2])) 142 print out 143 144# EndMacro: showinterruptsourceinfo 145 146@lldb_command('showcurrentabstime') 147def ShowCurremtAbsTime(cmd_args=None): 148 """ Routine to print latest absolute time known to system before being stopped. 149 Usage: showcurrentabstime 150 """ 151 pset = addressof(kern.globals.pset0) 152 processor_array = kern.globals.processor_array 153 cur_abstime = 0 154 155 while unsigned(pset) != 0: 156 cpu_bitmap = int(pset.cpu_bitmask) 157 for cpuid in IterateBitmap(cpu_bitmap): 158 processor = processor_array[cpuid] 159 if unsigned(processor.last_dispatch) > cur_abstime: 160 cur_abstime = unsigned(processor.last_dispatch) 161 162 pset = pset.pset_list 163 164 print "Last dispatch time known: %d MATUs" % cur_abstime 165 166bucketStr = ["FIXPRI (>UI)", "TIMESHARE_FG", "TIMESHARE_IN", "TIMESHARE_DF", "TIMESHARE_UT", "TIMESHARE_BG"] 167 168@header("{:<18s} | {:>20s} | {:>20s} | {:>10s} | {:>10s}".format('Thread Group', 'Pending (us)', 'Interactivity Score', 'TG Boost', 'Highest Thread Pri')) 169def GetSchedClutchBucketSummary(clutch_bucket): 170 tg_boost = kern.globals.sched_clutch_bucket_group_pri_boost[clutch_bucket.scb_group.scbg_clutch.sc_tg_priority] 171 pending_delta = kern.GetNanotimeFromAbstime(GetRecentTimestamp() - clutch_bucket.scb_group.scbg_pending_data.scct_timestamp) / 1000 172 if (int)(clutch_bucket.scb_group.scbg_pending_data.scct_timestamp) == 18446744073709551615: 173 pending_delta = 0 174 return "0x{:<16x} | {:>20d} | {:>20d} | {:>10d} | {:>10d}".format(clutch_bucket.scb_group.scbg_clutch.sc_tg, pending_delta, clutch_bucket.scb_group.scbg_interactivity_data.scct_count, tg_boost, SchedPriorityStableQueueRootPri(clutch_bucket.scb_thread_runq, 'struct thread', 'th_clutch_runq_link')) 175 176def ShowSchedClutchForPset(pset): 177 root_clutch = pset.pset_clutch_root 178 print "\n{:s} : {:d}\n\n".format("Current Timestamp", GetRecentTimestamp()) 179 print "{:>10s} | {:>20s} | {:>30s} | {:>25s} | {:<18s} | {:>10s} | {:>10s} | {:>15s} | ".format("Root", "Root Buckets", "Clutch Buckets", "Threads", "Address", "Pri (Base)", "Count", "Deadline (us)") + GetSchedClutchBucketSummary.header 180 print "=" * 300 181 print "{:>10s} | {:>20s} | {:>30s} | {:>25s} | 0x{:<16x} | {:>10d} | {:>10d} | {:>15s} | ".format("Root", "*", "*", "*", addressof(root_clutch), (root_clutch.scr_priority if root_clutch.scr_thr_count > 0 else -1), root_clutch.scr_thr_count, "*") 182 print "-" * 300 183 184 for i in range(0, 6): 185 root_bucket = root_clutch.scr_unbound_buckets[i] 186 root_bucket_deadline = 0 187 if root_bucket.scrb_clutch_buckets.scbrq_count != 0 and i != 0: 188 root_bucket_deadline = kern.GetNanotimeFromAbstime(root_bucket.scrb_pqlink.deadline - GetRecentTimestamp()) / 1000 189 print "{:>10s} | {:>20s} | {:>30s} | {:>25s} | 0x{:<16x} | {:>10s} | {:>10s} | {:>15d} | ".format("*", bucketStr[int(root_bucket.scrb_bucket)], "*", "*", addressof(root_bucket), "*", "*", root_bucket_deadline) 190 clutch_bucket_runq = root_bucket.scrb_clutch_buckets 191 clutch_bucket_list = [] 192 for pri in range(0,128): 193 clutch_bucket_circleq = clutch_bucket_runq.scbrq_queues[pri] 194 for clutch_bucket in IterateCircleQueue(clutch_bucket_circleq, 'struct sched_clutch_bucket', 'scb_runqlink'): 195 clutch_bucket_list.append(clutch_bucket) 196 if len(clutch_bucket_list) > 0: 197 clutch_bucket_list.sort(key=lambda x: x.scb_priority, reverse=True) 198 for clutch_bucket in clutch_bucket_list: 199 print "{:>10s} | {:>20s} | {:>30s} | {:>25s} | {:<18s} | {:>10s} | {:>10s} | {:>15s} | ".format("", "", "", "", "", "", "", "") 200 print "{:>10s} | {:>20s} | {:>30s} | {:>25s} | 0x{:<16x} | {:>10d} | {:>10d} | {:>15s} | ".format("*", "*", clutch_bucket.scb_group.scbg_clutch.sc_tg.tg_name, "*", clutch_bucket, clutch_bucket.scb_priority, clutch_bucket.scb_thr_count, "*") + GetSchedClutchBucketSummary(clutch_bucket) 201 runq = clutch_bucket.scb_clutchpri_prioq 202 for thread in IterateSchedPriorityQueue(runq, 'struct thread', 'th_clutch_pri_link'): 203 thread_name = GetThreadName(thread)[-24:] 204 if len(thread_name) == 0: 205 thread_name = "<unnamed thread>" 206 print "{:>10s} | {:>20s} | {:>30s} | {:<25s} | 0x{:<16x} | {:>10d} | {:>10s} | {:>15s} | ".format("*", "*", "*", thread_name, thread, thread.base_pri, "*", "*") 207 print "-" * 300 208 root_bucket = root_clutch.scr_bound_buckets[i] 209 root_bucket_deadline = 0 210 if root_bucket.scrb_bound_thread_runq.count != 0: 211 root_bucket_deadline = kern.GetNanotimeFromAbstime(root_bucket.scrb_pqlink.deadline - GetRecentTimestamp()) / 1000 212 print "{:>10s} | {:>20s} | {:>30s} | {:>25s} | 0x{:<16x} | {:>10s} | {:>10d} | {:>15d} | ".format("*", bucketStr[int(root_bucket.scrb_bucket)] + " [Bound]", "*", "*", addressof(root_bucket), "*", root_bucket.scrb_bound_thread_runq.count, root_bucket_deadline) 213 if root_bucket.scrb_bound_thread_runq.count == 0: 214 print "-" * 300 215 continue 216 thread_runq = root_bucket.scrb_bound_thread_runq 217 for pri in range(0, 128): 218 thread_circleq = thread_runq.queues[pri] 219 for thread in IterateCircleQueue(thread_circleq, 'struct thread', 'runq_links'): 220 thread_name = GetThreadName(thread)[-24:] 221 if len(thread_name) == 0: 222 thread_name = "<unnamed thread>" 223 print "{:>10s} | {:>20s} | {:>30s} | {:<25s} | 0x{:<16x} | {:>10d} | {:>10s} | {:>15s} | ".format("*", "*", "*", thread_name, thread, thread.base_pri, "*", "*") 224 print "-" * 300 225 226@lldb_command('showschedclutch') 227def ShowSchedClutch(cmd_args=[]): 228 """ Routine to print the clutch scheduler hierarchy. 229 Usage: showschedclutch <pset> 230 """ 231 if not cmd_args: 232 raise ArgumentError("Invalid argument") 233 pset = kern.GetValueFromAddress(cmd_args[0], "processor_set_t") 234 ShowSchedClutchForPset(pset) 235 236@lldb_command('showschedclutchroot') 237def ShowSchedClutchRoot(cmd_args=[]): 238 """ show information about the root of the sched clutch hierarchy 239 Usage: showschedclutchroot <root> 240 """ 241 if not cmd_args: 242 raise ArgumentError("Invalid argument") 243 root = kern.GetValueFromAddress(cmd_args[0], "struct sched_clutch_root *") 244 if not root: 245 print "unknown arguments:", str(cmd_args) 246 return False 247 print "{:>30s} : 0x{:<16x}".format("Root", root) 248 print "{:>30s} : 0x{:<16x}".format("Pset", root.scr_pset) 249 print "{:>30s} : {:d}".format("Priority", (root.scr_priority if root.scr_thr_count > 0 else -1)) 250 print "{:>30s} : {:d}".format("Urgency", root.scr_urgency) 251 print "{:>30s} : {:d}".format("Threads", root.scr_thr_count) 252 print "{:>30s} : {:d}".format("Current Timestamp", GetRecentTimestamp()) 253 print "{:>30s} : {:b} (BG/UT/DF/IN/FG/FIX/NULL)".format("Runnable Root Buckets Bitmap", int(root.scr_runnable_bitmap[0])) 254 255@lldb_command('showschedclutchrootbucket') 256def ShowSchedClutchRootBucket(cmd_args=[]): 257 """ show information about a root bucket in the sched clutch hierarchy 258 Usage: showschedclutchrootbucket <root_bucket> 259 """ 260 if not cmd_args: 261 raise ArgumentError("Invalid argument") 262 root_bucket = kern.GetValueFromAddress(cmd_args[0], "struct sched_clutch_root_bucket *") 263 if not root_bucket: 264 print "unknown arguments:", str(cmd_args) 265 return False 266 print "{:<30s} : 0x{:<16x}".format("Root Bucket", root_bucket) 267 print "{:<30s} : {:s}".format("Bucket Name", bucketStr[int(root_bucket.scrb_bucket)]) 268 print "{:<30s} : {:d}".format("Deadline", (root_bucket.scrb_pqlink.deadline if root_bucket.scrb_clutch_buckets.scbrq_count != 0 else 0)) 269 print "{:<30s} : {:d}".format("Current Timestamp", GetRecentTimestamp()) 270 print "\n" 271 clutch_bucket_runq = root_bucket.scrb_clutch_buckets 272 clutch_bucket_list = [] 273 for pri in range(0,128): 274 clutch_bucket_circleq = clutch_bucket_runq.scbrq_queues[pri] 275 for clutch_bucket in IterateCircleQueue(clutch_bucket_circleq, 'struct sched_clutch_bucket', 'scb_runqlink'): 276 clutch_bucket_list.append(clutch_bucket) 277 if len(clutch_bucket_list) > 0: 278 print "=" * 240 279 print "{:>30s} | {:>18s} | {:>20s} | {:>20s} | ".format("Name", "Clutch Bucket", "Priority", "Count") + GetSchedClutchBucketSummary.header 280 print "=" * 240 281 clutch_bucket_list.sort(key=lambda x: x.scb_priority, reverse=True) 282 for clutch_bucket in clutch_bucket_list: 283 print "{:>30s} | 0x{:<16x} | {:>20d} | {:>20d} | ".format(clutch_bucket.scb_group.scbg_clutch.sc_tg.tg_name, clutch_bucket, clutch_bucket.scb_priority, clutch_bucket.scb_thr_count) + GetSchedClutchBucketSummary(clutch_bucket) 284 285def SchedClutchBucketDetails(clutch_bucket): 286 print "{:<30s} : 0x{:<16x}".format("Clutch Bucket", clutch_bucket) 287 print "{:<30s} : {:s}".format("Scheduling Bucket", bucketStr[(int)(clutch_bucket.scb_bucket)]) 288 print "{:<30s} : 0x{:<16x}".format("Clutch Bucket Group", clutch_bucket.scb_group) 289 print "{:<30s} : {:s}".format("TG Name", clutch_bucket.scb_group.scbg_clutch.sc_tg.tg_name) 290 print "{:<30s} : {:d}".format("Priority", clutch_bucket.scb_priority) 291 print "{:<30s} : {:d}".format("Thread Count", clutch_bucket.scb_thr_count) 292 print "{:<30s} : 0x{:<16x}".format("Thread Group", clutch_bucket.scb_group.scbg_clutch.sc_tg) 293 print "{:<30s} : {:6d} (inherited from clutch bucket group)".format("Interactivity Score", clutch_bucket.scb_group.scbg_interactivity_data.scct_count) 294 print "{:<30s} : {:6d} (inherited from clutch bucket group)".format("Last Timeshare Update Tick", clutch_bucket.scb_group.scbg_timeshare_tick) 295 print "{:<30s} : {:6d} (inherited from clutch bucket group)".format("Priority Shift", clutch_bucket.scb_group.scbg_pri_shift) 296 print "\n" 297 runq = clutch_bucket.scb_clutchpri_prioq 298 thread_list = [] 299 for thread in IterateSchedPriorityQueue(runq, 'struct thread', 'th_clutch_pri_link'): 300 thread_list.append(thread) 301 if len(thread_list) > 0: 302 print "=" * 240 303 print GetThreadSummary.header + "{:s}".format("Process Name") 304 print "=" * 240 305 for thread in thread_list: 306 proc = thread.t_tro.tro_proc 307 print GetThreadSummary(thread) + "{:s}".format(GetProcName(proc)) 308 309@lldb_command('showschedclutchbucket') 310def ShowSchedClutchBucket(cmd_args=[]): 311 """ show information about a clutch bucket in the sched clutch hierarchy 312 Usage: showschedclutchbucket <clutch_bucket> 313 """ 314 if not cmd_args: 315 raise ArgumentError("Invalid argument") 316 clutch_bucket = kern.GetValueFromAddress(cmd_args[0], "struct sched_clutch_bucket *") 317 if not clutch_bucket: 318 print "unknown arguments:", str(cmd_args) 319 return False 320 SchedClutchBucketDetails(clutch_bucket) 321 322@lldb_command('abs2nano') 323def ShowAbstimeToNanoTime(cmd_args=[]): 324 """ convert mach_absolute_time units to nano seconds 325 Usage: (lldb) abs2nano <timestamp in MATUs> 326 """ 327 if not cmd_args: 328 raise ArgumentError("Invalid argument") 329 timedata = ArgumentStringToInt(cmd_args[0]) 330 ns = kern.GetNanotimeFromAbstime(timedata) 331 us = float(ns) / 1000 332 ms = us / 1000 333 s = ms / 1000 334 335 if s > 60 : 336 m = s / 60 337 h = m / 60 338 d = h / 24 339 340 print "{:d} ns, {:f} us, {:f} ms, {:f} s, {:f} m, {:f} h, {:f} d".format(ns, us, ms, s, m, h, d) 341 else: 342 print "{:d} ns, {:f} us, {:f} ms, {:f} s".format(ns, us, ms, s) 343 344 # Macro: showschedhistory 345 346def GetRecentTimestamp(): 347 """ 348 Return a recent timestamp. 349 TODO: on x86, if not in the debugger, then look at the scheduler 350 """ 351 if kern.arch == 'x86_64': 352 most_recent_dispatch = GetSchedMostRecentDispatch(False) 353 if most_recent_dispatch > kern.globals.debugger_entry_time : 354 return most_recent_dispatch 355 else : 356 return kern.globals.debugger_entry_time 357 else : 358 return GetSchedMostRecentDispatch(False) 359 360def GetSchedMostRecentDispatch(show_processor_details=False): 361 """ Return the most recent dispatch on the system, printing processor 362 details if argument is true. 363 """ 364 processor_list = kern.globals.processor_list 365 366 most_recent_dispatch = 0 367 current_processor = processor_list 368 369 while unsigned(current_processor) > 0: 370 active_thread = current_processor.active_thread 371 if unsigned(active_thread) != 0 : 372 task_val = active_thread.t_tro.tro_task 373 proc_val = active_thread.t_tro.tro_proc 374 proc_name = "<unknown>" if unsigned(proc_val) == 0 else GetProcName(proc_val) 375 376 last_dispatch = unsigned(current_processor.last_dispatch) 377 378 if kern.arch == 'x86_64': 379 cpu_data = kern.globals.cpu_data_ptr[current_processor.cpu_id] 380 if (cpu_data != 0) : 381 cpu_debugger_time = max(cpu_data.debugger_entry_time, cpu_data.debugger_ipi_time) 382 time_since_dispatch = unsigned(cpu_debugger_time - last_dispatch) 383 time_since_dispatch_us = kern.GetNanotimeFromAbstime(time_since_dispatch) / 1000.0 384 time_since_debugger = unsigned(cpu_debugger_time - kern.globals.debugger_entry_time) 385 time_since_debugger_us = kern.GetNanotimeFromAbstime(time_since_debugger) / 1000.0 386 387 if show_processor_details: 388 print "Processor last dispatch: {:16d} Entered debugger: {:16d} ({:8.3f} us after dispatch, {:8.3f} us after debugger) Active thread: 0x{t:<16x} 0x{t.thread_id:<8x} {proc_name:s}".format(last_dispatch, cpu_debugger_time, 389 time_since_dispatch_us, time_since_debugger_us, t=active_thread, proc_name=proc_name) 390 else: 391 if show_processor_details: 392 print "Processor last dispatch: {:16d} Active thread: 0x{t:<16x} 0x{t.thread_id:<8x} {proc_name:s}".format(last_dispatch, t=active_thread, proc_name=proc_name) 393 394 if last_dispatch > most_recent_dispatch: 395 most_recent_dispatch = last_dispatch 396 397 current_processor = current_processor.processor_list 398 399 return most_recent_dispatch 400 401@header("{:<18s} {:<10s} {:>16s} {:>16s} {:>16s} {:>16s} {:>18s} {:>16s} {:>16s} {:>16s} {:>16s} {:2s} {:2s} {:2s} {:>2s} {:<19s} {:<9s} {:>10s} {:>10s} {:>10s} {:>10s} {:>10s} {:>11s} {:>8s}".format("thread", "id", "on-core", "off-core", "runnable", "prichange", "last-duration (us)", "since-off (us)", "since-on (us)", "pending (us)", "pri-change (us)", "BP", "SP", "TP", "MP", "sched-mode", "state", "cpu-usage", "delta", "sch-usage", "stamp", "shift", "task", "thread-name")) 402def ShowThreadSchedHistory(thread, most_recent_dispatch): 403 """ Given a thread and the most recent dispatch time of a thread on the 404 system, print out details about scheduler history for the thread. 405 """ 406 407 thread_name = "" 408 409 uthread = GetBSDThread(thread) 410 # Doing the straightforward thing blows up weirdly, so use some indirections to get back on track 411 if unsigned(uthread.pth_name) != 0 : 412 thread_name = str(kern.GetValueFromAddress(unsigned(uthread.pth_name), 'char*')) 413 414 task = thread.t_tro.tro_task 415 task_name = "unknown" 416 if task and unsigned(task.bsd_info): 417 p = Cast(task.bsd_info, 'proc *') 418 task_name = GetProcName(p) 419 420 sched_mode = "" 421 422 mode = str(thread.sched_mode) 423 if "TIMESHARE" in mode: 424 sched_mode+="timeshare" 425 elif "FIXED" in mode: 426 sched_mode+="fixed" 427 elif "REALTIME" in mode: 428 sched_mode+="realtime" 429 430 if (unsigned(thread.bound_processor) != 0): 431 sched_mode+="-bound" 432 433 # TH_SFLAG_THROTTLED 434 if (unsigned(thread.sched_flags) & 0x0004): 435 sched_mode+="-BG" 436 437 state = thread.state 438 439 thread_state_chars = {0x0:'', 0x1:'W', 0x2:'S', 0x4:'R', 0x8:'U', 0x10:'H', 0x20:'A', 0x40:'P', 0x80:'I'} 440 state_str = '' 441 mask = 0x1 442 while mask <= 0x80 : 443 state_str += thread_state_chars[int(state & mask)] 444 mask = mask << 1 445 446 last_on = thread.computation_epoch 447 last_off = thread.last_run_time 448 last_runnable = thread.last_made_runnable_time 449 last_prichange = thread.last_basepri_change_time 450 451 if int(last_runnable) == 18446744073709551615 : 452 last_runnable = 0 453 454 if int(last_prichange) == 18446744073709551615 : 455 last_prichange = 0 456 457 time_on_abs = unsigned(last_off - last_on) 458 time_on_us = kern.GetNanotimeFromAbstime(time_on_abs) / 1000.0 459 460 time_pending_abs = unsigned(most_recent_dispatch - last_runnable) 461 time_pending_us = kern.GetNanotimeFromAbstime(time_pending_abs) / 1000.0 462 463 if int(last_runnable) == 0 : 464 time_pending_us = 0 465 466 last_prichange_abs = unsigned(most_recent_dispatch - last_prichange) 467 last_prichange_us = kern.GetNanotimeFromAbstime(last_prichange_abs) / 1000.0 468 469 if int(last_prichange) == 0 : 470 last_prichange_us = 0 471 472 time_since_off_abs = unsigned(most_recent_dispatch - last_off) 473 time_since_off_us = kern.GetNanotimeFromAbstime(time_since_off_abs) / 1000.0 474 time_since_on_abs = unsigned(most_recent_dispatch - last_on) 475 time_since_on_us = kern.GetNanotimeFromAbstime(time_since_on_abs) / 1000.0 476 477 fmt = "0x{t:<16x} 0x{t.thread_id:<8x} {t.computation_epoch:16d} {t.last_run_time:16d} {last_runnable:16d} {last_prichange:16d} {time_on_us:18.3f} {time_since_off_us:16.3f} {time_since_on_us:16.3f} {time_pending_us:16.3f} {last_prichange_us:16.3f}" 478 fmt2 = " {t.base_pri:2d} {t.sched_pri:2d} {t.task_priority:2d} {t.max_priority:2d} {sched_mode:19s}" 479 fmt3 = " {state:9s} {t.cpu_usage:10d} {t.cpu_delta:10d} {t.sched_usage:10d} {t.sched_stamp:10d} {t.pri_shift:10d} {name:s} {thread_name:s}" 480 481 out_str = fmt.format(t=thread, time_on_us=time_on_us, time_since_off_us=time_since_off_us, time_since_on_us=time_since_on_us, last_runnable=last_runnable, time_pending_us=time_pending_us, last_prichange=last_prichange, last_prichange_us=last_prichange_us) 482 out_str += fmt2.format(t=thread, sched_mode=sched_mode) 483 out_str += fmt3.format(t=thread, state=state_str, name=task_name, thread_name=thread_name) 484 485 print out_str 486 487def SortThreads(threads, column): 488 if column != 'on-core' and column != 'off-core' and column != 'last-duration': 489 raise ArgumentError("unsupported sort column") 490 if column == 'on-core': 491 threads.sort(key=lambda t: t.computation_epoch) 492 elif column == 'off-core': 493 threads.sort(key=lambda t: t.last_run_time) 494 else: 495 threads.sort(key=lambda t: t.last_run_time - t.computation_epoch) 496 497@lldb_command('showschedhistory', 'S:') 498def ShowSchedHistory(cmd_args=None, cmd_options=None): 499 """ Routine to print out thread scheduling history, optionally sorted by a 500 column. 501 502 Usage: showschedhistory [-S on-core|off-core|last-duration] [<thread-ptr> ...] 503 """ 504 505 sort_column = None 506 if '-S' in cmd_options: 507 sort_column = cmd_options['-S'] 508 509 if cmd_args: 510 most_recent_dispatch = GetSchedMostRecentDispatch(False) 511 512 print ShowThreadSchedHistory.header 513 514 if sort_column: 515 threads = [] 516 for thread_ptr in cmd_args: 517 threads.append(kern.GetValueFromAddress(ArgumentStringToInt(thread_ptr), 'thread *')) 518 519 SortThreads(threads, sort_column) 520 521 for thread in threads: 522 ShowThreadSchedHistory(thread, most_recent_dispatch) 523 else: 524 for thread_ptr in cmd_args: 525 thread = kern.GetValueFromAddress(ArgumentStringToInt(thread_ptr), 'thread *') 526 ShowThreadSchedHistory(thread, most_recent_dispatch) 527 528 return 529 530 run_buckets = kern.globals.sched_run_buckets 531 532 run_count = run_buckets[GetEnumValue('sched_bucket_t::TH_BUCKET_RUN')] 533 fixpri_count = run_buckets[GetEnumValue('sched_bucket_t::TH_BUCKET_FIXPRI')] 534 share_fg_count = run_buckets[GetEnumValue('sched_bucket_t::TH_BUCKET_SHARE_FG')] 535 share_df_count = run_buckets[GetEnumValue('sched_bucket_t::TH_BUCKET_SHARE_DF')] 536 share_ut_count = run_buckets[GetEnumValue('sched_bucket_t::TH_BUCKET_SHARE_UT')] 537 share_bg_count = run_buckets[GetEnumValue('sched_bucket_t::TH_BUCKET_SHARE_BG')] 538 539 sched_pri_shifts = kern.globals.sched_run_buckets 540 541 share_fg_shift = sched_pri_shifts[GetEnumValue('sched_bucket_t::TH_BUCKET_SHARE_FG')] 542 share_df_shift = sched_pri_shifts[GetEnumValue('sched_bucket_t::TH_BUCKET_SHARE_DF')] 543 share_ut_shift = sched_pri_shifts[GetEnumValue('sched_bucket_t::TH_BUCKET_SHARE_UT')] 544 share_bg_shift = sched_pri_shifts[GetEnumValue('sched_bucket_t::TH_BUCKET_SHARE_BG')] 545 546 547 print "Processors: {g.processor_avail_count:d} Runnable threads: {:d} Fixpri threads: {:d}\n".format(run_count, fixpri_count, g=kern.globals) 548 print "FG Timeshare threads: {:d} DF Timeshare threads: {:d} UT Timeshare threads: {:d} BG Timeshare threads: {:d}\n".format(share_fg_count, share_df_count, share_ut_count, share_bg_count) 549 print "Mach factor: {g.sched_mach_factor:d} Load factor: {g.sched_load_average:d} Sched tick: {g.sched_tick:d} timestamp: {g.sched_tick_last_abstime:d} interval:{g.sched_tick_interval:d}\n".format(g=kern.globals) 550 print "Fixed shift: {g.sched_fixed_shift:d} FG shift: {:d} DF shift: {:d} UT shift: {:d} BG shift: {:d}\n".format(share_fg_shift, share_df_shift, share_ut_shift, share_bg_shift, g=kern.globals) 551 print "sched_pri_decay_band_limit: {g.sched_pri_decay_band_limit:d} sched_decay_usage_age_factor: {g.sched_decay_usage_age_factor:d}\n".format(g=kern.globals) 552 553 if kern.arch == 'x86_64': 554 print "debugger_entry_time: {g.debugger_entry_time:d}\n".format(g=kern.globals) 555 556 most_recent_dispatch = GetSchedMostRecentDispatch(True) 557 print "Most recent dispatch: " + str(most_recent_dispatch) 558 559 print ShowThreadSchedHistory.header 560 561 if sort_column: 562 threads = [t for t in IterateQueue(kern.globals.threads, 'thread *', 'threads')] 563 564 SortThreads(threads, sort_column) 565 566 for thread in threads: 567 ShowThreadSchedHistory(thread, most_recent_dispatch) 568 else: 569 for thread in IterateQueue(kern.globals.threads, 'thread *', 'threads'): 570 ShowThreadSchedHistory(thread, most_recent_dispatch) 571 572 573# EndMacro: showschedhistory 574 575def int32(n): 576 n = n & 0xffffffff 577 return (n ^ 0x80000000) - 0x80000000 578 579# Macro: showallprocessors 580 581def ShowGroupSetSummary(runq, task_map): 582 """ Internal function to print summary of group run queue 583 params: runq - value representing struct run_queue * 584 """ 585 586 print " runq: count {: <10d} highq: {: <10d} urgency {: <10d}\n".format(runq.count, int32(runq.highq), runq.urgency) 587 588 runq_queue_i = 0 589 runq_queue_count = sizeof(runq.queues)/sizeof(runq.queues[0]) 590 591 for runq_queue_i in xrange(runq_queue_count) : 592 runq_queue_head = addressof(runq.queues[runq_queue_i]) 593 runq_queue_p = runq_queue_head.next 594 595 if unsigned(runq_queue_p) != unsigned(runq_queue_head): 596 runq_queue_this_count = 0 597 598 for entry in ParanoidIterateLinkageChain(runq_queue_head, "sched_entry_t", "entry_links", circleQueue=True): 599 runq_queue_this_count += 1 600 601 print " Queue [{: <#012x}] Priority {: <3d} count {:d}\n".format(runq_queue_head, runq_queue_i, runq_queue_this_count) 602 for entry in ParanoidIterateLinkageChain(runq_queue_head, "sched_entry_t", "entry_links", circleQueue=True): 603 group_addr = unsigned(entry) - (sizeof(dereference(entry)) * unsigned(entry.sched_pri)) 604 group = kern.GetValueFromAddress(unsigned(group_addr), 'sched_group_t') 605 task = task_map.get(unsigned(group), 0x0) 606 if task == 0x0 : 607 print "Cannot find task for group: {: <#012x}".format(group) 608 print "\tEntry [{: <#012x}] Priority {: <3d} Group {: <#012x} Task {: <#012x}\n".format(unsigned(entry), entry.sched_pri, unsigned(group), unsigned(task)) 609 610@lldb_command('showrunq') 611def ShowRunq(cmd_args=None): 612 """ Routine to print information of a runq 613 Usage: showrunq <runq> 614 """ 615 616 if not cmd_args: 617 print "No arguments passed" 618 print ShowRunq.__doc__ 619 return False 620 621 runq = kern.GetValueFromAddress(cmd_args[0], 'struct run_queue *') 622 ShowRunQSummary(runq) 623 624def ShowRunQSummary(runq): 625 """ Internal function to print summary of run_queue 626 params: runq - value representing struct run_queue * 627 """ 628 629 print " runq: count {: <10d} highq: {: <10d} urgency {: <10d}\n".format(runq.count, int32(runq.highq), runq.urgency) 630 631 runq_queue_i = 0 632 runq_queue_count = sizeof(runq.queues)/sizeof(runq.queues[0]) 633 634 for runq_queue_i in xrange(runq_queue_count) : 635 runq_queue_head = addressof(runq.queues[runq_queue_i]) 636 runq_queue_p = runq_queue_head.head 637 638 if unsigned(runq_queue_p): 639 runq_queue_this_count = 0 640 641 for thread in ParanoidIterateLinkageChain(runq_queue_head, "thread_t", "runq_links", circleQueue=True): 642 runq_queue_this_count += 1 643 644 print " Queue [{: <#012x}] Priority {: <3d} count {:d}\n".format(runq_queue_head, runq_queue_i, runq_queue_this_count) 645 print "\t" + GetThreadSummary.header + "\n" 646 for thread in ParanoidIterateLinkageChain(runq_queue_head, "thread_t", "runq_links", circleQueue=True): 647 print "\t" + GetThreadSummary(thread) + "\n" 648 if config['verbosity'] > vHUMAN : 649 print "\t" + GetThreadBackTrace(thread, prefix="\t\t") + "\n" 650 651def ShowRTRunQSummary(rt_runq): 652 if (hex(rt_runq.count) == hex(0xfdfdfdfd)) : 653 print " Realtime Queue ({:<#012x}) uninitialized\n".format(rt_runq) 654 return 655 print " Realtime Queue ({:<#012x}) Count {:d}\n".format(rt_runq, rt_runq.count) 656 if rt_runq.count != 0: 657 rt_pri_bitmap = int(rt_runq.bitmap[0]) 658 for rt_index in IterateBitmap(rt_pri_bitmap): 659 rt_pri_rq = addressof(rt_runq.rt_queue_pri[rt_index]) 660 print " Realtime Queue Index {:d} ({:<#012x}) Count {:d}\n".format(rt_index, rt_pri_rq, rt_pri_rq.pri_count) 661 print "\t" + GetThreadSummary.header + "\n" 662 for rt_runq_thread in ParanoidIterateLinkageChain(rt_pri_rq.pri_queue, "thread_t", "runq_links", circleQueue=False): 663 print "\t" + GetThreadSummary(rt_runq_thread) + "\n" 664 665def ShowGrrrSummary(grrr_runq): 666 """ Internal function to print summary of grrr_run_queue 667 params: grrr_runq - value representing struct grrr_run_queue * 668 """ 669 print " GRRR Info: Count {: <10d} Weight {: <10d} Current Group {: <#012x}\n".format(grrr_runq.count, 670 grrr_runq.weight, grrr_runq.current_group) 671 grrr_group_i = 0 672 grrr_group_count = sizeof(grrr_runq.groups)/sizeof(grrr_runq.groups[0]) 673 for grrr_group_i in xrange(grrr_group_count) : 674 grrr_group = addressof(grrr_runq.groups[grrr_group_i]) 675 if grrr_group.count > 0: 676 print " Group {: <3d} [{: <#012x}] ".format(grrr_group.index, grrr_group) 677 print "Count {:d} Weight {:d}\n".format(grrr_group.count, grrr_group.weight) 678 grrr_group_client_head = addressof(grrr_group.clients) 679 print GetThreadSummary.header 680 for thread in ParanoidIterateLinkageChain(grrr_group_client_head, "thread_t", "runq_links", circleQueue=True): 681 print "\t" + GetThreadSummary(thread) + "\n" 682 if config['verbosity'] > vHUMAN : 683 print "\t" + GetThreadBackTrace(thread, prefix="\t\t") + "\n" 684 685def ShowActiveThread(processor): 686 if (processor.active_thread != 0) : 687 print "\t" + GetThreadSummary.header + "\n" 688 print "\t" + GetThreadSummary(processor.active_thread) + "\n" 689 690@lldb_command('showallprocessors') 691@lldb_command('showscheduler') 692def ShowScheduler(cmd_args=None): 693 """ Routine to print information of all psets and processors 694 Usage: showscheduler 695 """ 696 node = addressof(kern.globals.pset_node0) 697 show_grrr = 0 698 show_priority_runq = 0 699 show_priority_pset_runq = 0 700 show_group_pset_runq = 0 701 show_clutch = 0 702 show_edge = 0 703 sched_string = str(kern.globals.sched_string) 704 705 if sched_string == "traditional": 706 show_priority_runq = 1 707 elif sched_string == "traditional_with_pset_runqueue": 708 show_priority_pset_runq = 1 709 elif sched_string == "grrr": 710 show_grrr = 1 711 elif sched_string == "multiq": 712 show_priority_runq = 1 713 show_group_pset_runq = 1 714 elif sched_string == "dualq": 715 show_priority_pset_runq = 1 716 show_priority_runq = 1 717 elif sched_string == "amp": 718 show_priority_pset_runq = 1 719 show_priority_runq = 1 720 elif sched_string == "clutch": 721 show_clutch = 1 722 elif sched_string == "edge": 723 show_edge = 1 724 else : 725 print "Unknown sched_string {:s}".format(sched_string) 726 727 print "Scheduler: {:s}\n".format(sched_string) 728 729 if show_clutch == 0 and show_edge == 0: 730 run_buckets = kern.globals.sched_run_buckets 731 run_count = run_buckets[GetEnumValue('sched_bucket_t::TH_BUCKET_RUN')] 732 fixpri_count = run_buckets[GetEnumValue('sched_bucket_t::TH_BUCKET_FIXPRI')] 733 share_fg_count = run_buckets[GetEnumValue('sched_bucket_t::TH_BUCKET_SHARE_FG')] 734 share_df_count = run_buckets[GetEnumValue('sched_bucket_t::TH_BUCKET_SHARE_DF')] 735 share_ut_count = run_buckets[GetEnumValue('sched_bucket_t::TH_BUCKET_SHARE_UT')] 736 share_bg_count = run_buckets[GetEnumValue('sched_bucket_t::TH_BUCKET_SHARE_BG')] 737 print "Processors: {g.processor_avail_count:d} Runnable threads: {:d} Fixpri threads: {:d}\n".format(run_count, fixpri_count, g=kern.globals) 738 print "FG Timeshare threads: {:d} DF Timeshare threads: {:d} UT Timeshare threads: {:d} BG Timeshare threads: {:d}\n".format(share_fg_count, share_df_count, share_ut_count, share_bg_count) 739 740 processor_offline = GetEnumValue('processor_state_t::PROCESSOR_OFF_LINE') 741 processor_idle = GetEnumValue('processor_state_t::PROCESSOR_IDLE') 742 processor_dispatching = GetEnumValue('processor_state_t::PROCESSOR_DISPATCHING') 743 processor_running = GetEnumValue('processor_state_t::PROCESSOR_RUNNING') 744 745 if show_group_pset_runq: 746 if hasattr(kern.globals, "multiq_sanity_check"): 747 print "multiq scheduler config: deep-drain {g.deep_drain:d}, ceiling {g.drain_ceiling:d}, depth limit {g.drain_depth_limit:d}, band limit {g.drain_band_limit:d}, sanity check {g.multiq_sanity_check:d}\n".format(g=kern.globals) 748 else: 749 print "multiq scheduler config: deep-drain {g.deep_drain:d}, ceiling {g.drain_ceiling:d}, depth limit {g.drain_depth_limit:d}, band limit {g.drain_band_limit:d}\n".format(g=kern.globals) 750 751 # Create a group->task mapping 752 task_map = {} 753 for task in kern.tasks: 754 task_map[unsigned(task.sched_group)] = task 755 for task in kern.terminated_tasks: 756 task_map[unsigned(task.sched_group)] = task 757 758 print " \n" 759 760 while node != 0: 761 pset = node.psets 762 pset = kern.GetValueFromAddress(unsigned(pset), 'struct processor_set *') 763 764 while pset != 0: 765 print "Processor Set {: <#012x} Count {:d} (cpu_id {:<#x}-{:<#x})\n".format(pset, 766 unsigned(pset.cpu_set_count), pset.cpu_set_low, pset.cpu_set_hi) 767 768 rt_runq = kern.GetValueFromAddress(unsigned(addressof(pset.rt_runq)), 'struct rt_queue *') 769 ShowRTRunQSummary(rt_runq) 770 771 if show_priority_pset_runq: 772 runq = kern.GetValueFromAddress(unsigned(addressof(pset.pset_runq)), 'struct run_queue *') 773 ShowRunQSummary(runq) 774 775 if show_group_pset_runq: 776 print "Main Runq:\n" 777 runq = kern.GetValueFromAddress(unsigned(addressof(pset.pset_runq)), 'struct run_queue *') 778 ShowGroupSetSummary(runq, task_map) 779 print "All Groups:\n" 780 # TODO: Possibly output task header for each group 781 for group in IterateQueue(kern.globals.sched_groups, "sched_group_t", "sched_groups"): 782 if (group.runq.count != 0) : 783 task = task_map.get(unsigned(group), "Unknown task!") 784 print "Group {: <#012x} Task {: <#012x}\n".format(unsigned(group), unsigned(task)) 785 ShowRunQSummary(group.runq) 786 print " \n" 787 788 processor_array = kern.globals.processor_array 789 790 print "Active Processors:\n" 791 active_bitmap = int(pset.cpu_state_map[processor_dispatching]) | int(pset.cpu_state_map[processor_running]) 792 for cpuid in IterateBitmap(active_bitmap): 793 processor = processor_array[cpuid] 794 if processor != 0: 795 print " " + GetProcessorSummary(processor) 796 ShowActiveThread(processor) 797 798 if show_priority_runq: 799 runq = processor.runq 800 ShowRunQSummary(runq) 801 if show_grrr: 802 grrr_runq = processor.grrr_runq 803 ShowGrrrSummary(grrr_runq) 804 print " \n" 805 806 807 print "Idle Processors:\n" 808 idle_bitmap = int(pset.cpu_state_map[processor_idle]) & int(pset.primary_map) 809 for cpuid in IterateBitmap(idle_bitmap): 810 processor = processor_array[cpuid] 811 if processor != 0: 812 print " " + GetProcessorSummary(processor) 813 ShowActiveThread(processor) 814 815 if show_priority_runq: 816 ShowRunQSummary(processor.runq) 817 print " \n" 818 819 820 print "Idle Secondary Processors:\n" 821 idle_bitmap = int(pset.cpu_state_map[processor_idle]) & ~(int(pset.primary_map)) 822 for cpuid in IterateBitmap(idle_bitmap): 823 processor = processor_array[cpuid] 824 if processor != 0: 825 print " " + GetProcessorSummary(processor) 826 ShowActiveThread(processor) 827 828 if show_priority_runq: 829 print ShowRunQSummary(processor.runq) 830 print " \n" 831 832 833 print "Other Processors:\n" 834 other_bitmap = 0 835 for i in range(processor_offline, processor_idle): 836 other_bitmap |= int(pset.cpu_state_map[i]) 837 other_bitmap &= int(pset.cpu_bitmask) 838 for cpuid in IterateBitmap(other_bitmap): 839 processor = processor_array[cpuid] 840 if processor != 0: 841 print " " + GetProcessorSummary(processor) 842 ShowActiveThread(processor) 843 844 if show_priority_runq: 845 ShowRunQSummary(processor.runq) 846 print " \n" 847 848 if show_clutch or show_edge: 849 cluster_type = "SMP" 850 if pset.pset_type == 1: 851 cluster_type = "E" 852 elif pset.pset_type == 2: 853 cluster_type = "P" 854 print "=== Clutch Scheduler Hierarchy Pset{:d} (Type: {:s}) ] ===\n\n".format(pset.pset_cluster_id, cluster_type) 855 ShowSchedClutchForPset(pset) 856 857 pset = pset.pset_list 858 859 node = node.node_list 860 861 print "\nCrashed Threads Queue: ({:<#012x})\n".format(addressof(kern.globals.crashed_threads_queue)) 862 first = True 863 for thread in ParanoidIterateLinkageChain(kern.globals.crashed_threads_queue, "thread_t", "runq_links"): 864 if first: 865 print "\t" + GetThreadSummary.header 866 first = False 867 print "\t" + GetThreadSummary(thread) 868 869 def dump_mpsc_thread_queue(name, head): 870 head = addressof(head) 871 print "\n{:s}: ({:<#012x})\n".format(name, head) 872 first = True 873 for thread in IterateMPSCQueue(head.mpd_queue, 'struct thread', 'mpsc_links'): 874 if first: 875 print "\t" + GetThreadSummary.header 876 first = False 877 print "\t" + GetThreadSummary(thread) 878 879 dump_mpsc_thread_queue("Terminate Queue", kern.globals.thread_terminate_queue) 880 dump_mpsc_thread_queue("Waiting For Kernel Stacks Queue", kern.globals.thread_stack_queue) 881 dump_mpsc_thread_queue("Thread Exception Queue", kern.globals.thread_exception_queue) 882 dump_mpsc_thread_queue("Thread Deallocate Queue", kern.globals.thread_deallocate_queue) 883 884 print "\n" 885 886 print "\n" 887 888# EndMacro: showallprocessors 889 890 891def ParanoidIterateLinkageChain(queue_head, element_type, field_name, field_ofst=0, circleQueue=False): 892 """ Iterate over a Linkage Chain queue in kernel of type queue_head_t or circle_queue_head_t. (osfmk/kern/queue.h method 1 or circle_queue.h) 893 This is equivalent to the qe_foreach_element() macro 894 Blows up aggressively and descriptively when something goes wrong iterating a queue. 895 Prints correctness errors, and throws exceptions on 'cannot proceed' errors 896 If this is annoying, set the global 'enable_paranoia' to false. 897 898 params: 899 queue_head - value : Value object for queue_head. 900 element_type - lldb.SBType : pointer type of the element which contains the queue_chain_t. Typically its structs like thread, task etc.. 901 - str : OR a string describing the type. ex. 'task *' 902 field_name - str : Name of the field (in element) which holds a queue_chain_t 903 field_ofst - int : offset from the 'field_name' (in element) which holds a queue_chain_t 904 This is mostly useful if a particular element contains an array of queue_chain_t 905 returns: 906 A generator does not return. It is used for iterating. 907 value : An object thats of type (element_type). Always a pointer object 908 example usage: 909 for thread in IterateQueue(kern.globals.threads, 'thread *', 'threads'): 910 print thread.thread_id 911 """ 912 913 if type(element_type) is str: 914 element_type = gettype(element_type) 915 916 # Some ways of constructing a queue head seem to end up with the 917 # struct object as the value and not a pointer to the struct head 918 # In that case, addressof will give us a pointer to the struct, which is what we need 919 if not queue_head.GetSBValue().GetType().IsPointerType() : 920 queue_head = addressof(queue_head) 921 922 if circleQueue: 923 # Mosh the value into a brand new value, to really get rid of its old cvalue history 924 queue_head = kern.GetValueFromAddress(unsigned(queue_head), 'struct circle_queue_head *').head 925 else: 926 # Mosh the value into a brand new value, to really get rid of its old cvalue history 927 queue_head = kern.GetValueFromAddress(unsigned(queue_head), 'struct queue_entry *') 928 929 if unsigned(queue_head) == 0: 930 if not circleQueue and ParanoidIterateLinkageChain.enable_paranoia: 931 print "bad queue_head_t: {:s}".format(queue_head) 932 return 933 934 if element_type.IsPointerType(): 935 struct_type = element_type.GetPointeeType() 936 else: 937 struct_type = element_type 938 939 elem_ofst = getfieldoffset(struct_type, field_name) + field_ofst 940 941 try: 942 link = queue_head.next 943 last_link = queue_head 944 try_read_next = unsigned(queue_head.next) 945 except: 946 print "Exception while looking at queue_head: {:>#18x}".format(unsigned(queue_head)) 947 raise 948 949 if ParanoidIterateLinkageChain.enable_paranoia: 950 if unsigned(queue_head.next) == 0: 951 raise ValueError("NULL next pointer on head: queue_head {:>#18x} next: {:>#18x} prev: {:>#18x}".format(queue_head, queue_head.next, queue_head.prev)) 952 if unsigned(queue_head.prev) == 0: 953 print "NULL prev pointer on head: queue_head {:>#18x} next: {:>#18x} prev: {:>#18x}".format(queue_head, queue_head.next, queue_head.prev) 954 if unsigned(queue_head.next) == unsigned(queue_head) and unsigned(queue_head.prev) != unsigned(queue_head): 955 print "corrupt queue_head {:>#18x} next: {:>#18x} prev: {:>#18x}".format(queue_head, queue_head.next, queue_head.prev) 956 957 if ParanoidIterateLinkageChain.enable_debug : 958 print "starting at queue_head {:>#18x} next: {:>#18x} prev: {:>#18x}".format(queue_head, queue_head.next, queue_head.prev) 959 960 addr = 0 961 obj = 0 962 963 try: 964 while True: 965 if not circleQueue and unsigned(queue_head) == unsigned(link): 966 break; 967 if ParanoidIterateLinkageChain.enable_paranoia: 968 if unsigned(link.next) == 0: 969 raise ValueError("NULL next pointer: queue_head {:>#18x} link: {:>#18x} next: {:>#18x} prev: {:>#18x}".format(queue_head, link, link.next, link.prev)) 970 if unsigned(link.prev) == 0: 971 print "NULL prev pointer: queue_head {:>#18x} link: {:>#18x} next: {:>#18x} prev: {:>#18x}".format(queue_head, link, link.next, link.prev) 972 if unsigned(last_link) != unsigned(link.prev): 973 print "Corrupt prev pointer: queue_head {:>#18x} link: {:>#18x} next: {:>#18x} prev: {:>#18x} prev link: {:>#18x} ".format( 974 queue_head, link, link.next, link.prev, last_link) 975 976 addr = unsigned(link) - unsigned(elem_ofst); 977 obj = kern.GetValueFromAddress(addr, element_type) 978 if ParanoidIterateLinkageChain.enable_debug : 979 print "yielding link: {:>#18x} next: {:>#18x} prev: {:>#18x} addr: {:>#18x} obj: {:>#18x}".format(link, link.next, link.prev, addr, obj) 980 yield obj 981 last_link = link 982 link = link.next 983 if circleQueue and unsigned(queue_head) == unsigned(link): 984 break; 985 except: 986 exc_info = sys.exc_info() 987 try: 988 print "Exception while iterating queue: {:>#18x} link: {:>#18x} addr: {:>#18x} obj: {:>#18x} last link: {:>#18x}".format(queue_head, link, addr, obj, last_link) 989 except: 990 import traceback 991 traceback.print_exc() 992 raise exc_info[0], exc_info[1], exc_info[2] 993 994ParanoidIterateLinkageChain.enable_paranoia = True 995ParanoidIterateLinkageChain.enable_debug = False 996 997def LinkageChainEmpty(queue_head): 998 if not queue_head.GetSBValue().GetType().IsPointerType() : 999 queue_head = addressof(queue_head) 1000 1001 # Mosh the value into a brand new value, to really get rid of its old cvalue history 1002 # avoid using GetValueFromAddress 1003 queue_head = value(queue_head.GetSBValue().CreateValueFromExpression(None,'(void *)'+str(unsigned(queue_head)))) 1004 queue_head = cast(queue_head, 'struct queue_entry *') 1005 1006 link = queue_head.next 1007 1008 return unsigned(queue_head) == unsigned(link) 1009 1010def bit_first(bitmap): 1011 return bitmap.bit_length() - 1 1012 1013def lsb_first(bitmap): 1014 bitmap = bitmap & -bitmap 1015 return bit_first(bitmap) 1016 1017def IterateBitmap(bitmap): 1018 """ Iterate over a bitmap, returning the index of set bits starting from 0 1019 1020 params: 1021 bitmap - value : bitmap 1022 returns: 1023 A generator does not return. It is used for iterating. 1024 value : index of a set bit 1025 example usage: 1026 for cpuid in IterateBitmap(running_bitmap): 1027 print processor_array[cpuid] 1028 """ 1029 i = lsb_first(bitmap) 1030 while (i >= 0): 1031 yield i 1032 bitmap = bitmap & ~((1 << (i + 1)) - 1) 1033 i = lsb_first(bitmap) 1034 1035 1036# Macro: showallcallouts 1037 1038from kevent import GetKnoteKqueue 1039 1040def ShowThreadCall(prefix, call, recent_timestamp, is_pending=False): 1041 """ 1042 Print a description of a thread_call_t and its relationship to its expected fire time 1043 """ 1044 func = call.tc_func 1045 param0 = call.tc_param0 1046 param1 = call.tc_param1 1047 1048 is_iotes = False 1049 1050 func_name = kern.Symbolicate(func) 1051 1052 extra_string = "" 1053 1054 strip_func = kern.StripKernelPAC(unsigned(func)) 1055 1056 func_syms = kern.SymbolicateFromAddress(strip_func) 1057 # returns an array of SBSymbol 1058 1059 if func_syms and func_syms[0] : 1060 func_name = func_syms[0].GetName() 1061 1062 try : 1063 if ("IOTimerEventSource::timeoutAndRelease" in func_name or 1064 "IOTimerEventSource::timeoutSignaled" in func_name) : 1065 iotes = Cast(call.tc_param0, 'IOTimerEventSource*') 1066 try: 1067 func = iotes.action 1068 param0 = iotes.owner 1069 param1 = unsigned(iotes) 1070 except AttributeError: 1071 # This is horrible, horrible, horrible. But it works. Needed because IOEventSource hides the action member in an 1072 # anonymous union when XNU_PRIVATE_SOURCE is set. To grab it, we work backwards from the enabled member. 1073 func = dereference(kern.GetValueFromAddress(addressof(iotes.enabled) - sizeof('IOEventSource::Action'), 'uint64_t *')) 1074 param0 = iotes.owner 1075 param1 = unsigned(iotes) 1076 1077 workloop = iotes.workLoop 1078 thread = workloop.workThread 1079 1080 is_iotes = True 1081 1082 # re-symbolicate the func we found inside the IOTES 1083 strip_func = kern.StripKernelPAC(unsigned(func)) 1084 func_syms = kern.SymbolicateFromAddress(strip_func) 1085 if func_syms and func_syms[0] : 1086 func_name = func_syms[0].GetName() 1087 else : 1088 func_name = str(FindKmodNameForAddr(func)) 1089 1090 # cast from IOThread to thread_t, because IOThread is sometimes opaque 1091 thread = Cast(thread, 'thread_t') 1092 thread_id = thread.thread_id 1093 thread_name = GetThreadName(thread) 1094 1095 extra_string += "workloop thread: {:#x} ({:#x}) {:s}".format(thread, thread_id, thread_name) 1096 1097 if "filt_timerexpire" in func_name : 1098 knote = Cast(call.tc_param0, 'struct knote *') 1099 kqueue = GetKnoteKqueue(knote) 1100 proc = kqueue.kq_p 1101 proc_name = GetProcName(proc) 1102 proc_pid = GetProcPID(proc) 1103 1104 extra_string += "kq: {:#018x} {:s}[{:d}]".format(kqueue, proc_name, proc_pid) 1105 1106 if "mk_timer_expire" in func_name : 1107 timer = Cast(call.tc_param0, 'struct mk_timer *') 1108 port = timer.port 1109 1110 extra_string += "port: {:#018x} {:s}".format(port, GetPortDestinationSummary(port)) 1111 1112 if "workq_kill_old_threads_call" in func_name : 1113 workq = Cast(call.tc_param0, 'struct workqueue *') 1114 proc = workq.wq_proc 1115 proc_name = GetProcName(proc) 1116 proc_pid = GetProcPID(proc) 1117 1118 extra_string += "{:s}[{:d}]".format(proc_name, proc_pid) 1119 1120 if ("workq_add_new_threads_call" in func_name or 1121 "realitexpire" in func_name): 1122 proc = Cast(call.tc_param0, 'struct proc *') 1123 proc_name = GetProcName(proc) 1124 proc_pid = GetProcPID(proc) 1125 1126 extra_string += "{:s}[{:d}]".format(proc_name, proc_pid) 1127 1128 except: 1129 print "exception generating extra_string for call: {:#018x}".format(call) 1130 if ShowThreadCall.enable_debug : 1131 raise 1132 1133 if (func_name == "") : 1134 func_name = FindKmodNameForAddr(func) 1135 1136 if (call.tc_flags & GetEnumValue('thread_call_flags_t::THREAD_CALL_FLAG_CONTINUOUS')) : 1137 timer_fire = call.tc_pqlink.deadline - (recent_timestamp + kern.globals.mach_absolutetime_asleep) 1138 else : 1139 timer_fire = call.tc_pqlink.deadline - recent_timestamp 1140 1141 timer_fire_s = kern.GetNanotimeFromAbstime(timer_fire) / 1000000000.0 1142 1143 ttd_s = kern.GetNanotimeFromAbstime(call.tc_ttd) / 1000000000.0 1144 1145 if (is_pending) : 1146 pending_time = call.tc_pending_timestamp - recent_timestamp 1147 pending_time = kern.GetNanotimeFromAbstime(pending_time) / 1000000000.0 1148 1149 flags = int(call.tc_flags) 1150 # TODO: extract this out of the thread_call_flags_t enum 1151 thread_call_flags = {0x0:'', 0x1:'A', 0x2:'W', 0x4:'D', 0x8:'R', 0x10:'S', 0x20:'O', 1152 0x40:'P', 0x80:'L', 0x100:'C'} 1153 1154 flags_str = '' 1155 mask = 0x1 1156 while mask <= 0x100 : 1157 flags_str += thread_call_flags[int(flags & mask)] 1158 mask = mask << 1 1159 1160 if is_iotes : 1161 flags_str += 'I' 1162 1163 if (is_pending) : 1164 print ("{:s}{:#018x}: {:18d} {:18d} {:16.06f} {:16.06f} {:16.06f} {:9s} " + 1165 "{:#018x} ({:#018x}, {:#018x}) ({:s}) {:s}").format(prefix, 1166 unsigned(call), call.tc_pqlink.deadline, call.tc_soft_deadline, ttd_s, 1167 timer_fire_s, pending_time, flags_str, 1168 func, param0, param1, func_name, extra_string) 1169 else : 1170 print ("{:s}{:#018x}: {:18d} {:18d} {:16.06f} {:16.06f} {:9s} " + 1171 "{:#018x} ({:#018x}, {:#018x}) ({:s}) {:s}").format(prefix, 1172 unsigned(call), call.tc_pqlink.deadline, call.tc_soft_deadline, ttd_s, 1173 timer_fire_s, flags_str, 1174 func, param0, param1, func_name, extra_string) 1175 1176ShowThreadCall.enable_debug = False 1177 1178@header("{:>18s} {:>18s} {:>18s} {:>16s} {:>16s} {:9s} {:>18s}".format( 1179 "entry", "deadline", "soft_deadline", 1180 "duration (s)", "to go (s)", "flags", "(*func) (param0, param1)")) 1181def PrintThreadGroup(group): 1182 header = PrintThreadGroup.header 1183 pending_header = "{:>18s} {:>18s} {:>18s} {:>16s} {:>16s} {:>16s} {:9s} {:>18s}".format( 1184 "entry", "deadline", "soft_deadline", 1185 "duration (s)", "to go (s)", "pending", "flags", "(*func) (param0, param1)") 1186 1187 recent_timestamp = GetRecentTimestamp() 1188 1189 idle_timestamp_distance = group.idle_timestamp - recent_timestamp 1190 idle_timestamp_distance_s = kern.GetNanotimeFromAbstime(idle_timestamp_distance) / 1000000000.0 1191 1192 is_parallel = "" 1193 1194 if (group.tcg_flags & GetEnumValue('thread_call_group_flags_t::TCG_PARALLEL')) : 1195 is_parallel = " (parallel)" 1196 1197 print "Group: {g.tcg_name:s} ({:#18x}){:s}".format(unsigned(group), is_parallel, g=group) 1198 print "\t" +"Thread Priority: {g.tcg_thread_pri:d}\n".format(g=group) 1199 print ("\t" +"Active: {g.active_count:<3d} Idle: {g.idle_count:<3d}" + 1200 "Blocked: {g.blocked_count:<3d} Pending: {g.pending_count:<3d}" + 1201 "Target: {g.target_thread_count:<3d}\n").format(g=group) 1202 1203 if unsigned(group.idle_timestamp) is not 0 : 1204 print "\t" +"Idle Timestamp: {g.idle_timestamp:d} ({:03.06f})\n".format(idle_timestamp_distance_s, 1205 g=group) 1206 1207 print "\t" +"Pending Queue: ({:>#18x})\n".format(addressof(group.pending_queue)) 1208 if not LinkageChainEmpty(group.pending_queue) : 1209 print "\t\t" + pending_header 1210 for call in ParanoidIterateLinkageChain(group.pending_queue, "thread_call_t", "tc_qlink"): 1211 ShowThreadCall("\t\t", call, recent_timestamp, is_pending=True) 1212 1213 print "\t" +"Delayed Queue (Absolute Time): ({:>#18x}) timer: ({:>#18x})\n".format( 1214 addressof(group.delayed_queues[0]), addressof(group.delayed_timers[0])) 1215 if not LinkageChainEmpty(group.delayed_queues[0]) : 1216 print "\t\t" + header 1217 for call in ParanoidIterateLinkageChain(group.delayed_queues[0], "thread_call_t", "tc_qlink"): 1218 ShowThreadCall("\t\t", call, recent_timestamp) 1219 1220 print "\t" +"Delayed Queue (Continuous Time): ({:>#18x}) timer: ({:>#18x})\n".format( 1221 addressof(group.delayed_queues[1]), addressof(group.delayed_timers[1])) 1222 if not LinkageChainEmpty(group.delayed_queues[1]) : 1223 print "\t\t" + header 1224 for call in ParanoidIterateLinkageChain(group.delayed_queues[1], "thread_call_t", "tc_qlink"): 1225 ShowThreadCall("\t\t", call, recent_timestamp) 1226 1227def PrintThreadCallThreads() : 1228 callout_flag = GetEnumValue('thread_tag_t::THREAD_TAG_CALLOUT') 1229 recent_timestamp = GetRecentTimestamp() 1230 1231 for thread in IterateQueue(kern.globals.kernel_task.threads, 'thread *', 'task_threads'): 1232 if (thread.thread_tag & callout_flag) : 1233 print " {:#20x} {:#12x} {:s}".format(thread, thread.thread_id, GetThreadName(thread)) 1234 state = thread.thc_state 1235 if state and state.thc_call : 1236 print "\t" + PrintThreadGroup.header 1237 ShowThreadCall("\t", state.thc_call, recent_timestamp) 1238 soft_deadline = state.thc_call_soft_deadline 1239 slop_time = state.thc_call_hard_deadline - soft_deadline 1240 slop_time = kern.GetNanotimeFromAbstime(slop_time) / 1000000000.0 1241 print "\t original soft deadline {:d}, hard deadline {:d} (leeway {:.06f}s)".format( 1242 soft_deadline, state.thc_call_hard_deadline, slop_time) 1243 enqueue_time = state.thc_call_pending_timestamp - soft_deadline 1244 enqueue_time = kern.GetNanotimeFromAbstime(enqueue_time) / 1000000000.0 1245 print "\t time to enqueue after deadline: {:.06f}s (enqueued at: {:d})".format( 1246 enqueue_time, state.thc_call_pending_timestamp) 1247 wait_time = state.thc_call_start - state.thc_call_pending_timestamp 1248 wait_time = kern.GetNanotimeFromAbstime(wait_time) / 1000000000.0 1249 print "\t time to start executing after enqueue: {:.06f}s (executing at: {:d})".format( 1250 wait_time, state.thc_call_start) 1251 1252 if (state.thc_IOTES_invocation_timestamp) : 1253 iotes_acquire_time = state.thc_IOTES_invocation_timestamp - state.thc_call_start 1254 iotes_acquire_time = kern.GetNanotimeFromAbstime(iotes_acquire_time) / 1000000000.0 1255 print "\t IOTES acquire time: {:.06f}s (acquired at: {:d})".format( 1256 iotes_acquire_time, state.thc_IOTES_invocation_timestamp) 1257 1258 1259@lldb_command('showcalloutgroup') 1260def ShowCalloutGroup(cmd_args=None): 1261 """ Prints out the pending and delayed thread calls for a specific group 1262 1263 Pass 'threads' to show the thread call threads themselves. 1264 1265 Callout flags: 1266 1267 A - Allocated memory owned by thread_call.c 1268 W - Wait - thread waiting for call to finish running 1269 D - Delayed - deadline based 1270 R - Running - currently executing on a thread 1271 S - Signal - call from timer interrupt instead of thread 1272 O - Once - pend the enqueue if re-armed while running 1273 P - Reschedule pending - enqueue is pending due to re-arm while running 1274 L - Rate-limited - (App Nap) 1275 C - Continuous time - Timeout is in mach_continuous_time 1276 I - Callout is an IOTimerEventSource 1277 """ 1278 if not cmd_args: 1279 print "No arguments passed" 1280 print ShowCalloutGroup.__doc__ 1281 return False 1282 1283 if "threads" in cmd_args[0] : 1284 PrintThreadCallThreads() 1285 return 1286 1287 group = kern.GetValueFromAddress(cmd_args[0], 'struct thread_call_group *') 1288 if not group: 1289 print "unknown arguments:", str(cmd_args) 1290 return False 1291 1292 PrintThreadGroup(group) 1293 1294@lldb_command('showallcallouts') 1295def ShowAllCallouts(cmd_args=None): 1296 """ Prints out the pending and delayed thread calls for the thread call groups 1297 1298 Callout flags: 1299 1300 A - Allocated memory owned by thread_call.c 1301 W - Wait - thread waiting for call to finish running 1302 D - Delayed - deadline based 1303 R - Running - currently executing on a thread 1304 S - Signal - call from timer interrupt instead of thread 1305 O - Once - pend the enqueue if re-armed while running 1306 P - Reschedule pending - enqueue is pending due to re-arm while running 1307 L - Rate-limited - (App Nap) 1308 C - Continuous time - Timeout is in mach_continuous_time 1309 I - Callout is an IOTimerEventSource 1310 """ 1311 index_max = GetEnumValue('thread_call_index_t::THREAD_CALL_INDEX_MAX') 1312 1313 for i in range (0, index_max) : 1314 group = addressof(kern.globals.thread_call_groups[i]) 1315 PrintThreadGroup(group) 1316 1317 print "Thread Call Threads:" 1318 PrintThreadCallThreads() 1319 1320# EndMacro: showallcallouts 1321 1322