2 #include <dispatch/dispatch.h>
6 #include <mach/task_info.h>
10 #include <sys/sysctl.h>
13 // from System.framework/Versions/B/PrivateHeaders/sys/codesign.h
14 #define CS_OPS_STATUS 0 /* return status */
15 #define CS_RESTRICT 0x0000800 /* tell dyld to treat restricted */
16 int csops(pid_t pid
, unsigned int ops
, void *useraddr
, size_t usersize
);
18 /* Step through the process table, find a matching process name, return
19 the pid of that matched process.
20 If there are multiple processes with that name, issue a warning on stdout
21 and return the highest numbered process.
22 The proc_pidpath() call is used which gets the full process name including
23 directories to the executable and the full (longer than 16 character)
26 pid_t
get_pid_for_process_name(const char *procname
) {
27 int process_count
= proc_listpids(PROC_ALL_PIDS
, 0, NULL
, 0) / sizeof(pid_t
);
28 if (process_count
< 1) {
29 printf("Only found %d processes running!\n", process_count
);
33 // Allocate a few extra slots in case new processes are spawned
34 int all_pids_size
= sizeof(pid_t
) * (process_count
+ 3);
35 pid_t
*all_pids
= (pid_t
*)malloc(all_pids_size
);
37 // re-set process_count in case the number of processes changed (got smaller;
38 // we won't do bigger)
40 proc_listpids(PROC_ALL_PIDS
, 0, all_pids
, all_pids_size
) / sizeof(pid_t
);
43 pid_t highest_pid
= 0;
45 for (i
= 1; i
< process_count
; i
++) {
46 char pidpath
[PATH_MAX
];
47 int pidpath_len
= proc_pidpath(all_pids
[i
], pidpath
, sizeof(pidpath
));
50 char *j
= strrchr(pidpath
, '/');
51 if ((j
== NULL
&& strcmp(procname
, pidpath
) == 0) ||
52 (j
!= NULL
&& strcmp(j
+ 1, procname
) == 0)) {
54 if (all_pids
[i
] > highest_pid
)
55 highest_pid
= all_pids
[i
];
60 if (match_count
== 0) {
61 printf("Did not find process '%s'.\n", procname
);
64 if (match_count
> 1) {
65 printf("Warning: More than one process '%s'!\n", procname
);
66 printf(" defaulting to the highest-pid one, %d\n", highest_pid
);
71 /* Given a pid, get the full executable name (including directory
72 paths and the longer-than-16-chars executable name) and return
73 the basename of that (i.e. do not include the directory components).
74 This function mallocs the memory for the string it returns;
75 the caller must free this memory. */
77 const char *get_process_name_for_pid(pid_t pid
) {
78 char tmp_name
[PATH_MAX
];
79 if (proc_pidpath(pid
, tmp_name
, sizeof(tmp_name
)) == 0) {
80 printf("Could not find process with pid of %d\n", (int)pid
);
83 if (strrchr(tmp_name
, '/'))
84 return strdup(strrchr(tmp_name
, '/') + 1);
86 return strdup(tmp_name
);
89 /* Get a struct kinfo_proc structure for a given pid.
90 Process name is required for error printing.
91 Gives you the current state of the process and whether it is being debugged
93 memory is malloc()'ed for the returned struct kinfo_proc
94 and must be freed by the caller. */
96 struct kinfo_proc
*get_kinfo_proc_for_pid(pid_t pid
, const char *process_name
) {
97 struct kinfo_proc
*kinfo
=
98 (struct kinfo_proc
*)malloc(sizeof(struct kinfo_proc
));
99 int mib
[] = {CTL_KERN
, KERN_PROC
, KERN_PROC_PID
, pid
};
100 size_t len
= sizeof(struct kinfo_proc
);
101 if (sysctl(mib
, sizeof(mib
) / sizeof(mib
[0]), kinfo
, &len
, NULL
, 0) != 0) {
103 printf("Could not get kinfo_proc for pid %d\n", (int)pid
);
109 /* Get the basic information (thread_basic_info_t) about a given
111 Gives you the suspend count; thread state; user time; system time; sleep
113 The return value is a pointer to malloc'ed memory - it is the caller's
114 responsibility to free it. */
116 thread_basic_info_t
get_thread_basic_info(thread_t thread
) {
118 integer_t
*thinfo
= (integer_t
*)malloc(sizeof(integer_t
) * THREAD_INFO_MAX
);
119 mach_msg_type_number_t thread_info_count
= THREAD_INFO_MAX
;
120 kr
= thread_info(thread
, THREAD_BASIC_INFO
, (thread_info_t
)thinfo
,
122 if (kr
!= KERN_SUCCESS
) {
123 printf("Error - unable to get basic thread info for a thread\n");
126 return (thread_basic_info_t
)thinfo
;
129 /* Get the thread identifier info (thread_identifier_info_data_t)
130 about a given thread.
131 Gives you the system-wide unique thread number; the pthread identifier number
134 thread_identifier_info_data_t
get_thread_identifier_info(thread_t thread
) {
136 thread_identifier_info_data_t tident
;
137 mach_msg_type_number_t tident_count
= THREAD_IDENTIFIER_INFO_COUNT
;
138 kr
= thread_info(thread
, THREAD_IDENTIFIER_INFO
, (thread_info_t
)&tident
,
140 if (kr
!= KERN_SUCCESS
) {
141 printf("Error - unable to get thread ident for a thread\n");
147 /* Given a mach port # (in the examine-threads mach port namespace) for a
149 find the mach port # in the inferior program's port namespace.
150 Sets inferior_port if successful.
151 Returns true if successful, false if unable to find the port number. */
153 bool inferior_namespace_mach_port_num(task_t task
,
154 thread_t examine_threads_port
,
155 thread_t
*inferior_port
) {
156 kern_return_t retval
;
157 mach_port_name_array_t names
;
158 mach_msg_type_number_t nameslen
;
159 mach_port_type_array_t types
;
160 mach_msg_type_number_t typeslen
;
162 if (inferior_port
== NULL
)
165 retval
= mach_port_names(task
, &names
, &nameslen
, &types
, &typeslen
);
166 if (retval
!= KERN_SUCCESS
) {
167 printf("Error - unable to get mach port names for inferior.\n");
171 for (i
= 0; i
< nameslen
; i
++) {
172 mach_port_t local_name
;
173 mach_msg_type_name_t local_type
;
174 retval
= mach_port_extract_right(task
, names
[i
], MACH_MSG_TYPE_COPY_SEND
,
175 &local_name
, &local_type
);
176 if (retval
== KERN_SUCCESS
) {
177 mach_port_deallocate(mach_task_self(), local_name
);
178 if (local_name
== examine_threads_port
) {
179 *inferior_port
= names
[i
];
180 vm_deallocate(mach_task_self(), (vm_address_t
)names
,
181 nameslen
* sizeof(mach_port_t
));
182 vm_deallocate(mach_task_self(), (vm_address_t
)types
,
183 typeslen
* sizeof(mach_port_t
));
188 vm_deallocate(mach_task_self(), (vm_address_t
)names
,
189 nameslen
* sizeof(mach_port_t
));
190 vm_deallocate(mach_task_self(), (vm_address_t
)types
,
191 typeslen
* sizeof(mach_port_t
));
195 /* Get the current pc value for a given thread. */
197 uint64_t get_current_pc(thread_t thread
, int *wordsize
) {
200 #if defined(__x86_64__) || defined(__i386__)
201 x86_thread_state_t gp_regs
;
202 mach_msg_type_number_t gp_count
= x86_THREAD_STATE_COUNT
;
203 kr
= thread_get_state(thread
, x86_THREAD_STATE
, (thread_state_t
)&gp_regs
,
205 if (kr
!= KERN_SUCCESS
) {
206 printf("Error - unable to get registers for a thread\n");
210 if (gp_regs
.tsh
.flavor
== x86_THREAD_STATE64
) {
212 return gp_regs
.uts
.ts64
.__rip
;
215 return gp_regs
.uts
.ts32
.__eip
;
220 arm_thread_state_t gp_regs
;
221 mach_msg_type_number_t gp_count
= ARM_THREAD_STATE_COUNT
;
222 kr
= thread_get_state(thread
, ARM_THREAD_STATE
, (thread_state_t
)&gp_regs
,
224 if (kr
!= KERN_SUCCESS
) {
225 printf("Error - unable to get registers for a thread\n");
232 #if defined(__arm64__)
233 arm_thread_state64_t gp_regs
;
234 mach_msg_type_number_t gp_count
= ARM_THREAD_STATE64_COUNT
;
235 kr
= thread_get_state(thread
, ARM_THREAD_STATE64
, (thread_state_t
)&gp_regs
,
237 if (kr
!= KERN_SUCCESS
) {
238 printf("Error - unable to get registers for a thread\n");
246 /* Get the proc_threadinfo for a given thread.
247 Gives you the thread name, if set; current and max priorities.
248 Returns 1 if successful
249 Returns 0 if proc_pidinfo() failed
252 int get_proc_threadinfo(pid_t pid
, uint64_t thread_handle
,
253 struct proc_threadinfo
*pth
) {
254 pth
->pth_name
[0] = '\0';
255 int ret
= proc_pidinfo(pid
, PROC_PIDTHREADINFO
, thread_handle
, pth
,
256 sizeof(struct proc_threadinfo
));
263 int main(int argc
, char **argv
) {
267 char *procname
= NULL
;
268 int arg_is_procname
= 0;
271 int resume_when_done
= 0;
272 mach_port_t mytask
= mach_task_self();
274 if (argc
!= 2 && argc
!= 3 && argc
!= 4 && argc
!= 5) {
275 printf("Usage: tdump [-l] [-v] [-r] pid/procname\n");
279 if (argc
== 3 || argc
== 4) {
281 while (i
< argc
- 1) {
282 if (strcmp(argv
[i
], "-l") == 0)
284 if (strcmp(argv
[i
], "-v") == 0)
286 if (strcmp(argv
[i
], "-r") == 0)
292 char *c
= argv
[argc
- 1];
294 printf("Usage: tdump [-l] [-v] pid/procname\n");
300 procname
= argv
[argc
- 1];
306 if (arg_is_procname
&& procname
) {
307 pid
= get_pid_for_process_name(procname
);
310 pid
= (pid_t
)strtol(argv
[argc
- 1], NULL
, 10);
311 if (pid
== 0 && errno
== EINVAL
) {
312 printf("Usage: tdump [-l] [-v] pid/procname\n");
317 const char *process_name
= get_process_name_for_pid(pid
);
319 // At this point "pid" is the process id and "process_name" is the process
321 // Now we have to get the process list from the kernel (which only has the
325 struct kinfo_proc
*kinfo
= get_kinfo_proc_for_pid(pid
, process_name
);
327 printf("pid %d (%s) is currently ", pid
, process_name
);
328 switch (kinfo
->kp_proc
.p_stat
) {
330 printf("being created by fork");
336 printf("sleeping on an address");
342 printf("zombie state - awaiting collection by parent");
347 if (kinfo
->kp_proc
.p_flag
& P_TRACED
)
348 printf(" and is being debugged.");
354 if (csops(pid
, CS_OPS_STATUS
, &csops_flags
, sizeof(csops_flags
)) != -1 &&
355 (csops_flags
& CS_RESTRICT
)) {
356 printf("pid %d (%s) is restricted so nothing can attach to it.\n", pid
,
360 kr
= task_for_pid(mach_task_self(), pid
, &task
);
361 if (kr
!= KERN_SUCCESS
) {
362 printf("Error - unable to task_for_pid()\n");
366 struct task_basic_info info
;
367 unsigned int info_count
= TASK_BASIC_INFO_COUNT
;
369 kr
= task_info(task
, TASK_BASIC_INFO
, (task_info_t
)&info
, &info_count
);
370 if (kr
!= KERN_SUCCESS
) {
371 printf("Error - unable to call task_info.\n");
374 printf("Task suspend count: %d.\n", info
.suspend_count
);
376 struct timespec
*rqtp
= (struct timespec
*)malloc(sizeof(struct timespec
));
378 rqtp
->tv_nsec
= 150000000;
384 printf("Iteration %d:\n", loop_cnt
++);
385 thread_array_t thread_list
;
386 mach_msg_type_number_t thread_count
;
388 kr
= task_threads(task
, &thread_list
, &thread_count
);
389 if (kr
!= KERN_SUCCESS
) {
390 printf("Error - unable to get thread list\n");
393 printf("pid %d has %d threads\n", pid
, thread_count
);
397 for (i
= 0; i
< thread_count
; i
++) {
398 thread_basic_info_t basic_info
= get_thread_basic_info(thread_list
[i
]);
400 thread_identifier_info_data_t identifier_info
=
401 get_thread_identifier_info(thread_list
[i
]);
404 uint64_t pc
= get_current_pc(thread_list
[i
], &wordsize
);
406 printf("thread #%d, system-wide-unique-tid 0x%llx, suspend count is %d, ",
407 i
, identifier_info
.thread_id
, basic_info
->suspend_count
);
409 printf("pc 0x%016llx, ", pc
);
411 printf("pc 0x%08llx, ", pc
);
412 printf("run state is ");
413 switch (basic_info
->run_state
) {
414 case TH_STATE_RUNNING
:
417 case TH_STATE_STOPPED
:
420 case TH_STATE_WAITING
:
423 case TH_STATE_UNINTERRUPTIBLE
:
424 puts("uninterruptible");
426 case TH_STATE_HALTED
:
433 printf(" pthread handle id 0x%llx (not the same value as "
434 "pthread_self() returns)\n",
435 (uint64_t)identifier_info
.thread_handle
);
437 struct proc_threadinfo pth
;
438 int proc_threadinfo_succeeded
=
439 get_proc_threadinfo(pid
, identifier_info
.thread_handle
, &pth
);
441 if (proc_threadinfo_succeeded
&& pth
.pth_name
[0] != '\0')
442 printf(" thread name '%s'\n", pth
.pth_name
);
444 printf(" libdispatch qaddr 0x%llx (not the same as the "
445 "dispatch_queue_t token)\n",
446 (uint64_t)identifier_info
.dispatch_qaddr
);
450 " (examine-threads port namespace) mach port # 0x%4.4x\n",
451 (int)thread_list
[i
]);
452 thread_t mach_port_inferior_namespace
;
453 if (inferior_namespace_mach_port_num(task
, thread_list
[i
],
454 &mach_port_inferior_namespace
))
455 printf(" (inferior port namepsace) mach port # 0x%4.4x\n",
456 (int)mach_port_inferior_namespace
);
457 printf(" user %d.%06ds, system %d.%06ds",
458 basic_info
->user_time
.seconds
,
459 basic_info
->user_time
.microseconds
,
460 basic_info
->system_time
.seconds
,
461 basic_info
->system_time
.microseconds
);
462 if (basic_info
->cpu_usage
> 0) {
463 float cpu_percentage
= basic_info
->cpu_usage
/ 10.0;
464 printf(", using %.1f%% cpu currently", cpu_percentage
);
466 if (basic_info
->sleep_time
> 0)
467 printf(", this thread has slept for %d seconds",
468 basic_info
->sleep_time
);
471 printf("scheduling policy %d", basic_info
->policy
);
473 if (basic_info
->flags
!= 0) {
474 printf(", flags %d", basic_info
->flags
);
475 if ((basic_info
->flags
| TH_FLAGS_SWAPPED
) == TH_FLAGS_SWAPPED
)
476 printf(" (thread is swapped out)");
477 if ((basic_info
->flags
| TH_FLAGS_IDLE
) == TH_FLAGS_IDLE
)
478 printf(" (thread is idle)");
480 if (proc_threadinfo_succeeded
)
481 printf(", current pri %d, max pri %d", pth
.pth_curpri
,
482 pth
.pth_maxpriority
);
487 free((void *)basic_info
);
491 vm_deallocate(mytask
, (vm_address_t
)thread_list
,
492 thread_count
* sizeof(thread_act_t
));
493 nanosleep(rqtp
, NULL
);
496 while (resume_when_done
> 0) {
497 kern_return_t err
= task_resume(task
);
498 if (err
!= KERN_SUCCESS
)
499 printf("Error resuming task: %d.", err
);
503 vm_deallocate(mytask
, (vm_address_t
)task
, sizeof(task_t
));
504 free((void *)process_name
);