Sync may31 release/8.0-tizen (#510)
[platform/upstream/dotnet/runtime.git] / src / native / eventpipe / ep-event-source.c
1 #include "ep-rt-config.h"
2
3 #ifdef ENABLE_PERFTRACING
4 #if !defined(EP_INCLUDE_SOURCE_FILES) || defined(EP_FORCE_INCLUDE_SOURCE_FILES)
5
6 #define EP_IMPL_EVENT_SOURCE_GETTER_SETTER
7 #include "ep.h"
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"
13 #include "ep-rt.h"
14
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";
31 #else
32 const ep_char8_t* _ep_os_info = "Unknown";
33 #endif
34
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";
53 #else
54 const ep_char8_t* _ep_arch_info = "Unknown";
55 #endif
56
57 EventPipeEventSource _ep_event_source_instance = { 0 };
58
59 /*
60  * Forward declares of all static functions.
61  */
62
63 static
64 void
65 event_source_fini (EventPipeEventSource *event_source);
66
67 /*
68  * EventPipeEventSource.
69  */
70
71 static
72 void
73 event_source_fini (EventPipeEventSource *event_source)
74 {
75         ep_delete_provider (event_source->provider);
76 }
77
78 EventPipeEventSource *
79 ep_event_source_alloc (void)
80 {
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);
84
85 ep_on_exit:
86         return instance;
87
88 ep_on_error:
89         ep_event_source_free (instance);
90         instance = NULL;
91         ep_exit_error_handler ();
92 }
93
94 EventPipeEventSource *
95 ep_event_source_init (EventPipeEventSource *event_source)
96 {
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;
102
103         EP_ASSERT (event_source != NULL);
104
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);
107
108         event_source->provider_name = ep_provider_get_default_name_utf8 ();
109
110         // Generate metadata.
111         EventPipeParameterDesc params [3];
112         uint32_t params_len;
113         params_len = (uint32_t)ARRAY_SIZE (params);
114
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 (&params[0], EP_PARAMETER_TYPE_STRING, command_line_arg_utf16);
118
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 (&params[1], EP_PARAMETER_TYPE_STRING, os_info_arg_utf16);
122
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 (&params[2], EP_PARAMETER_TYPE_STRING, arch_info_arg_utf16);
126
127         event_name_utf16 = ep_rt_utf8_to_utf16le_string ("ProcessInfo", -1);
128         ep_raise_error_if_nok (event_name_utf16 != NULL);
129
130         size_t metadata_len;
131         metadata_len = 0;
132         metadata = ep_metadata_generator_generate_event_metadata (
133                 1,              /* eventID */
134                 event_name_utf16,
135                 0,              /* keywords */
136                 1,              /* version */
137                 EP_EVENT_LEVEL_LOGALWAYS,
138                 0,              /* opcode */
139                 params,
140                 params_len,
141                 &metadata_len);
142
143         ep_raise_error_if_nok (metadata != NULL);
144
145         // Add the event.
146         event_source->process_info_event = ep_provider_add_event (
147                 event_source->provider,
148                 1,              /* eventID */
149                 0,              /* keywords */
150                 0,              /* eventVersion */
151                 EP_EVENT_LEVEL_LOGALWAYS,
152                 false,  /* needStack */
153                 metadata,
154                 (uint32_t)metadata_len);
155
156         ep_raise_error_if_nok (event_source->process_info_event);
157
158 ep_on_exit:
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);
162
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);
169
170         return event_source;
171
172 ep_on_error:
173         ep_event_source_free (event_source);
174
175         event_source = NULL;
176         ep_exit_error_handler ();
177 }
178
179 void
180 ep_event_source_fini (EventPipeEventSource *event_source)
181 {
182         ep_return_void_if_nok (event_source);
183         event_source_fini (event_source);
184 }
185
186 void
187 ep_event_source_free (EventPipeEventSource *event_source)
188 {
189         ep_return_void_if_nok (event_source);
190         event_source_fini (event_source);
191         ep_rt_object_free (event_source);
192 }
193
194 bool
195 ep_event_source_enable (
196         EventPipeEventSource *event_source,
197         EventPipeSession *session)
198 {
199         EP_ASSERT (event_source != NULL);
200         EP_ASSERT (session != NULL);
201
202         ep_requires_lock_held ();
203
204         bool result = true;
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);
208         return result;
209 }
210
211 void
212 ep_event_source_send_process_info (
213         EventPipeEventSource * event_source,
214         const ep_char8_t *command_line)
215 {
216         EP_ASSERT (event_source != NULL);
217
218         ep_char16_t *command_line_utf16 = NULL;
219         ep_char16_t *os_info_utf16 = NULL;
220         ep_char16_t *arch_info_utf16 = NULL;
221
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);
225
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);
229         if (os_info_utf16)
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);
231         if (arch_info_utf16)
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);
233
234         ep_write_event_2 (event_source->process_info_event, data, (uint32_t)ARRAY_SIZE (data), NULL, NULL);
235
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);
239 }
240
241 #endif /* !defined(EP_INCLUDE_SOURCE_FILES) || defined(EP_FORCE_INCLUDE_SOURCE_FILES) */
242 #endif /* ENABLE_PERFTRACING */
243
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;
247 #endif