PDA

View Full Version : displaying stack contents



threahdead
05-22-2003, 01:36 PM
hi

i thought it would be funny to see what the stack looks like in general so i prorgammed something that shows it in a very rough way.
that code took me less than 5 minutes, so dont blame me for it. ;)
maybe you know how to make it better?



/* ever wondered what your stack may look like? */

#include <stdio.h>

/* uncomment lines to log to a file called \'memlog\' */

int main (void)
{
//FILE *fd;
char buffer[] = "this is a test!\n";
int i;

/*
if(((fd = fopen("memlog", "a"))) == 0){
exit(1);
}
*/

for(i=0;i<20000;i++){
printf("%p\t%c\n", &buffer[i], buffer[i]);
//fprintf(fd, "%p\t%c\n", &buffer[i], buffer[i]);
}

//fclose(fd);

return 0;
}


thanks

XSquared
05-22-2003, 01:44 PM
Add a error handler in case of a memory access error:



for(i=0;i<20000;i++){
try {
printf("%p\t%c\n", &buffer[i], buffer[i]);
//fprintf(fd, "%p\t%c\n", &buffer[i], buffer[i]);
}

catch( ... ) {
printf( "%p\tUnaccessible\n", &buffer[ i ] );
//fprintf( fd, "%p\tUnaccessible\n", &buffer[ i ] );
}

}

XSquared
05-22-2003, 02:16 PM
Sorry, didn't realize this was the Linux forum.

threahdead
05-23-2003, 11:15 AM
Originally posted by vVv
Oh boy...
This is the most useless snippet I've seen for a long time. It buys nothing and makes very poor assumptions about memory mapping (this would dump core with SIGBUS/SIGSEGV on many systems). My advice: Throw this trash away and use gdb if you want to see useful stack traces.

oh boy, ok. ;)