1 #include "ep-rt-config.h"
3 #ifdef ENABLE_PERFTRACING
4 #if !defined(EP_INCLUDE_SOURCE_FILES) || defined(EP_FORCE_INCLUDE_SOURCE_FILES)
6 #define EP_IMPL_EVENT_SOURCE_GETTER_SETTER
8 #include "ep-event-source.h"
9 #include "ep-event-payload.h"
10 #include "ep-metadata-generator.h"
11 #include "ep-session.h"
12 #include "ep-session-provider.h"
15 #if defined(HOST_WINDOWS) || defined(HOST_WIN32)
16 const ep_char8_t* _ep_os_info = "Windows";
17 #elif defined(HOST_TVOS)
18 const ep_char8_t* _ep_os_info = "tvOS";
19 #elif defined(HOST_IOS)
20 const ep_char8_t* _ep_os_info = "iOS";
21 #elif defined(HOST_WATCHOS)
22 const ep_char8_t* _ep_os_info = "watchOS";
23 #elif defined(HOST_MACCAT)
24 const ep_char8_t* _ep_os_info = "MacCatalyst";
25 #elif defined(__APPLE__)
26 const ep_char8_t* _ep_os_info = "macOS";
27 #elif defined(HOST_ANDROID)
28 const ep_char8_t* _ep_os_info = "Android";
29 #elif defined(__linux__)
30 const ep_char8_t* _ep_os_info = "Linux";
32 const ep_char8_t* _ep_os_info = "Unknown";
35 #if defined(TARGET_X86)
36 const ep_char8_t* _ep_arch_info = "x86";
37 #elif defined(TARGET_AMD64)
38 const ep_char8_t* _ep_arch_info = "x64";
39 #elif defined(TARGET_ARMV6)
40 const ep_char8_t* _ep_arch_info = "arm32";
41 #elif defined(TARGET_ARM)
42 const ep_char8_t* _ep_arch_info = "arm32";
43 #elif defined(TARGET_ARM64)
44 const ep_char8_t* _ep_arch_info = "arm64";
45 #elif defined(TARGET_S390X)
46 const ep_char8_t* _ep_arch_info = "s390x";
47 #elif defined(TARGET_LOONGARCH64)
48 const ep_char8_t* _ep_arch_info = "loongarch64";
49 #elif defined(TARGET_POWERPC64)
50 const ep_char8_t* _ep_arch_info = "ppc64le";
51 #elif defined(TARGET_RISCV64)
52 const ep_char8_t* _ep_arch_info = "riscv64";
54 const ep_char8_t* _ep_arch_info = "Unknown";
57 EventPipeEventSource _ep_event_source_instance = { 0 };
60 * Forward declares of all static functions.
65 event_source_fini (EventPipeEventSource *event_source);
68 * EventPipeEventSource.
73 event_source_fini (EventPipeEventSource *event_source)
75 ep_delete_provider (event_source->provider);
78 EventPipeEventSource *
79 ep_event_source_alloc (void)
81 EventPipeEventSource *instance = ep_rt_object_alloc (EventPipeEventSource);
82 ep_raise_error_if_nok (instance != NULL);
83 ep_raise_error_if_nok (ep_event_source_init (instance) != NULL);
89 ep_event_source_free (instance);
91 ep_exit_error_handler ();
94 EventPipeEventSource *
95 ep_event_source_init (EventPipeEventSource *event_source)
97 ep_char16_t *command_line_arg_utf16 = NULL;
98 ep_char16_t *os_info_arg_utf16 = NULL;
99 ep_char16_t *arch_info_arg_utf16 = NULL;
100 ep_char16_t *event_name_utf16 = NULL;
101 uint8_t *metadata = NULL;
103 EP_ASSERT (event_source != NULL);
105 event_source->provider = ep_create_provider (ep_provider_get_default_name_utf8 (), NULL, NULL);
106 ep_raise_error_if_nok (event_source->provider != NULL);
108 event_source->provider_name = ep_provider_get_default_name_utf8 ();
110 // Generate metadata.
111 EventPipeParameterDesc params [3];
113 params_len = (uint32_t)ARRAY_SIZE (params);
115 command_line_arg_utf16 = ep_rt_utf8_to_utf16le_string ("CommandLine", -1);
116 ep_raise_error_if_nok (command_line_arg_utf16 != NULL);
117 ep_parameter_desc_init (¶ms[0], EP_PARAMETER_TYPE_STRING, command_line_arg_utf16);
119 os_info_arg_utf16 = ep_rt_utf8_to_utf16le_string ("OSInformation", -1);
120 ep_raise_error_if_nok (os_info_arg_utf16 != NULL);
121 ep_parameter_desc_init (¶ms[1], EP_PARAMETER_TYPE_STRING, os_info_arg_utf16);
123 arch_info_arg_utf16 = ep_rt_utf8_to_utf16le_string ("ArchInformation", -1);
124 ep_raise_error_if_nok (arch_info_arg_utf16 != NULL);
125 ep_parameter_desc_init (¶ms[2], EP_PARAMETER_TYPE_STRING, arch_info_arg_utf16);
127 event_name_utf16 = ep_rt_utf8_to_utf16le_string ("ProcessInfo", -1);
128 ep_raise_error_if_nok (event_name_utf16 != NULL);
132 metadata = ep_metadata_generator_generate_event_metadata (
137 EP_EVENT_LEVEL_LOGALWAYS,
143 ep_raise_error_if_nok (metadata != NULL);
146 event_source->process_info_event = ep_provider_add_event (
147 event_source->provider,
150 0, /* eventVersion */
151 EP_EVENT_LEVEL_LOGALWAYS,
152 false, /* needStack */
154 (uint32_t)metadata_len);
156 ep_raise_error_if_nok (event_source->process_info_event);
159 // Delete the metadata after the event is created.
160 // The metadata blob will be copied into EventPipe-owned memory.
161 ep_rt_byte_array_free (metadata);
163 // Delete the strings after the event is created.
164 // The strings will be copied into EventPipe-owned memory.
165 ep_rt_utf16_string_free (event_name_utf16);
166 ep_rt_utf16_string_free (arch_info_arg_utf16);
167 ep_rt_utf16_string_free (os_info_arg_utf16);
168 ep_rt_utf16_string_free (command_line_arg_utf16);
173 ep_event_source_free (event_source);
176 ep_exit_error_handler ();
180 ep_event_source_fini (EventPipeEventSource *event_source)
182 ep_return_void_if_nok (event_source);
183 event_source_fini (event_source);
187 ep_event_source_free (EventPipeEventSource *event_source)
189 ep_return_void_if_nok (event_source);
190 event_source_fini (event_source);
191 ep_rt_object_free (event_source);
195 ep_event_source_enable (
196 EventPipeEventSource *event_source,
197 EventPipeSession *session)
199 EP_ASSERT (event_source != NULL);
200 EP_ASSERT (session != NULL);
202 ep_requires_lock_held ();
205 EventPipeSessionProvider *session_provider = ep_session_provider_alloc (event_source->provider_name, (uint64_t)-1, EP_EVENT_LEVEL_LOGALWAYS, NULL);
206 if (session_provider != NULL)
207 result = ep_session_add_session_provider (session, session_provider);
212 ep_event_source_send_process_info (
213 EventPipeEventSource * event_source,
214 const ep_char8_t *command_line)
216 EP_ASSERT (event_source != NULL);
218 ep_char16_t *command_line_utf16 = NULL;
219 ep_char16_t *os_info_utf16 = NULL;
220 ep_char16_t *arch_info_utf16 = NULL;
222 command_line_utf16 = ep_rt_utf8_to_utf16le_string (command_line, -1);
223 os_info_utf16 = ep_rt_utf8_to_utf16le_string (ep_event_source_get_os_info (), -1);
224 arch_info_utf16 = ep_rt_utf8_to_utf16le_string (ep_event_source_get_arch_info (), -1);
226 EventData data [3] = { { 0 } };
227 if (command_line_utf16)
228 ep_event_data_init (&data[0], (uint64_t)command_line_utf16, (uint32_t)((ep_rt_utf16_string_len (command_line_utf16) + 1) * sizeof (ep_char16_t)), 0);
230 ep_event_data_init (&data[1], (uint64_t)os_info_utf16, (uint32_t)((ep_rt_utf16_string_len (os_info_utf16) + 1) * sizeof (ep_char16_t)), 0);
232 ep_event_data_init (&data[2], (uint64_t)arch_info_utf16, (uint32_t)((ep_rt_utf16_string_len (arch_info_utf16) + 1) * sizeof (ep_char16_t)), 0);
234 ep_write_event_2 (event_source->process_info_event, data, (uint32_t)ARRAY_SIZE (data), NULL, NULL);
236 ep_rt_utf16_string_free (arch_info_utf16);
237 ep_rt_utf16_string_free (os_info_utf16);
238 ep_rt_utf16_string_free (command_line_utf16);
241 #endif /* !defined(EP_INCLUDE_SOURCE_FILES) || defined(EP_FORCE_INCLUDE_SOURCE_FILES) */
242 #endif /* ENABLE_PERFTRACING */
244 #if !defined(ENABLE_PERFTRACING) || (defined(EP_INCLUDE_SOURCE_FILES) && !defined(EP_FORCE_INCLUDE_SOURCE_FILES))
245 extern const char quiet_linker_empty_file_warning_eventpipe_event_source;
246 const char quiet_linker_empty_file_warning_eventpipe_event_source = 0;