@matheusbsbs / matheus-bagliano / commits / 6ce4e61f1b
Fork 0

Trace into a file or an open fd and refactor tracing code.

If GIT_TRACE is set to an absolute path (starting with a '/' character), we interpret this as a file path and we trace into it. Also if GIT_TRACE is set to an integer value greater than 1 and lower than 10, we interpret this as an open fd value and we trace into it. Note that this behavior is not compatible with the previous one. We also trace whole messages using one write(2) call to make sure messages from processes do net get mixed up in the middle. This patch makes it possible to get trace information when running "make test". Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Christian Couder committed Sep 2, 2006 at 18:23 UTC 6ce4e61f1be690681f6494eb5ca26540c2316f81
10 files changed +229 -64
Makefile
+1 -1
@@ -249,7 +249,7 @@ LIB_OBJS = \
249 server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
250 tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
251 fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
252 - write_or_die.o \
252 + write_or_die.o trace.o \
253 alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS)
254
255 BUILTIN_OBJS = \
cache.h
+6
@@ -398,6 +398,7 @@ extern char git_commit_encoding[MAX_ENCODING_LENGTH];
398
399 extern int copy_fd(int ifd, int ofd);
400 extern void write_or_die(int fd, const void *buf, size_t count);
401 +extern int write_or_whine(int fd, const void *buf, size_t count, const char *msg);
402
403 /* Finish off pack transfer receiving end */
404 extern int receive_unpack_pack(int fd[2], const char *me, int quiet, int);
@@ -423,4 +424,9 @@ extern struct commit *alloc_commit_node(void);
424 extern struct tag *alloc_tag_node(void);
425 extern void alloc_report(void);
426
427 +/* trace.c */
428 +extern int nfvasprintf(char **str, const char *fmt, va_list va);
429 +extern void trace_printf(const char *format, ...);
430 +extern void trace_argv_printf(const char **argv, int count, const char *format, ...);
431 +
432 #endif /* CACHE_H */
exec_cmd.c
+2 -16
@@ -97,26 +97,12 @@ int execv_git_cmd(const char **argv)
97 tmp = argv[0];
98 argv[0] = git_command;
99
100 - if (getenv("GIT_TRACE")) {
101 - const char **p = argv;
102 - fputs("trace: exec:", stderr);
103 - while (*p) {
104 - fputc(' ', stderr);
105 - sq_quote_print(stderr, *p);
106 - ++p;
107 - }
108 - putc('\n', stderr);
109 - fflush(stderr);
110 - }
100 + trace_argv_printf(argv, -1, "trace: exec:");
101
102 /* execve() can only ever return if it fails */
103 execve(git_command, (char **)argv, environ);
104
115 - if (getenv("GIT_TRACE")) {
116 - fprintf(stderr, "trace: exec failed: %s\n",
117 - strerror(errno));
118 - fflush(stderr);
119 - }
105 + trace_printf("trace: exec failed: %s\n", strerror(errno));
106
107 argv[0] = tmp;
108 }
git.c
+4 -21
@@ -179,17 +179,9 @@ static int handle_alias(int *argcp, const char ***argv)
179 if (!strcmp(alias_command, new_argv[0]))
180 die("recursive alias: %s", alias_command);
181
182 - if (getenv("GIT_TRACE")) {
183 - int i;
184 - fprintf(stderr, "trace: alias expansion: %s =>",
185 - alias_command);
186 - for (i = 0; i < count; ++i) {
187 - fputc(' ', stderr);
188 - sq_quote_print(stderr, new_argv[i]);
189 - }
190 - fputc('\n', stderr);
191 - fflush(stderr);
192 - }
182 + trace_argv_printf(new_argv, count,
183 + "trace: alias expansion: %s =>",
184 + alias_command);
185
186 new_argv = xrealloc(new_argv, sizeof(char*) *
187 (count + *argcp + 1));
@@ -292,16 +284,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
284 prefix = setup_git_directory();
285 if (p->option & USE_PAGER)
286 setup_pager();
295 - if (getenv("GIT_TRACE")) {
296 - int j;
297 - fprintf(stderr, "trace: built-in: git");
298 - for (j = 0; j < argc; ++j) {
299 - fputc(' ', stderr);
300 - sq_quote_print(stderr, argv[j]);
301 - }
302 - putc('\n', stderr);
303 - fflush(stderr);
304 - }
287 + trace_argv_printf(argv, argc, "trace: built-in: git");
288
289 exit(p->fn(argc, argv, prefix));
290 }
imap-send.c
-25
@@ -110,7 +110,6 @@ static char *next_arg( char ** );
110
111 static void free_generic_messages( message_t * );
112
113 -static int nfvasprintf( char **str, const char *fmt, va_list va );
113 static int nfsnprintf( char *buf, int blen, const char *fmt, ... );
114
115
@@ -371,21 +370,6 @@ free_generic_messages( message_t *msgs )
370 }
371 }
372
374 -static int
375 -git_vasprintf( char **strp, const char *fmt, va_list ap )
376 -{
377 - int len;
378 - char tmp[1024];
379 -
380 - if ((len = vsnprintf( tmp, sizeof(tmp), fmt, ap )) < 0 || !(*strp = xmalloc( len + 1 )))
381 - return -1;
382 - if (len >= (int)sizeof(tmp))
383 - vsprintf( *strp, fmt, ap );
384 - else
385 - memcpy( *strp, tmp, len + 1 );
386 - return len;
387 -}
388 -
373 static int
374 nfsnprintf( char *buf, int blen, const char *fmt, ... )
375 {
@@ -399,15 +383,6 @@ nfsnprintf( char *buf, int blen, const char *fmt, ... )
383 return ret;
384 }
385
402 -static int
403 -nfvasprintf( char **str, const char *fmt, va_list va )
404 -{
405 - int ret = git_vasprintf( str, fmt, va );
406 - if (ret < 0)
407 - die( "Fatal: Out of memory\n");
408 - return ret;
409 -}
410 -
386 static struct {
387 unsigned char i, j, s[256];
388 } rs;
quote.c
+32
@@ -74,6 +74,38 @@ char *sq_quote(const char *src)
74 return buf;
75 }
76
77 +char *sq_quote_argv(const char** argv, int count)
78 +{
79 + char *buf, *to;
80 + int i;
81 + size_t len = 0;
82 +
83 + /* Count argv if needed. */
84 + if (count < 0) {
85 + for (count = 0; argv[count]; count++)
86 + ; /* just counting */
87 + }
88 +
89 + /* Special case: no argv. */
90 + if (!count)
91 + return xcalloc(1,1);
92 +
93 + /* Get destination buffer length. */
94 + for (i = 0; i < count; i++)
95 + len += sq_quote_buf(NULL, 0, argv[i]) + 1;
96 +
97 + /* Alloc destination buffer. */
98 + to = buf = xmalloc(len + 1);
99 +
100 + /* Copy into destination buffer. */
101 + for (i = 0; i < count; ++i) {
102 + *to++ = ' ';
103 + to += sq_quote_buf(to, len, argv[i]);
104 + }
105 +
106 + return buf;
107 +}
108 +
109 char *sq_dequote(char *arg)
110 {
111 char *dst = arg;
quote.h
+1
@@ -31,6 +31,7 @@
31 extern char *sq_quote(const char *src);
32 extern void sq_quote_print(FILE *stream, const char *src);
33 extern size_t sq_quote_buf(char *dst, size_t n, const char *src);
34 +extern char *sq_quote_argv(const char** argv, int count);
35
36 /* This unwraps what sq_quote() produces in place, but returns
37 * NULL if the input does not look like what sq_quote would have
t/test-lib.sh
+9 -1
@@ -28,13 +28,21 @@ unset GIT_DIR
28 unset GIT_EXTERNAL_DIFF
29 unset GIT_INDEX_FILE
30 unset GIT_OBJECT_DIRECTORY
31 -unset GIT_TRACE
31 unset SHA1_FILE_DIRECTORIES
32 unset SHA1_FILE_DIRECTORY
33 export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
34 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
35 export EDITOR VISUAL
36
37 +case $(echo $GIT_TRACE |tr [A-Z] [a-z]) in
38 + 1|2|true)
39 + echo "* warning: Some tests will not work if GIT_TRACE" \
40 + "is set as to trace on STDERR ! *"
41 + echo "* warning: Please set GIT_TRACE to something" \
42 + "other than 1, 2 or true ! *"
43 + ;;
44 +esac
45 +
46 # Each test should start with something like this, after copyright notices:
47 #
48 # test_description='Description of this test...
trace.c new
+149
@@ -0,0 +1,149 @@
1 +/*
2 + * GIT - The information manager from hell
3 + *
4 + * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
5 + * Copyright (C) 2002-2004 Oswald Buddenhagen <ossi@users.sf.net>
6 + * Copyright (C) 2004 Theodore Y. Ts'o <tytso@mit.edu>
7 + * Copyright (C) 2006 Mike McCormack
8 + * Copyright (C) 2006 Christian Couder
9 + *
10 + * This program is free software; you can redistribute it and/or modify
11 + * it under the terms of the GNU General Public License as published by
12 + * the Free Software Foundation; either version 2 of the License, or
13 + * (at your option) any later version.
14 + *
15 + * This program is distributed in the hope that it will be useful,
16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 + * GNU General Public License for more details.
19 + *
20 + * You should have received a copy of the GNU General Public License
21 + * along with this program; if not, write to the Free Software
22 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 + */
24 +
25 +#include "cache.h"
26 +#include "quote.h"
27 +
28 +/* Stolen from "imap-send.c". */
29 +static int git_vasprintf(char **strp, const char *fmt, va_list ap)
30 +{
31 + int len;
32 + char tmp[1024];
33 +
34 + if ((len = vsnprintf(tmp, sizeof(tmp), fmt, ap)) < 0 ||
35 + !(*strp = xmalloc(len + 1)))
36 + return -1;
37 + if (len >= (int)sizeof(tmp))
38 + vsprintf(*strp, fmt, ap);
39 + else
40 + memcpy(*strp, tmp, len + 1);
41 + return len;
42 +}
43 +
44 +/* Stolen from "imap-send.c". */
45 +int nfvasprintf(char **str, const char *fmt, va_list va)
46 +{
47 + int ret = git_vasprintf(str, fmt, va);
48 + if (ret < 0)
49 + die("Fatal: Out of memory\n");
50 + return ret;
51 +}
52 +
53 +/* Get a trace file descriptor from GIT_TRACE env variable. */
54 +static int get_trace_fd(int *need_close)
55 +{
56 + char *trace = getenv("GIT_TRACE");
57 +
58 + if (!trace || !strcmp(trace, "0") || !strcasecmp(trace," false"))
59 + return 0;
60 + if (!strcmp(trace, "1") || !strcasecmp(trace, "true"))
61 + return STDERR_FILENO;
62 + if (strlen(trace) == 1 && isdigit(*trace))
63 + return atoi(trace);
64 + if (*trace == '/') {
65 + int fd = open(trace, O_WRONLY | O_APPEND | O_CREAT, 0666);
66 + if (fd == -1) {
67 + fprintf(stderr,
68 + "Could not open '%s' for tracing: %s\n"
69 + "Defaulting to tracing on stderr...\n",
70 + trace, strerror(errno));
71 + return STDERR_FILENO;
72 + }
73 + *need_close = 1;
74 + return fd;
75 + }
76 +
77 + fprintf(stderr, "What does '%s' for GIT_TRACE means ?\n", trace);
78 + fprintf(stderr, "If you want to trace into a file, "
79 + "then please set GIT_TRACE to an absolute pathname "
80 + "(starting with /).\n");
81 + fprintf(stderr, "Defaulting to tracing on stderr...\n");
82 +
83 + return STDERR_FILENO;
84 +}
85 +
86 +static const char err_msg[] = "Could not trace into fd given by "
87 + "GIT_TRACE environment variable";
88 +
89 +void trace_printf(const char *format, ...)
90 +{
91 + char *trace_str;
92 + va_list rest;
93 + int need_close = 0;
94 + int fd = get_trace_fd(&need_close);
95 +
96 + if (!fd)
97 + return;
98 +
99 + va_start(rest, format);
100 + nfvasprintf(&trace_str, format, rest);
101 + va_end(rest);
102 +
103 + write_or_whine(fd, trace_str, strlen(trace_str), err_msg);
104 +
105 + free(trace_str);
106 +
107 + if (need_close)
108 + close(fd);
109 +}
110 +
111 +void trace_argv_printf(const char **argv, int count, const char *format, ...)
112 +{
113 + char *argv_str, *format_str, *trace_str;
114 + size_t argv_len, format_len, trace_len;
115 + va_list rest;
116 + int need_close = 0;
117 + int fd = get_trace_fd(&need_close);
118 +
119 + if (!fd)
120 + return;
121 +
122 + /* Get the argv string. */
123 + argv_str = sq_quote_argv(argv, count);
124 + argv_len = strlen(argv_str);
125 +
126 + /* Get the formated string. */
127 + va_start(rest, format);
128 + nfvasprintf(&format_str, format, rest);
129 + va_end(rest);
130 +
131 + /* Allocate buffer for trace string. */
132 + format_len = strlen(format_str);
133 + trace_len = argv_len + format_len + 1; /* + 1 for \n */
134 + trace_str = xmalloc(trace_len + 1);
135 +
136 + /* Copy everything into the trace string. */
137 + strncpy(trace_str, format_str, format_len);
138 + strncpy(trace_str + format_len, argv_str, argv_len);
139 + strcpy(trace_str + trace_len - 1, "\n");
140 +
141 + write_or_whine(fd, trace_str, trace_len, err_msg);
142 +
143 + free(argv_str);
144 + free(format_str);
145 + free(trace_str);
146 +
147 + if (need_close)
148 + close(fd);
149 +}
write_or_die.c
+25
@@ -18,3 +18,28 @@ void write_or_die(int fd, const void *buf, size_t count)
18 p += written;
19 }
20 }
21 +
22 +int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
23 +{
24 + const char *p = buf;
25 + ssize_t written;
26 +
27 + while (count > 0) {
28 + written = xwrite(fd, p, count);
29 + if (written == 0) {
30 + fprintf(stderr, "%s: disk full?\n", msg);
31 + return 0;
32 + }
33 + else if (written < 0) {
34 + if (errno == EPIPE)
35 + exit(0);
36 + fprintf(stderr, "%s: write error (%s)\n",
37 + msg, strerror(errno));
38 + return 0;
39 + }
40 + count -= written;
41 + p += written;
42 + }
43 +
44 + return 1;
45 +}