Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ds-profiler-protocol.c « eventpipe « native « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ea7035eb980c68ee0c08994c0b6995387628dad3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "ds-rt-config.h"

#ifdef ENABLE_PERFTRACING
#if !defined(DS_INCLUDE_SOURCE_FILES) || defined(DS_FORCE_INCLUDE_SOURCE_FILES)

#define DS_IMPL_PROFILER_PROTOCOL_GETTER_SETTER
#include "ds-protocol.h"
#include "ds-profiler-protocol.h"
#include "ds-server.h"
#include "ds-rt.h"

#ifdef PROFILING_SUPPORTED

/*
 * Forward declares of all static functions.
 */
static
uint8_t *
attach_profiler_command_try_parse_payload (
	uint8_t *buffer,
	uint16_t buffer_len);

static
uint8_t *
startup_profiler_command_try_parse_payload (
	uint8_t *buffer,
	uint16_t buffer_len);

static
bool
profiler_protocol_helper_attach_profiler (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream);

static
bool
profiler_protocol_helper_startup_profiler (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream);

static
bool
profiler_protocol_helper_unknown_command (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream);

#ifdef FEATURE_PROFAPI_ATTACH_DETACH

/*
* DiagnosticsAttachProfilerCommandPayload
*/

static
uint8_t *
attach_profiler_command_try_parse_payload (
	uint8_t *buffer,
	uint16_t buffer_len)
{
	EP_ASSERT (buffer != NULL);

	uint8_t * buffer_cursor = buffer;
	uint32_t buffer_cursor_len = buffer_len;

	DiagnosticsAttachProfilerCommandPayload *instance = ds_attach_profiler_command_payload_alloc ();
	ep_raise_error_if_nok (instance != NULL);

	instance->incoming_buffer = buffer;

	if (!ds_ipc_message_try_parse_uint32_t (&buffer_cursor, &buffer_cursor_len, &instance->attach_timeout ) ||
		!ds_ipc_message_try_parse_value (&buffer_cursor, &buffer_cursor_len, (uint8_t *)&instance->profiler_guid, (uint32_t)ARRAY_SIZE (instance->profiler_guid)) ||
		!ds_ipc_message_try_parse_string_utf16_t (&buffer_cursor, &buffer_cursor_len, &instance->profiler_path) ||
		!ds_ipc_message_try_parse_uint32_t (&buffer_cursor, &buffer_cursor_len, &instance->client_data_len) ||
		!(buffer_cursor_len <= instance->client_data_len))
		ep_raise_error ();

	instance->client_data = buffer_cursor;

ep_on_exit:
	return (uint8_t *)instance;

ep_on_error:
	ds_attach_profiler_command_payload_free (instance);
	instance = NULL;
	ep_exit_error_handler ();
}

DiagnosticsAttachProfilerCommandPayload *
ds_attach_profiler_command_payload_alloc (void)
{
	return ep_rt_object_alloc (DiagnosticsAttachProfilerCommandPayload);
}

void
ds_attach_profiler_command_payload_free (DiagnosticsAttachProfilerCommandPayload *payload)
{
	ep_return_void_if_nok (payload != NULL);
	ep_rt_byte_array_free (payload->incoming_buffer);
	ep_rt_object_free (payload);
}

static
bool
profiler_protocol_helper_attach_profiler (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream)
{
	EP_ASSERT (message != NULL);
	EP_ASSERT (stream != NULL);

	if (!stream)
		return false;

    bool result = false;
    DiagnosticsAttachProfilerCommandPayload *payload = NULL;

    if (!ep_rt_is_running ()) {
        ds_ipc_message_send_error (stream, DS_IPC_E_NOT_YET_AVAILABLE);
        ep_raise_error ();
    }

	payload = (DiagnosticsAttachProfilerCommandPayload *)ds_ipc_message_try_parse_payload (message, attach_profiler_command_try_parse_payload);

	if (!payload) {
		ds_ipc_message_send_error (stream, DS_IPC_E_BAD_ENCODING);
		ep_raise_error ();
	}

	ds_ipc_result_t ipc_result;
	ipc_result = ds_rt_profiler_attach (payload);
	if (ipc_result != DS_IPC_S_OK) {
		ds_ipc_message_send_error (stream, ipc_result);
		ep_raise_error ();
	} else {
		ds_ipc_message_send_success (stream, ipc_result);
	}

	result = true;

ep_on_exit:
	ds_attach_profiler_command_payload_free (payload);
    ds_ipc_stream_free (stream);
	return result;

ep_on_error:
	EP_ASSERT (!result);
	ep_exit_error_handler ();
}

#endif // FEATURE_PROFAPI_ATTACH_DETACH

DiagnosticsStartupProfilerCommandPayload *
ds_startup_profiler_command_payload_alloc (void)
{
	return ep_rt_object_alloc (DiagnosticsStartupProfilerCommandPayload);
}

void
ds_startup_profiler_command_payload_free (DiagnosticsStartupProfilerCommandPayload *payload)
{
	ep_return_void_if_nok (payload != NULL);
	ep_rt_byte_array_free (payload->incoming_buffer);
	ep_rt_object_free (payload);
}

/*
 * DiagnosticsProfilerProtocolHelper.
 */

static
bool
profiler_protocol_helper_unknown_command (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream)
{
	DS_LOG_WARNING_1 ("Received unknown request type (%d)", ds_ipc_header_get_commandset (ds_ipc_message_get_header_ref (message)));
	ds_ipc_message_send_error (stream, DS_IPC_E_UNKNOWN_COMMAND);
	return true;
}

static
uint8_t *
startup_profiler_command_try_parse_payload (
	uint8_t *buffer,
	uint16_t buffer_len)
{
	EP_ASSERT (buffer != NULL);

	uint8_t * buffer_cursor = buffer;
	uint32_t buffer_cursor_len = buffer_len;

	DiagnosticsStartupProfilerCommandPayload *instance = ds_startup_profiler_command_payload_alloc ();
	ep_raise_error_if_nok (instance != NULL);

	instance->incoming_buffer = buffer;

	if (!ds_ipc_message_try_parse_value (&buffer_cursor, &buffer_cursor_len, (uint8_t *)&instance->profiler_guid, (uint32_t)ARRAY_SIZE (instance->profiler_guid)) ||
		!ds_ipc_message_try_parse_string_utf16_t (&buffer_cursor, &buffer_cursor_len, &instance->profiler_path))
		ep_raise_error ();

ep_on_exit:
	return (uint8_t *)instance;

ep_on_error:
	ds_startup_profiler_command_payload_free (instance);
	instance = NULL;
	ep_exit_error_handler ();
}

bool
profiler_protocol_helper_startup_profiler (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream)
{
	EP_ASSERT (message != NULL);
	EP_ASSERT (stream != NULL);

	if (!stream)
		return false;

    bool result = false;
    DiagnosticsStartupProfilerCommandPayload *payload = NULL;

	if (!ds_server_is_paused_in_startup()) {
		ds_ipc_message_send_error (stream, DS_IPC_E_INVALIDARG);
		ep_raise_error ();
	}

	payload = (DiagnosticsStartupProfilerCommandPayload *)ds_ipc_message_try_parse_payload (message, startup_profiler_command_try_parse_payload);

	if (!payload) {
		ds_ipc_message_send_error (stream, DS_IPC_E_BAD_ENCODING);
		ep_raise_error ();
	}

	ds_ipc_result_t ipc_result;
	ipc_result = ds_rt_profiler_startup (payload);
	if (ipc_result != DS_IPC_S_OK) {
		ds_ipc_message_send_error (stream, ipc_result);
		ep_raise_error ();
	} else {
		ds_ipc_message_send_success (stream, ipc_result);
	}

	result = true;

ep_on_exit:
	ds_startup_profiler_command_payload_free (payload);
    ds_ipc_stream_free (stream);
	return result;

ep_on_error:
	EP_ASSERT (!result);
	ep_exit_error_handler ();
}

bool
ds_profiler_protocol_helper_handle_ipc_message (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream)
{
	EP_ASSERT (message != NULL);
	EP_ASSERT (stream != NULL);

	bool result = false;

	switch ((DiagnosticsProfilerCommandId)ds_ipc_header_get_commandid (ds_ipc_message_get_header_ref (message))) {
#ifdef FEATURE_PROFAPI_ATTACH_DETACH
	case DS_PROFILER_COMMANDID_ATTACH_PROFILER:
		result = profiler_protocol_helper_attach_profiler (message, stream);
		break;
#endif // FEATURE_PROFAPI_ATTACH_DETACH
	case DS_PROFILER_COMMANDID_STARTUP_PROFILER:
		result = profiler_protocol_helper_startup_profiler (message, stream);
		break;
 	default:
		result = profiler_protocol_helper_unknown_command (message, stream);
		break;
	}

	return result;
}
#else // PROFILING_SUPPORTED
bool
ds_profiler_protocol_helper_handle_ipc_message (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream)
{
	EP_ASSERT (message != NULL);
	EP_ASSERT (stream != NULL);

	DS_LOG_WARNING_0 ("Profiler support not enabled in this runtime");
	ds_ipc_message_send_error (stream, DS_IPC_E_NOTSUPPORTED);
    ds_ipc_stream_free (stream);

	return true;
}
#endif // PROFILING_SUPPORTED

#endif /* !defined(DS_INCLUDE_SOURCE_FILES) || defined(DS_FORCE_INCLUDE_SOURCE_FILES) */
#endif /* ENABLE_PERFTRACING */

#if !defined(ENABLE_PERFTRACING) || (defined(DS_INCLUDE_SOURCE_FILES) && !defined(DS_FORCE_INCLUDE_SOURCE_FILES))
extern const char quiet_linker_empty_file_warning_diagnostics_profiler_protocol;
const char quiet_linker_empty_file_warning_diagnostics_profiler_protocol = 0;
#endif