1 #!/usr/local/bin/luatrace -s
 
   3 trace_codename = function(codename, callback)
 
   4         local debugid = trace.debugid(codename)
 
   6                 trace.single(debugid,callback)
 
   8                 printf("WARNING: Cannot locate debugid for '%s'\n", codename)
 
  13 workqueue_ptr_map = {};
 
  14 get_prefix = function(buf)
 
  15         if initial_timestamp == 0 then
 
  16                 initial_timestamp = buf.timestamp
 
  18         local secs = trace.convert_timestamp_to_nanoseconds(buf.timestamp - initial_timestamp) / 1000000000
 
  21         if trace.debugid_is_start(buf.debugid) then
 
  23         elseif trace.debugid_is_end(buf.debugid) then
 
  30         if buf.command ~= "kernel_task" then
 
  32                 workqueue_ptr_map[buf[1]] = buf.command
 
  33         elseif workqueue_ptr_map[buf[1]] ~= nil then
 
  34                 proc = workqueue_ptr_map[buf[1]]
 
  39         return string.format("%s %6.9f %-17s [%05d.%06x] %-24s",
 
  40                 prefix, secs, proc, buf.pid, buf.threadid, buf.debugname)
 
  43 trace_codename("pthread_thread_create", function(buf)
 
  44     local prefix = get_prefix(buf)
 
  45     if trace.debugid_is_start(buf.debugid) then
 
  46         printf("%s\tthread creation request: 0x%x\n", prefix, buf[1])
 
  47     elseif trace.debugid_is_end(buf.debugid) then
 
  48         printf("%s\t  thread creation complete: pthread 0x%x (error: %d)\n", prefix, buf[2], buf[1])
 
  49     elseif buf[4] == 2 then
 
  50         printf("%s\t  thread stack created: 0x%x + 0x%x\n", prefix, buf[2], buf[1])
 
  51     elseif buf[4] == 3 then
 
  52         printf("%s\t  thread using custom stack")
 
  56 trace_codename("pthread_thread_terminate", function(buf)
 
  57     local prefix = get_prefix(buf)
 
  58     if trace.debugid_is_start(buf.debugid) then
 
  59         printf("%s\tthread terminate: stack 0x%x + 0x%x, kthport 0x%x\n", prefix, buf[1], buf[2], buf[3])
 
  60     elseif trace.debugid_is_end(buf.debugid) then
 
  61         printf("%s\t  thread terminate: ret %d\n", prefix, buf[1])
 
  65 -- The trace codes we need aren't enabled by default
 
  66 darwin.sysctlbyname("kern.pthread_debug_tracing", 1)
 
  67 completion_handler = function()
 
  68         darwin.sysctlbyname("kern.pthread_debug_tracing", 0)
 
  70 trace.set_completion_handler(completion_handler)