delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/30/23:20:40

Xref: news2.mv.net comp.os.msdos.djgpp:4403
From: Roland Exler <R DOT Exler AT jk DOT uni-linz DOT ac DOT at>
Newsgroups: comp.os.msdos.djgpp
Subject: user-requested stack-trace at exit
Date: Thu, 30 May 1996 10:32:35 -0700
Organization: Institute for el. Measurement, University of Linz, Austria
Lines: 66
Message-ID: <31ADDBB3.45D7@jk.uni-linz.ac.at>
NNTP-Posting-Host: sensor4.emt.uni-linz.ac.at
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Taking a recently posted message from A.Appleyard I've hacked a 
stack-trace for V2. With this functions it's possible to determine how 
deep the function-nesting is at a particular place in the program and 
print a stack-trace suitable for use with symify.

This way you can trace down how you've reached some point in your program 
without generating a floating-point error or a NULL-pointer dereference.

I think this part of code would help debugging many programs. I'll try to 
redefine assert() so it will print a stack-trace too.

Roland

------------------------------ trace.cc ------------------------------
// try using the following commands:
//   gcc -Wall -O2 -g trace.cc -o trace
//   trace
//   symify trace
#include <stdio.h>

// return depth of function-calls:
//   depth()==2 is in main(), depth()==3 in fist subroutine, ...
int depth()
{
  long *bp=(long*)&bp+1;
  int j=0;
  while ((bp=(long*)*bp)) j++;
  return j;
}

// abort program with stacktrace suitable for symify
// if -g was used for compiling
void abort_with_stacktrace(FILE *out=stderr)
{
  long *bp=(long*)&bp+1;
  fprintf(out,"user requested abort of program using 
'abort_with_stacktrace()'!\n");
  while (*bp)
  {
    fprintf(out,"  0x%08lx\n",((long*)bp)[1]);
    bp=(long*)*bp;
  }
  abort();
}

// test-program to verify functions for stack-tracing
void sub()
{
  printf("depth in sub()=%d\n",depth());
  abort_with_stacktrace();
}

int main()
{
  printf("depth in main()=%d\n",depth());
  sub();
  return 0;
}

+---------------------------------------+---------------------------+
I Roland Exler                          I EMAIL:                    I
I Universitaet Linz                     I R DOT Exler AT jk DOT uni-linz DOT ac DOT at I
I Institut fuer Elektrische Messtechnik I                           I
I Altenbergerstr. 69                    I Phone:                    I
I A-4040 Linz, AUSTRIA                  I + 43 732 2468 9205        I
+---------------------------------------+---------------------------+

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019