驅動程式 - VxWorks - 23.09 - C/C++ - Syscall Hook (Entry and Exit)



參考資訊:
https://zhuanlan.zhihu.com/p/597577575
https://cloud.tencent.com/developer/article/1460923
https://studies.ac.upc.edu/doctorat/ENGRAP/VxWorks-device-drivers.htm
https://forums.windriver.com/t/vxworks-software-development-kit-sdk/43
https://mail.prz-rzeszow.pl/~ssamolej/vxworks/vxworks_kernel_programmers_guide_6.6.pdf
https://d13321s3lxgewa.cloudfront.net/downloads/wrsdk-vxworks7-docs/2309/README_qemu.html
https://learning.windriver.com/path/vxworks7-essentials-workbench-and-tools/vxworks-kernel-shell

main.c

#include <stdio.h>
#include <string.h>
#include <syscall.h>
#include <syscallLib.h>
#include <private/rtpLibP.h>
#include <private/syscallLibP.h>
#include <syscallTbl.h>

static int cc = 0;

int entry_hook(const SYSCALL_ENTRY_STATE *pState)
{
    if (pState->scn == SCN__open) {
        const char *p = (const char *)pState->args[0];

        if ((p[0] == 'm') && (p[1] == 'a') && (p[2] == 'i') && (p[3] == 'n')) {
            cc = 100;
        }
    }
    return OK;
}

void exit_hook(long returnValue)
{
}

int print_cc(void)
{
    printf("cc %d\n", cc);
    return OK;
}

int add_hook(void)
{
    syscallEntryHookAdd(entry_hook, FALSE); 
    syscallExitHookAdd(exit_hook, FALSE); 
    return OK;
}

P.S. 不能在syscallEntryHook()裏面呼叫任何libc的函數

app.c

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char **argv)
{
    int fd = -1;

    fd = open("main", O_RDONLY);
    close(fd);
    return 0;
}

編譯

$ wr-cc app.c -o app
$ wr-cc main.c -o main -dkm

執行

-> ld < main

-> add_hook

-> print_cc
    cc 0

-> cmd

[vxWorks *]# app

[vxWorks *]# C

-> print_cc
    cc 100

-> unld "main"