2011-11-28 13:32:39 +00:00
|
|
|
/* $OpenBSD: trace.c,v 1.16 2010/09/07 19:58:09 marco Exp $ */
|
2002-02-16 21:27:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001 Marc Espie.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
|
|
|
|
* PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2002-04-19 17:26:21 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2011-11-28 13:32:39 +00:00
|
|
|
#include <err.h>
|
2002-02-16 21:27:48 +00:00
|
|
|
#include <stddef.h>
|
2011-11-28 13:32:39 +00:00
|
|
|
#include <stdint.h>
|
2002-02-16 21:27:48 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "mdef.h"
|
|
|
|
#include "stdd.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
2002-04-19 17:26:21 +00:00
|
|
|
FILE *traceout;
|
2002-02-16 21:27:48 +00:00
|
|
|
|
2011-11-28 13:32:39 +00:00
|
|
|
#define TRACE_ARGS 1
|
2002-02-16 21:27:48 +00:00
|
|
|
#define TRACE_EXPANSION 2
|
|
|
|
#define TRACE_QUOTE 4
|
|
|
|
#define TRACE_FILENAME 8
|
|
|
|
#define TRACE_LINENO 16
|
|
|
|
#define TRACE_CONT 32
|
|
|
|
#define TRACE_ID 64
|
|
|
|
#define TRACE_NEWFILE 128 /* not implemented yet */
|
|
|
|
#define TRACE_INPUT 256 /* not implemented yet */
|
|
|
|
|
|
|
|
static unsigned int letter_to_flag(int);
|
|
|
|
static void print_header(struct input_file *);
|
|
|
|
static int frame_level(void);
|
|
|
|
|
|
|
|
|
2011-11-28 13:32:39 +00:00
|
|
|
unsigned int trace_flags = TRACE_QUOTE | TRACE_EXPANSION;
|
2002-02-16 21:27:48 +00:00
|
|
|
|
|
|
|
void
|
2002-05-01 21:37:29 +00:00
|
|
|
trace_file(const char *name)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
|
2011-11-28 13:32:39 +00:00
|
|
|
if (traceout && traceout != stderr)
|
2002-02-16 21:27:48 +00:00
|
|
|
fclose(traceout);
|
|
|
|
traceout = fopen(name, "w");
|
|
|
|
if (!traceout)
|
|
|
|
err(1, "can't open %s", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
2002-05-01 21:37:29 +00:00
|
|
|
letter_to_flag(int c)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
switch(c) {
|
|
|
|
case 'a':
|
|
|
|
return TRACE_ARGS;
|
|
|
|
case 'e':
|
|
|
|
return TRACE_EXPANSION;
|
|
|
|
case 'q':
|
|
|
|
return TRACE_QUOTE;
|
|
|
|
case 'c':
|
|
|
|
return TRACE_CONT;
|
|
|
|
case 'x':
|
|
|
|
return TRACE_ID;
|
|
|
|
case 'f':
|
|
|
|
return TRACE_FILENAME;
|
|
|
|
case 'l':
|
|
|
|
return TRACE_LINENO;
|
|
|
|
case 'p':
|
|
|
|
return TRACE_NEWFILE;
|
|
|
|
case 'i':
|
|
|
|
return TRACE_INPUT;
|
|
|
|
case 't':
|
|
|
|
return TRACE_ALL;
|
|
|
|
case 'V':
|
|
|
|
return ~0;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-05-01 21:37:29 +00:00
|
|
|
set_trace_flags(const char *s)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
char mode = 0;
|
|
|
|
unsigned int f = 0;
|
|
|
|
|
|
|
|
if (*s == '+' || *s == '-')
|
|
|
|
mode = *s++;
|
|
|
|
while (*s)
|
|
|
|
f |= letter_to_flag(*s++);
|
|
|
|
switch(mode) {
|
|
|
|
case 0:
|
2011-11-28 13:32:39 +00:00
|
|
|
trace_flags = f;
|
2002-02-16 21:27:48 +00:00
|
|
|
break;
|
|
|
|
case '+':
|
2011-11-28 13:32:39 +00:00
|
|
|
trace_flags |= f;
|
2002-02-16 21:27:48 +00:00
|
|
|
break;
|
|
|
|
case '-':
|
2011-11-28 13:32:39 +00:00
|
|
|
trace_flags &= ~f;
|
2002-02-16 21:27:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-07-14 02:03:23 +00:00
|
|
|
frame_level(void)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
int level;
|
|
|
|
int framep;
|
|
|
|
|
2002-07-15 02:15:12 +00:00
|
|
|
for (framep = fp, level = 0; framep != 0;
|
2011-11-28 13:32:39 +00:00
|
|
|
level++,framep = mstack[framep-3].sfra)
|
2002-02-16 21:27:48 +00:00
|
|
|
;
|
|
|
|
return level;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-05-01 21:37:29 +00:00
|
|
|
print_header(struct input_file *inp)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
fprintf(traceout, "m4trace:");
|
2011-11-28 13:32:39 +00:00
|
|
|
if (trace_flags & TRACE_FILENAME)
|
2002-02-16 21:27:48 +00:00
|
|
|
fprintf(traceout, "%s:", inp->name);
|
2011-11-28 13:32:39 +00:00
|
|
|
if (trace_flags & TRACE_LINENO)
|
2002-02-16 21:27:48 +00:00
|
|
|
fprintf(traceout, "%lu:", inp->lineno);
|
|
|
|
fprintf(traceout, " -%d- ", frame_level());
|
2011-11-28 13:32:39 +00:00
|
|
|
if (trace_flags & TRACE_ID)
|
2002-02-16 21:27:48 +00:00
|
|
|
fprintf(traceout, "id %lu: ", expansion_id);
|
|
|
|
}
|
|
|
|
|
2011-11-28 13:32:39 +00:00
|
|
|
size_t
|
2002-05-01 21:37:29 +00:00
|
|
|
trace(const char *argv[], int argc, struct input_file *inp)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
2011-11-28 13:32:39 +00:00
|
|
|
if (!traceout)
|
|
|
|
traceout = stderr;
|
2002-02-16 21:27:48 +00:00
|
|
|
print_header(inp);
|
2011-11-28 13:32:39 +00:00
|
|
|
if (trace_flags & TRACE_CONT) {
|
2002-02-16 21:27:48 +00:00
|
|
|
fprintf(traceout, "%s ...\n", argv[1]);
|
|
|
|
print_header(inp);
|
|
|
|
}
|
|
|
|
fprintf(traceout, "%s", argv[1]);
|
2011-11-28 13:32:39 +00:00
|
|
|
if ((trace_flags & TRACE_ARGS) && argc > 2) {
|
2002-02-16 21:27:48 +00:00
|
|
|
char delim[3];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
delim[0] = LPAREN;
|
|
|
|
delim[1] = EOS;
|
|
|
|
for (i = 2; i < argc; i++) {
|
2002-07-15 02:15:12 +00:00
|
|
|
fprintf(traceout, "%s%s%s%s", delim,
|
2011-11-28 13:32:39 +00:00
|
|
|
(trace_flags & TRACE_QUOTE) ? lquote : "",
|
2002-07-15 02:15:12 +00:00
|
|
|
argv[i],
|
2011-11-28 13:32:39 +00:00
|
|
|
(trace_flags & TRACE_QUOTE) ? rquote : "");
|
2002-02-16 21:27:48 +00:00
|
|
|
delim[0] = COMMA;
|
|
|
|
delim[1] = ' ';
|
|
|
|
delim[2] = EOS;
|
|
|
|
}
|
|
|
|
fprintf(traceout, "%c", RPAREN);
|
|
|
|
}
|
2011-11-28 13:32:39 +00:00
|
|
|
if (trace_flags & TRACE_CONT) {
|
2002-02-16 21:27:48 +00:00
|
|
|
fprintf(traceout, " -> ???\n");
|
|
|
|
print_header(inp);
|
|
|
|
fprintf(traceout, argc > 2 ? "%s(...)" : "%s", argv[1]);
|
|
|
|
}
|
2011-11-28 13:32:39 +00:00
|
|
|
if (trace_flags & TRACE_EXPANSION)
|
2002-02-16 21:27:48 +00:00
|
|
|
return buffer_mark();
|
|
|
|
else {
|
|
|
|
fprintf(traceout, "\n");
|
2011-11-28 13:32:39 +00:00
|
|
|
return SIZE_MAX;
|
2002-02-16 21:27:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-15 02:15:12 +00:00
|
|
|
void
|
2002-05-01 21:37:29 +00:00
|
|
|
finish_trace(size_t mark)
|
2002-02-16 21:27:48 +00:00
|
|
|
{
|
|
|
|
fprintf(traceout, " -> ");
|
2011-11-28 13:32:39 +00:00
|
|
|
if (trace_flags & TRACE_QUOTE)
|
2002-02-16 21:27:48 +00:00
|
|
|
fprintf(traceout, "%s", lquote);
|
|
|
|
dump_buffer(traceout, mark);
|
2011-11-28 13:32:39 +00:00
|
|
|
if (trace_flags & TRACE_QUOTE)
|
2002-02-16 21:27:48 +00:00
|
|
|
fprintf(traceout, "%s", rquote);
|
|
|
|
fprintf(traceout, "\n");
|
|
|
|
}
|