ActivityStreamAPI.h 6.8 KB

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