Debian Codegraph @

src/print.c

raw symbols SCIP
1 /* Output or logging functions for GNU Make.
2
3 Copyright (C) 2005, 2007, 2008, 2020 R. Bernstein <[email protected]>
4 This file is part of GNU Make (remake variant).
5 Copyright (C) 2004, 2005, 2007, 2008, Free Software Foundation, Inc.
6
7 GNU Make is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Make is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Make; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "makeint.h"
23 #include "make.h"
24 #include "main.h"
25 #include "commands.h"
26 // debugger include(s)
27 #include "cmd.h"
28 #include "debug.h"
29 #include "dep.h"
30 #include "read.h"
31 #include "print.h"
32
33 #include <stdarg.h>
34
35 /* Think of the below not as an enumeration but as #defines done in a
36 way that we'll be able to use the value in a gdb. */
37 enum debug_print_enums_e debug_print_enums1;
38
39
40 /* Write a message indicating what directory we are in
41 and the make level number. */
42
43 static void
44 err_log_working_directory (void)
45 {
46 static char *buf = NULL;
47 static unsigned int len = 0;
48 unsigned int need;
49 const char *fmt;
50 char *p;
51
52 /* Get enough space for the longest possible output. */
53 need = strlen (program) + INTSTR_LENGTH + 2 + 1;
54 if (starting_directory)
55 need += strlen (starting_directory);
56
57 /* Use entire sentences to give the translators a fighting chance. */
58 if (makelevel == 0)
59 if (starting_directory == 0)
60 fmt = _("%s\n");
61 else
62 fmt = _("%s: directory '%s'\n");
63 else
64 if (starting_directory == 0)
65 fmt = _("%s[%u]: unknown directory\n");
66 else
67 fmt = _("%s[%u]: directory '%s'\n");
68
69 need += strlen (fmt);
70
71 if (need > len)
72 {
73 buf = xrealloc (buf, need);
74 len = need;
75 }
76
77 p = buf;
78 if (print_data_base_flag)
79 {
80 *(p++) = '#';
81 *(p++) = ' ';
82 }
83
84 if (makelevel == 0)
85 if (starting_directory == 0)
86 sprintf (p, fmt , program);
87 else
88 sprintf (p, fmt, program, starting_directory);
89 else if (starting_directory == 0)
90 sprintf (p, fmt, program, makelevel);
91 else
92 sprintf (p, fmt, program, makelevel, starting_directory);
93
94 outputs (0, buf);
95 }
96
97
98 void
99 err_with_stack (target_stack_node_t *p_call, const char *fmt, ...)
100 {
101 va_list args;
102 gmk_floc *p_floc = NULL;
103 file_t *p_target = NULL;
104
105 if (p_call && p_call->p_target) {
106 p_target = p_call->p_target;
107 p_floc = &(p_target->floc);
108 }
109
110 if (p_floc) {
111 if (!p_floc->filenm)
112 fprintf (stderr, "<builtin>");
113 else
114 fprintf (stderr, "%s:%lu: ", p_floc->filenm, p_floc->lineno + p_floc->offset);
115 } else {
116 err_log_working_directory ();
117 if (makelevel == 0)
118 fprintf (stderr, "%s: ", program);
119 else
120 fprintf (stderr, "%s[%u]: ", program, makelevel);
121 }
122
123 va_start (args, fmt);
124 vfprintf (stderr, fmt, args);
125 va_end (args);
126
127 putc ('\n', stderr);
128 if (!no_extended_errors) {
129 if (p_call) {
130 putc ('\n', stdout);
131 print_target_stack(p_call, -1, MAX_STACK_SHOW);
132 } else if (p_stack_floc_top) {
133 putc ('\n', stdout);
134 print_floc_stack(-1, MAX_STACK_SHOW);
135 }
136 }
137 fflush (stdout);
138 fflush (stderr);
139 if (debugger_on_error & DEBUGGER_ON_ERROR)
140 enter_debugger(p_call, p_target, -1, DEBUG_ERROR_HIT);
141 }
142
143 /* Print an error message and exit. */
144 void
145 fatal_err (target_stack_node_t *p_call, const char *fmt, ...)
146 {
147 va_list args;
148 gmk_floc *p_floc = NULL;
149 file_t *p_target = NULL;
150
151 err_log_working_directory ();
152
153 if (p_call && p_call->p_target) {
154 p_target = p_call->p_target;
155 p_floc = &(p_target->floc);
156 }
157
158 if (p_floc && p_floc->filenm)
159 fprintf (stderr, "%s:%lu: *** ", p_floc->filenm, p_floc->lineno);
160 else if (makelevel == 0)
161 fprintf (stderr, "%s: *** ", program);
162 else
163 fprintf (stderr, "%s[%u]: *** ", program, makelevel);
164
165 va_start (args, fmt);
166 vfprintf (stderr, fmt, args);
167 va_end (args);
168
169 fputs (_(". Stop.\n"), stderr);
170 if (!no_extended_errors) {
171 if (p_call)
172 print_target_stack(p_call, -1, MAX_STACK_SHOW);
173 else if (p_stack_floc_top)
174 print_floc_stack(-1, MAX_STACK_SHOW);
175 }
176 if ( (debugger_on_error & DEBUGGER_ON_FATAL) || debugger_enabled )
177 enter_debugger(p_call, p_target, 2, DEBUG_ERROR_HIT);
178 die (MAKE_FAILURE);
179 }
180
181 /*! Under -d, write a message describing the current IDs. */
182
183 void
184 log_access (char *flavor)
185 {
186 if (! ISDB (DB_JOBS))
187 return;
188
189 /* All the other debugging messages go to stdout,
190 but we write this one to stderr because it might be
191 run in a child fork whose stdout is piped. */
192
193 fprintf (stderr, _("%s: user %lu (real %lu), group %lu (real %lu)\n"),
194 flavor, (unsigned long) geteuid (), (unsigned long) getuid (),
195 (unsigned long) getegid (), (unsigned long) getgid ());
196 fflush (stderr);
197 }
198
199 /*! Display a variable and its value. */
200 void
201 print_variable (variable_t *p_v)
202 {
203 if (p_v) {
204 const char *psz_origin = origin2str(p_v->origin);
205 if (NULL != p_v->fileinfo.filenm) {
206 printf(_("%s:%lu (origin: %s) %s = %s\n"),
207 p_v->fileinfo.filenm, p_v->fileinfo.lineno,
208 psz_origin,
209 p_v->name, p_v->value);
210 } else {
211 printf("(origin %s) %s = %s\n", psz_origin, p_v->name, p_v->value);
212 }
213 }
214 }
215
216 /*! Display a variable and its value with all substitutions included. */
217 void
218 print_variable_expand (variable_t *p_v)
219 {
220 if (p_v) {
221 const char *psz_origin = origin2str(p_v->origin);
222 if (NULL != p_v->fileinfo.filenm) {
223 printf(_("%s:%lu (origin: %s) %s := %s\n"),
224 p_v->fileinfo.filenm, p_v->fileinfo.lineno,
225 psz_origin,
226 p_v->name, variable_expand(p_v->value));
227 } else {
228 printf("(origin %s) %s := %s\n", psz_origin,
229 p_v->name, variable_expand(p_v->value));
230 }
231 }
232 }
233
234 /*! Show a command before executing it. */
235 extern void
236 print_target_prefix (const char *p_name)
237 {
238 printf(" %s", p_name);
239 if (makelevel != 0) {
240 printf ("[%u]", makelevel);
241 }
242 }
243
244 /*! Show target information: location and name. */
245 extern void
246 print_file_target_prefix (const file_t *p_target)
247 {
248 print_floc_prefix(&(p_target->floc));
249 print_target_prefix(p_target->name);
250 }
251
252 /*! Show a command before executing it. */
253 extern void
254 print_floc_prefix (const gmk_floc *p_floc)
255 {
256 if (!p_floc) return;
257 if (p_floc->filenm) {
258 if (!basename_filenames && strlen(p_floc->filenm)
259 && p_floc->filenm[0] != '/')
260 printf("%s/", starting_directory);
261 printf("%s:%lu", p_floc->filenm, p_floc->lineno);
262 } else {
263 if (!basename_filenames)
264 printf("%s/", starting_directory);
265 printf("??:%lu", p_floc->lineno);
266 }
267 }
268
269 /*! Show a command before executing it. */
270 extern debug_return_t
271 print_child_cmd (child_t *p_child, target_stack_node_t *p)
272 {
273 debug_return_t rc = continue_execution;
274
275
276 if (!p_child) return continue_execution;
277
278 if (i_debugger_stepping || p_child->file->tracing) {
279 debug_enter_reason_t reason = DEBUG_STEP_HIT;
280 if (i_debugger_stepping)
281 reason = DEBUG_STEP_HIT;
282 else if (p_child->file->tracing & BRK_BEFORE_PREREQ)
283 reason = DEBUG_BRKPT_BEFORE_PREREQ;
284 else if (p_child->file->tracing & BRK_BEFORE_PREREQ)
285 reason = DEBUG_BRKPT_AFTER_PREREQ;
286
287 rc=enter_debugger(p, p_child->file, 0, reason);
288 }
289
290 return rc;
291 }
292
293 void
294 print_target_stack_entry (const file_t *p_target, int i, int i_pos)
295 {
296 gmk_floc floc;
297 const char *psz_target_name =
298 (p_target && p_target->name) ? p_target->name : "(null)";
299
300 /* If we don't have a line recorded for the target,
301 but we do have one for the commands it runs,
302 use that.
303 */
304 if (p_target->floc.filenm) {
305 memcpy(&floc, &(p_target->floc), sizeof(gmk_floc));
306 } else if (p_target->cmds) {
307 memcpy(&floc, &(p_target->cmds->fileinfo.filenm), sizeof(gmk_floc));
308 /* HACK: is it okay to assume that the target is on the line
309 before the first command? Or should we list the line
310 that the command starts on - so we know we've faked the location?
311 */
312 floc.lineno--;
313 } else {
314 floc.filenm = NULL;
315 }
316
317 if (floc.filenm) {
318 if (i_pos != -1) {
319 printf("%s", (i == i_pos) ? "=>" : " ");
320 }
321 printf ("#%d %s at ", i, psz_target_name);
322 print_floc_prefix(&floc);
323 } else {
324 if (i_pos != -1) {
325 printf("%s", (i == i_pos) ? "=>" : " ");
326 }
327 if (p_target->phony)
328 printf ("#%d %s (.PHONY target)", i, psz_target_name);
329 else
330 printf ("#%d %s at ??", i, psz_target_name);
331
332 }
333 printf ("\n");
334 }
335
336
337 /*! Display the target stack. i_pos is the position we are currently.
338 i_max is the maximum number of entries to show.
339 */
340 extern void
341 print_target_stack (target_stack_node_t *p, int i_pos, int i_max)
342 {
343 int i=0;
344 for ( ; p && i < i_max ;
345 i++, p = p->p_parent ) {
346 print_target_stack_entry (p->p_target, i, i_pos);
347 }
348 }
349
350 /*! Display the Makefile read stack. i_pos is the position we are currently.
351 i_max is the maximum number of entries to show. */
352 extern void
353 print_floc_stack (int i_pos, int i_max)
354 {
355 int i=0;
356 floc_stack_node_t *p;
357 printf("\n");
358 for ( p=p_stack_floc_top; p && i < i_max ;
359 i++, p = p->p_parent ) {
360 if (i_pos != -1) {
361 printf("%s", (i == i_pos) ? "=>" : " ");
362 }
363 printf ("#%d ", i);
364 if (p->p_floc->filenm) {
365 print_floc_prefix(p->p_floc);
366 }
367 printf ("\n");
368 }
369 }
370
371 /*! Print the file information. */
372 void print_file (file_t *p_file)
373 {
374 char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
375 printf("File %s:\n", p_file->name);
376 file_timestamp_sprintf (buf, p_file->last_mtime);
377 printf("\tLast modified: %s\n", buf);
378 if (p_file->mtime_before_update != p_file->last_mtime) {
379 file_timestamp_sprintf (buf, p_file->mtime_before_update);
380 printf("\tBefore update: %s\n", buf);
381 }
382 printf("\tNumber of lines: %u\n", p_file->nlines);
383 }
384
385 /*! Print the list makefiles read by read_makefiles(). */
386 bool print_read_makefiles(const char *psz_filename)
387 {
388 goaldep_t *p_dep;
389 if (!read_makefiles) return false;
390 if (NULL == psz_filename) {
391 for (p_dep = read_makefiles; p_dep; p_dep = p_dep->next) {
392 if (p_dep->file) {
393 print_file(p_dep->file);
394 }
395 }
396 return true;
397 } else {
398 for (p_dep = read_makefiles; p_dep; p_dep = p_dep->next) {
399 if (p_dep->file && 0 == strcmp(p_dep->file->name, psz_filename)) {
400 print_file(p_dep->file);
401 return true;
402 }
403 }
404 }
405 return false;
406 }
407
408 /*! Print the command line used to invoke Make. */
409 void print_cmdline (void)
410 {
411 unsigned int i;
412 printf(_("Command-line invocation:"));
413 printf("\n\t\"");
414 if (global_argv[1]) {
415 printf("%s", argv0);
416 for (i = 1; global_argv[i]; i++) {
417 printf(" %s", global_argv[i]);
418 }
419 printf("\"");
420 } else {
421 printf(_(" none"));
422 }
423 printf("\n");
424 }
425
426
427 /*
428 * Local variables:
429 * eval: (c-set-style "gnu")
430 * indent-tabs-mode: nil
431 * End:
432 */