5 // Created by James McIlree on 2/5/14.
6 // Copyright (c) 2014 Apple. All rights reserved.
9 #ifndef msa_EventProcessing_hpp
10 #define msa_EventProcessing_hpp
12 template <typename SIZE>
13 bool is_mach_msg_interesting(const Machine<SIZE>& machine, const MachineMachMsg<SIZE>* mach_msg)
15 // If this message is carrying importance, it is interesting.
16 if ((mach_msg->has_sender() && MACH_MSGH_BITS_RAISED_IMPORTANCE(mach_msg->send_msgh_bits())) ||
17 (mach_msg->has_receiver() && MACH_MSGH_BITS_RAISED_IMPORTANCE(mach_msg->recv_msgh_bits())))
20 // If this message has a non-null voucher, it is interesting.
21 if ((mach_msg->has_sender() && !mach_msg->send_voucher()->is_null()) ||
22 (mach_msg->has_receiver() && !mach_msg->recv_voucher()->is_null()))
25 // If the message does NOT have a voucher, and the sender has a voucher set, it is interesting.
26 if (mach_msg->has_sender()) {
27 if (const MachineThread<SIZE>* sender_thread = machine.thread(mach_msg->send_tid(), mach_msg->send_time())) {
28 const MachineVoucher<SIZE>* sender_voucher = sender_thread->voucher(mach_msg->send_time());
29 if (!sender_voucher->is_unset() && !sender_voucher->is_null()) {
38 template <typename SIZE>
39 void reset_task_data_on_exec_or_exit(const MachineProcess<SIZE>& process,
40 std::unordered_map<pid_t, bool>& task_appnap_state,
41 std::unordered_map<pid_t, TaskRequestedPolicy>& task_requested_state,
42 std::unordered_map<pid_t, std::pair<TaskEffectivePolicy, uint32_t>>& task_effective_state,
43 std::unordered_map<pid_t, std::pair<uint32_t, uint32_t>>& task_boosts)
45 ASSERT(!process.is_kernel(), "Kernel process should not ever exec or exit");
46 ASSERT(process.pid() > 0, "Process with pid less than 1 exec'd ?");
48 if (pid_t pid = process.pid()) {
49 auto task_appnap_it = task_appnap_state.find(pid);
50 if (task_appnap_it != task_appnap_state.end()) {
51 task_appnap_state.erase(task_appnap_it);
54 auto task_requested_it = task_requested_state.find(pid);
55 if (task_requested_it != task_requested_state.end()) {
56 task_requested_state.erase(task_requested_it);
59 auto task_effective_it = task_effective_state.find(pid);
60 if (task_effective_it != task_effective_state.end()) {
61 task_effective_state.erase(task_effective_it);
64 auto task_boosts_it = task_boosts.find(pid);
65 if (task_boosts_it != task_boosts.end()) {
66 task_boosts.erase(task_boosts_it);
71 // From osfmk/kern/task.h
72 #define TASK_POLICY_INTERNAL 0x0
73 #define TASK_POLICY_EXTERNAL 0x1
75 #define TASK_POLICY_TASK 0x4
76 #define TASK_POLICY_THREAD 0x8
78 template <typename SIZE>
79 void process_events(Globals& globals,
80 const Machine<SIZE>& machine,
81 std::unordered_map<pid_t, bool>& task_appnap_state,
82 std::unordered_map<pid_t, TaskRequestedPolicy>& task_requested_state,
83 std::unordered_map<typename SIZE::ptr_t, TaskRequestedPolicy>& thread_requested_state,
84 std::unordered_map<pid_t, std::pair<TaskEffectivePolicy, uint32_t>>& task_effective_state,
85 std::unordered_map<typename SIZE::ptr_t, std::pair<TaskEffectivePolicy, uint32_t>>& thread_effective_state,
86 std::unordered_map<pid_t, std::pair<uint32_t, uint32_t>>& task_boosts)
88 const KDEvent<SIZE>* events = machine.events();
89 uintptr_t count = machine.event_count();
91 ASSERT(count, "Expected at least one event");
94 // Filtering thoughts...
96 // Two levels of filtering.
98 // 1) global supression of events that are "uninteresting".
100 // We filter on each event "class", with a keyword, so something like
102 // --lifecycle [ all | user | none ] ;; This is fork, exec, exit, thread-create, thread-exit
103 // --mach-msgs [ all | user | voucher | none ] ;; This is all mach msgs
105 // 2) targetted supression of events that are not related to a user focus.
107 // We filter by process name/pid
109 // --track [ pid | name ]
112 PrintBuffer print_buffer(8192, 1024, globals.output_fd());
114 for (uintptr_t index=0; index < count; ++index) {
115 const KDEvent<SIZE>& event = events[index];
121 switch (event.dbg_cooked()) {
122 case TRACE_DATA_EXEC: {
123 bool should_print = false;
124 if (globals.lifecycle_filter() >= kLifecycleFilter::User)
128 print_generic(print_buffer, globals, machine, event, index, "exec");
130 if (const MachineThread<SIZE>* exec_thread = machine.thread(event.tid(), event.timestamp())) {
131 reset_task_data_on_exec_or_exit(exec_thread->process(), task_appnap_state, task_requested_state, task_effective_state, task_boosts);
136 case TRACE_DATA_NEWTHREAD: {
137 bool should_print = false;
138 auto new_thread_tid = (typename SIZE::ptr_t)event.arg1();
139 if (const MachineThread<SIZE>* new_thread = machine.thread(new_thread_tid, event.timestamp())) {
140 switch (globals.lifecycle_filter()) {
141 case kLifecycleFilter::None:
143 case kLifecycleFilter::User:
144 if (!new_thread->process().is_kernel())
147 case kLifecycleFilter::All:
153 auto& new_process = new_thread->process();
154 ASSERT(new_process.pid() == (pid_t)event.arg2(), "Pid does not match");
155 if (new_process.timespan().location() == event.timestamp()) {
156 print_fork(print_buffer, globals, machine, event, index, new_process);
159 // We're not printing the actual event data, but instead the exiting thread's data:
160 print_base(print_buffer, globals, event.timestamp(), new_thread, event, index, "thread-create", true);
166 case TRACEDBG_CODE(DBG_TRACE_DATA, TRACE_DATA_THREAD_TERMINATE): {
167 // This event may spawn two prints
169 // 1) thread termination
170 // 2) task termination
171 bool should_print = false;
172 typename SIZE::ptr_t terminated_tid = event.arg1();
173 if (const MachineThread<SIZE>* terminated_thread = machine.thread(terminated_tid, event.timestamp())) {
174 switch (globals.lifecycle_filter()) {
175 case kLifecycleFilter::None:
177 case kLifecycleFilter::User:
178 if (!terminated_thread->process().is_kernel())
181 case kLifecycleFilter::All:
187 // We're not printing the actual event data, but instead the exiting thread's data:
188 print_base(print_buffer, globals, event.timestamp(), terminated_thread, event, index, "thread-exit", true);
191 // Was this the last thread in the process? (Do we also need to print a process exit?)
192 auto& terminated_process = terminated_thread->process();
193 if (terminated_process.is_trace_terminated()) {
194 if (event.timestamp() >= terminated_process.exit_timestamp()) {
196 print_exit(print_buffer, globals, event, terminated_thread, index);
198 reset_task_data_on_exec_or_exit(terminated_process, task_appnap_state, task_requested_state, task_effective_state, task_boosts);
202 auto thread_requested_it = thread_requested_state.find(terminated_tid);
203 if (thread_requested_it != thread_requested_state.end()) {
204 thread_requested_state.erase(thread_requested_it);
207 auto thread_effective_it = thread_effective_state.find(terminated_tid);
208 if (thread_effective_it != thread_effective_state.end()) {
209 thread_effective_state.erase(thread_effective_it);
212 ASSERT(false, "Failed to find exit thread");
216 case MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_MSG_SEND): {
217 // trace event data is:
218 // kmsg_addr, msgh_bits, msgh_id, voucher_addr,
222 // For now, we aren't recording mach msg's with endpoints in
223 // the kernel. If we don't find a mach msg, assume its a kernel
225 if (const MachineMachMsg<SIZE>* mach_msg = machine.mach_msg(index)) {
226 if (is_mach_msg_interesting(machine, mach_msg)) {
227 print_mach_msg(print_buffer, globals, machine, event, index, true, mach_msg);
233 case MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_MSG_RECV):
234 case MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_MSG_RECV_VOUCHER_REFUSED): {
235 // trace event data is
236 // kmsg_addr, msgh_bits, msgh_id, recv_voucher_addr
240 // For now, we aren't recording mach msg's with endpoints in
241 // the kernel. If we don't find a mach msg, assume its a kernel
243 if (const MachineMachMsg<SIZE>* mach_msg = machine.mach_msg(index)) {
244 if (is_mach_msg_interesting(machine, mach_msg)) {
245 print_mach_msg(print_buffer, globals, machine, event, index, false, mach_msg);
251 case MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_VOUCHER_CREATE): {
252 // trace event data is
253 // voucher address, voucher table size, system voucher count, voucher content bytes
255 if (auto voucher = machine.voucher(event.arg1(), event.timestamp())) {
256 print_voucher(print_buffer, globals, machine, event, index, "voucher_create", voucher, true);
258 if (voucher->has_valid_contents()) {
259 print_voucher_contents(print_buffer, globals, machine, event, index, voucher);
262 ASSERT(false, "Failed to find voucher");
267 case MACHDBG_CODE(DBG_MACH_IPC, MACH_IPC_VOUCHER_DESTROY): {
268 // trace event data is
269 // voucher address, 0, system voucher count, 0
270 if (auto voucher = machine.voucher(event.arg1(), event.timestamp())) {
271 print_voucher(print_buffer, globals, machine, event, index, "voucher_destroy", voucher, false);
273 ASSERT(false, "Failed to find voucher");
278 case MACHDBG_CODE(DBG_MACH_IPC, MACH_THREAD_SET_VOUCHER): {
279 print_generic(print_buffer, globals, machine, event, index, "thread_adopt_voucher");
283 case IMPORTANCE_CODE(IMP_ASSERTION, IMP_EXTERN):
284 print_importance_assert(print_buffer, globals, machine, event, index, "externalize_importance", task_boosts);
287 case IMPORTANCE_CODE(IMP_ASSERTION, IMP_HOLD | TASK_POLICY_EXTERNAL):
288 case IMPORTANCE_CODE(IMP_ASSERTION, IMP_HOLD | TASK_POLICY_INTERNAL):
289 print_importance_assert(print_buffer, globals, machine, event, index, "importance_hold", task_boosts);
292 case IMPORTANCE_CODE(IMP_ASSERTION, IMP_DROP | TASK_POLICY_EXTERNAL):
293 case IMPORTANCE_CODE(IMP_ASSERTION, IMP_DROP | TASK_POLICY_INTERNAL):
294 print_importance_assert(print_buffer, globals, machine, event, index, "importance_drop", task_boosts);
297 case IMPORTANCE_CODE(IMP_WATCHPORT, 0):
299 // proc_selfpid(), pid, boost, released_pid, 0);
300 if (event.arg3() > 0) {
301 print_watchport_importance_transfer(print_buffer, globals, machine, event, index);
305 case IMPORTANCE_CODE(IMP_TASK_SUPPRESSION, 0):
306 case IMPORTANCE_CODE(IMP_TASK_SUPPRESSION, 1):
308 // self_pid, audit_token_pid_from_task(task), trequested_0(task, NULL), trequested_1(task, NULL)
309 print_appnap(print_buffer, globals, machine, event, index, (bool)event.dbg_code(), task_appnap_state, task_requested_state);
312 case IMPORTANCE_CODE(IMP_BOOST, IMP_BOOSTED):
313 case IMPORTANCE_CODE(IMP_BOOST, IMP_UNBOOSTED):
315 // proc_selfpid(), audit_token_pid_from_task(task), trequested_0(task, NULL), trequested_1(task, NULL)
316 if (event.is_func_start()) {
317 print_boost(print_buffer, globals, machine, event, index, (pid_t)event.arg2(), (event.dbg_code() == IMP_BOOSTED));
322 // IMP_TASK_APPTYPE trace args are:
325 // target_pid, trequested_0, trequested_1, apptype
327 // target_pid, trequested_0, trequested_1, is_importance_receiver
328 case IMPORTANCE_CODE(IMP_TASK_APPTYPE, TASK_APPTYPE_NONE):
329 case IMPORTANCE_CODE(IMP_TASK_APPTYPE, TASK_APPTYPE_DAEMON_INTERACTIVE):
330 case IMPORTANCE_CODE(IMP_TASK_APPTYPE, TASK_APPTYPE_DAEMON_STANDARD):
331 case IMPORTANCE_CODE(IMP_TASK_APPTYPE, TASK_APPTYPE_DAEMON_ADAPTIVE):
332 case IMPORTANCE_CODE(IMP_TASK_APPTYPE, TASK_APPTYPE_DAEMON_BACKGROUND):
333 case IMPORTANCE_CODE(IMP_TASK_APPTYPE, TASK_APPTYPE_APP_DEFAULT):
334 case IMPORTANCE_CODE(IMP_TASK_APPTYPE, TASK_APPTYPE_APP_TAL):
335 if (event.is_func_end()) {
336 print_importance_apptype(print_buffer, globals, machine, event, index);
338 // FIX ME, not handling trequested status.
340 // process_trequested_task(print_buffer, globals, machine, event, index, (pid_t)event.arg1(), event.arg2(), event.arg3(), task_requested_policies);
344 case IMPORTANCE_CODE(IMP_UPDATE, (IMP_UPDATE_TASK_CREATE | TASK_POLICY_TASK)):
346 // targetpid, teffective_0(task, NULL), teffective_1(task, NULL), tpriority(task, NULL)
347 print_importance_update_task(print_buffer, globals, machine, event, index, "imp_update_task_create", task_effective_state);
350 case IMPORTANCE_CODE(IMP_UPDATE, (IMP_UPDATE_TASK_CREATE | TASK_POLICY_THREAD)):
352 // targettid, teffective_0(task, thread), teffective_1(task, thread), tpriority(thread, NULL)
353 print_importance_update_thread(print_buffer, globals, machine, event, index, "imp_update_thread_create", thread_effective_state);
356 case IMPORTANCE_CODE(IMP_UPDATE, TASK_POLICY_TASK):
358 // targetpid, teffective_0(task, NULL), teffective_1(task, NULL), tpriority(task, THREAD_NULL)
359 print_importance_update_task(print_buffer, globals, machine, event, index, "imp_update_task", task_effective_state);
362 case IMPORTANCE_CODE(IMP_UPDATE, TASK_POLICY_THREAD):
364 // targettid, teffective_0(task, thread), teffective_1(task, thread), tpriority(task, THREAD_NULL)
365 print_importance_update_thread(print_buffer, globals, machine, event, index, "imp_update_thread", thread_effective_state);
368 case IMPORTANCE_CODE(IMP_MSG, IMP_MSG_SEND):
370 // current_pid, sender_pid, imp_msgh_id, (bool)importance_cleared
372 // NOTE! Only end events carry "importance cleared"
373 if (event.is_func_end() && (event.arg4() != 0)) {
374 print_importance_send_failed(print_buffer, globals, machine, event, index);
378 case IMPORTANCE_CODE(IMP_MSG, IMP_MSG_DELV): {
380 // sending_pid, task_pid /* recv_pid?? */, msgh_id, impresult
384 // 0: BOOST NOT APPLIED
385 // 1: BOOST EXTERNALIZED
386 // 2: LIVE_IMPORTANCE_LINKAGE!
387 print_impdelv(print_buffer, globals, machine, event, index, (pid_t)event.arg1(), (uint32_t)event.arg4());
392 if (event.dbg_class() == DBG_IMPORTANCE) {
394 // Every task policy set trace code carries "trequested" data, we would like to grab them all.
396 // This subclass spans the range of 0x20 through 0x3F
399 uint32_t subclass = event.dbg_subclass();
400 if (subclass >= 0x20 && subclass <= 0x3F) {
401 // Trace event data is
402 // targetid(task, thread), trequested_0(task, thread), trequested_1(task, thread), value
404 bool is_task_event = (event.dbg_code() & TASK_POLICY_TASK) > 0;
406 // Should not be both a task and thread event.
407 ASSERT(is_task_event != (event.dbg_code() & TASK_POLICY_THREAD), "BEWM!");
410 // FIX ME, not handling trequested status.
412 // process_trequested_task(print_buffer, globals, machine, event, index, (pid_t)event.arg1(), event.arg2(), event.arg3(), task_requested_policies);
414 // FIX ME, not handling trequested status.
416 // process_trequested_thread(print_buffer, globals, machine, event, index, event.arg1(), event.arg2(), event.arg3(), task_requested_policies, thread_requested_policies);