1998Q3/
<!-- MHonArc v2.4.4 -->
<!--X-Subject: [MUD&#45;Dev] Re: Technical C/C++ coding question -->
<!--X-From-R13: Tvaa Oear Unatfgnq <svaantNthneqvna.ab> -->
<!--X-Date: Sat, 25 Jul 1998 00:29:29 &#45;0700 -->
<!--X-Message-Id: Pine.LNX.3.95.980725091129.20744A&#45;100000#lucifer,guardian.no -->
<!--X-Content-Type: text/plain -->
<!--X-Reference: Pine.LNX.3.96.980613223516.31300A&#45;100000#shamen,cyberhighway.net -->
<!--X-Head-End-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>MUD-Dev message, [MUD-Dev] Re: Technical C/C++ coding question</title>
<!-- meta name="robots" content="noindex,nofollow" -->
<link rev="made" href="mailto:finnag#guardian,no">
</head>
<body background="/backgrounds/paperback.gif" bgcolor="#ffffff"
      text="#000000" link="#0000FF" alink="#FF0000" vlink="#006000">

  <font size="+4" color="#804040">
    <strong><em>MUD-Dev<br>mailing list archive</em></strong>
  </font>
      
<br>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
<br clear=all><hr>
<!--X-Body-Begin-->
<!--X-User-Header-->
<!--X-User-Header-End-->
<!--X-TopPNI-->

Date:&nbsp;
[&nbsp;<a href="msg00351.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00353.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Thread:&nbsp;
[&nbsp;<a href="msg00759.html">Previous</a>
&nbsp;|&nbsp;<a href="msg00348.html">Next</a>
&nbsp;]
&nbsp;&nbsp;&nbsp;&nbsp;
Index:&nbsp;
[&nbsp;<A HREF="author.html#00352">Author</A>
&nbsp;|&nbsp;<A HREF="#00352">Date</A>
&nbsp;|&nbsp;<A HREF="thread.html#00352">Thread</A>
&nbsp;]

<!--X-TopPNI-End-->
<!--X-MsgBody-->
<!--X-Subject-Header-Begin-->
<H1>[MUD-Dev] Re: Technical C/C++ coding question</H1>
<HR>
<!--X-Subject-Header-End-->
<!--X-Head-of-Message-->
<UL>
<LI><em>To</em>: mud-dev &lt;<A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A>&gt;</LI>
<LI><em>Subject</em>: [MUD-Dev] Re: Technical C/C++ coding question</LI>
<LI><em>From</em>: Finn Arne Gangstad &lt;<A HREF="mailto:finnag#guardian,no">finnag#guardian,no</A>&gt;</LI>
<LI><em>Date</em>: Sat, 25 Jul 1998 09:27:07 +0200 (MET DST)</LI>
<LI><em>cc</em>: Charlie Ponder &lt;<A HREF="mailto:cponder#tripod,net">cponder#tripod,net</A>&gt;</LI>
<LI><em>Reply-To</em>: <A HREF="mailto:mud-dev#kanga,nu">mud-dev#kanga,nu</A></LI>
</UL>
<!--X-Head-of-Message-End-->
<!--X-Head-Body-Sep-Begin-->
<HR>
<!--X-Head-Body-Sep-End-->
<!--X-Body-of-Message-->
<PRE>



On Sat, 13 Jun 1998, Ben Greear wrote:
&gt; 
&gt; Java has this nice feature called printStackTrace(), that allows
&gt; you to get a stack trace (and log it or whatever) during runtime.

Sorry to answer so late, but here are some functions i made for the
mud I'm working on to do what you want.

stack_trace requires gcc, and will only print a stack-trace containing the
addresses (use gdb or similar to look them up).

const char *
stack_trace()
{
#ifdef __GNUC__
  static char trace_buf[400];
  char *t = trace_buf;
  int m = 1;
  char *p = 0;
  /* The general idea here is that you're not interested in call-trace
   * that goes up above main() */
  const unsigned int delta = size_of_main;
  /* Don't look */
  *t++ = '(';
  m = m &amp;&amp; (p = __builtin_return_address(1)) &amp;&amp; (t += sprintf(t, "%p", p)) &amp;&amp;
    (p - (char *)main) &gt; delta;
  m = m &amp;&amp; (p = __builtin_return_address(2)) &amp;&amp; (t += sprintf(t, " %p", p)) &amp;&amp;
    (p - (char *)main) &gt; delta;
  m = m &amp;&amp; (p = __builtin_return_address(3)) &amp;&amp; (t += sprintf(t, " %p", p)) &amp;&amp;
    (p - (char *)main) &gt; delta;
 /* .... etc as long as you bother (i did it up to 29, if you extend it
  * increase the size of trace_buf */
  *t++ = ')';
  *t++ = 0;
  return trace_buf;
#else
  return "(no stack-trace using this compiler)";
#endif
}

To initialise the global variable "size_of_main", do this in main():

int main(int argc, char **argv)
{
  /* ... */
  size_of_main = (char *)&amp;&amp;main_end - (char *)main;

  /* ... */

main_end:
  exit(0);
}
  

&gt; Is there a way to (easily) do that in c++ as well?  Can I make it
&gt; dump a core file and yet still keep runing?


And here is a function to fork and dump core:

void
fork_and_dump_core(const char *why)
{
    pid_t pid;
    fprintf(stderr, "DEBUG: fork_and_dump_core: %s", why);
    fflush(NULL); /* flush all streams so fork doesn't mess things up */
    pid = fork();
    if (pid == -1) {
        perror("fork_and_dump_core: fork failed");
    } else if (pid == 0) {
        volatile int n = 0; 
        n = 1 / n; /* die horribly */ 
        _exit(0);
    } else {
       waitpid(pid, 0, 0); /* wait for the core dump.. usefulness questioned */
    }
}


The reason for using "1 / n" (where n is 0) to die instead of abort(), is
that abort() didn't include the last function in the call-trace on the OS
i tested it on.

- Finn Arne



</PRE>

<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<HR>
<!--X-Follow-Ups-End-->
<!--X-References-->
<!--X-References-End-->
<!--X-BotPNI-->
<UL>
<LI>Prev by Date:
<STRONG><A HREF="msg00351.html">[MUD-Dev] Re: WIRED: Kilers have more fun</A></STRONG>
</LI>
<LI>Next by Date:
<STRONG><A HREF="msg00353.html">[MUD-Dev] Re: Fun vs Realism [ Was: OT: Sid Meier ]</A></STRONG>
</LI>
<LI>Prev by thread:
<STRONG><A HREF="msg00759.html">[MUD-Dev] Re: Fun vs Realism [ Was: OT: Sid Meier ]</A></STRONG>
</LI>
<LI>Next by thread:
<STRONG><A HREF="msg00348.html">[MUD-Dev] Fun vs Realism [ Was: OT: Sid Meier ]</A></STRONG>
</LI>
<LI>Index(es):
<UL>
<LI><A HREF="index.html#00352"><STRONG>Date</STRONG></A></LI>
<LI><A HREF="thread.html#00352"><STRONG>Thread</STRONG></A></LI>
</UL>
</LI>
</UL>

<!--X-BotPNI-End-->
<!--X-User-Footer-->
<!--X-User-Footer-End-->
<ul><li>Thread context:
<BLOCKQUOTE><UL>
<LI><STRONG>[MUD-Dev] Re: Fun vs Realism [ Was: OT: Sid Meier ]</STRONG>, <EM>(continued)</EM>
<ul compact>
<LI><strong><A NAME="00410" HREF="msg00410.html">[MUD-Dev] Re: Fun vs Realism [ Was: OT: Sid Meier ]</A></strong>, 
Damion Schubert <a href="mailto:zjiria#texas,net">zjiria#texas,net</a>, Wed 29 Jul 1998, 03:06 GMT
</LI>
<LI><strong><A NAME="00431" HREF="msg00431.html">[MUD-Dev] Re: Fun vs Realism [ Was: OT: Sid Meier ]</A></strong>, 
Koster, Raph <a href="mailto:rkoster#origin,ea.com">rkoster#origin,ea.com</a>, Thu 30 Jul 1998, 14:37 GMT
<UL>
<LI><strong><A NAME="00450" HREF="msg00450.html">[MUD-Dev] Re: Fun vs Realism [ Was: OT: Sid Meier ]</A></strong>, 
Brandon J. Rickman <a href="mailto:ashes#pc4,zennet.com">ashes#pc4,zennet.com</a>, Sat 01 Aug 1998, 23:27 GMT
</LI>
</UL>
</LI>
<LI><strong><A NAME="00759" HREF="msg00759.html">[MUD-Dev] Re: Fun vs Realism [ Was: OT: Sid Meier ]</A></strong>, 
Damion Schubert <a href="mailto:zjiria#texas,net">zjiria#texas,net</a>, Tue 18 Aug 1998, 04:48 GMT
</LI>
</ul>
</LI>
<LI><strong><A NAME="00352" HREF="msg00352.html">[MUD-Dev] Re: Technical C/C++ coding question</A></strong>, 
Finn Arne Gangstad <a href="mailto:finnag#guardian,no">finnag#guardian,no</a>, Sat 25 Jul 1998, 07:29 GMT
<LI><strong><A NAME="00348" HREF="msg00348.html">[MUD-Dev] Fun vs Realism [ Was: OT: Sid Meier ]</A></strong>, 
Leach, Brad BA <a href="mailto:Leach.Brad.BA#bhp,com.au">Leach.Brad.BA#bhp,com.au</a>, Sat 25 Jul 1998, 04:50 GMT
<UL>
<LI><strong><A NAME="00350" HREF="msg00350.html">[MUD-Dev] Re: Fun vs Realism [ Was: OT: Sid Meier ]</A></strong>, 
Caliban Tiresias Darklock <a href="mailto:caliban#darklock,com">caliban#darklock,com</a>, Sat 25 Jul 1998, 05:47 GMT
<UL>
<LI><strong><A NAME="00353" HREF="msg00353.html">[MUD-Dev] Re: Fun vs Realism [ Was: OT: Sid Meier ]</A></strong>, 
Nathan F Yospe <a href="mailto:yospe#hawaii,edu">yospe#hawaii,edu</a>, Sat 25 Jul 1998, 09:09 GMT
<UL>
<LI><strong><A NAME="00355" HREF="msg00355.html">[MUD-Dev] Re: Fun vs Realism [ Was: OT: Sid Meier ]</A></strong>, 
Caliban Tiresias Darklock <a href="mailto:caliban#darklock,com">caliban#darklock,com</a>, Sat 25 Jul 1998, 09:54 GMT
</LI>
</UL>
</LI>
</UL>
</LI>
</UL>
</LI>
</UL></BLOCKQUOTE>

</ul>
<hr>
<center>
[&nbsp;<a href="../">Other Periods</a>
&nbsp;|&nbsp;<a href="../../">Other mailing lists</a>
&nbsp;|&nbsp;<a href="/search.php3">Search</a>
&nbsp;]
</center>
<hr>
</body>
</html>