48 #define _________________________________________DEFINITIONS_SECTION________________________________________________________________________ 50 #define MAX_LOGGED_EVENTS 30 52 #undef _________________________________________DEFINITIONS_SECTION________________________________________________________________________ 60 #define _________________________________________TYPES_SECTION______________________________________________________________________________ 65 const char * EventName;
66 char AdditionalInfo[150];
67 oC_Timestamp_t Timestamp;
71 bool StackAddressCorrect;
72 bool InterruptsEnabled;
73 oC_ExcHan_ExceptionType_t ExceptionType;
74 bool ErrorInSystemContext;
77 oC_Int_t FreeStackBytes;
80 #undef _________________________________________TYPES_SECTION______________________________________________________________________________ 88 #define _________________________________________PROTOTYPES_SECTION_________________________________________________________________________ 91 static void MemoryEventHandler (
void * Address , MemoryEventFlags_t Event ,
const char * Function, uint32_t LineNumber );
95 #undef _________________________________________PROTOTYPES_SECTION_________________________________________________________________________ 102 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________ 115 static EventLog_t LoggedEvents[MAX_LOGGED_EVENTS] = {{ 0 }};
119 uintptr_t __stack_chk_guard = 0xfdecbaba;
121 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________ 129 #define _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 142 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
144 oC_IntMan_EnterCriticalSection();
151 ( !oC_ErrorOccur(errorCode) || errorCode == oC_ErrorCode_ModuleIsTurnedOn )
158 ( !oC_ErrorOccur(errorCode) || errorCode == oC_ErrorCode_ModuleIsTurnedOn )
163 DaemonProcess =
NULL;
164 __stack_chk_guard = 0xfeedbaba;
166 errorCode = oC_ErrorCode_None;
172 oC_IntMan_ExitCriticalSection();
184 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
186 oC_IntMan_EnterCriticalSection();
191 errorCode = oC_ErrorCode_None;
194 oC_IntMan_ExitCriticalSection();
208 oC_ARRAY_FOREACH_IN_ARRAY(LoggedEvents,log)
210 oC_IntMan_EnterCriticalSection();
215 bzero(
string,
sizeof(
string));
217 oC_KPrint_Printf(
string,
sizeof(
string),oC_IoFlags_WaitForAllElements | oC_IoFlags_WriteToStdError, oC_VT100_FG_RED oC_VT100_BG_BLACK);
219 kdebuglog(oC_LogType_Error,
"ExcHan - %s: 0x%X", log->EventName, log->ExceptionType);
221 if(log->ExceptionType & oC_ExcHan_ExceptionType_HardFault)
223 bzero(
string,
sizeof(
string));
225 oC_KPrint_Printf(
string,
sizeof(
string),oC_IoFlags_WaitForSomeElements | oC_IoFlags_WriteToStdError,
"\n--- HARD FAULT --- \n");
230 if(log->ExceptionType & oC_ExcHan_ExceptionType_KernelPanic)
232 bzero(
string,
sizeof(
string));
234 oC_KPrint_Printf(
string,
sizeof(
string),oC_IoFlags_WaitForSomeElements | oC_IoFlags_WriteToStdError,
"\n--- KERNEL PANIC --- \n");
238 if(log->ExceptionType & oC_ExcHan_ExceptionType_MemoryAccess)
240 bzero(
string,
sizeof(
string));
242 oC_KPrint_Printf(
string,
sizeof(
string),oC_IoFlags_WaitForSomeElements | oC_IoFlags_WriteToStdError,
"\n--- MEMORY ACCESS --- \n");
246 if(log->ExceptionType & oC_ExcHan_ExceptionType_MemoryAllocationError)
248 bzero(
string,
sizeof(
string));
250 oC_KPrint_Printf(
string,
sizeof(
string),oC_IoFlags_WaitForSomeElements | oC_IoFlags_WriteToStdError,
"\n--- MEMORY ALLOCATION ERROR --- \n");
254 if(log->ExceptionType & oC_ExcHan_ExceptionType_RequiredReboot)
256 bzero(
string,
sizeof(
string));
258 oC_KPrint_Printf(
string,
sizeof(
string),oC_IoFlags_WaitForSomeElements | oC_IoFlags_WriteToStdError,
"\n--- REQUIRE REBOOT --- \n");
262 if(log->ExceptionType & oC_ExcHan_ExceptionType_ProcessDamaged)
264 bzero(
string,
sizeof(
string));
266 oC_KPrint_Printf(
string,
sizeof(
string),oC_IoFlags_WaitForSomeElements | oC_IoFlags_WriteToStdError,
"\n--- PROCESS DAMAGED --- \n");
270 bzero(
string,
sizeof(
string));
272 oC_KPrint_Printf(
string,
sizeof(
string),oC_IoFlags_WaitForSomeElements | oC_IoFlags_WriteToStdError,
"System Event [%f s]: %s\n", log->Timestamp, log->EventName);
275 bzero(
string,
sizeof(
string));
277 oC_KPrint_Printf(
string,
sizeof(
string),oC_IoFlags_WaitForSomeElements | oC_IoFlags_WriteToStdError,
"Additional info: %s\n", log->AdditionalInfo);
282 bzero(
string,
sizeof(
string));
284 oC_KPrint_Printf(
string,
sizeof(
string),oC_IoFlags_WaitForSomeElements | oC_IoFlags_WriteToStdError,
" Harmful process: [%p] %s\n", log->Process, oC_Process_GetName(log->Process));
288 bzero(
string,
sizeof(
string));
290 oC_KPrint_Printf(
string,
sizeof(
string),oC_IoFlags_WaitForSomeElements | oC_IoFlags_WriteToStdError,
" Expected thread: [%p] %s\n", log->ExpectedThread, oC_Thread_GetName(log->ExpectedThread));
293 bzero(
string,
sizeof(
string));
295 oC_KPrint_Printf(
string,
sizeof(
string),oC_IoFlags_WaitForSomeElements | oC_IoFlags_WriteToStdError,
" Real thread: [%p] %s\n", log->RealThread, oC_Thread_GetName(log->RealThread));
298 if(log->AchievedRedZone)
300 bzero(
string,
sizeof(
string));
302 oC_KPrint_Printf(
string,
sizeof(
string),oC_IoFlags_WaitForSomeElements | oC_IoFlags_WriteToStdError,
" Thread has achieved the red zone - stack is exhausted (lack of %d bytes)\n",
303 0 - oC_Thread_GetFreeStackSize( log->RealThread ==
NULL ? log->ExpectedThread : log->RealThread,
true));
306 if(log->StackAddressCorrect ==
false)
308 bzero(
string,
sizeof(
string));
310 oC_KPrint_Printf(
string,
sizeof(
string),oC_IoFlags_WaitForSomeElements | oC_IoFlags_WriteToStdError,
" Stack pointer (%p) is out of the thread stack! - lack of %d bytes\n", log->StackAddress, log->FreeStackBytes);
314 bzero(
string,
sizeof(
string));
316 oC_KPrint_Printf(
string,
sizeof(
string),oC_IoFlags_WaitForSomeElements | oC_IoFlags_WriteToStdError,
" Stack pointer (%p) is correct. Free stack: %d bytes\n", log->StackAddress, log->FreeStackBytes);
319 bzero(
string,
sizeof(
string));
321 oC_KPrint_Printf(
string,
sizeof(
string),oC_IoFlags_WaitForSomeElements | oC_IoFlags_WriteToStdError,
" Interrupts %s\n", log->InterruptsEnabled ?
"enabled" :
"disabled");
323 if(log->ErrorInSystemContext)
325 bzero(
string,
sizeof(
string));
327 oC_KPrint_Printf(
string,
sizeof(
string),oC_IoFlags_WaitForSomeElements | oC_IoFlags_WriteToStdError,
" Error occurs in the system context\n");
330 if(log->ExceptionType & oC_ExcHan_ExceptionType_RequiredReboot)
332 oC_Boot_Restart(oC_Boot_Reason_SystemException,oC_UserMan_GetRootUser());
338 oC_IntMan_ExitCriticalSection();
349 if(isaddresscorrect(Name))
351 LogEvent(Name,AdditionalInfo,Stack,Thread,Type);
364 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
368 DaemonProcess = oC_Process_New( oC_Process_Priority_SystemSecurityDeamon,
"exc-han", oC_UserMan_GetRootUser(), 0,
NULL, oC_StreamMan_GetStdErrorStream(), oC_StreamMan_GetStdErrorStream());
370 if(ErrorCondition(DaemonProcess !=
NULL, oC_ErrorCode_AllocationError))
374 if(ErrorCondition(DaemonThread !=
NULL , oC_ErrorCode_AllocationError))
376 EventLogged = oC_Event_New(oC_Event_State_Inactive, &Allocator, AllocationFlags_Default);
379 ErrorCondition ( EventLogged !=
NULL, oC_ErrorCode_AllocationError)
380 && ErrorCode ( oC_ProcessMan_AddProcess(DaemonProcess) )
381 && ErrorCondition ( oC_Thread_Run(DaemonThread) , oC_ErrorCode_CannotRunThread )
384 errorCode = oC_ErrorCode_None;
388 oC_SaveIfFalse(
"ExcHan - cannot delete daemon thread" , oC_Thread_Delete(&DaemonThread) , oC_ErrorCode_ReleaseError);
389 oC_SaveIfFalse(
"ExcHan - cannot delete daemon process" , oC_Process_Delete(&DaemonProcess) , oC_ErrorCode_ReleaseError);
390 oC_SaveIfFalse(
"ExcHan - cannot delete event" , EventLogged ==
NULL || oC_Event_Delete(&EventLogged,AllocationFlags_Default) , oC_ErrorCode_ReleaseError);
395 oC_SaveIfFalse(
"ExcHan - cannot delete daemon process", oC_Process_Delete(&DaemonProcess) , oC_ErrorCode_ReleaseError);
404 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 411 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ 422 char string[60] = {0};
424 sprintf(
string,
"Error during accessing %p address\n", MemoryAddress);
426 LogEvent(
"Hard Fault",
string,
NULL, Context, oC_ExcHan_ExceptionType_HardFault);
432 oC_Boot_Restart(oC_Boot_Reason_SystemException, oC_UserMan_GetRootUser());
441 static void MemoryEventHandler(
void * Address , MemoryEventFlags_t Event ,
const char * Function, uint32_t LineNumber )
443 if(Event & MemoryEventFlags_ModuleTurningOff)
445 LogEvent(
"Memory Manager has been disabled",
NULL,
NULL,
NULL, oC_ExcHan_ExceptionType_MemoryAccess);
447 if(Event & MemoryEventFlags_DataSectionOverflow)
449 LogEvent(
"Overflow in the DATA ram section",
NULL,
NULL,
NULL, oC_ExcHan_ExceptionType_KernelPanic);
451 if(Event & MemoryEventFlags_BufferOverflow)
455 char string[100] = {0};
457 if(allocator !=
NULL)
459 sprintf(
string,
"Buffer overflow at address %p of allocator %s (allocated in %s at line %d)\n", Address, allocator->
Name, Function, LineNumber);
463 sprintf(
string,
"Buffer overflow at address %p of unknown allocator\n", Address);
466 LogEvent(
"Overflow in the DATA ram section",
string,
NULL,
NULL, oC_ExcHan_ExceptionType_ProcessDamaged);
468 if(Event & MemoryEventFlags_PanicMemoryExhausted)
470 static oC_Timestamp_t nextLogTimestamp = 0;
472 if(nextLogTimestamp <= oC_KTime_GetTimestamp())
474 nextLogTimestamp = oC_KTime_GetTimestamp() + oC_DynamicConfig_GetValue(ExcHan,MemoryEventsLoggingPeriod);
475 LogEvent(
"Panic memory limit exhausted",
"Check your configuration in the command 'system'",
NULL,
NULL, oC_ExcHan_ExceptionType_KernelPanic);
478 if(Event & MemoryEventFlags_MemoryExhausted)
480 static oC_Timestamp_t nextLogTimestamp = 0;
482 if(nextLogTimestamp <= oC_KTime_GetTimestamp())
484 nextLogTimestamp = oC_KTime_GetTimestamp() + oC_DynamicConfig_GetValue(ExcHan,MemoryEventsLoggingPeriod);
485 LogEvent(
"First memory limit exhausted",
"Check your configuration in the command 'system'",
NULL,
NULL, oC_ExcHan_ExceptionType_MemoryAccess);
488 if(Event & MemoryEventFlags_MemoryFault)
492 if(Event & MemoryEventFlags_BusFault)
500 oC_Boot_Restart(oC_Boot_Reason_SystemException, oC_UserMan_GetRootUser());
511 bool interruptsTurnedOn = oC_IntMan_AreInterruptsTurnedOn();
512 oC_IntMan_EnterCriticalSection();
514 oC_Event_SetState(EventLogged, oC_Event_State_Active);
516 oC_ARRAY_FOREACH_IN_ARRAY(LoggedEvents,log)
518 if(log->Used ==
false)
526 Thread = oC_ThreadMan_GetCurrentThread();
531 log->StackAddress = stackPointer;
532 log->EventName = Name;
533 log->ExpectedThread = oC_ThreadMan_GetCurrentThread();
534 log->RealThread = oC_ThreadMan_GetThreadOfContext( Context );
535 log->Timestamp = oC_KTime_GetTimestamp();
536 log->InterruptsEnabled = interruptsTurnedOn;
537 log->ExceptionType = Type;
542 strncpy(log->AdditionalInfo,AdditionalInfo,
sizeof(log->AdditionalInfo));
543 log->AdditionalInfo[
sizeof(log->AdditionalInfo) - 1] = 0;
547 memset(log->AdditionalInfo,0,
sizeof(log->AdditionalInfo));
550 if(log->ErrorInSystemContext ==
false)
552 log->Process = oC_ProcessMan_GetCurrentProcess();
559 if(oC_Thread_IsCorrect(log->RealThread))
562 log->FreeStackBytes = oC_Thread_GetFreeStackSize(log->RealThread,
true);
567 log->FreeStackBytes = oC_Thread_GetFreeStackSize(log->ExpectedThread,
true);
573 oC_IntMan_ExitCriticalSection();
589 oC_Event_SetState(EventLogged, oC_Event_State_Inactive);
599 __attribute__((noreturn))
void __stack_chk_fail(
void)
601 oC_Thread_t thread = oC_ThreadMan_GetCurrentThread();
602 char info[100] = {0};
604 sprintf(info,
"Behavior of the thread `%s` [%p] was unexpected - closing", oC_Thread_GetName(thread), thread);
606 LogEvent(
"Segmentation fault", info, thread,
NULL, oC_ExcHan_ExceptionType_ProcessDamaged );
608 if(__stack_chk_guard != 0)
610 oC_Thread_Cancel(&thread);
616 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ #define min(time)
Number of min.
Hard fault, divide by 0 detected.
oC_SYS_LLD_Context_t * oC_SYS_LLD_GetSystemContext(void)
returns pointer to the system context
oC_SYS_LLD_EventFlags_t
event flags handled by the module.
oC_ErrorCode_t oC_ExcHan_TurnOff(void)
turns off exception handler
bool oC_Event_WaitForState(oC_Event_t Event, oC_Event_State_t State, oC_Event_StateMask_t StateMask, oC_Time_t Timeout)
void oC_ExcHan_PrintLoggedEvents(void)
prints logged events in the STD out
static void LogEvent(const char *Name, char *AdditionalInfo, oC_Thread_t Thread, oC_SYS_LLD_Context_t *Context, oC_ExcHan_ExceptionType_t Type)
logs event
oC_ErrorCode_t oC_ExcHan_RunDaemon(void)
starts daemon of the Exception Handler
identifier for allocations
oC_SYS_LLD_Context_t * oC_SYS_LLD_GetCurrentContext(void)
returns pointer to the current context
static void DaemonThread(void *Argument)
main thread for handling network interfaces
The file with interface for process manager.
File with interface for user system manager.
oC_ErrorCode_t oC_SYS_LLD_TurnOnDriver(void)
initializes the driver to work
static void DaemonThreadHandler(void *Context)
thread for printing logs
void oC_SYS_LLD_Context_t
type for storing context of the machine
oC_ErrorCode_t oC_MemMan_TurnOn(void)
turns on memory manager
Loading or storing data to memory occurs with not aligned address.
The file with interface for the module library.
static void SystemEventHandler(oC_SYS_LLD_EventFlags_t EventFlags, oC_SYS_LLD_Context_t *Context, void *MemoryAddress)
handler for system events
The file with interface for stream manager.
Allocator_t oC_MemMan_GetAllocatorOfAddress(const void *Address)
returns allocator of address
void oC_Boot_Restart(oC_Boot_Reason_t Reason, oC_User_t User)
restarts the system
The file with LLD interface for the CLOCK driver.
The file with interface for kernel print operations.
void oC_ExcHan_LogEvent(const char *Name, char *AdditionalInfo, void *Stack, oC_Thread_t Thread, oC_ExcHan_ExceptionType_t Type)
logs system event
The file with interface for interrupt manager.
Handles configuration of the Dynamic.
static void MemoryEventHandler(void *Address, MemoryEventFlags_t Event, const char *Function, uint32_t LineNumber)
handler for memory manager events
oC_ErrorCode_t oC_ExcHan_TurnOn(void)
turns on exception handler
static void oC_Module_TurnOn(oC_Module_t Module)
sets module as turned on
oC_ErrorCode_t oC_SYS_LLD_SetEventInterruptHandler(oC_SYS_LLD_EventInterrupt_t EventHandler, oC_SYS_LLD_EventFlags_t EventsFlags)
sets event interrupt handler
The file with memory manager interface.
The file with interface for process mechanism.
File with interface of Exception Handler.
static bool oC_Module_TurnOffVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned off
static bool oC_Module_TurnOnVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned on
The file with LLD interface for the SYS driver.
bool oC_Thread_IsOwnedByStack(oC_Thread_t Thread, const void *Address, bool *outAddressInRedZone)
checks if the given address is owned by the thread stack
#define kB(kBytes)
Number of kB.
oC_ErrorCode_t oC_MemMan_SetEventHandler(MemoryEventHandler_t EventHandler)
sets event handler function
The process is not correct, or the process stack was overflowed.
void * oC_SYS_LLD_GetLastProcessStackPointer(void)
returns stack pointer of last executed process
The file with interface for Thread Manager.
Undefined instruction detected.
bool oC_CLOCK_LLD_DelayForMicroseconds(oC_UInt_t Microseconds)
perform a delay for us
The file with interface of kernel time module.
Hard fault, reason is unknown.
#define NULL
pointer to a zero
static void oC_Module_TurnOff(oC_Module_t Module)
sets module as turned off