1 //===-- Activity.h -----------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef __Genealogy_h__
11 #define __Genealogy_h__
12 
13 #include <string>
14 #include <vector>
15 #include <map>
16 #include <pthread.h>
17 #include <mach/task.h>
18 
19 #include "GenealogySPI.h"
20 #include "MachThreadList.h"
21 
22 class Genealogy
23 {
24 public:
25 
26     Genealogy ();
27 
28     ~Genealogy ()
29     {
30     }
31 
32     void
33     Clear();
34 
35     struct Breadcrumb
36     {
37         uint32_t breadcrumb_id;
38         uint64_t activity_id;
39         uint64_t timestamp;
40         std::string name;
41     };
42 
43     struct Activity
44     {
45         uint64_t activity_start;
46         uint64_t activity_id;
47         uint64_t parent_id;
48         std::string activity_name;
49         std::string reason;
50     };
51 
52     struct Message
53     {
54         uint64_t    timestamp;
55         uint64_t    activity_id;
56         uint64_t    trace_id;
57         uint64_t    thread;
58         uint8_t     type;  // OS_TRACE_TYPE_RELEASE, OS_TRACE_TYPE_DEBUG, OS_TRACE_TYPE_ERROR, OS_TRACE_TYPE_FAULT
59         uint32_t    process_info_index;  // index # of the image uuid/file path, 0 means unknown
60         std::string message;
61     };
62 
63     typedef std::vector<Message> MessageList;
64     typedef std::vector<Breadcrumb> BreadcrumbList;
65     typedef std::vector<Activity> ActivityList;
66 
67     struct ThreadActivity
68     {
69         Activity       current_activity;
70         MessageList    messages;
71         BreadcrumbList breadcrumbs;    // should be 0 or 1 breadcrumbs; no more than 1 BC for any given activity
72     };
73 
74     typedef std::shared_ptr<ThreadActivity>   ThreadActivitySP;
75 
76     ThreadActivitySP
77     GetGenealogyInfoForThread (pid_t pid, nub_thread_t tid, const MachThreadList &thread_list, task_t task, bool &timed_out);
78 
79     struct ProcessExecutableInfo
80     {
81         std::string image_path;
82         uuid_t      image_uuid;
83     };
84 
85     typedef std::shared_ptr<ProcessExecutableInfo> ProcessExecutableInfoSP;
86 
87     ProcessExecutableInfoSP
88     GetProcessExecutableInfosAtIndex(uint32_t idx);
89 
90     uint32_t
91     AddProcessExecutableInfo(ProcessExecutableInfoSP process_exe_info);
92 
93 private:
94 
95     void
96     GetActivities(pid_t pid, const MachThreadList &thread_list, task_t task);
97 
98     // the spi we need to call into libtrace - look them up via dlsym at runtime
99     bool                    (*m_os_activity_diagnostic_for_pid)  (pid_t pid, os_activity_t activity, uint32_t flags, os_diagnostic_block_t block);
100     void                    (*m_os_activity_iterate_processes) (os_activity_process_list_t processes, bool (^iterator)(os_activity_process_t process_info));
101     void                    (*m_os_activity_iterate_breadcrumbs) (os_activity_process_t process_info, bool (^iterator)(os_activity_breadcrumb_t breadcrumb));
102     void                    (*m_os_activity_iterate_messages) (os_trace_message_list_t messages, os_activity_process_t process_info, bool (^iterator)(os_trace_message_t tracemsg));
103     void                    (*m_os_activity_iterate_activities) (os_activity_list_t activities, os_activity_process_t process_info, bool (^iterator)(os_activity_entry_t activity));
104     uint8_t                 (*m_os_trace_get_type) (os_trace_message_t trace_msg);
105     char *                  (*m_os_trace_copy_formatted_message) (os_trace_message_t trace_msg);
106     os_activity_t           (*m_os_activity_for_thread) (os_activity_process_t process, uint64_t thread_id);
107     os_activity_t           (*m_os_activity_for_task_thread) (task_t target, uint64_t thread_id);
108     os_trace_message_list_t (*m_os_activity_messages_for_thread) (os_activity_process_t process, os_activity_t activity, uint64_t thread_id);
109 
110 
111     std::map<nub_thread_t, ThreadActivitySP> m_thread_activities;
112     std::vector<ProcessExecutableInfoSP>     m_process_executable_infos;
113     bool                                     m_diagnosticd_call_timed_out;
114 };
115 
116 #endif // __Genealogy_h__
117