1 #include <AvailabilityMacros.h>
2 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER
3 #include </System/Library/Frameworks/System.framework/PrivateHeaders/mach/thread_policy.h>
6 #include <mach/mach_traps.h>
7 #include <mach/mach_error.h>
8 #include <mach/mach_time.h>
17 #define DBG(x...) do { \
18 if (verbosity > 1) { \
23 #define mutter(x...) do { \
24 if (verbosity > 0) { \
29 #define s_if_plural(x) (((x) > 1) ? "s" : "")
35 "usage: tags [-i] interactive/input\n"
36 " [-v V] verbosity level 0..2 (1)\n"
38 " pid process id of target task\n"
44 thread_tag_set(thread_t thread
, int tag
)
47 thread_affinity_policy_data_t policy
;
49 policy
.affinity_tag
= tag
;
50 ret
= thread_policy_set(
51 thread
, THREAD_AFFINITY_POLICY
,
52 (thread_policy_t
) &policy
,
53 THREAD_AFFINITY_POLICY_COUNT
);
54 if (ret
!= KERN_SUCCESS
) {
55 printf("thread_policy_set(1) returned %d\n", ret
);
61 thread_tag_get(thread_t thread
)
64 boolean_t get_default
= FALSE
;
65 thread_affinity_policy_data_t policy
;
66 mach_msg_type_number_t count
= THREAD_AFFINITY_POLICY_COUNT
;
68 ret
= thread_policy_get(
69 thread
, THREAD_AFFINITY_POLICY
,
70 (thread_policy_t
) &policy
, &count
, &get_default
);
71 if (ret
!= KERN_SUCCESS
) {
72 printf("thread_policy_set(1) returned %d\n", ret
);
76 return policy
.affinity_tag
;
81 main(int argc
, char *argv
[])
84 mach_port_name_t port
;
87 thread_act_t
*thread_array
;
88 mach_msg_type_number_t num_threads
;
90 boolean_t interactive
= FALSE
;
94 printf("Must be run as root\n");
98 /* Do switch parsing: */
99 while ((c
= getopt (argc
, argv
, "hiv:")) != -1) {
105 verbosity
= atoi(optarg
);
113 argc
-= optind
; argv
+= optind
;
117 ret
= task_for_pid(mach_task_self(), pid
, &port
);
118 if (ret
!= KERN_SUCCESS
)
119 err(1, "task_for_pid(,%d,) returned %d", pid
, ret
);
121 mutter("task %p\n", port
);
122 ret
= task_threads(port
, &thread_array
, &num_threads
);
123 if (ret
!= KERN_SUCCESS
)
124 err(1, "task_threads() returned %d", pid
, ret
);
126 for (i
= 0; i
< num_threads
; i
++) {
127 printf(" %d: thread 0x%08x tag %d\n",
128 i
, thread_array
[i
], thread_tag_get(thread_array
[i
]));
131 while (interactive
) {
132 printf("Enter new tag or <return> to skip or ^D to quit\n");
133 for (i
= 0; i
< num_threads
; i
++) {
134 tag
= thread_tag_get(thread_array
[i
]);
135 printf(" %d: thread 0x%08x tag %d: ",
136 i
, thread_array
[i
], tag
);
138 (void) fgets(input
, 20, stdin
);
144 if (strlen(input
) > 1) {
146 thread_tag_set(thread_array
[i
], tag
);