{"thread":{"id":"5458","subject":"[PATCH] Trace into a file or an open fd and refactor tracing code.","startedAt":"2006-09-02T16:23:48Z","lastAt":"2006-09-02T16:23:48Z","messageCount":1,"participants":["Christian Couder"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"26227","messageId":"20060902182348.bdbe9d60.chriscool@tuxfamily.org","threadId":"5458","inReplyTo":null,"subject":"[PATCH] Trace into a file or an open fd and refactor tracing code.","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2006-09-02T16:23:48Z","receivedAt":"2006-09-02T16:23:48Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"If GIT_TRACE is set to an absolute path (starting with a\n'/' character), we interpret this as a file path and we\ntrace into it.\n\nAlso if GIT_TRACE is set to an integer value greater than\n1 and lower than 10, we interpret this as an open fd value\nand we trace into it.\n\nNote that this behavior is not compatible with the\nprevious one.\n\nWe also trace whole messages using one write(2) call to\nmake sure messages from processes do net get mixed up in\nthe middle.\n\nThis patch makes it possible to get trace information when\nrunning \"make test\".\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n Makefile       |    2 -\n cache.h        |    6 ++\n exec_cmd.c     |   18 +------\n git.c          |   25 ++-------\n imap-send.c    |   25 ---------\n quote.c        |   32 ++++++++++++\n quote.h        |    1 \n t/test-lib.sh  |   10 +++-\n trace.c        |  149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n write_or_die.c |   25 +++++++++\n 10 files changed, 229 insertions(+), 64 deletions(-)\n\ndiff --git a/Makefile b/Makefile\nindex 05bd77f..a639cdf 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -249,7 +249,7 @@ LIB_OBJS = \\\n \tserver-info.o setup.o sha1_file.o sha1_name.o strbuf.o \\\n \ttag.o tree.o usage.o config.o environment.o ctype.o copy.o \\\n \tfetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \\\n-\twrite_or_die.o \\\n+\twrite_or_die.o trace.o \\\n \talloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS)\n \n BUILTIN_OBJS = \\\ndiff --git a/cache.h b/cache.h\nindex 7257c4c..195908f 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -398,6 +398,7 @@ extern char git_commit_encoding[MAX_ENCO\n \n extern int copy_fd(int ifd, int ofd);\n extern void write_or_die(int fd, const void *buf, size_t count);\n+extern int write_or_whine(int fd, const void *buf, size_t count, const char *msg);\n \n /* Finish off pack transfer receiving end */\n extern int receive_unpack_pack(int fd[2], const char *me, int quiet, int);\n@@ -423,4 +424,9 @@ extern struct commit *alloc_commit_node(\n extern struct tag *alloc_tag_node(void);\n extern void alloc_report(void);\n \n+/* trace.c */\n+extern int nfvasprintf(char **str, const char *fmt, va_list va);\n+extern void trace_printf(const char *format, ...);\n+extern void trace_argv_printf(const char **argv, int count, const char *format, ...);\n+\n #endif /* CACHE_H */\ndiff --git a/exec_cmd.c b/exec_cmd.c\nindex e30936d..5d6a124 100644\n--- a/exec_cmd.c\n+++ b/exec_cmd.c\n@@ -97,26 +97,12 @@ int execv_git_cmd(const char **argv)\n \t\ttmp = argv[0];\n \t\targv[0] = git_command;\n \n-\t\tif (getenv(\"GIT_TRACE\")) {\n-\t\t\tconst char **p = argv;\n-\t\t\tfputs(\"trace: exec:\", stderr);\n-\t\t\twhile (*p) {\n-\t\t\t\tfputc(' ', stderr);\n-\t\t\t\tsq_quote_print(stderr, *p);\n-\t\t\t\t++p;\n-\t\t\t}\n-\t\t\tputc('\\n', stderr);\n-\t\t\tfflush(stderr);\n-\t\t}\n+\t\ttrace_argv_printf(argv, -1, \"trace: exec:\");\n \n \t\t/* execve() can only ever return if it fails */\n \t\texecve(git_command, (char **)argv, environ);\n \n-\t\tif (getenv(\"GIT_TRACE\")) {\n-\t\t\tfprintf(stderr, \"trace: exec failed: %s\\n\",\n-\t\t\t\tstrerror(errno));\n-\t\t\tfflush(stderr);\n-\t\t}\n+\t\ttrace_printf(\"trace: exec failed: %s\\n\", strerror(errno));\n \n \t\targv[0] = tmp;\n \t}\ndiff --git a/git.c b/git.c\nindex 05871ad..403fb3a 100644\n--- a/git.c\n+++ b/git.c\n@@ -179,17 +179,9 @@ static int handle_alias(int *argcp, cons\n \t\tif (!strcmp(alias_command, new_argv[0]))\n \t\t\tdie(\"recursive alias: %s\", alias_command);\n \n-\t\tif (getenv(\"GIT_TRACE\")) {\n-\t\t\tint i;\n-\t\t\tfprintf(stderr, \"trace: alias expansion: %s =>\",\n-\t\t\t\talias_command);\n-\t\t\tfor (i = 0; i < count; ++i) {\n-\t\t\t\tfputc(' ', stderr);\n-\t\t\t\tsq_quote_print(stderr, new_argv[i]);\n-\t\t\t}\n-\t\t\tfputc('\\n', stderr);\n-\t\t\tfflush(stderr);\n-\t\t}\n+\t\ttrace_argv_printf(new_argv, count,\n+\t\t\t\t  \"trace: alias expansion: %s =>\",\n+\t\t\t\t  alias_command);\n \n \t\tnew_argv = xrealloc(new_argv, sizeof(char*) *\n \t\t\t\t    (count + *argcp + 1));\n@@ -292,16 +284,7 @@ static void handle_internal_command(int \n \t\t\tprefix = setup_git_directory();\n \t\tif (p->option & USE_PAGER)\n \t\t\tsetup_pager();\n-\t\tif (getenv(\"GIT_TRACE\")) {\n-\t\t\tint j;\n-\t\t\tfprintf(stderr, \"trace: built-in: git\");\n-\t\t\tfor (j = 0; j < argc; ++j) {\n-\t\t\t\tfputc(' ', stderr);\n-\t\t\t\tsq_quote_print(stderr, argv[j]);\n-\t\t\t}\n-\t\t\tputc('\\n', stderr);\n-\t\t\tfflush(stderr);\n-\t\t}\n+\t\ttrace_argv_printf(argv, argc, \"trace: built-in: git\");\n \n \t\texit(p->fn(argc, argv, prefix));\n \t}\ndiff --git a/imap-send.c b/imap-send.c\nindex 65c71c6..6a52dbd 100644\n--- a/imap-send.c\n+++ b/imap-send.c\n@@ -110,7 +110,6 @@ static char *next_arg( char ** );\n \n static void free_generic_messages( message_t * );\n \n-static int nfvasprintf( char **str, const char *fmt, va_list va );\n static int nfsnprintf( char *buf, int blen, const char *fmt, ... );\n \n \n@@ -372,21 +371,6 @@ free_generic_messages( message_t *msgs )\n }\n \n static int\n-git_vasprintf( char **strp, const char *fmt, va_list ap )\n-{\n-\tint len;\n-\tchar tmp[1024];\n-\n-\tif ((len = vsnprintf( tmp, sizeof(tmp), fmt, ap )) < 0 || !(*strp = xmalloc( len + 1 )))\n-\t\treturn -1;\n-\tif (len >= (int)sizeof(tmp))\n-\t\tvsprintf( *strp, fmt, ap );\n-\telse\n-\t\tmemcpy( *strp, tmp, len + 1 );\n-\treturn len;\n-}\n-\n-static int\n nfsnprintf( char *buf, int blen, const char *fmt, ... )\n {\n \tint ret;\n@@ -399,15 +383,6 @@ nfsnprintf( char *buf, int blen, const c\n \treturn ret;\n }\n \n-static int\n-nfvasprintf( char **str, const char *fmt, va_list va )\n-{\n-\tint ret = git_vasprintf( str, fmt, va );\n-\tif (ret < 0)\n-\t\tdie( \"Fatal: Out of memory\\n\");\n-\treturn ret;\n-}\n-\n static struct {\n \tunsigned char i, j, s[256];\n } rs;\ndiff --git a/quote.c b/quote.c\nindex e220dcc..f90091e 100644\n--- a/quote.c\n+++ b/quote.c\n@@ -74,6 +74,38 @@ char *sq_quote(const char *src)\n \treturn buf;\n }\n \n+char *sq_quote_argv(const char** argv, int count)\n+{\n+\tchar *buf, *to;\n+\tint i;\n+\tsize_t len = 0;\n+\n+\t/* Count argv if needed. */\n+\tif (count < 0) {\n+\t\tfor (count = 0; argv[count]; count++)\n+\t\t\t; /* just counting */\n+\t}\n+\n+\t/* Special case: no argv. */\n+\tif (!count)\n+\t\treturn xcalloc(1,1);\n+\t\n+\t/* Get destination buffer length. */\n+\tfor (i = 0; i < count; i++)\n+\t\tlen += sq_quote_buf(NULL, 0, argv[i]) + 1;\n+\n+\t/* Alloc destination buffer. */\n+\tto = buf = xmalloc(len + 1);\n+\n+\t/* Copy into destination buffer. */\n+\tfor (i = 0; i < count; ++i) {\n+\t\t*to++ = ' ';\n+\t\tto += sq_quote_buf(to, len, argv[i]);\n+\t}\n+\n+\treturn buf;\n+}\n+\n char *sq_dequote(char *arg)\n {\n \tchar *dst = arg;\ndiff --git a/quote.h b/quote.h\nindex fc5481e..a6c4611 100644\n--- a/quote.h\n+++ b/quote.h\n@@ -31,6 +31,7 @@ #include <stdio.h>\n extern char *sq_quote(const char *src);\n extern void sq_quote_print(FILE *stream, const char *src);\n extern size_t sq_quote_buf(char *dst, size_t n, const char *src);\n+extern char *sq_quote_argv(const char** argv, int count);\n \n /* This unwraps what sq_quote() produces in place, but returns\n  * NULL if the input does not look like what sq_quote would have\ndiff --git a/t/test-lib.sh b/t/test-lib.sh\nindex 470a909..e262933 100755\n--- a/t/test-lib.sh\n+++ b/t/test-lib.sh\n@@ -28,13 +28,21 @@ unset GIT_DIR\n unset GIT_EXTERNAL_DIFF\n unset GIT_INDEX_FILE\n unset GIT_OBJECT_DIRECTORY\n-unset GIT_TRACE\n unset SHA1_FILE_DIRECTORIES\n unset SHA1_FILE_DIRECTORY\n export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME\n export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME\n export EDITOR VISUAL\n \n+case $(echo $GIT_TRACE |tr [A-Z] [a-z]) in\n+\t1|2|true)\n+\t\techo \"* warning: Some tests will not work if GIT_TRACE\" \\\n+\t\t\t\"is set as to trace on STDERR ! *\"\n+\t\techo \"* warning: Please set GIT_TRACE to something\" \\\n+\t\t\t\"other than 1, 2 or true ! *\"\n+\t\t;;\n+esac\n+\n # Each test should start with something like this, after copyright notices:\n #\n # test_description='Description of this test...\ndiff --git a/trace.c b/trace.c\nnew file mode 100644\nindex 0000000..fecd897\n--- /dev/null\n+++ b/trace.c\n@@ -0,0 +1,149 @@\n+/*\n+ * GIT - The information manager from hell\n+ *\n+ * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>\n+ * Copyright (C) 2002-2004 Oswald Buddenhagen <ossi@users.sf.net>\n+ * Copyright (C) 2004 Theodore Y. Ts'o <tytso@mit.edu>\n+ * Copyright (C) 2006 Mike McCormack\n+ * Copyright (C) 2006 Christian Couder \n+ *\n+ *  This program is free software; you can redistribute it and/or modify\n+ *  it under the terms of the GNU General Public License as published by\n+ *  the Free Software Foundation; either version 2 of the License, or\n+ *  (at your option) any later version.\n+ *\n+ *  This program is distributed in the hope that it will be useful,\n+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+ *  GNU General Public License for more details.\n+ *\n+ *  You should have received a copy of the GNU General Public License\n+ *  along with this program; if not, write to the Free Software\n+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n+ */\n+\n+#include \"cache.h\"\n+#include \"quote.h\"\n+\n+/* Stolen from \"imap-send.c\". */\n+static int git_vasprintf(char **strp, const char *fmt, va_list ap)\n+{\n+\tint len;\n+\tchar tmp[1024];\n+\n+\tif ((len = vsnprintf(tmp, sizeof(tmp), fmt, ap)) < 0 ||\n+\t    !(*strp = xmalloc(len + 1)))\n+\t\treturn -1;\n+\tif (len >= (int)sizeof(tmp))\n+\t\tvsprintf(*strp, fmt, ap);\n+\telse\n+\t\tmemcpy(*strp, tmp, len + 1);\n+\treturn len;\n+}\n+\n+/* Stolen from \"imap-send.c\". */\n+int nfvasprintf(char **str, const char *fmt, va_list va)\n+{\n+\tint ret = git_vasprintf(str, fmt, va);\n+\tif (ret < 0)\n+\t\tdie(\"Fatal: Out of memory\\n\");\n+\treturn ret;\n+}\n+\n+/* Get a trace file descriptor from GIT_TRACE env variable. */\n+static int get_trace_fd(int *need_close)\n+{\n+\tchar *trace = getenv(\"GIT_TRACE\");\n+\n+\tif (!trace || !strcmp(trace, \"0\") || !strcasecmp(trace,\" false\"))\n+\t\treturn 0;\n+\tif (!strcmp(trace, \"1\") || !strcasecmp(trace, \"true\"))\n+\t\treturn STDERR_FILENO;\n+\tif (strlen(trace) == 1 && isdigit(*trace))\n+\t\treturn atoi(trace);\n+\tif (*trace == '/') {\n+\t\tint fd = open(trace, O_WRONLY | O_APPEND | O_CREAT, 0666);\n+\t\tif (fd == -1) {\n+\t\t\tfprintf(stderr,\n+\t\t\t\t\"Could not open '%s' for tracing: %s\\n\"\n+\t\t\t\t\"Defaulting to tracing on stderr...\\n\",\n+\t\t\t\ttrace, strerror(errno));\n+\t\t\treturn STDERR_FILENO;\n+\t\t}\n+\t\t*need_close = 1;\n+\t\treturn fd;\n+\t}\n+\n+\tfprintf(stderr, \"What does '%s' for GIT_TRACE means ?\\n\", trace);\n+\tfprintf(stderr, \"If you want to trace into a file, \"\n+\t\t\"then please set GIT_TRACE to an absolute pathname \"\n+\t\t\"(starting with /).\\n\");\n+\tfprintf(stderr, \"Defaulting to tracing on stderr...\\n\");\n+\t\n+\treturn STDERR_FILENO;\n+}\n+\n+static const char err_msg[] = \"Could not trace into fd given by \"\n+\t\"GIT_TRACE environment variable\";\n+\n+void trace_printf(const char *format, ...)\n+{\n+\tchar *trace_str;\n+\tva_list rest;\n+\tint need_close = 0;\n+\tint fd = get_trace_fd(&need_close);\n+\n+\tif (!fd)\n+\t\treturn;\n+\n+\tva_start(rest, format);\n+\tnfvasprintf(&trace_str, format, rest);\n+\tva_end(rest);\n+\n+\twrite_or_whine(fd, trace_str, strlen(trace_str), err_msg);\n+\n+\tfree(trace_str);\n+\n+\tif (need_close)\n+\t\tclose(fd);\n+}\n+\n+void trace_argv_printf(const char **argv, int count, const char *format, ...)\n+{\n+\tchar *argv_str, *format_str, *trace_str;\n+\tsize_t argv_len, format_len, trace_len;\n+\tva_list rest;\n+\tint need_close = 0;\n+\tint fd = get_trace_fd(&need_close);\n+\n+\tif (!fd)\n+\t\treturn;\n+\n+\t/* Get the argv string. */\n+\targv_str = sq_quote_argv(argv, count);\n+\targv_len = strlen(argv_str);\n+\n+\t/* Get the formated string. */\n+\tva_start(rest, format);\n+\tnfvasprintf(&format_str, format, rest);\n+\tva_end(rest);\n+\n+\t/* Allocate buffer for trace string. */\n+\tformat_len = strlen(format_str);\n+\ttrace_len = argv_len + format_len + 1; /* + 1 for \\n */\n+\ttrace_str = xmalloc(trace_len + 1);\n+\n+\t/* Copy everything into the trace string. */\n+\tstrncpy(trace_str, format_str, format_len);\n+\tstrncpy(trace_str + format_len, argv_str, argv_len);\n+\tstrcpy(trace_str + trace_len - 1, \"\\n\");\n+\n+\twrite_or_whine(fd, trace_str, trace_len, err_msg);\n+\t\n+\tfree(argv_str);\n+\tfree(format_str);\n+\tfree(trace_str);\n+\n+\tif (need_close)\n+\t\tclose(fd);\n+}\ndiff --git a/write_or_die.c b/write_or_die.c\nindex ab4cb8a..bfe4eeb 100644\n--- a/write_or_die.c\n+++ b/write_or_die.c\n@@ -18,3 +18,28 @@ void write_or_die(int fd, const void *bu\n \t\tp += written;\n \t}\n }\n+\n+int write_or_whine(int fd, const void *buf, size_t count, const char *msg)\n+{\n+\tconst char *p = buf;\n+\tssize_t written;\n+\n+\twhile (count > 0) {\n+\t\twritten = xwrite(fd, p, count);\n+\t\tif (written == 0) {\n+\t\t\tfprintf(stderr, \"%s: disk full?\\n\", msg);\n+\t\t\treturn 0;\n+\t\t}\n+\t\telse if (written < 0) {\n+\t\t\tif (errno == EPIPE)\n+\t\t\t\texit(0);\n+\t\t\tfprintf(stderr, \"%s: write error (%s)\\n\",\n+\t\t\t\tmsg, strerror(errno));\n+\t\t\treturn 0;\n+\t\t}\n+\t\tcount -= written;\n+\t\tp += written;\n+\t}\n+\n+\treturn 1;\n+}\n-- \n1.4.2.g0f2c-dirty\n\n\n-- \nVGER BF report: U 0.5\n"}]}