1 //===-- ActivityStreamSPI.h -------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_DARWINLOG_ACTIVITYSTREAMSPI_H
10 #define LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_DARWINLOG_ACTIVITYSTREAMSPI_H
11 
12 #include <sys/time.h>
13 #include <xpc/xpc.h>
14 
15 #define OS_ACTIVITY_MAX_CALLSTACK 32
16 
17 // Enums
18 
19 enum {
20   OS_ACTIVITY_STREAM_PROCESS_ONLY = 0x00000001,
21   OS_ACTIVITY_STREAM_SKIP_DECODE = 0x00000002,
22   OS_ACTIVITY_STREAM_PAYLOAD = 0x00000004,
23   OS_ACTIVITY_STREAM_HISTORICAL = 0x00000008,
24   OS_ACTIVITY_STREAM_CALLSTACK = 0x00000010,
25   OS_ACTIVITY_STREAM_DEBUG = 0x00000020,
26   OS_ACTIVITY_STREAM_BUFFERED = 0x00000040,
27   OS_ACTIVITY_STREAM_NO_SENSITIVE = 0x00000080,
28   OS_ACTIVITY_STREAM_INFO = 0x00000100,
29   OS_ACTIVITY_STREAM_PROMISCUOUS = 0x00000200,
30   OS_ACTIVITY_STREAM_PRECISE_TIMESTAMPS = 0x00000200
31 };
32 typedef uint32_t os_activity_stream_flag_t;
33 
34 enum {
35   OS_ACTIVITY_STREAM_TYPE_ACTIVITY_CREATE = 0x0201,
36   OS_ACTIVITY_STREAM_TYPE_ACTIVITY_TRANSITION = 0x0202,
37   OS_ACTIVITY_STREAM_TYPE_ACTIVITY_USERACTION = 0x0203,
38 
39   OS_ACTIVITY_STREAM_TYPE_TRACE_MESSAGE = 0x0300,
40 
41   OS_ACTIVITY_STREAM_TYPE_LOG_MESSAGE = 0x0400,
42   OS_ACTIVITY_STREAM_TYPE_LEGACY_LOG_MESSAGE = 0x0480,
43 
44   OS_ACTIVITY_STREAM_TYPE_SIGNPOST_BEGIN = 0x0601,
45   OS_ACTIVITY_STREAM_TYPE_SIGNPOST_END = 0x0602,
46   OS_ACTIVITY_STREAM_TYPE_SIGNPOST_EVENT = 0x0603,
47 
48   OS_ACTIVITY_STREAM_TYPE_STATEDUMP_EVENT = 0x0A00,
49 };
50 typedef uint32_t os_activity_stream_type_t;
51 
52 enum {
53   OS_ACTIVITY_STREAM_EVENT_STARTED = 1,
54   OS_ACTIVITY_STREAM_EVENT_STOPPED = 2,
55   OS_ACTIVITY_STREAM_EVENT_FAILED = 3,
56   OS_ACTIVITY_STREAM_EVENT_CHUNK_STARTED = 4,
57   OS_ACTIVITY_STREAM_EVENT_CHUNK_FINISHED = 5,
58 };
59 typedef uint32_t os_activity_stream_event_t;
60 
61 // Types
62 
63 typedef uint64_t os_activity_id_t;
64 typedef struct os_activity_stream_s *os_activity_stream_t;
65 typedef struct os_activity_stream_entry_s *os_activity_stream_entry_t;
66 
67 #define OS_ACTIVITY_STREAM_COMMON()                                            \
68   uint64_t trace_id;                                                           \
69   uint64_t timestamp;                                                          \
70   uint64_t thread;                                                             \
71   const uint8_t *image_uuid;                                                   \
72   const char *image_path;                                                      \
73   struct timeval tv_gmt;                                                       \
74   struct timezone tz;                                                          \
75   uint32_t offset
76 
77 typedef struct os_activity_stream_common_s {
78   OS_ACTIVITY_STREAM_COMMON();
79 } * os_activity_stream_common_t;
80 
81 struct os_activity_create_s {
82   OS_ACTIVITY_STREAM_COMMON();
83   const char *name;
84   os_activity_id_t creator_aid;
85   uint64_t unique_pid;
86 };
87 
88 struct os_activity_transition_s {
89   OS_ACTIVITY_STREAM_COMMON();
90   os_activity_id_t transition_id;
91 };
92 
93 typedef struct os_log_message_s {
94   OS_ACTIVITY_STREAM_COMMON();
95   const char *format;
96   const uint8_t *buffer;
97   size_t buffer_sz;
98   const uint8_t *privdata;
99   size_t privdata_sz;
100   const char *subsystem;
101   const char *category;
102   uint32_t oversize_id;
103   uint8_t ttl;
104   bool persisted;
105 } * os_log_message_t;
106 
107 typedef struct os_trace_message_v2_s {
108   OS_ACTIVITY_STREAM_COMMON();
109   const char *format;
110   const void *buffer;
111   size_t bufferLen;
112   xpc_object_t __unsafe_unretained payload;
113 } * os_trace_message_v2_t;
114 
115 typedef struct os_activity_useraction_s {
116   OS_ACTIVITY_STREAM_COMMON();
117   const char *action;
118   bool persisted;
119 } * os_activity_useraction_t;
120 
121 typedef struct os_signpost_s {
122   OS_ACTIVITY_STREAM_COMMON();
123   const char *format;
124   const uint8_t *buffer;
125   size_t buffer_sz;
126   const uint8_t *privdata;
127   size_t privdata_sz;
128   const char *subsystem;
129   const char *category;
130   uint64_t duration_nsec;
131   uint32_t callstack_depth;
132   uint64_t callstack[OS_ACTIVITY_MAX_CALLSTACK];
133 } * os_signpost_t;
134 
135 typedef struct os_activity_statedump_s {
136   OS_ACTIVITY_STREAM_COMMON();
137   char *message;
138   size_t message_size;
139   char image_path_buffer[PATH_MAX];
140 } * os_activity_statedump_t;
141 
142 struct os_activity_stream_entry_s {
143   os_activity_stream_type_t type;
144 
145   // information about the process streaming the data
146   pid_t pid;
147   uint64_t proc_id;
148   const uint8_t *proc_imageuuid;
149   const char *proc_imagepath;
150 
151   // the activity associated with this streamed event
152   os_activity_id_t activity_id;
153   os_activity_id_t parent_id;
154 
155   union {
156     struct os_activity_stream_common_s common;
157     struct os_activity_create_s activity_create;
158     struct os_activity_transition_s activity_transition;
159     struct os_log_message_s log_message;
160     struct os_trace_message_v2_s trace_message;
161     struct os_activity_useraction_s useraction;
162     struct os_signpost_s signpost;
163     struct os_activity_statedump_s statedump;
164   };
165 };
166 
167 // Blocks
168 
169 typedef bool (^os_activity_stream_block_t)(os_activity_stream_entry_t entry,
170                                            int error);
171 
172 typedef void (^os_activity_stream_event_block_t)(
173     os_activity_stream_t stream, os_activity_stream_event_t event);
174 
175 // SPI entry point prototypes
176 
177 typedef os_activity_stream_t (*os_activity_stream_for_pid_t)(
178     pid_t pid, os_activity_stream_flag_t flags,
179     os_activity_stream_block_t stream_block);
180 
181 typedef void (*os_activity_stream_resume_t)(os_activity_stream_t stream);
182 
183 typedef void (*os_activity_stream_cancel_t)(os_activity_stream_t stream);
184 
185 typedef char *(*os_log_copy_formatted_message_t)(os_log_message_t log_message);
186 
187 typedef void (*os_activity_stream_set_event_handler_t)(
188     os_activity_stream_t stream, os_activity_stream_event_block_t block);
189 
190 #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_MACOSX_DARWINLOG_ACTIVITYSTREAMSPI_H
191