//------------------------------------------------------------------- // seestack.cpp // // This program displays a hexadecimal and ascii dump of the // application's stack as it existed upon entry to 'main()', // and it illustrates the use of "inline" assembly language. // // (A student's question prompted the writing of this demo.) // // Suggestion: try executing this program with some command- // line arguments, and see where they show up in the output: // // Example: $ ./seestack xx yyyy zzzz // // programmer: ALLAN CRUSE // written on: 07 OCT 2003 // revised on: 15 MAR 2003 -- to show ESP, EBP, and envp values //------------------------------------------------------------------- #include // for printf() #define KERNEL_BASE_ADDRESS 0xC0000000 unsigned long tos, ebp, len; unsigned char *where = (unsigned char *)KERNEL_BASE_ADDRESS; unsigned char ch; int i, j; int main( int argc, char **argv, char **envp ) { // insert statement that copies %esp-value to a variable asm(" movl %esp, tos "); asm(" movl %ebp, ebp "); // calculate the size of the active stack area (in bytes) len = KERNEL_BASE_ADDRESS - tos; // loop to display the contents of the active stack area for (i = 0; i < len; i += 16) { where -= 16; printf( "\n%08X: ", where ); for (j = 0; j < 16; j++) printf( "%02X ", where[j] ); for (j = 0; j < 16; j++) { ch = where[ j ]; if (( ch < 0x20 )||( ch > 0x7E )) ch = '.'; printf( "%c", ch ); } } printf( "\n" ); printf( "\n ebp=%08X ", ebp ); printf( "\n esp=%08X ", tos ); printf( "\n&argc=%08X argc=%08X ", &argc, argc ); printf( "\n&argv=%08X argv=%08X ", &argv, argv ); printf( "\n&envp=%08X envp=%08X ", &envp, envp ); printf( "\n\n" ); }