1 1.9 uwe /* $NetBSD: trace.c,v 1.9 2020/06/25 02:25:53 uwe Exp $ */ 2 1.6 christos /* $OpenBSD: trace.c,v 1.15 2006/03/24 08:03:44 espie Exp $ */ 3 1.1 tv /* 4 1.1 tv * Copyright (c) 2001 Marc Espie. 5 1.1 tv * 6 1.1 tv * Redistribution and use in source and binary forms, with or without 7 1.1 tv * modification, are permitted provided that the following conditions 8 1.1 tv * are met: 9 1.1 tv * 1. Redistributions of source code must retain the above copyright 10 1.1 tv * notice, this list of conditions and the following disclaimer. 11 1.1 tv * 2. Redistributions in binary form must reproduce the above copyright 12 1.1 tv * notice, this list of conditions and the following disclaimer in the 13 1.1 tv * documentation and/or other materials provided with the distribution. 14 1.1 tv * 15 1.1 tv * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS 16 1.1 tv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 1.1 tv * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 1.1 tv * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD 19 1.1 tv * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 1.1 tv * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 1.1 tv * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 1.1 tv * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 1.1 tv * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 1.1 tv * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 1.1 tv * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 1.1 tv */ 27 1.5 jmc #if HAVE_NBTOOL_CONFIG_H 28 1.5 jmc #include "nbtool_config.h" 29 1.5 jmc #endif 30 1.6 christos #include <sys/cdefs.h> 31 1.9 uwe __RCSID("$NetBSD: trace.c,v 1.9 2020/06/25 02:25:53 uwe Exp $"); 32 1.5 jmc 33 1.1 tv #include <sys/types.h> 34 1.6 christos #include <err.h> 35 1.1 tv #include <stddef.h> 36 1.6 christos #include <stdint.h> 37 1.1 tv #include <stdio.h> 38 1.1 tv #include <stdlib.h> 39 1.1 tv #include "mdef.h" 40 1.1 tv #include "stdd.h" 41 1.1 tv #include "extern.h" 42 1.1 tv 43 1.4 tv FILE *traceout; 44 1.1 tv 45 1.1 tv #define TRACE_ARGS 1 46 1.1 tv #define TRACE_EXPANSION 2 47 1.1 tv #define TRACE_QUOTE 4 48 1.1 tv #define TRACE_FILENAME 8 49 1.1 tv #define TRACE_LINENO 16 50 1.1 tv #define TRACE_CONT 32 51 1.1 tv #define TRACE_ID 64 52 1.1 tv #define TRACE_NEWFILE 128 /* not implemented yet */ 53 1.1 tv #define TRACE_INPUT 256 /* not implemented yet */ 54 1.1 tv 55 1.6 christos static unsigned int letter_to_flag(int); 56 1.6 christos static void print_header(struct input_file *); 57 1.6 christos static int frame_level(void); 58 1.1 tv 59 1.1 tv 60 1.6 christos unsigned int trace_flags = TRACE_QUOTE | TRACE_EXPANSION; 61 1.1 tv 62 1.9 uwe int 63 1.6 christos trace_file(const char *name) 64 1.1 tv { 65 1.9 uwe FILE *newfp; 66 1.9 uwe 67 1.9 uwe if (name == NULL) 68 1.9 uwe newfp = stderr; 69 1.9 uwe #if 0 /* not yet */ 70 1.9 uwe else if (*name == '\0') 71 1.9 uwe newfp = NULL; 72 1.9 uwe #endif 73 1.9 uwe else { 74 1.9 uwe newfp = fopen(name, "a"); 75 1.9 uwe if (newfp == NULL) 76 1.9 uwe return -1; 77 1.9 uwe } 78 1.1 tv 79 1.6 christos if (traceout && traceout != stderr) 80 1.1 tv fclose(traceout); 81 1.9 uwe traceout = newfp; 82 1.9 uwe return 0; 83 1.1 tv } 84 1.1 tv 85 1.1 tv static unsigned int 86 1.6 christos letter_to_flag(int c) 87 1.1 tv { 88 1.1 tv switch(c) { 89 1.1 tv case 'a': 90 1.1 tv return TRACE_ARGS; 91 1.1 tv case 'e': 92 1.1 tv return TRACE_EXPANSION; 93 1.1 tv case 'q': 94 1.1 tv return TRACE_QUOTE; 95 1.1 tv case 'c': 96 1.1 tv return TRACE_CONT; 97 1.1 tv case 'x': 98 1.1 tv return TRACE_ID; 99 1.1 tv case 'f': 100 1.1 tv return TRACE_FILENAME; 101 1.1 tv case 'l': 102 1.1 tv return TRACE_LINENO; 103 1.1 tv case 'p': 104 1.1 tv return TRACE_NEWFILE; 105 1.1 tv case 'i': 106 1.1 tv return TRACE_INPUT; 107 1.1 tv case 't': 108 1.1 tv return TRACE_ALL; 109 1.1 tv case 'V': 110 1.1 tv return ~0; 111 1.1 tv default: 112 1.1 tv return 0; 113 1.1 tv } 114 1.1 tv } 115 1.1 tv 116 1.1 tv void 117 1.6 christos set_trace_flags(const char *s) 118 1.1 tv { 119 1.1 tv char mode = 0; 120 1.1 tv unsigned int f = 0; 121 1.1 tv 122 1.1 tv if (*s == '+' || *s == '-') 123 1.1 tv mode = *s++; 124 1.1 tv while (*s) 125 1.1 tv f |= letter_to_flag(*s++); 126 1.1 tv switch(mode) { 127 1.1 tv case 0: 128 1.6 christos trace_flags = f; 129 1.1 tv break; 130 1.1 tv case '+': 131 1.6 christos trace_flags |= f; 132 1.1 tv break; 133 1.1 tv case '-': 134 1.6 christos trace_flags &= ~f; 135 1.1 tv break; 136 1.1 tv } 137 1.1 tv } 138 1.1 tv 139 1.1 tv static int 140 1.8 matt frame_level(void) 141 1.1 tv { 142 1.1 tv int level; 143 1.1 tv int framep; 144 1.1 tv 145 1.1 tv for (framep = fp, level = 0; framep != 0; 146 1.6 christos level++,framep = mstack[framep-3].sfra) 147 1.1 tv ; 148 1.1 tv return level; 149 1.1 tv } 150 1.1 tv 151 1.1 tv static void 152 1.6 christos print_header(struct input_file *inp) 153 1.1 tv { 154 1.6 christos fprintf(traceout, "m4trace:"); 155 1.6 christos if (trace_flags & TRACE_FILENAME) 156 1.6 christos fprintf(traceout, "%s:", inp->name); 157 1.6 christos if (trace_flags & TRACE_LINENO) 158 1.7 christos fprintf(traceout, "%lu:", TOKEN_LINE(inp)); 159 1.6 christos fprintf(traceout, " -%d- ", frame_level()); 160 1.6 christos if (trace_flags & TRACE_ID) 161 1.6 christos fprintf(traceout, "id %lu: ", expansion_id); 162 1.1 tv } 163 1.1 tv 164 1.6 christos size_t 165 1.6 christos trace(const char *argv[], int argc, struct input_file *inp) 166 1.1 tv { 167 1.6 christos if (!traceout) 168 1.6 christos traceout = stderr; 169 1.1 tv print_header(inp); 170 1.6 christos if (trace_flags & TRACE_CONT) { 171 1.6 christos fprintf(traceout, "%s ...\n", argv[1]); 172 1.1 tv print_header(inp); 173 1.1 tv } 174 1.6 christos fprintf(traceout, "%s", argv[1]); 175 1.6 christos if ((trace_flags & TRACE_ARGS) && argc > 2) { 176 1.1 tv char delim[3]; 177 1.1 tv int i; 178 1.1 tv 179 1.1 tv delim[0] = LPAREN; 180 1.1 tv delim[1] = EOS; 181 1.1 tv for (i = 2; i < argc; i++) { 182 1.6 christos fprintf(traceout, "%s%s%s%s", delim, 183 1.6 christos (trace_flags & TRACE_QUOTE) ? lquote : "", 184 1.1 tv argv[i], 185 1.6 christos (trace_flags & TRACE_QUOTE) ? rquote : ""); 186 1.1 tv delim[0] = COMMA; 187 1.1 tv delim[1] = ' '; 188 1.1 tv delim[2] = EOS; 189 1.1 tv } 190 1.6 christos fprintf(traceout, "%c", RPAREN); 191 1.1 tv } 192 1.6 christos if (trace_flags & TRACE_CONT) { 193 1.6 christos fprintf(traceout, " -> ???\n"); 194 1.1 tv print_header(inp); 195 1.6 christos fprintf(traceout, argc > 2 ? "%s(...)" : "%s", argv[1]); 196 1.1 tv } 197 1.6 christos if (trace_flags & TRACE_EXPANSION) 198 1.1 tv return buffer_mark(); 199 1.1 tv else { 200 1.6 christos fprintf(traceout, "\n"); 201 1.6 christos return SIZE_MAX; 202 1.1 tv } 203 1.1 tv } 204 1.1 tv 205 1.1 tv void 206 1.6 christos finish_trace(size_t mark) 207 1.1 tv { 208 1.6 christos fprintf(traceout, " -> "); 209 1.6 christos if (trace_flags & TRACE_QUOTE) 210 1.6 christos fprintf(traceout, "%s", lquote); 211 1.6 christos dump_buffer(traceout, mark); 212 1.6 christos if (trace_flags & TRACE_QUOTE) 213 1.6 christos fprintf(traceout, "%s", rquote); 214 1.6 christos fprintf(traceout, "\n"); 215 1.1 tv } 216