80 likes | 261 Views
‘struct sigcontext’. On using Linux’s signaling mechanism for debugqing application programs. Installing a signal-handler. #include <signal.h> void handler( int signo, siginfo_t *si, void *sc ); int main( void ) { struct sigaction oa, sa = {0}; sa.sa_flags = SA_SIGINFO;
E N D
‘struct sigcontext’ On using Linux’s signaling mechanism for debugqing application programs
Installing a signal-handler #include <signal.h> void handler( int signo, siginfo_t *si, void *sc ); int main( void ) { struct sigaction oa, sa = {0}; sa.sa_flags = SA_SIGINFO; sa.sa_sigaction = handler; sigaction( SIGSEGV, &sa, &oa ); }
Layout of kernel-stack (by cpu) Upon entry to kernel’s exception-handler: SS saved pointer to user-stack ESP EFLAGS CS saved pointer to user-opcode EIP error code SS:ESP ‘error code’ = segment-selector for the illegal memory-segment access
Layout of kernel-stack (by Linux) ss esp eflags cs eip error gs fs es ds eax ebp edi esi edx ecx SS:ESP ebx
Layout of user-stack Upon entering ‘main()’ Upon entering ‘handler()’ environment strings environment strings command-line arguments command-line arguments envp envp argv argv argc argc tos return-address return-address sigstub sigcontext siginfo sc si signum tos return-address
‘segvtrap.cpp’ • We have constructed this demo-program to show how you could utilize the context information that Linux can provide to your signal-handler (if you use SA_SIGINFO) • You can use our ‘run.cpp’ tool to examine an application-program’s exit-status
In-class exercises • Try commenting out the ‘exit(1)’ statement • Then add statement: sc ->eip += 1; • Try replacing ‘asm(“ hlt “)’ statement with this privileged instruction: inb( 0x1F7 ); • Try replacing ‘asm(“ hlt “)’ statement with this illegal assignment: *(char*)0 = 0; • Try replacing ‘asm(“ hlt” )’ statement with an instruction that reads a kernel-address