ActivityStreamAPI.h 6.8 KB

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