Blame src/internal/ntapi_log.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
dde53a
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
dd89bb
#include <psxtypes/psxtypes.h>
dd89bb
#include <ntapi/nt_file.h>
dd89bb
#include <ntapi/ntapi.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
a7ffe3
ssize_t __cdecl __ntapi_log_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
a7ffe3
int32_t __cdecl __ntapi_log_fn_call(
dd89bb
	__in	void *			hfile		__optional,
d95831
	__in	const char *		fn_caller_name,
dd89bb
	__in	void *			fn_callee_addr,
dd89bb
	__in	uintptr_t		fn_ret,
a7ffe3
	__in	ntapi_log_write*	pfn_log_write	__optional,
d95831
	__in	const 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;
a7ffe3
	ssize_t				bytes;
a7ffe3
	char				log_buf[2048];
dd89bb
a7ffe3
	if (!pfn_log_write)
a7ffe3
		pfn_log_write = __ntapi_log_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 (source && fn_name)
dd89bb
		bytes = __ntapi->sprintf(
a7ffe3
				log_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(
a7ffe3
				log_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(
a7ffe3
				log_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(
a7ffe3
				log_buf,
dd89bb
				"%s: calling 0x%08x returned 0x%08x\n\n",
dd89bb
				fn_caller_name, fn_callee_addr, fn_ret);
dd89bb
dd89bb
	if (bytes) {
a7ffe3
		bytes = __ntapi->strlen(log_buf);
dd89bb
a7ffe3
		if (bytes == pfn_log_write(hfile,log_buf,bytes))
dd89bb
			return NT_STATUS_SUCCESS;
dd89bb
		else
dd89bb
			return NT_STATUS_UNSUCCESSFUL;
dd89bb
	} else
dd89bb
		return NT_STATUS_UNSUCCESSFUL;
dd89bb
}
dd89bb
dd89bb
a7ffe3
int32_t __cdecl __ntapi_log_msg(
dd89bb
	__in	void *			hfile		__optional,
d95831
	__in	const char *		source		__optional,
dd89bb
	__in	int			line		__optional,
d95831
	__in	const char *		fn_caller_name,
d95831
	__in	const 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,
a7ffe3
	__in	ntapi_log_write*	pfn_log_write	__optional)
dd89bb
{
dd89bb
	char *		buffer;
a7ffe3
	ssize_t		bytes;
a7ffe3
	char		log_buf[2048];
dd89bb
a7ffe3
	if (!pfn_log_write)
a7ffe3
		pfn_log_write = __ntapi_log_write;
dd89bb
dd89bb
	bytes  = 0;
a7ffe3
	buffer = log_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
a7ffe3
		log_buf[0] = '\0';
dd89bb
dd89bb
	if (bytes >= 0)
a7ffe3
		buffer += __ntapi->strlen(log_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) {
a7ffe3
		bytes = __ntapi->strlen(log_buf);
dd89bb
a7ffe3
		if (bytes == pfn_log_write(hfile,log_buf,bytes))
dd89bb
			return NT_STATUS_SUCCESS;
dd89bb
		else
dd89bb
			return NT_STATUS_UNSUCCESSFUL;
dd89bb
	} else
dd89bb
		return NT_STATUS_UNSUCCESSFUL;
dd89bb
}