AUTOSAR中Hook总结

Hook是什么

在计算机编程中,Hook是一系列技术,是截获计算机软件组件之间交互或者信息交互来增强和改善系统,而这种截获行为的函数调用,就可以理解为Hook,也可以理解为我们平常听说的钩子函数。Hook的一般目的是用于调试和扩展功能,当然一些黑客也会利用这些Hook做一些不正当的事,这是另外的事了。

在OSEK OS中,PreTask和PostTask Hook在具有不受限制的访问权限的OS级别上运行,因此必须受到信任。强烈建议这些Hook函数仅在调试期间使用,而不在最终产品中使用。

当OS-Application被终止时,不调用OSApplication的Shutdown和Startup Hook。可以在重新启动任务中完成OS-Application特定数据的清理。

所有特定于应用程序的Hook函数(Startup, Shutdown and Error)必须返回(不接受阻塞或无限循环)。

接下来,看看在Configurator中,这些Hook在哪配置的。

Hook配置

在Configuration Editor窗口中可以找到一个叫“Runtime System”的菜单,里面有个子菜单叫“Runtime System General”,这个是配置RTE和OS General参数的地方。

点击进去这个菜单,可以找到个Hook Routines,里面有很多Hook选项。

 

以下,我做了个汇总表,来罗列这些Hook的作用:

Hooks

Description

Error Hook

If selected, the ErrorHook routine is called, if an API function returns with an error code unequal to E_OK. It is also called if OS internal errors are detected.

The function ErrorHook has to be provided by the application.

Panic Hook

If selected, the PanicHook routine will be called by the operating system if an inconsistent OS state is detected and regular shutdown cannot be reached.

The function Os_PanicHook has to be provided by the application.

Post Task Hook

If selected, the PostTaskHook routine will be called any time the operating system interrupts or terminates a task.

The function PostTaskHook has to be provided by the application.

Pre Task Hook

If selected, the PreTaskHook routine will be called any time the operating system starts or resumes a task.

The function PreTaskHook has to be provided by the application.

Protection Hook

Switch to enable/disable the call to the (user supplied) protection hook.

Shutdown Hook

If selected, the ShutdownHook routine will be called, if the API-function ShutdownOS is either called by the application or by the operating system.

The function ShutdownHook has to be provided by the application.

Startup Hook

If selected, the StartupHook routine will be called by the operating system in the startup phase (after calling StartOS and before starting the first task).

The function StartupHook has to be provided by the application.

以上,我是摘自Davinci Configurator的说明,没玩过这些东西的同学,看起来会有点懵逼,这到底是什么意思呢?怎么用的呢?

我打算将全部hook都讲解一遍,包括其使用方法和调用过程剖析。

Error Hook

从Vector的OS参考文档中可以看到,MICROSAR OS is 可以检测并处理以下errors:

Application Errors …

> Are raised if the OS could not execute a requested OS API service correctly. Typically the OS API is used wrong (e.g. invalid object ID).

> Do not corrupt OS internal data.

>    Will result in call of the global ErrorHook() for centralized error handling (if configured).

> Will result in call of an application specific ErrorHook (if configured).

> May not induce shutdown / terminate reactions. Instead the application may continue execution by simply returning from the ErrorHooks.

Protection Errors …

> Are raised if the application violates its configured boundaries (e.g. memory access violations, timing violations).

> Do not corrupt OS internal data.

> Are raised upon occurrence of unhandled exceptions and interrupts.

> Will result in call of the ProtectionHook() where a shutdown or terminate handling (with or without restart) is induced.

> If Shutdown is induced the ShutdownHook() is called (if configured).

> If no ProtectionHook() is configured shutdown is induced.

Kernel Errors …

> Are raised if the OS cannot longer assume the correctness of its internal data (e.g. memory access violation during ProtectionHook())

> The OS will disable all interrupts and call the Os_PanicHook() to inform the application.

> Afterwards the OS enters an infinite loop.

这就用到ErrorHook了,Vector的MicroSAR OS使用8 bit即一个byte来记录和扩展Error Code,一些Error已经是在AUTOSAR OS或者OSEK OS已经定义的了。

Type of Error

Related Error Code

Value

An internal OS buffer used for cross core communication is full.

E_OS_SYS_OVERFLOW

0xF5

A forcible termination of a kernel object has been requested e.g. terminate system applications.

E_OS_SYS_KILL_KERNEL_OBJ

0xF6

An OS-Application has been terminated with requested restart but no restart task

has been configured.

E_OS_SYS_NO_RESTARTTASK

0xF7

The application tries to use an API cross core, but the target core has not been configured for cross core API

E_OS_SYS_CALL_NOT_ALLOWED

0xF8

The triggered cross core function is not available on the target core.

E_OS_SYS_FUNCTION_UNAVAILABLE

0xF9

A syscall instruction has been executed with an invalid syscall number.

E_OS_SYS_PROTECTION_SYSCALL

0xFA

An unhandled interrupt occurred.

E_OS_SYS_PROTECTION_IRQ

0xFB

The interrupt handling API is used wrong.

E_OS_SYS_API_ERROR

0xFC

Internal OS assertion (not issued to

customer).

E_OS_SYS_ASSERTION

0xFD

A system timer ISR was delayed too long.

E_OS_SYS_OVERLOAD

0xFE

更详细的Error Code ID,请见Os_Types.h中的Os_StatusType定义。

然后,我们是可以通过以下函数来捕获这些Error,记录或者打印出来,以提示当前系统出现了什么问题。

FUNC(void, OS_ERRORHOOK_CODE) ErrorHook(StatusType Error)
{
/*Customer source code implement here!*/
}

我有一个非常详细的讲解ErrorHook的文章,见《OS ErrorHook

Panic Hook

Panic Hook是什么东西?

恐慌,呵呵……

我们可以理解Panic Hook是捕获“恐慌”用的。

什么时候才会“恐慌”呢?当然是,程序没按照你设定的路线运行!你让程序走左边,它偏偏走右边,问你慌不慌?

举个例子来说明吧。

以下这个Os_CoreShutdown(我省略了部分代码),是在ECU shutdown的时候调用的,按照我们的设定,Os_HookCallStatusHook(core->ShutdownHookRef, Error);调用完,程序就结束了,即MCU睡眠了,不再运行了。那万一它还继续跑呢,那就是有问题了,那就可以通过Os_ErrKernelPanic()来捕获这种行为。这样可以理解了吧。

OS_LOCAL OS_FUNC_ATTRIBUTE_DEFINITION(void, OS_CODE, OS_NORETURN, Os_CoreShutdown,
(
  StatusType Error,
  boolean Synchronize
))
{
  // … …
​
  /* #100 Call system shutdown hook. */
  Os_HookCallStatusHook(core->ShutdownHookRef, Error); 
  /* #110 Kernel panic. */
  Os_ErrKernelPanic();
}

等等,为啥不是PanicHook函数,而是Os_ErrKernelPanic()?实际上,AUTOSAR这个OS里面有很多很多Os_ErrKernelPanic()调用的地方,考虑还是挺周到的。

接下来,一步步讲解这个调用过程

先看Os_ErrKernelPanic,里面调用了Os_HookCallPanicHook

OS_FUNC_ATTRIBUTE_DEFINITION(void, OS_CODE, OS_NORETURN, Os_ErrKernelPanic, (void))
{
  /* #10 Disable interrupts. */
  Os_IntDisable();
  /* #20 Call Panic Hook. */
  Os_HookCallPanicHook();
  /* #30 Freeze. */
  Os_Hal_CoreFreeze();
}

 

FUNC(void, OS_CODE) Os_HookCallPanicHook(void)
{
  // … …
  if(Os_HookIsPanicHookEnabled() != 0u) 
  {
    OS_PANICHOOK();
  }
}

 

而这个OS_PANICHOOK是个宏定义,需要在Configurator里面配置才能自定义的,否则,默认就是Os_Hal_CoreFreeze而Os_HookCallPanicHook调用了OS_PANICHOOK

#if OS_CFG_PANICHOOK == STD_ON
# define OS_PANICHOOK        Os_PanicHook
#else
# define OS_PANICHOOK        Os_Hal_CoreFreeze
#endif
FUNC(void, OS_PANICHOOK_CODE) Os_PanicHook(void)
{
/* Customer source code shuld be implemented here!*/
}

所以,要不要捕获“恐慌”,或者就用Os_Hal_CoreFreeze,你看着办吧。 

Post Task Hook和PreTask Hook

这两个Hook,其实很简单,就是进入和退出一个Task的时候调用的。

有什么用?那就好玩了,可以做CPU的占用率统计啊,可以统计各个Task的占用时间段啊等等。

部分内容省略……

Protection Hook

……

Shutdown Hook

……

Startup Hook

……

 

部分内容,详见公众号:嵌入式软件实战派,关注并回复“AUTOSAR”获得

  • 10
    点赞
  • 87
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值