Blame src/debug/ntapi_tt_debug_execution_flow.c

03cd3c
/********************************************************/
03cd3c
/*  ntapi: Native API core library                      */
64e606
/*  Copyright (C) 2013--2021  SysDeer Technologies, LLC */
03cd3c
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
03cd3c
/********************************************************/
03cd3c
bf05bd
#include <psxtypes/psxtypes.h>
bf05bd
#include <ntapi/nt_object.h>
bf05bd
#include <ntapi/nt_debug.h>
bf05bd
#include <ntapi/nt_guid.h>
bf05bd
#include <ntapi/nt_acl.h>
bf05bd
#include <ntapi/nt_tty.h>
bf05bd
#include "ntapi_impl.h"
bf05bd
bf05bd
static int32_t __log_exception_to_server(
bf05bd
	nt_dbg_wait_state_change *	dbgstate,
bf05bd
	void *				hserver)
bf05bd
{
bf05bd
	int32_t		status;
bf05bd
	nt_tty_log_msg	msg;
bf05bd
bf05bd
	if (!hserver)
bf05bd
		return NT_STATUS_SUCCESS;
bf05bd
bf05bd
	__ntapi->tt_aligned_block_memset(
bf05bd
		&msg,0,sizeof(msg));
bf05bd
bf05bd
	msg.header.msg_type		= NT_LPC_NEW_MESSAGE;
bf05bd
	msg.header.data_size		= sizeof(msg.data);
bf05bd
	msg.header.msg_size		= sizeof(msg);
bf05bd
	msg.data.ttyinfo.opcode		= NT_TTY_LOG_ENTRY;
bf05bd
	msg.data.loginfo.type		= NT_TTY_LOG_INFO_EXCEPTION_RECORD;
7969fc
	msg.data.loginfo.meta		= dbgstate->_u.exception_info.exception_priority;
bf05bd
	msg.data.loginfo.cid.process_id	= dbgstate->cid.process_id;
bf05bd
	msg.data.loginfo.cid.thread_id	= dbgstate->cid.thread_id;
bf05bd
bf05bd
	__ntapi->tt_generic_memcpy(
bf05bd
		&msg.data.loginfo.data,
bf05bd
		&dbgstate->_u.exception_info.exception_record,
bf05bd
		sizeof(nt_exception_record));
bf05bd
bf05bd
	if ((status = __ntapi->zw_request_wait_reply_port(hserver,&msg,&msg)))
bf05bd
		return status;
bf05bd
	else if (msg.data.ttyinfo.status)
bf05bd
		return msg.data.ttyinfo.status;
bf05bd
bf05bd
	return NT_STATUS_SUCCESS;
bf05bd
}
bf05bd
bf05bd
int32_t __stdcall __ntapi_tt_debug_execution_flow(
bf05bd
	__in	void *		hdbgobj,
dce12f
	__in	void *		hprocess,
bf05bd
	__in	void *		hserver,
bf05bd
	__in	void *		hlogfile,
bf05bd
	__in	uint32_t	evtmask,
bf05bd
	__in	uint64_t *	nevents)
bf05bd
{
bf05bd
	int32_t				status;
bf05bd
	int32_t				response;
bf05bd
	int				floop;
bf05bd
	uint64_t			nevts;
bf05bd
	uint64_t			necap;
bf05bd
	nt_dbg_wait_state_change	dbgstate;
bf05bd
bf05bd
	(void)hlogfile;
bf05bd
bf05bd
	necap = (nevents && *nevents) ? *nevents : (uint64_t)(-1);
bf05bd
bf05bd
	for (nevts=0, floop=1; floop && (nevts < necap); nevts++) {
bf05bd
		if ((status = __ntapi->zw_wait_for_debug_event(
bf05bd
				hdbgobj,
bf05bd
				NT_SYNC_NON_ALERTABLE,
bf05bd
				0,&dbgstate)))
bf05bd
			return status;
bf05bd
bf05bd
		switch (dbgstate.state) {
bf05bd
			case NT_DBG_STATE_EXCEPTION:
bf05bd
				if (evtmask & NT_DBG_FLOW_MASK_EXCEPTION) {
bf05bd
					__log_exception_to_server(&dbgstate,hserver);
bf05bd
				}
bf05bd
bf05bd
				response = NT_DBG_EXCEPTION_NOT_HANDLED;
bf05bd
				break;
bf05bd
bf05bd
			case NT_DBG_STATE_EXIT_PROCESS:
bf05bd
				response = NT_DBG_CONTINUE;
bf05bd
				floop    = 0;
bf05bd
				break;
bf05bd
bf05bd
			default:
bf05bd
				response = NT_DBG_CONTINUE;
bf05bd
				break;
bf05bd
		}
bf05bd
bf05bd
		__ntapi->zw_debug_continue(
bf05bd
			hdbgobj,
bf05bd
			&dbgstate.cid,
bf05bd
			response);
bf05bd
	}
bf05bd
dce12f
	if (evtmask & NT_DBG_FLOW_MASK_DETACH_AND_CLOSE) {
dce12f
		__ntapi->zw_remove_process_debug(
dce12f
			hprocess,hdbgobj);
dce12f
dce12f
		__ntapi->zw_close(
dce12f
			hdbgobj);
dce12f
	}
dce12f
bf05bd
	return NT_STATUS_SUCCESS;
bf05bd
}