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