Add aaa-12 增加内核日志接口模块,并提供对应的测试例程

RCA:
SOL:
修改人:liangxia
检视人:
This commit is contained in:
zhangtaohz 2019-08-19 18:20:06 +08:00
parent c4343cb336
commit 9f183b4708
10 changed files with 367 additions and 4 deletions

28
Common/klog_api.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef _KLOG_API_H
#define _KLOG_API_H
#include <linux/kernel.h>
#include "common_types.h"
#define MAX_MODULE_NAME_SZ 16
typedef struct _klog {
char module_name[MAX_MODULE_NAME_SZ];
}klog_t;
extern klog_t *klog_init(const char *module_name);
extern void klog_close(klog_t *log);
extern void klog_record(const klog_t *log, const char* level_str, const char *fmt, ...);
#define KLOG_DEBUG(log, fmt, ...) klog_record(log, KERN_DEBUG, fmt, ##__VA_ARGS__)
#define KLOG_INFO(log, fmt, ...) klog_record(log, KERN_INFO, fmt, ##__VA_ARGS__)
#define KLOG_NOTICE(log, fmt, ...) klog_record(log, KERN_NOTICE, fmt, ##__VA_ARGS__)
#define KLOG_WARNING(log, fmt, ...) klog_record(log, KERN_WARNING, fmt, ##__VA_ARGS__)
#define KLOG_ERR(log, fmt, ...) klog_record(log, KERN_ERR, fmt, ##__VA_ARGS__)
#define KLOG_CRIT(log, fmt, ...) klog_record(log, KERN_CRIT, fmt, ##__VA_ARGS__)
#define KLOG_ALERT(log, fmt, ...) klog_record(log, KERN_ALERT, fmt, ##__VA_ARGS__)
#define KLOG_EMERG(log, fmt, ...) klog_record(log, KERN_EMERG, fmt, ##__VA_ARGS__)
#endif

View File

@ -28,9 +28,9 @@ MAKE_FLAGS += -j$(shell cat /proc/cpuinfo | grep processor | wc -l)
endif endif
endif endif
.PHONY : demo database openrpc ulog conntrack netlink trace redismq usermanager configm webauth matchrule logging .PHONY : demo database openrpc ulog klog klog_test conntrack netlink trace redismq usermanager configm webauth matchrule logging
all: demo database openrpc ulog conntrack netlink trace redismq usermanager configm webauth matchrule logging all: demo database openrpc ulog klog klog_test conntrack netlink trace redismq usermanager configm webauth matchrule logging
ifeq ($(OPT), install) ifeq ($(OPT), install)
#$(shell `find ../release -name "*.zip" -delete`) #$(shell `find ../release -name "*.zip" -delete`)
@ -212,6 +212,20 @@ else
$(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f user.logging.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=logging $(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f user.logging.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=logging
endif endif
klog:
ifeq ($(OPT), clean)
$(MLOG)make $(MAKE_FLAGS) -C Platform/build -f module.klog_api.api.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=klog_api
else ifeq ($(OPT), install)
$(MLOG)make $(MAKE_FLAGS) -C Platform/build -f module.klog_api.api.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=klog_api
else
$(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f module.klog_api.api.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=klog_api
endif
klog_test:
ifeq ($(OPT), clean)
$(MLOG)make $(MAKE_FLAGS) -C Platform/build -f module.klog_api.test.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=test_klog_api
else ifeq ($(OPT), install)
$(MLOG)make $(MAKE_FLAGS) -C Platform/build -f module.klog_api.test.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=test_klog_api
else
$(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f module.klog_api.test.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=test_klog_api
endif

View File

@ -0,0 +1,49 @@
# target name, the target name must have the same name of c source file
TARGET_NAME=klog_api
# target
# for linux module driver: KO
# for application: EXE
# for dynamic library: DLL
TARGET_TYPE = KO
# target object
# for application: APP
# for device driver: DRV
TARGET_OBJ = DRV
# custom install dir
TARGET_BOX =
#debug mode or release mode
DEBUG = TRUE
PLAT_LINUX ?= TRUE
PLAT_ARM64 ?= TRUE
VPATH = ../modules/klog_api/api
# source code
# set the source file, don't used .o because of ...
COMMON_SRCS = klog_api.c klog_api_mod.c
# MRS Board Source Files
PLAT_LINUX_SRCS = $(COMMON_SRCS)
PLAT_ARM64_SRCS = $(COMMON_SRCS)
# gcc CFLAGS
PLAT_ARM64_CFLAGS := -I../../Common
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)
# this line must be at below of thus, because of...
include ../../Common/common.Makefile
ifeq ($(MAKECMDGOALS), )
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
else
ifeq ($(MAKECMDGOALS), all)
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
endif
endif

View File

@ -0,0 +1,49 @@
# target name, the target name must have the same name of c source file
TARGET_NAME=test_klog_api
# target
# for linux module driver: KO
# for application: EXE
# for dynamic library: DLL
TARGET_TYPE = KO
# target object
# for application: APP
# for device driver: DRV
TARGET_OBJ = DRV
# custom install dir
TARGET_BOX =
#debug mode or release mode
DEBUG = TRUE
PLAT_LINUX ?= TRUE
PLAT_ARM64 ?= TRUE
VPATH = ../modules/klog_api/test
# source code
# set the source file, don't used .o because of ...
COMMON_SRCS = test_klog_api.c test_klog_api_mod.c
# MRS Board Source Files
PLAT_LINUX_SRCS = $(COMMON_SRCS)
PLAT_ARM64_SRCS = $(COMMON_SRCS)
# gcc CFLAGS
PLAT_ARM64_CFLAGS := -I../../Common
PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)
# this line must be at below of thus, because of...
include ../../Common/common.Makefile
ifeq ($(MAKECMDGOALS), )
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
else
ifeq ($(MAKECMDGOALS), all)
$(shell find ./ -name "$(TARGET)-*.ko" -delete)
endif
endif

View File

@ -0,0 +1,15 @@
ifneq ($(KERNELRELEASE),)
obj-m += klog_api.o
else
PWD := $(shell pwd)
KVER := $(shell uname -r)
# KDIR := /lib/modules/$(KVER)/build
KDIR := $(HUACHENG_LINUX_KERNEL)
# KBUILD_EXTRA_SYMBOLS += /data/code/modules/netlink_api/app_k/Module.symvers
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -rf *.o *.mod.c *.ko *.symvers *.order *.makers .*.cmd *.cmd .tmp_versions .cache.mks
endif

View File

@ -0,0 +1,88 @@
#include "klog_api.h"
#include <linux/slab.h>
#include <linux/module.h>
#define LOG_MSG_SZ 1024
#define MODULE_FMT "[%s]"
klog_t *g_klog = NULL;
klog_t *klog_init(const char *module_name)
{
klog_t *log = NULL;
u32 len = 0;
if(NULL == module_name) {
return NULL;
}
len = strlen(module_name);
if (len >= MAX_MODULE_NAME_SZ) {
printk(KERN_ERR"The length:%d of module_name must be less than %d, but input module_name is %s", len, MAX_MODULE_NAME_SZ, module_name);
return NULL;
}
log = (klog_t *)kmalloc(sizeof(*log), GFP_KERNEL);
if (NULL == log) {
printk(KERN_ERR"Allocating log memory is failure");
return NULL;
}
memset(log->module_name, '\0', MAX_MODULE_NAME_SZ);
strncpy(log->module_name, module_name, len);
return log;
}
void klog_close(klog_t *log)
{
if (log != NULL) {
kfree(log);
}
}
void klog_record(const klog_t *log, const char* level_str, const char *fmt, ...)
{
char log_buf[LOG_MSG_SZ];
va_list args;
va_start(args, fmt);
vsnprintf(log_buf, sizeof(log_buf), fmt, args);
va_end(args);
if (NULL != log) {
printk("%s"MODULE_FMT" %s", level_str, log->module_name, log_buf) ;
}
else {
printk("%s %s", level_str, log_buf) ;
}
}
EXPORT_SYMBOL_GPL(klog_init);
EXPORT_SYMBOL_GPL(klog_close);
EXPORT_SYMBOL_GPL(klog_record);
static int __init klog_api_init(void)
{
g_klog = klog_init("klog_api");
KLOG_INFO(g_klog, "Klog is initiated");
return 0;
}
static void __exit klog_api_exit(void)
{
KLOG_INFO(g_klog, "Klog is exited");
klog_close(g_klog);
}
module_init(klog_api_init);
module_exit(klog_api_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Klog_api process module");
MODULE_AUTHOR("zhangtao");

View File

@ -0,0 +1,28 @@
#include <linux/module.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>
MODULE_INFO(vermagic, VERMAGIC_STRING);
MODULE_INFO(name, KBUILD_MODNAME);
__visible struct module __this_module
__attribute__((section(".gnu.linkonce.this_module"))) = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};
#ifdef RETPOLINE
MODULE_INFO(retpoline, "Y");
#endif
static const char __module_depends[]
__used
__attribute__((section(".modinfo"))) =
"depends=";
MODULE_INFO(srcversion, "176959D2D9B79E0BAD85AE2");

View File

@ -0,0 +1,15 @@
ifneq ($(KERNELRELEASE),)
obj-m += test_klog_api.o
else
PWD := $(shell pwd)
KVER := $(shell uname -r)
# KDIR := /lib/modules/$(KVER)/build
KDIR := $(HUACHENG_LINUX_KERNEL)
# KBUILD_EXTRA_SYMBOLS += /data/code/modules/netlink_api/app_k/Module.symvers
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -rf *.o *.mod.c *.ko *.symvers *.order *.makers .*.cmd *.cmd .tmp_versions .cache.mks
endif

View File

@ -0,0 +1,49 @@
#include "klog_api.h"
#include <linux/module.h>
klog_t *g_klog = NULL;
static void test_klog_api(const char *module_name)
{
g_klog = klog_init(module_name);
KLOG_DEBUG (g_klog, "test for klog_api %s", "debug");
KLOG_INFO (g_klog, "test for klog_api %s", "info");
KLOG_NOTICE (g_klog, "test for klog_api %s", "notice");
KLOG_WARNING(g_klog, "test for klog_api %s", "warning");
KLOG_ERR (g_klog, "test for klog_api %s", "err");
KLOG_CRIT (g_klog, "test for klog_api %s", "crit");
KLOG_ALERT (g_klog, "test for klog_api %s", "alert");
KLOG_EMERG (g_klog, "test for klog_api %s", "emerg");
klog_close(g_klog);
}
static int __init test_klog_api_init(void)
{
printk(KERN_INFO"Test-klog_api is initiated");
test_klog_api("test_klog_api");
test_klog_api(NULL);
test_klog_api("");
test_klog_api("a");
test_klog_api("123456789012345");
test_klog_api("1234567890123456");
test_klog_api("12345678901234567");
return 0;
}
static void __exit test_klog_api_exit(void)
{
printk(KERN_INFO"Test-klog_api is exited");
}
module_init(test_klog_api_init);
module_exit(test_klog_api_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Test-klog_api process module");
MODULE_AUTHOR("zhangtao");

View File

@ -0,0 +1,28 @@
#include <linux/module.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>
MODULE_INFO(vermagic, VERMAGIC_STRING);
MODULE_INFO(name, KBUILD_MODNAME);
__visible struct module __this_module
__attribute__((section(".gnu.linkonce.this_module"))) = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};
#ifdef RETPOLINE
MODULE_INFO(retpoline, "Y");
#endif
static const char __module_depends[]
__used
__attribute__((section(".modinfo"))) =
"depends=";
MODULE_INFO(srcversion, "176959D2D9B79E0BAD85AE2");