]> Pileus Git - ~andy/git/commitdiff
add packet tracing debug code
authorJeff King <peff@peff.net>
Thu, 24 Feb 2011 14:30:19 +0000 (09:30 -0500)
committerJunio C Hamano <gitster@pobox.com>
Tue, 8 Mar 2011 20:12:04 +0000 (12:12 -0800)
This shows a trace of all packets coming in or out of a given
program. This can help with debugging object negotiation or
other protocol issues.

To keep the code changes simple, we operate at the lowest
level, meaning we don't necessarily understand what's in the
packets. The one exception is a packet starting with "PACK",
which causes us to skip that packet and turn off tracing
(since the gigantic pack data will not be interesting to
read, at least not in the trace format).

We show both written and read packets. In the local case,
this may mean you will see packets twice (written by the
sender and read by the receiver). However, for cases where
the other end is remote, this allows you to see the full
conversation.

Packet tracing can be enabled with GIT_TRACE_PACKET=<foo>,
where <foo> takes the same arguments as GIT_TRACE.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/clone.c
builtin/fetch-pack.c
builtin/fetch.c
builtin/push.c
builtin/receive-pack.c
cache.h
pkt-line.c
upload-pack.c

index 60d9a64280b0318b1248b229d2ff99b27eb0e5c6..38b4b71cd28065d5eea2443745d20343b93324e8 100644 (file)
@@ -383,6 +383,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 
        junk_pid = getpid();
 
+       packet_trace_identity("clone");
        argc = parse_options(argc, argv, prefix, builtin_clone_options,
                             builtin_clone_usage, 0);
 
index b9994139345834a58b08a5ce57cf59c124e21760..272bc383d6de5c19970f7c65b924551d9b76598a 100644 (file)
@@ -804,6 +804,8 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
        char **pack_lockfile_ptr = NULL;
        struct child_process *conn;
 
+       packet_trace_identity("fetch-pack");
+
        nr_heads = 0;
        heads = NULL;
        for (i = 1; i < argc; i++) {
index 357f3cdbbfd601e2ce3f1261a4d6b30c1257cda4..e94e0015c9c3ae2f638cad686d78b3fa16be2231 100644 (file)
@@ -906,6 +906,8 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
        struct remote *remote;
        int result = 0;
 
+       packet_trace_identity("fetch");
+
        /* Record the command line for the reflog */
        strbuf_addstr(&default_rla, "fetch");
        for (i = 1; i < argc; i++)
index e655eb7695faba13c4d9e8f25b9649ffec7195be..26171ff50fa91eb57594004d5dc55670f08a6257 100644 (file)
@@ -228,6 +228,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
                OPT_END()
        };
 
+       packet_trace_identity("push");
        git_config(git_default_config, NULL);
        argc = parse_options(argc, argv, prefix, options, push_usage, 0);
 
index 760817dbd7fec0086a0a1b62e58ab4438f227b7b..5fa4be8fb4265d903903fbf19dc6c7ce19a8eed4 100644 (file)
@@ -778,6 +778,8 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
        char *dir = NULL;
        struct command *commands;
 
+       packet_trace_identity("receive-pack");
+
        argv++;
        for (i = 1; i < argc; i++) {
                const char *arg = *argv++;
diff --git a/cache.h b/cache.h
index 3978112f55e258bf67fef16dcbed2dd49feb4552..2e59aae179ec4b19b9773bcafc5eb021568c0f00 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -1074,6 +1074,8 @@ extern void trace_repo_setup(const char *prefix);
 extern int trace_want(const char *key);
 extern void trace_strbuf(const char *key, const struct strbuf *buf);
 
+void packet_trace_identity(const char *prog);
+
 /* convert.c */
 /* returns 1 if *dst was used */
 extern int convert_to_git(const char *path, const char *src, size_t len,
index 295ba2b16c8675d8c5c5d0a5ee6e61e044ce99dd..cd1bd264139bf15cca06f900b7921f0956c6458b 100644 (file)
@@ -1,6 +1,51 @@
 #include "cache.h"
 #include "pkt-line.h"
 
+const char *packet_trace_prefix = "git";
+static const char trace_key[] = "GIT_TRACE_PACKET";
+
+void packet_trace_identity(const char *prog)
+{
+       packet_trace_prefix = xstrdup(prog);
+}
+
+static void packet_trace(const char *buf, unsigned int len, int write)
+{
+       int i;
+       struct strbuf out;
+
+       if (!trace_want(trace_key))
+               return;
+
+       /* +32 is just a guess for header + quoting */
+       strbuf_init(&out, len+32);
+
+       strbuf_addf(&out, "packet: %12s%c ",
+                   packet_trace_prefix, write ? '>' : '<');
+
+       if ((len >= 4 && !prefixcmp(buf, "PACK")) ||
+           (len >= 5 && !prefixcmp(buf+1, "PACK"))) {
+               strbuf_addstr(&out, "PACK ...");
+               unsetenv(trace_key);
+       }
+       else {
+               /* XXX we should really handle printable utf8 */
+               for (i = 0; i < len; i++) {
+                       /* suppress newlines */
+                       if (buf[i] == '\n')
+                               continue;
+                       if (buf[i] >= 0x20 && buf[i] <= 0x7e)
+                               strbuf_addch(&out, buf[i]);
+                       else
+                               strbuf_addf(&out, "\\%o", buf[i]);
+               }
+       }
+
+       strbuf_addch(&out, '\n');
+       trace_strbuf(trace_key, &out);
+       strbuf_release(&out);
+}
+
 /*
  * Write a packetized stream, where each line is preceded by
  * its length (including the header) as a 4-byte hex number.
@@ -39,11 +84,13 @@ ssize_t safe_write(int fd, const void *buf, ssize_t n)
  */
 void packet_flush(int fd)
 {
+       packet_trace("0000", 4, 1);
        safe_write(fd, "0000", 4);
 }
 
 void packet_buf_flush(struct strbuf *buf)
 {
+       packet_trace("0000", 4, 1);
        strbuf_add(buf, "0000", 4);
 }
 
@@ -62,6 +109,7 @@ static unsigned format_packet(const char *fmt, va_list args)
        buffer[1] = hex(n >> 8);
        buffer[2] = hex(n >> 4);
        buffer[3] = hex(n);
+       packet_trace(buffer+4, n-4, 1);
        return n;
 }
 
@@ -130,13 +178,16 @@ int packet_read_line(int fd, char *buffer, unsigned size)
        len = packet_length(linelen);
        if (len < 0)
                die("protocol error: bad line length character: %.4s", linelen);
-       if (!len)
+       if (!len) {
+               packet_trace("0000", 4, 0);
                return 0;
+       }
        len -= 4;
        if (len >= size)
                die("protocol error: bad line length %d", len);
        safe_read(fd, buffer, len);
        buffer[len] = 0;
+       packet_trace(buffer, len, 0);
        return len;
 }
 
@@ -153,6 +204,7 @@ int packet_get_line(struct strbuf *out,
        if (!len) {
                *src_buf += 4;
                *src_len -= 4;
+               packet_trace("0000", 4, 0);
                return 0;
        }
        if (*src_len < len)
@@ -165,5 +217,6 @@ int packet_get_line(struct strbuf *out,
        strbuf_add(out, *src_buf, len);
        *src_buf += len;
        *src_len -= len;
+       packet_trace(out->buf, out->len, 0);
        return len;
 }
index b40a43f27d0e16f0305153f984741938b3a6ab89..0c87bc00f0e06e57b10154a026d797fb1500b4fe 100644 (file)
@@ -682,6 +682,7 @@ int main(int argc, char **argv)
        int i;
        int strict = 0;
 
+       packet_trace_identity("upload-pack");
        git_extract_argv0_path(argv[0]);
        read_replace_refs = 0;