自定义printf输出格式

glibc版,代码可参考

1
2
3
//libstrongswan/utils/printf_hook/printf_hook_glibc.c
register_printf_specifier
register_printf_function

要注意的是,由于gcc在编译时不认识自定义格式,默认会提示warning。
禁止此提示的方法是在gcc命令参数中加入

1
-Wno-format

vstr from http://www.and.org/vstr/
代码可参考

1
2
//libstrongswan/utils/printf_hook/printf_hook_vstr.c
vstr_fmt_add

在win32下,libstrongswan自已写了一个printf,代码在位置在

1
2
//libstrongswan/utils/printf_hook/printf_hook_builtin.c
builtin_vsnprintf

设置线程中止时的回调函数

pthread版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
 
#define WhThreadHandle pthread_t
#define WH_THREAD_DEF(_proc, _arg) void *_proc(void *_arg)
#define whThreadCreate(_handle, _loop, _param) pthread_create(&_handle, NULL, _loop, _param)
 
void cleanup(void *p)
{
	printf("thread killed.\n");
}
 
WH_THREAD_DEF(func1, arg)
{
	int oldState;
	pthread_cleanup_push(cleanup, NULL);
 
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldState);
	while (1)
		sleep(1); // here must call sleep, or else cleanup will not be called.
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState);
 
	pthread_cleanup_pop(0); // here if want cleanup called, set param to non-zero.
	return NULL;
}
 
WhThreadHandle threadHandle;
 
void onSignal(int sign)
{
    switch (sign)
    {
    case SIGINT:
	pthread_cancel(threadHandle);
        printf("process ctrl+c pressed.\n");
        break;
    }
}
 
int main()
{
	signal(SIGINT, onSignal);
 
	whThreadCreate(threadHandle, func1, 0);
 
	while (1)
		;
	return 1;
}

linux驱动备忘录

驱动签名

1
2
3
4
5
CONFIG_MODULE_SIG=y
# CONFIG_MODULE_SIG_FORCE is not set
CONFIG_MODULE_SIG_ALL=y
 
${KERNEL_SRC}/scripts/sign-file sha512 ${KERNEL_SRC}/certs/signing_key.pem ${KERNEL_SRC}/certs/signing_key.x509 hello.ko

驱动调试

debugee

1
2
3
4
5
6
7
MOD_NAME=simple_mod
insmod ./${MOD_NAME}.ko
MOD_TEXT=`cat /sys/module/${MOD_NAME}/sections/.text`
MOD_DATA=`cat /sys/module/${MOD_NAME}/sections/.data`
MOD_BSS=`cat /sys/module/${MOD_NAME}/sections/.bss`
MOD_ADD="add-symbol-file /path/to/${MOD_NAME}.ko ${MOD_TEXT} -s .data ${MOD_DATA} -s .bss ${MOD_BSS}"
echo ${MOD_ADD}

debugger

1
(gdb) ${MOD_ADD}

refer to:
https://www.cnblogs.com/rivsidn/p/9481037.html
https://www.cnblogs.com/powerrailgun/p/12161295.html
https://blog.csdn.net/chdhust/article/details/8820628

利用vmware调试kernel

在vmware虚拟机A上

1
2
apt install libssl-dev
apt install libncurses-dev

从文后链接中下载kernel源码,比如版本4.15.18,解压编译

1
2
3
4
make menuconfig
make
make modules_install
make install

A关机,克隆A为B,A的虚拟机设置中增加串口

1
2
3
使用命名管道:\\.\pipe\com_1
该端是服务器。
另一端是虚拟机。

B的虚拟机设置中增加串口

1
2
3
使用命名管道:\\.\pipe\com_1
该端是客户端。
另一端是虚拟机。

A以新编译的内核引导,可能事先要加大内存;B以旧内核引导。在B中运行

1
cat < /dev/ttyS1

在A中运行

1
echo Helloworld > /dev/ttyS1

如果B中回显消息,说明串口连通。
编辑A中的/boot/grub/grub.cfg,找到新编译内核启动项,在handoff后加入kgdbwait kgdboc=ttyS1,115200 nokaslr,如

1
linux /boot/vmlinuz-4.15.18 root=UUID=7ccc722d-2cbd-4597-a367-e0635333ddbf ro quiet splash $vt_handoff kgdbwait kgdboc=ttyS1,115200 nokaslr

B退出cat程序,A重启以新编译的内核引导到kdb等待状态。在B中kernel源码根目录运行

1
2
3
4
5
gdb vmlinux
set serial baud 115200
target remote /dev/ttyS1
lx-symbols
c

A中进入系统后可用下面语句触发调试

1
2
3
#echo 1 > /proc/sys/kernel/sysrq
#echo kms,kbd > /sys/module/kgdboc/parameters/kgdboc
echo g > /proc/sysrq-trigger

vmware串口调试提速的方法为,关掉此虚拟机,编辑启动文件.vmx,加入

1
serial1.pipe.charTimePercent = "25"

其中serial1要因时而变,要查找同文件中是否有同样名称。
运行后很稳定的话,还可将25再改小再试。

注1:因为新内核的KASLR机制,如果在内核启动命令行中不加入nokaslr,调试时看到不到堆栈也下不了断点。Cannot insert breakpoint。

注2:因为源地址无法访问了,有个未尝试的点摘抄下来

1
2
3
在没有编译内核的情况下,还可以直接通过修改虚拟机的启动文件.vmx,添加:
debugStub.listen.guest32 = "TRUE"
然后在调试机器中通过:target remote ip:8832(8864)来调试,ip为真实机器的IP

refer to:
https://mirrors.edge.kernel.org/pub/linux/kernel/
https://stackoverflow.com/questions/49360506/in-kgdb-i-cannot-set-the-breakpoint
https://askubuntu.com/questions/964540/gdb-qemu-cant-put-break-point-on-kernel-function-kernel-4-10-0-35
https://www.cnblogs.com/xiaofool/p/5377737.html
https://docs.vmware.com/en/VMware-Workstation-Pro/12.0/com.vmware.ws.using.doc/GUID-B285C62D-1E7E-49E8-81D6-77910B2024A6.html

vim memo

# First,
apt install vim-gtk3
vim --version
# ensure clipboard feature added,
# then copy to system clipboard,
"+y
# Paste from system clipboard,
<Ctrl+R>+
# content of all registers
:reg
 
:marks
 
:version
:set nocindent
:set noexpandtab
:set paste
 
# record macro to register a, press q if end
qa
# replay macro from register a
@a
 
# open folder containing current file
:Ex
 
# Replace return, space and return chars in a series with one return char, entire file.
:%s/\n\ \n/\r/
 
# In insert mode,
# add char like above the cursor
<Ctrl+Y>
# add char like below the cursor
<Ctrl+E>
 
# till after "
T"
# till before "
t"
 
# exchange current window position with others
<Ctrl+W>r
# or
<Ctrl+W>x
:all
:vertical all
:only

Plugins,

SrcExpl
	https://www.vim.org/scripts/script.php?script_id=2179
	https://github.com/wenlongche/SrcExpl
taglist.vim
	https://www.vim.org/scripts/script.php?script_id=273
	https://github.com/yegappan/taglist

refer to:
https://www.imooc.com/wenda/detail/588162
https://www.cnblogs.com/bwangel23/p/4421957.html
https://blog.csdn.net/Coppa/article/details/108026080

国密备忘录

1
2
3
4
SM2用于替换RSA/Diffie-Hellman/ECDSA/ECDH等
SM3用于替代MD5/SHA-1/SHA-256等
SM4用于替代DES/AES等
SM9用于替代PKI/CA

gmssl编译指定路径

1
./config --prefix=`pwd`/installed no-shared

refer to:
http://gmssl.org
https://ai-science-ape.blog.csdn.net/article/details/113551179
https://www.jianshu.com/p/e41bc1eb1d81