Blame src/internal/ntapi_debug.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
4256e2
/*  Copyright (C) 2013--2016  Z. Gilboa                 */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
dd89bb
#ifdef __DEBUG
dd89bb
dd89bb
#include <psxtypes/psxtypes.h>
dd89bb
#include <ntapi/nt_file.h>
dd89bb
#include <ntapi/ntapi.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
dd89bb
char dbg_buf[0x1000];
dd89bb
dd89bb
ssize_t __cdecl __dbg_write(
dd89bb
	__in	void *		hfile,
dd89bb
	__in	const void *	buf,
dd89bb
	__in	size_t		bytes)
dd89bb
{
dd89bb
	nt_iosb		iosb;
dd89bb
	int32_t		status;
dd89bb
dd89bb
	status = __ntapi->zw_write_file(
dd89bb
		hfile,
dd89bb
		(void *)0,
dd89bb
		(nt_io_apc_routine *)0,
dd89bb
		(void *)0,
dd89bb
		&iosb,
dd89bb
		(void *)buf,
dd89bb
		(uint32_t)bytes,
dd89bb
		(nt_large_integer *)0,
dd89bb
		(uint32_t *)0);
dd89bb
dd89bb
	if (status == NT_STATUS_SUCCESS)
dd89bb
		return iosb.info;
dd89bb
	else
dd89bb
		return -1;
dd89bb
}
dd89bb
dd89bb
dd89bb
int32_t __cdecl __dbg_fn_call(
dd89bb
	__in	void *			hfile		__optional,
dd89bb
	__in	char *			fn_caller_name,
dd89bb
	__in	void *			fn_callee_addr,
dd89bb
	__in	uintptr_t		fn_ret,
dd89bb
	__in	ntapi_dbg_write*	pfn_dbg_write	__optional,
dd89bb
	__in	char *			source		__optional,
dd89bb
	__in	int			line		__optional)
dd89bb
{
dd89bb
	struct pe_ldr_tbl_entry *	image_meta;
dd89bb
	void *				image_base;
dd89bb
	char *				fn_name;
dd89bb
	size_t				bytes;
dd89bb
	char				dbg_buf[256];
dd89bb
dd89bb
	if (!pfn_dbg_write)
dd89bb
		pfn_dbg_write = __dbg_write;
dd89bb
dd89bb
	image_meta = pe_get_symbol_module_info(fn_callee_addr);
dd89bb
	fn_name    = (char *)0;
dd89bb
dd89bb
	if (image_meta)
dd89bb
		image_base = image_meta->dll_base;
dd89bb
	else
dd89bb
		image_base = (void *)0;
dd89bb
dd89bb
dd89bb
	if (image_base)
dd89bb
		fn_name = pe_get_symbol_name(
dd89bb
			image_base,
dd89bb
			fn_callee_addr);
dd89bb
dd89bb
	if (!fn_name)
dd89bb
		fn_name = pe_get_import_symbol_info(
dd89bb
			fn_callee_addr,
dd89bb
			(void **)0,
dd89bb
			(char **)0,
dd89bb
			&image_meta);
dd89bb
dd89bb
	if (source && fn_name)
dd89bb
		bytes = __ntapi->sprintf(
dd89bb
				dbg_buf,
dd89bb
				"%s: (%s:%d):\n"
dd89bb
				"--> %s returned 0x%08x\n\n",
dd89bb
				fn_caller_name, source, line, fn_name, fn_ret);
dd89bb
	else if (fn_name)
dd89bb
		bytes = __ntapi->sprintf(
dd89bb
				dbg_buf,
dd89bb
				"%s: %s returned 0x%08x\n\n",
dd89bb
				fn_caller_name, fn_name, fn_ret);
dd89bb
	else if (source)
dd89bb
		bytes = __ntapi->sprintf(
dd89bb
				dbg_buf,
dd89bb
				"%s: (%s:%d):\n"
dd89bb
				"--> calling 0x%08x returned 0x%08x\n\n",
dd89bb
				fn_caller_name, source, line, fn_callee_addr, fn_ret);
dd89bb
	else
dd89bb
		bytes = __ntapi->sprintf(
dd89bb
				dbg_buf,
dd89bb
				"%s: calling 0x%08x returned 0x%08x\n\n",
dd89bb
				fn_caller_name, fn_callee_addr, fn_ret);
dd89bb
dd89bb
	if (bytes) {
dd89bb
		bytes = __ntapi->strlen(dbg_buf);
dd89bb
dd89bb
		if (bytes == pfn_dbg_write(hfile,dbg_buf,bytes))
dd89bb
			return NT_STATUS_SUCCESS;
dd89bb
		else
dd89bb
			return NT_STATUS_UNSUCCESSFUL;
dd89bb
	} else
dd89bb
		return NT_STATUS_UNSUCCESSFUL;
dd89bb
}
dd89bb
dd89bb
dd89bb
int32_t __cdecl __dbg_msg(
dd89bb
	__in	void *			hfile		__optional,
dd89bb
	__in	char *			source		__optional,
dd89bb
	__in	int			line		__optional,
dd89bb
	__in	char *			fn_caller_name,
dd89bb
	__in	char *			fmt,
dd89bb
	__in	uintptr_t		arg1,
dd89bb
	__in	uintptr_t		arg2,
dd89bb
	__in	uintptr_t		arg3,
dd89bb
	__in	uintptr_t		arg4,
dd89bb
	__in	uintptr_t		arg5,
dd89bb
	__in	uintptr_t		arg6,
dd89bb
	__in	ntapi_dbg_write*	pfn_dbg_write	__optional)
dd89bb
{
dd89bb
	char *		buffer;
dd89bb
	size_t		bytes;
dd89bb
dd89bb
	if (!pfn_dbg_write)
dd89bb
		pfn_dbg_write = __dbg_write;
dd89bb
dd89bb
	bytes  = 0;
dd89bb
	buffer = dbg_buf;
dd89bb
dd89bb
	if (source)
dd89bb
		bytes = __ntapi->sprintf(
dd89bb
				buffer,
dd89bb
				"%s: (%s:%d):\n--> ",
dd89bb
				fn_caller_name,source,line);
dd89bb
	else if (fn_caller_name)
dd89bb
		bytes = __ntapi->sprintf(
dd89bb
				buffer,
dd89bb
				"%s: ",
dd89bb
				fn_caller_name);
dd89bb
	else
dd89bb
		dbg_buf[0] = '\0';
dd89bb
dd89bb
	if (bytes >= 0)
dd89bb
		buffer += __ntapi->strlen(dbg_buf);
dd89bb
	else
dd89bb
		return NT_STATUS_UNSUCCESSFUL;
dd89bb
dd89bb
	bytes = __ntapi->sprintf(buffer,fmt,arg1,arg2,arg3,arg4,arg5,arg6);
dd89bb
dd89bb
	if (bytes) {
dd89bb
		bytes = __ntapi->strlen(dbg_buf);
dd89bb
dd89bb
		if (bytes == pfn_dbg_write(hfile,dbg_buf,bytes))
dd89bb
			return NT_STATUS_SUCCESS;
dd89bb
		else
dd89bb
			return NT_STATUS_UNSUCCESSFUL;
dd89bb
	} else
dd89bb
		return NT_STATUS_UNSUCCESSFUL;
dd89bb
}
dd89bb
dd89bb
#endif