File: debug.c

package info (click to toggle)
entity 0.7.2-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,352 kB
  • ctags: 5,272
  • sloc: ansic: 61,707; sh: 7,921; makefile: 732; perl: 399
file content (172 lines) | stat: -rw-r--r-- 4,376 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Debugging utilities.
 * Copyright (c) 1998 New Generation Software (NGS) Oy
 *
 * Author: Markku Rossi <mtr@ngs.fi>
 */

/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA
 */

/*
 * $Source: /home/cvs/entity/libentitynjs/debug.c,v $
 * $Id: debug.c,v 1.4 2000/07/25 05:25:59 imain Exp $
 */

#include "njs/internal.h"

/*
 * Global functions.
 */

#define STRING_MAX_PRINT_LEN	10

void
js_vm_stacktrace (JSVirtualMachine *vm, unsigned int num_frames)
{
  unsigned int frame = 0;
  JSNode *sp = vm->sp;
  void *pc = vm->pc;
  JSNode *fp;
  char buf[512];

  js_snprintf (buf, sizeof (buf), "VM: stacktrace: stacksize=%d, used=%d%s",
	   vm->stack_size,
	   (vm->stack + vm->stack_size - sp),
	   JS_HOST_LINE_BREAK);
  js_iostream_write (vm->s_stderr, buf, strlen (buf));

  /* STACKFRAME */

  /* Find frame pointer. */
  for (fp = sp + 1; fp->type != JS_IPTR; fp++)
    ;

  /* The first iptr is the return address. */
  fp++;

  /* The second iptr is the with pointer. */
  fp++;

  /* The third item is a JS_ARGS_FIX node. */
  assert (fp->type == JS_ARGS_FIX);
  fp++;

  while (fp && frame < num_frames)
    {
      JSNode *n;
      const char *func_name = js_vm_func_name (vm, pc);

      js_snprintf (buf, sizeof (buf), "#%-3u %s%s:", frame++, func_name,
	       func_name[0] == '.' ? "" : "()");
      js_iostream_write (vm->s_stderr, buf, strlen (buf));

      if (vm->verbose_stacktrace)
	{
	  js_snprintf (buf, sizeof (buf),
		   " ra=0x%lx, wp=0x%lx, af=%d:%d, ofp=0x%lx",
		   (unsigned long) (fp - 3)->u.iptr,
		   (unsigned long) JS_WITHPTR->u.iptr,
		   JS_ARGS_FIXP->u.args_fix.argc,
		   JS_ARGS_FIXP->u.args_fix.delta,
		   (unsigned long) fp->u.iptr);
	  js_iostream_write (vm->s_stderr, buf, strlen (buf));
	}

      for (n = sp + 1; n != fp - 3; n++)
	{
	  switch (n->type)
	    {
	    case JS_UNDEFINED:
	      js_snprintf (buf, sizeof (buf), " undefined");
	      break;

	    case JS_NULL:
	      js_snprintf (buf, sizeof (buf), " null");
	      break;

	    case JS_BOOLEAN:
	      js_snprintf (buf, sizeof (buf), " %s", n->u.vboolean ? "true" : "false");
	      break;

	    case JS_INTEGER:
	      js_snprintf (buf, sizeof (buf), " %ld", n->u.vinteger);
	      break;

	    case JS_STRING:
	      if (n->u.vstring->len > STRING_MAX_PRINT_LEN)
		js_snprintf (buf, sizeof (buf), " \"%.*s...\"",
			 STRING_MAX_PRINT_LEN,
			 n->u.vstring->data);
	      else
		js_snprintf (buf, sizeof (buf), " \"%.*s\"",
			 (int) n->u.vstring->len,
			 n->u.vstring->data);
	      break;

	    case JS_FLOAT:
	      js_snprintf (buf, sizeof (buf), " %g", n->u.vfloat);
	      break;

	    case JS_ARRAY:
	      js_snprintf (buf, sizeof (buf), " array");
	      break;

	    case JS_OBJECT:
	      js_snprintf (buf, sizeof (buf), " object");
	      break;

	    case JS_SYMBOL:
	      js_snprintf (buf, sizeof (buf), " %s", js_vm_symname (vm, n->u.vsymbol));
	      break;

	    case JS_BUILTIN:
	      js_snprintf (buf, sizeof (buf), " builtin");
	      break;

	    case JS_FUNC:
	      js_snprintf (buf, sizeof (buf), " function");
	      break;

	    case JS_IPTR:
	      js_snprintf (buf, sizeof (buf), " 0x%lx", (unsigned long) n->u.iptr);
	      break;

	    case JS_ARGS_FIX:
	      js_snprintf (buf, sizeof (buf), " <num=%d, delta=%d>", n->u.args_fix.argc,
		       n->u.args_fix.delta);
	      break;

	    default:
	      js_snprintf (buf, sizeof (buf), " type=%d???", n->type);
	      break;
	    }

	  js_iostream_write (vm->s_stderr, buf, strlen (buf));
	}

      js_iostream_write (vm->s_stderr, JS_HOST_LINE_BREAK,
			 JS_HOST_LINE_BREAK_LEN);

      /* Move to the caller. */
      sp = fp;
      pc = fp[-3].u.iptr;
      fp = fp->u.iptr;
    }
}