SkyEye的模块编程示例(一)

本文介绍了skyeye模块编程的一个示例程序log-pc模块。模块功能主要是用来记录skyeye执行过的所有PC指令。

 

一、源代码分析

1.1 log.c:实现了记录PC的功能,

#include <stdlib.h> #include <stdio.h> #include "skyeye_arch.h" #include "skyeye_callback.h" /* flag to enable log function. */ static int enable_log_flag; /* the log file for record. */ static const char* log_filename = "./pc.log"; /* fd of log_filename */ static FILE* log_fd; /* callback function for step exeuction. Will record pc here. */ static void log_pc_callback(generic_arch_t* arch_instance){ if(enable_log_flag){ fprintf(log_fd, "pc=0x%x/n", arch_instance->get_pc()); } } /* enable log functionality */ static void com_log_pc(char* arg){ enable_log_flag = 1; } /* some initialization for log functionality */ int log_init(){ exception_t exp; /* open file for record pc */ log_fd = fopen(log_filename, "w"); if(log_fd == NULL){ fprintf(stderr, "Can not open the file %s for log-pc module./n", log_filename); return; } /* register callback function */ register_callback(log_pc_callback, Step_callback); /* add correspinding command */ add_command("log-pc", com_log_pc, "record the every pc to log file./n"); } /* destruction function for log functionality */ int log_fini(){ if(log_fd != NULL){ fclose(log_fd); } }
}

 

1.2 log_module.c:用来实现模块的加载和卸载函数

 

#include <stdio.h> #include <errno.h> #include "skyeye_types.h" #include "skyeye_arch.h" #include "skyeye_module.h" /* module name */ const char* skyeye_module = "log-pc"; /* module initialization and will be executed automatically when loading. */ void module_init(){ log_init(); } /* module destruction and will be executed automatically when unloading */ void module_fini(){ log_fini(); }

 

1.3 Makefile

SKYEYE_PREFIX=/opt/skyeye/ CC=gcc liblog.so: $(CC) -I${SKYEYE_PREFIX}/include/include -L$(SKYEYE_PREFIX)/lib/skyeye/ -lcommon -shared -oliblog.so log.c log_module.c clean: rm liblog.so *.o -r -f install: cp liblog.so ${SKYEYE_PREFIX}/lib/skyeye/

 

二、代码编译与安装

2.1 编译模块

创建一个新的目录,并把上面的log.c log_module.c Makefile都放入此目录。

在编译模块之前,先确认skyeye已经安装到系统中,可以查看/opt/skyeye/include/include/中是否有相关头文件存在,如skyeye_types.h, skyeye_arch.h等等。

如果skyeye安装正常,我们可以在你的模块目录下运行"make"来编译模块生成log.so文件。

 

2.2 模块安装

运行"make install"命令,可以把你编译的模块安装到skyeye的相应模块目录,skyeye会在启动的时候记载模块。

 

三、运行测试

进入到/opt/skyeye/testsuite/arm_hello目录下,测试我们的log-pc模块的运行。运行skyeye如下:

ksh@linux-gvai:/opt/skyeye/testsuite/arm_hello> ../../bin/skyeye
SkyEye is an Open Source project under GPL. All rights of different parts or modules are reserved by their author. Any modification or redistributions of SkyEye should note remove or modify the annoucement of SkyEye copyright.
Get more information about it, please visit the homepage http://www.skyeye.org.
Type "help" to get command list.
(skyeye)

 

然后再运行list-modules可以发现,我们的log-pc模块已经被加载,输出如下:
(skyeye)list-modules
Module Name             File Name
nandflash               /opt/skyeye/lib/skyeye/libnandflash.so
arm                     /opt/skyeye/lib/skyeye/libarm.so
log-pc                  /opt/skyeye/lib/skyeye/log.so
bfin                    /opt/skyeye/lib/skyeye/libbfin.so
log-pc                  /opt/skyeye/lib/skyeye/liblog.so
uart                    /opt/skyeye/lib/skyeye/libuart.so
mips                    /opt/skyeye/lib/skyeye/libmips.so
net                     /opt/skyeye/lib/skyeye/libnet.so
code_cov                /opt/skyeye/lib/skyeye/libcodecov.so
sparc                   /opt/skyeye/lib/skyeye/libsparc.so
ppc                     /opt/skyeye/lib/skyeye/libppc.so
touchscreen             /opt/skyeye/lib/skyeye/libts.so
coldfire                /opt/skyeye/lib/skyeye/libcoldfire.so
flash                   /opt/skyeye/lib/skyeye/libflash.so
lcd                     /opt/skyeye/lib/skyeye/liblcd.so
gdbserver               /opt/skyeye/lib/skyeye/libgdbserver.so
(skyeye)

 

然后还可以运行我们在前面log.c文件中注册的命令log-pc,来使能日志功能:

ksh@linux-gvai:/opt/skyeye/testsuite/arm_hello> ../../bin/skyeye
SkyEye is an Open Source project under GPL. All rights of different parts or modules are reserved by their author. Any modification or redistributions of SkyEye should note remove or modify the annoucement of SkyEye copyright.
Get more information about it, please visit the homepage http://www.skyeye.org.
Type "help" to get command list.

(skyeye)start
arch: arm
cpu info: armv3, arm7tdmi, 41007700, fff8ff00, 0
In do_mach_option, mach info: name at91, mach_init addr 0xb72a0f70

uart_mod:3, desc_in:, desc_out:, converter:
In create_uart_console
cpu info: armv3, arm7tdmi
SKYEYE: use arm7100 mmu ops
In SIM_start, Set PC to the address 0x0

(skyeye)log-pc
(skyeye)

你可能感兴趣的:(编程,function,Module,command,callback,initialization)