ds-process-protocol.c 22.2 KB
Newer Older
1
#include "ds-rt-config.h"
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

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

#define DS_IMPL_PROCESS_PROTOCOL_GETTER_SETTER
#include "ds-protocol.h"
#include "ds-process-protocol.h"
#include "ds-server.h"
#include "ep.h"
#include "ds-rt.h"
#include "ep-event-source.h"

/*
 * Forward declares of all static functions.
 */

static
uint16_t
process_info_payload_get_size (DiagnosticsProcessInfoPayload *payload);

static
bool
process_info_payload_flatten (
	void *payload,
	uint8_t **buffer,
	uint16_t *size);

static
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
uint16_t
env_info_payload_get_size (DiagnosticsEnvironmentInfoPayload *payload);

static
uint32_t
env_info_env_block_get_size (DiagnosticsEnvironmentInfoPayload *payload);

static
bool
env_info_payload_flatten (
	void *payload,
	uint8_t **buffer,
	uint16_t *size);

static
bool
env_info_stream_env_block (
	DiagnosticsEnvironmentInfoPayload *payload,
	DiagnosticsIpcStream *stream);

static
bool
52 53 54 55
process_protocol_helper_get_process_info (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream);

56 57 58 59 60 61
static
bool
process_protocol_helper_get_process_info_2 (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream);

62
static
63
bool
64 65 66 67 68
process_protocol_helper_get_process_env (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream);

static
69
bool
70 71 72 73
process_protocol_helper_resume_runtime_startup (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream);

74 75 76 77 78 79
static
bool
process_protocol_helper_set_environment_variable (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream);

80
static
81
bool
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
process_protocol_helper_unknown_command (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream);

/*
 * DiagnosticsProcessInfoPayload.
 */

static
uint16_t
process_info_payload_get_size (DiagnosticsProcessInfoPayload *payload)
{
	// see IPC spec @ https://github.com/dotnet/diagnostics/blob/master/documentation/design-docs/ipc-protocol.md
	// for definition of serialization format

	// uint64_t ProcessId;  -> 8 bytes
	// GUID RuntimeCookie;  -> 16 bytes
	// LPCWSTR CommandLine; -> 4 bytes + strlen * sizeof(WCHAR)
	// LPCWSTR OS;          -> 4 bytes + strlen * sizeof(WCHAR)
	// LPCWSTR Arch;        -> 4 bytes + strlen * sizeof(WCHAR)

	EP_ASSERT (payload != NULL);

	size_t size = 0;
	size += sizeof(payload->process_id);
	size += sizeof(payload->runtime_cookie);

	size += sizeof(uint32_t);
	size += (payload->command_line != NULL) ?
		(ep_rt_utf16_string_len (payload->command_line) + 1) * sizeof(ep_char16_t) : 0;

	size += sizeof(uint32_t);
	size += (payload->os != NULL) ?
		(ep_rt_utf16_string_len (payload->os) + 1) * sizeof(ep_char16_t) : 0;

	size += sizeof(uint32_t);
	size += (payload->arch != NULL) ?
		(ep_rt_utf16_string_len (payload->arch) + 1) * sizeof(ep_char16_t) : 0;

	EP_ASSERT (size <= UINT16_MAX);
	return (uint16_t)size;
}

static
bool
process_info_payload_flatten (
	void *payload,
	uint8_t **buffer,
	uint16_t *size)
{
	DiagnosticsProcessInfoPayload *process_info = (DiagnosticsProcessInfoPayload*)payload;

	EP_ASSERT (payload != NULL);
	EP_ASSERT (buffer != NULL);
	EP_ASSERT (*buffer != NULL);
	EP_ASSERT (size != NULL);
	EP_ASSERT (process_info_payload_get_size (process_info) == *size);

	// see IPC spec @ https://github.com/dotnet/diagnostics/blob/master/documentation/design-docs/ipc-protocol.md
	// for definition of serialization format

	bool success = true;

	// uint64_t ProcessId;
	memcpy (*buffer, &process_info->process_id, sizeof (process_info->process_id));
	*buffer += sizeof (process_info->process_id);
	*size -= sizeof (process_info->process_id);

	// GUID RuntimeCookie;
	memcpy(*buffer, &process_info->runtime_cookie, sizeof (process_info->runtime_cookie));
	*buffer += sizeof (process_info->runtime_cookie);
	*size -= sizeof (process_info->runtime_cookie);

	// LPCWSTR CommandLine;
	success &= ds_ipc_message_try_write_string_utf16_t (buffer, size, process_info->command_line);

	// LPCWSTR OS;
	if (success)
		success &= ds_ipc_message_try_write_string_utf16_t (buffer, size, process_info->os);

	// LPCWSTR Arch;
	if (success)
		success &= ds_ipc_message_try_write_string_utf16_t (buffer, size, process_info->arch);

	// Assert we've used the whole buffer we were given
	EP_ASSERT(*size == 0);

	return success;
}

DiagnosticsProcessInfoPayload *
ds_process_info_payload_init (
	DiagnosticsProcessInfoPayload *payload,
	const ep_char16_t *command_line,
	const ep_char16_t *os,
	const ep_char16_t *arch,
	uint32_t process_id,
	const uint8_t *runtime_cookie)
{
	ep_return_null_if_nok (payload != NULL);

	payload->command_line = command_line;
	payload->os = os;
	payload->arch = arch;
	payload->process_id = process_id;

	if (runtime_cookie)
189
		memcpy (&payload->runtime_cookie, runtime_cookie, EP_GUID_SIZE);
190 191 192 193 194 195 196 197 198 199

	return payload;
}

void
ds_process_info_payload_fini (DiagnosticsProcessInfoPayload *payload)
{
	;
}

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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
/*
 * DiagnosticsProcessInfo2Payload.
 */

static
uint16_t
process_info_2_payload_get_size (DiagnosticsProcessInfo2Payload *payload)
{
	// see IPC spec @ https://github.com/dotnet/diagnostics/blob/master/documentation/design-docs/ipc-protocol.md
	// for definition of serialization format

	// uint64_t ProcessId;  -> 8 bytes
	// GUID RuntimeCookie;  -> 16 bytes
	// LPCWSTR CommandLine; -> 4 bytes + strlen * sizeof(WCHAR)
	// LPCWSTR OS;          -> 4 bytes + strlen * sizeof(WCHAR)
	// LPCWSTR Arch;        -> 4 bytes + strlen * sizeof(WCHAR)
	// LPCWSTR managed_entrypoint_assembly_name;	-> 4 bytes + strlen * sizeof(WCHAR)
	// LPCWSTR clr_product_version; 				-> 4 bytes + strlen * sizeof(WCHAR)

	EP_ASSERT (payload != NULL);

	size_t size = 0;
	size += sizeof(payload->process_id);
	size += sizeof(payload->runtime_cookie);

	size += sizeof(uint32_t);
	size += (payload->command_line != NULL) ?
		(ep_rt_utf16_string_len (payload->command_line) + 1) * sizeof(ep_char16_t) : 0;

	size += sizeof(uint32_t);
	size += (payload->os != NULL) ?
		(ep_rt_utf16_string_len (payload->os) + 1) * sizeof(ep_char16_t) : 0;

	size += sizeof(uint32_t);
	size += (payload->arch != NULL) ?
		(ep_rt_utf16_string_len (payload->arch) + 1) * sizeof(ep_char16_t) : 0;

	size += sizeof(uint32_t);
	size += (payload->managed_entrypoint_assembly_name != NULL) ?
		(ep_rt_utf16_string_len (payload->managed_entrypoint_assembly_name) + 1) * sizeof(ep_char16_t) : 0;

	size += sizeof(uint32_t);
	size += (payload->clr_product_version != NULL) ?
		(ep_rt_utf16_string_len (payload->clr_product_version) + 1) * sizeof(ep_char16_t) : 0;

	EP_ASSERT (size <= UINT16_MAX);
	return (uint16_t)size;
}

static
bool
process_info_2_payload_flatten (
	void *payload,
	uint8_t **buffer,
	uint16_t *size)
{
	DiagnosticsProcessInfo2Payload *process_info = (DiagnosticsProcessInfo2Payload*)payload;

	EP_ASSERT (payload != NULL);
	EP_ASSERT (buffer != NULL);
	EP_ASSERT (*buffer != NULL);
	EP_ASSERT (size != NULL);
	EP_ASSERT (process_info_2_payload_get_size (process_info) == *size);

	// see IPC spec @ https://github.com/dotnet/diagnostics/blob/master/documentation/design-docs/ipc-protocol.md
	// for definition of serialization format

	bool success = true;

	// uint64_t ProcessId;
	memcpy (*buffer, &process_info->process_id, sizeof (process_info->process_id));
	*buffer += sizeof (process_info->process_id);
	*size -= sizeof (process_info->process_id);

	// GUID RuntimeCookie;
	memcpy(*buffer, &process_info->runtime_cookie, sizeof (process_info->runtime_cookie));
	*buffer += sizeof (process_info->runtime_cookie);
	*size -= sizeof (process_info->runtime_cookie);

	// LPCWSTR CommandLine;
	success &= ds_ipc_message_try_write_string_utf16_t (buffer, size, process_info->command_line);

	// LPCWSTR OS;
	if (success)
		success &= ds_ipc_message_try_write_string_utf16_t (buffer, size, process_info->os);

	// LPCWSTR Arch;
	if (success)
		success &= ds_ipc_message_try_write_string_utf16_t (buffer, size, process_info->arch);

	// LPCWSTR managed_entrypoint_assembly_name;
	if (success)
		success &= ds_ipc_message_try_write_string_utf16_t (buffer, size, process_info->managed_entrypoint_assembly_name);

	// LPCWSTR clr_product_version;
	if (success)
		success &= ds_ipc_message_try_write_string_utf16_t (buffer, size, process_info->clr_product_version);

	// Assert we've used the whole buffer we were given
	EP_ASSERT(*size == 0);

	return success;
}

DiagnosticsProcessInfo2Payload *
ds_process_info_2_payload_init (
	DiagnosticsProcessInfo2Payload *payload,
	const ep_char16_t *command_line,
	const ep_char16_t *os,
	const ep_char16_t *arch,
	uint32_t process_id,
	const uint8_t *runtime_cookie,
	const ep_char16_t *managed_entrypoint_assembly_name,
	const ep_char16_t *clr_product_version)
{
	ep_return_null_if_nok (payload != NULL);

	payload->command_line = command_line;
	payload->os = os;
	payload->arch = arch;
	payload->process_id = process_id;
	payload->managed_entrypoint_assembly_name = managed_entrypoint_assembly_name;
	payload->clr_product_version = clr_product_version;

	if (runtime_cookie)
		memcpy (&payload->runtime_cookie, runtime_cookie, EP_GUID_SIZE);

	return payload;
}

void
ds_process_info_2_payload_fini (DiagnosticsProcessInfo2Payload *payload)
{
	;
}


337
/*
338
 * DiagnosticsEnvironmentInfoPayload.
339 340 341
 */

static
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
uint16_t
env_info_payload_get_size (DiagnosticsEnvironmentInfoPayload *payload)
{
	EP_ASSERT (payload != NULL);

	size_t size = 0;
	size += sizeof (payload->incoming_bytes);
	size += sizeof (payload->future);

	EP_ASSERT (size <= UINT16_MAX);
	return (uint16_t)size;
}

static
uint32_t
env_info_env_block_get_size (DiagnosticsEnvironmentInfoPayload *payload)
{
	EP_ASSERT (payload != NULL);

	size_t size = 0;

	size += sizeof (uint32_t);
	size += (sizeof (uint32_t) * ep_rt_env_array_utf16_size (&payload->env_array));

	ep_rt_env_array_utf16_iterator_t iterator = ep_rt_env_array_utf16_iterator_begin (&payload->env_array);
	while (!ep_rt_env_array_utf16_iterator_end (&payload->env_array, &iterator)) {
		size += ((ep_rt_utf16_string_len (ep_rt_env_array_utf16_iterator_value (&iterator)) + 1) * sizeof (ep_char16_t));
		ep_rt_env_array_utf16_iterator_next (&iterator);
	}

	EP_ASSERT (size <= UINT32_MAX);
	return (uint32_t)size;
}

static
bool
env_info_payload_flatten (
	void *payload,
	uint8_t **buffer,
	uint16_t *size)
{
	DiagnosticsEnvironmentInfoPayload *env_info = (DiagnosticsEnvironmentInfoPayload*)payload;

	EP_ASSERT (payload != NULL);
	EP_ASSERT (buffer != NULL);
	EP_ASSERT (*buffer != NULL);
	EP_ASSERT (size != NULL);
	EP_ASSERT (env_info_payload_get_size (env_info) == *size);

	// see IPC spec @ https://github.com/dotnet/diagnostics/blob/master/documentation/design-docs/ipc-protocol.md
	// for definition of serialization format

	bool success = true;

	// uint32_t incoming_bytes;
	memcpy (*buffer, &env_info->incoming_bytes, sizeof (env_info->incoming_bytes));
	*buffer += sizeof (env_info->incoming_bytes);
	*size -= sizeof (env_info->incoming_bytes);

	// uint16_t future;
	memcpy(*buffer, &env_info->future, sizeof (env_info->future));
	*buffer += sizeof (env_info->future);
	*size -= sizeof (env_info->future);

	// Assert we've used the whole buffer we were given
	EP_ASSERT(*size == 0);

	return success;
}

static
bool
env_info_stream_env_block (
	DiagnosticsEnvironmentInfoPayload *payload,
	DiagnosticsIpcStream *stream)
{
	DiagnosticsEnvironmentInfoPayload *env_info = (DiagnosticsEnvironmentInfoPayload*)payload;

	EP_ASSERT (payload != NULL);
	EP_ASSERT (stream != NULL);

	// see IPC spec @ https://github.com/dotnet/diagnostics/blob/master/documentation/design-docs/ipc-protocol.md
	// for definition of serialization format

	bool success = true;
	uint32_t bytes_written = 0;

	// Array<Array<WCHAR>>
	uint32_t env_len = (uint32_t)ep_rt_env_array_utf16_size (&env_info->env_array);
	success &= ds_ipc_stream_write (stream, (const uint8_t *)&env_len, sizeof (env_len), &bytes_written, EP_INFINITE_WAIT);

	ep_rt_env_array_utf16_iterator_t iterator = ep_rt_env_array_utf16_iterator_begin (&env_info->env_array);
	while (!ep_rt_env_array_utf16_iterator_end (&env_info->env_array, &iterator)) {
		success &= ds_ipc_message_try_write_string_utf16_t_to_stream (stream, ep_rt_env_array_utf16_iterator_value (&iterator));
		ep_rt_env_array_utf16_iterator_next (&iterator);
	}

	return success;
}

DiagnosticsEnvironmentInfoPayload *
ds_env_info_payload_init (DiagnosticsEnvironmentInfoPayload *payload)
{
	ep_return_null_if_nok (payload != NULL);

	ep_rt_env_array_utf16_alloc (&payload->env_array);
	ep_rt_os_environment_get_utf16 (&payload->env_array);

	payload->incoming_bytes = env_info_env_block_get_size (payload);
	payload->future = 0;

	return payload;
}

456
void
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
ds_env_info_payload_fini (DiagnosticsEnvironmentInfoPayload *payload)
{
	ep_rt_env_array_utf16_iterator_t iterator = ep_rt_env_array_utf16_iterator_begin (&payload->env_array);
	while (!ep_rt_env_array_utf16_iterator_end (&payload->env_array, &iterator)) {
		ep_rt_utf16_string_free (ep_rt_env_array_utf16_iterator_value (&iterator));
		ep_rt_env_array_utf16_iterator_next (&iterator);
	}

	ep_rt_env_array_utf16_free (&payload->env_array);
}

/*
 * DiagnosticsProcessProtocolHelper.
 */

static
bool
474 475 476 477 478 479 480
process_protocol_helper_get_process_info (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream)
{
	EP_ASSERT (message != NULL);
	EP_ASSERT (stream != NULL);

481
	bool result = false;
482 483 484
	ep_char16_t *command_line = NULL;
	ep_char16_t *os_info = NULL;
	ep_char16_t *arch_info = NULL;
485 486
	DiagnosticsProcessInfoPayload payload;
	DiagnosticsProcessInfoPayload *process_info_payload = NULL;
487

488 489
	command_line = ep_rt_utf8_to_utf16_string (ep_rt_diagnostics_command_line_get (), -1);
	ep_raise_error_if_nok (command_line != NULL);
490 491

	os_info = ep_rt_utf8_to_utf16_string (ep_event_source_get_os_info (), -1);
492 493
	ep_raise_error_if_nok (os_info != NULL);

494
	arch_info = ep_rt_utf8_to_utf16_string (ep_event_source_get_arch_info (), -1);
495
	ep_raise_error_if_nok (arch_info != NULL);
496

497
	process_info_payload = ds_process_info_payload_init (
498 499 500 501 502 503
		&payload,
		command_line,
		os_info,
		arch_info,
		ep_rt_current_process_get_id (),
		ds_ipc_advertise_cookie_v1_get ());
504
	ep_raise_error_if_nok (process_info_payload != NULL);
505 506 507 508

	ep_raise_error_if_nok (ds_ipc_message_initialize_buffer (
		message,
		ds_ipc_header_get_generic_success (),
509 510 511
		(void *)process_info_payload,
		process_info_payload_get_size (process_info_payload),
		process_info_payload_flatten));
512

513 514 515
	ep_raise_error_if_nok (ds_ipc_message_send (message, stream));

	result = true;
516 517

ep_on_exit:
518
	ds_process_info_payload_fini (process_info_payload);
519 520 521 522
	ep_rt_utf16_string_free (arch_info);
	ep_rt_utf16_string_free (os_info);
	ep_rt_utf16_string_free (command_line);
	ds_ipc_stream_free (stream);
523
	return result;
524 525

ep_on_error:
526
	EP_ASSERT (!result);
527 528 529 530 531
	ds_ipc_message_send_error (stream, DS_IPC_E_FAIL);
	DS_LOG_WARNING_0 ("Failed to send DiagnosticsIPC response");
	ep_exit_error_handler ();
}

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
static
bool
process_protocol_helper_get_process_info_2 (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream)
{
	EP_ASSERT (message != NULL);
	EP_ASSERT (stream != NULL);

	bool result = false;
	ep_char16_t *command_line = NULL;
	ep_char16_t *os_info = NULL;
	ep_char16_t *arch_info = NULL;
	ep_char16_t *managed_entrypoint_assembly_name = NULL;
	ep_char16_t *clr_product_version = NULL;
	DiagnosticsProcessInfo2Payload payload;
	DiagnosticsProcessInfo2Payload *process_info_2_payload = NULL;

	command_line = ep_rt_utf8_to_utf16_string (ep_rt_diagnostics_command_line_get (), -1);
	ep_raise_error_if_nok (command_line != NULL);

	os_info = ep_rt_utf8_to_utf16_string (ep_event_source_get_os_info (), -1);
	ep_raise_error_if_nok (os_info != NULL);

	arch_info = ep_rt_utf8_to_utf16_string (ep_event_source_get_arch_info (), -1);
	ep_raise_error_if_nok (arch_info != NULL);

	managed_entrypoint_assembly_name = ep_rt_utf8_to_utf16_string (ep_rt_entrypoint_assembly_name_get_utf8 (), -1);
	ep_raise_error_if_nok (managed_entrypoint_assembly_name != NULL);

	clr_product_version = ep_rt_utf8_to_utf16_string (ep_rt_runtime_version_get_utf8 (), -1);
	ep_raise_error_if_nok (clr_product_version != NULL);

	process_info_2_payload = ds_process_info_2_payload_init (
		&payload,
		command_line,
		os_info,
		arch_info,
		ep_rt_current_process_get_id (),
		ds_ipc_advertise_cookie_v1_get (),
		managed_entrypoint_assembly_name,
		clr_product_version);
	ep_raise_error_if_nok (process_info_2_payload != NULL);

	ep_raise_error_if_nok (ds_ipc_message_initialize_buffer (
		message,
		ds_ipc_header_get_generic_success (),
		(void *)process_info_2_payload,
		process_info_2_payload_get_size (process_info_2_payload),
		process_info_2_payload_flatten));

	ep_raise_error_if_nok (ds_ipc_message_send (message, stream));

	result = true;

ep_on_exit:
	ds_process_info_2_payload_fini (process_info_2_payload);
	ep_rt_utf16_string_free (arch_info);
	ep_rt_utf16_string_free (os_info);
	ep_rt_utf16_string_free (command_line);
	ep_rt_utf16_string_free (managed_entrypoint_assembly_name);
	ep_rt_utf16_string_free (clr_product_version);
	ds_ipc_stream_free (stream);
	return result;

ep_on_error:
	EP_ASSERT (!result);
	ds_ipc_message_send_error (stream, DS_IPC_E_FAIL);
	DS_LOG_WARNING_0 ("Failed to send DiagnosticsIPC response");
	ep_exit_error_handler ();
}

604
static
605
bool
606 607 608 609 610 611 612
process_protocol_helper_get_process_env (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream)
{
	EP_ASSERT (message != NULL);
	EP_ASSERT (stream != NULL);

613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
	bool result = false;
	DiagnosticsEnvironmentInfoPayload payload;
	DiagnosticsEnvironmentInfoPayload *env_info_payload;

	env_info_payload = ds_env_info_payload_init (&payload);
	ep_raise_error_if_nok (env_info_payload);

	ep_raise_error_if_nok (ds_ipc_message_initialize_buffer (
		message,
		ds_ipc_header_get_generic_success (),
		(void *)env_info_payload,
		env_info_payload_get_size (env_info_payload),
		env_info_payload_flatten));

	ep_raise_error_if_nok (ds_ipc_message_send (message, stream));
	ep_raise_error_if_nok (env_info_stream_env_block (env_info_payload, stream));

	result = true;
631

632 633
ep_on_exit:
	ds_env_info_payload_fini (env_info_payload);
634
	ds_ipc_stream_free (stream);
635 636 637 638 639 640 641
	return result;

ep_on_error:
	EP_ASSERT (!result);
	ds_ipc_message_send_error (stream, DS_IPC_E_FAIL);
	DS_LOG_WARNING_0 ("Failed to send DiagnosticsIPC response");
	ep_exit_error_handler ();
642 643 644
}

static
645
bool
646 647 648 649 650 651 652
process_protocol_helper_resume_runtime_startup (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream)
{
	EP_ASSERT (message != NULL);
	EP_ASSERT (stream != NULL);

653 654
	bool result = false;

655 656
	// no payload
	ds_server_resume_runtime_startup ();
657 658
	result = ds_ipc_message_send_success (stream, DS_IPC_S_OK);
	if (!result) {
659 660 661 662 663
		ds_ipc_message_send_error (stream, DS_IPC_E_FAIL);
		DS_LOG_WARNING_0 ("Failed to send DiagnosticsIPC response");
	}

	ds_ipc_stream_free (stream);
664
	return result;
665 666
}

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
DiagnosticsSetEnvironmentVariablePayload *
ds_set_environment_variable_payload_alloc (void)
{
	return ep_rt_object_alloc (DiagnosticsSetEnvironmentVariablePayload);
}

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

static
uint8_t *
set_environment_variable_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;

	DiagnosticsSetEnvironmentVariablePayload *instance = ds_set_environment_variable_payload_alloc ();
	ep_raise_error_if_nok (instance != NULL);

	instance->incoming_buffer = buffer;

	if (!ds_ipc_message_try_parse_string_utf16_t (&buffer_cursor, &buffer_cursor_len, &instance->name) ||
		!ds_ipc_message_try_parse_string_utf16_t (&buffer_cursor, &buffer_cursor_len, &instance->value))
		ep_raise_error ();

ep_on_exit:
	return (uint8_t *)instance;

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

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

	if (!stream)
		return false;

    bool result = false;
    DiagnosticsSetEnvironmentVariablePayload *payload = (DiagnosticsSetEnvironmentVariablePayload *)ds_ipc_message_try_parse_payload (message, set_environment_variable_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_set_environment_variable (payload->name, payload->value);
	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_set_environment_variable_payload_free (payload);
    ds_ipc_stream_free (stream);
	return result;

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

750
static
751
bool
752 753 754 755
process_protocol_helper_unknown_command (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream)
{
756
	DS_LOG_WARNING_1 ("Received unknown request type (%d)", ds_ipc_header_get_commandset (ds_ipc_message_get_header_ref (message)));
757 758
	ds_ipc_message_send_error (stream, DS_IPC_E_UNKNOWN_COMMAND);
	ds_ipc_stream_free (stream);
759
	return true;
760 761
}

762
bool
763 764 765 766 767 768 769
ds_process_protocol_helper_handle_ipc_message (
	DiagnosticsIpcMessage *message,
	DiagnosticsIpcStream *stream)
{
	EP_ASSERT (message != NULL);
	EP_ASSERT (stream != NULL);

770 771
	bool result = false;

772 773
	switch ((DiagnosticsProcessCommandId)ds_ipc_header_get_commandid (ds_ipc_message_get_header_ref (message))) {
	case DS_PROCESS_COMMANDID_GET_PROCESS_INFO:
774
		result = process_protocol_helper_get_process_info (message, stream);
775 776
		break;
	case DS_PROCESS_COMMANDID_RESUME_RUNTIME:
777
		result = process_protocol_helper_resume_runtime_startup (message, stream);
778 779
		break;
	case DS_PROCESS_COMMANDID_GET_PROCESS_ENV:
780
		result = process_protocol_helper_get_process_env (message, stream);
781
		break;
782 783
	case DS_PROCESS_COMMANDID_SET_ENV_VAR:
		result = process_protocol_helper_set_environment_variable (message, stream);
784 785 786
        break;
	case DS_PROCESS_COMMANDID_GET_PROCESS_INFO_2:
		result = process_protocol_helper_get_process_info_2 (message, stream);
787
		break;
788
	default:
789
		result = process_protocol_helper_unknown_command (message, stream);
790 791
		break;
	}
792 793

	return result;
794 795 796 797 798 799 800 801 802
}

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

#ifndef DS_INCLUDE_SOURCE_FILES
extern const char quiet_linker_empty_file_warning_diagnostics_process_protocol;
const char quiet_linker_empty_file_warning_diagnostics_process_protocol = 0;
#endif