From 3a0942598ce33b195bfaaf250b2da23e4eceb3c6 Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Sat, 8 May 2010 17:18:06 +0200 Subject: Do not call release_pack_memory in malloc wrappers when GIT_TRACE is used This avoids a potential race condition when async procedures are implemented as threads where release_pack_memory() can be called from different threads without locking under memory pressure. Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- trace.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'trace.c') diff --git a/trace.c b/trace.c index 4229ae123..1e560cb0b 100644 --- a/trace.c +++ b/trace.c @@ -25,6 +25,10 @@ #include "cache.h" #include "quote.h" +void do_nothing(size_t unused) +{ +} + /* Get a trace file descriptor from GIT_TRACE env variable. */ static int get_trace_fd(int *need_close) { @@ -72,6 +76,7 @@ void trace_printf(const char *fmt, ...) if (!fd) return; + set_try_to_free_routine(do_nothing); /* is never reset */ strbuf_init(&buf, 64); va_start(ap, fmt); len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap); @@ -103,6 +108,7 @@ void trace_argv_printf(const char **argv, const char *fmt, ...) if (!fd) return; + set_try_to_free_routine(do_nothing); /* is never reset */ strbuf_init(&buf, 64); va_start(ap, fmt); len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap); -- cgit v1.2.1 From a9ca8a85ef493aac947f9da44a4fbb5724768897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Fri, 26 Nov 2010 22:31:57 +0700 Subject: builtins: print setup info if repo is found MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- trace.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'trace.c') diff --git a/trace.c b/trace.c index 1e560cb0b..bdb5d2fa7 100644 --- a/trace.c +++ b/trace.c @@ -131,3 +131,45 @@ void trace_argv_printf(const char **argv, const char *fmt, ...) if (need_close) close(fd); } + +static const char *quote_crnl(const char *path) +{ + static char new_path[PATH_MAX]; + const char *p2 = path; + char *p1 = new_path; + + if (!path) + return NULL; + + while (*p2) { + switch (*p2) { + case '\\': *p1++ = '\\'; *p1++ = '\\'; break; + case '\n': *p1++ = '\\'; *p1++ = 'n'; break; + case '\r': *p1++ = '\\'; *p1++ = 'r'; break; + default: + *p1++ = *p2; + } + p2++; + } + *p1 = '\0'; + return new_path; +} + +/* FIXME: move prefix to startup_info struct and get rid of this arg */ +void trace_repo_setup(const char *prefix) +{ + char cwd[PATH_MAX]; + char *trace = getenv("GIT_TRACE"); + + if (!trace || !strcmp(trace, "") || + !strcmp(trace, "0") || !strcasecmp(trace, "false")) + return; + + if (!getcwd(cwd, PATH_MAX)) + die("Unable to get current working directory"); + + trace_printf("setup: git_dir: %s\n", quote_crnl(get_git_dir())); + trace_printf("setup: worktree: %s\n", quote_crnl(get_git_work_tree())); + trace_printf("setup: cwd: %s\n", quote_crnl(cwd)); + trace_printf("setup: prefix: %s\n", quote_crnl(prefix)); +} -- cgit v1.2.1 From 8e8aa44057b1f92bad43a0bd3a2d448e9cb45b18 Mon Sep 17 00:00:00 2001 From: Vasyl' Vavrychuk Date: Fri, 17 Dec 2010 00:38:42 +0200 Subject: trace.c: mark file-local function static Signed-off-by: Vasyl' Vavrychuk Signed-off-by: Junio C Hamano --- trace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'trace.c') diff --git a/trace.c b/trace.c index 1e560cb0b..62586fa37 100644 --- a/trace.c +++ b/trace.c @@ -25,7 +25,7 @@ #include "cache.h" #include "quote.h" -void do_nothing(size_t unused) +static void do_nothing(size_t unused) { } -- cgit v1.2.1 From 00b0d7f77b65ed2d059f893bbd2011ba5bb4252d Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 21 Dec 2010 09:24:18 -0800 Subject: set_try_to_free_routine(NULL) means "do nothing special" This way, the next caller that wants to disable our memory reclamation machinery does not have to define its own do_nothing() stub. Signed-off-by: Junio C Hamano --- trace.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'trace.c') diff --git a/trace.c b/trace.c index 62586fa37..0fb2a2c64 100644 --- a/trace.c +++ b/trace.c @@ -25,10 +25,6 @@ #include "cache.h" #include "quote.h" -static void do_nothing(size_t unused) -{ -} - /* Get a trace file descriptor from GIT_TRACE env variable. */ static int get_trace_fd(int *need_close) { @@ -76,7 +72,7 @@ void trace_printf(const char *fmt, ...) if (!fd) return; - set_try_to_free_routine(do_nothing); /* is never reset */ + set_try_to_free_routine(NULL); /* is never reset */ strbuf_init(&buf, 64); va_start(ap, fmt); len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap); @@ -108,7 +104,7 @@ void trace_argv_printf(const char **argv, const char *fmt, ...) if (!fd) return; - set_try_to_free_routine(do_nothing); /* is never reset */ + set_try_to_free_routine(NULL); /* is never reset */ strbuf_init(&buf, 64); va_start(ap, fmt); len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap); -- cgit v1.2.1 From e83c267f71dfef72926807cd69aa8d570e57714e Mon Sep 17 00:00:00 2001 From: Brandon Casey Date: Wed, 5 Jan 2011 18:30:01 -0600 Subject: trace.c: ensure NULL is not passed to printf GNU printf, and many others, will print the string "(null)" if a NULL pointer is passed as the argument to a "%s" format specifier. Some implementations (like on Solaris) do not detect a NULL pointer and will produce a segfault in this case. So, fix this by ensuring that pointer variables do not contain the value NULL. Assign the string "(null)" to the variables are NULL. Signed-off-by: Brandon Casey Signed-off-by: Junio C Hamano --- trace.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'trace.c') diff --git a/trace.c b/trace.c index 02279b82c..35d388dce 100644 --- a/trace.c +++ b/trace.c @@ -154,6 +154,7 @@ static const char *quote_crnl(const char *path) /* FIXME: move prefix to startup_info struct and get rid of this arg */ void trace_repo_setup(const char *prefix) { + const char *git_work_tree; char cwd[PATH_MAX]; char *trace = getenv("GIT_TRACE"); @@ -164,8 +165,14 @@ void trace_repo_setup(const char *prefix) if (!getcwd(cwd, PATH_MAX)) die("Unable to get current working directory"); + if (!(git_work_tree = get_git_work_tree())) + git_work_tree = "(null)"; + + if (!prefix) + prefix = "(null)"; + trace_printf("setup: git_dir: %s\n", quote_crnl(get_git_dir())); - trace_printf("setup: worktree: %s\n", quote_crnl(get_git_work_tree())); + trace_printf("setup: worktree: %s\n", quote_crnl(git_work_tree)); trace_printf("setup: cwd: %s\n", quote_crnl(cwd)); trace_printf("setup: prefix: %s\n", quote_crnl(prefix)); } -- cgit v1.2.1 From ebeb60900fbab569ed14f710a0a1abb1637ec792 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Fri, 25 Feb 2011 23:08:53 -0600 Subject: strbuf: add strbuf_vaddf In a variable-args function, the code for writing into a strbuf is non-trivial. We ended up cutting and pasting it in several places because there was no vprintf-style function for strbufs (which in turn was held up by a lack of va_copy). Now that we have a fallback va_copy, we can add strbuf_vaddf, the strbuf equivalent of vsprintf. And we can clean up the cut and paste mess. Signed-off-by: Jeff King Improved-by: Christian Couder Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- trace.c | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) (limited to 'trace.c') diff --git a/trace.c b/trace.c index 35d388dce..eda3f6d72 100644 --- a/trace.c +++ b/trace.c @@ -64,28 +64,18 @@ static const char err_msg[] = "Could not trace into fd given by " void trace_printf(const char *fmt, ...) { - struct strbuf buf; + struct strbuf buf = STRBUF_INIT; va_list ap; - int fd, len, need_close = 0; + int fd, need_close = 0; fd = get_trace_fd(&need_close); if (!fd) return; set_try_to_free_routine(NULL); /* is never reset */ - strbuf_init(&buf, 64); va_start(ap, fmt); - len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap); + strbuf_vaddf(&buf, fmt, ap); va_end(ap); - if (len >= strbuf_avail(&buf)) { - strbuf_grow(&buf, len - strbuf_avail(&buf) + 128); - va_start(ap, fmt); - len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap); - va_end(ap); - if (len >= strbuf_avail(&buf)) - die("broken vsnprintf"); - } - strbuf_setlen(&buf, len); write_or_whine_pipe(fd, buf.buf, buf.len, err_msg); strbuf_release(&buf); @@ -96,28 +86,18 @@ void trace_printf(const char *fmt, ...) void trace_argv_printf(const char **argv, const char *fmt, ...) { - struct strbuf buf; + struct strbuf buf = STRBUF_INIT; va_list ap; - int fd, len, need_close = 0; + int fd, need_close = 0; fd = get_trace_fd(&need_close); if (!fd) return; set_try_to_free_routine(NULL); /* is never reset */ - strbuf_init(&buf, 64); va_start(ap, fmt); - len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap); + strbuf_vaddf(&buf, fmt, ap); va_end(ap); - if (len >= strbuf_avail(&buf)) { - strbuf_grow(&buf, len - strbuf_avail(&buf) + 128); - va_start(ap, fmt); - len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap); - va_end(ap); - if (len >= strbuf_avail(&buf)) - die("broken vsnprintf"); - } - strbuf_setlen(&buf, len); sq_quote_argv(&buf, argv, 0); strbuf_addch(&buf, '\n'); -- cgit v1.2.1 From c6053543f288f503b39e946ef58bfcd59f935b5f Mon Sep 17 00:00:00 2001 From: Jeff King Date: Thu, 24 Feb 2011 09:28:15 -0500 Subject: trace: add trace_vprintf This is a necessary cleanup to adding new types of trace functions. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- trace.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'trace.c') diff --git a/trace.c b/trace.c index eda3f6d72..623e793e4 100644 --- a/trace.c +++ b/trace.c @@ -62,10 +62,9 @@ static int get_trace_fd(int *need_close) static const char err_msg[] = "Could not trace into fd given by " "GIT_TRACE environment variable"; -void trace_printf(const char *fmt, ...) +void trace_vprintf(const char *fmt, va_list ap) { struct strbuf buf = STRBUF_INIT; - va_list ap; int fd, need_close = 0; fd = get_trace_fd(&need_close); @@ -73,10 +72,7 @@ void trace_printf(const char *fmt, ...) return; set_try_to_free_routine(NULL); /* is never reset */ - va_start(ap, fmt); strbuf_vaddf(&buf, fmt, ap); - va_end(ap); - write_or_whine_pipe(fd, buf.buf, buf.len, err_msg); strbuf_release(&buf); @@ -84,6 +80,14 @@ void trace_printf(const char *fmt, ...) close(fd); } +void trace_printf(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + trace_vprintf(fmt, ap); + va_end(ap); +} + void trace_argv_printf(const char **argv, const char *fmt, ...) { struct strbuf buf = STRBUF_INIT; -- cgit v1.2.1 From 06796607ef557e8913f1797cca3c98ce4844c36c Mon Sep 17 00:00:00 2001 From: Jeff King Date: Thu, 24 Feb 2011 09:28:41 -0500 Subject: trace: refactor to support multiple env variables Right now you turn all tracing off and on with GIT_TRACE. To support new types of tracing without forcing the user to see all of them, we will soon support turning each tracing area on with GIT_TRACE_*. This patch lays the groundwork by providing an interface which does not assume GIT_TRACE. However, we still maintain the trace_printf interface so that existing callers do not need to be refactored. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- trace.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'trace.c') diff --git a/trace.c b/trace.c index 623e793e4..1d0e17e01 100644 --- a/trace.c +++ b/trace.c @@ -25,10 +25,10 @@ #include "cache.h" #include "quote.h" -/* Get a trace file descriptor from GIT_TRACE env variable. */ -static int get_trace_fd(int *need_close) +/* Get a trace file descriptor from "key" env variable. */ +static int get_trace_fd(const char *key, int *need_close) { - char *trace = getenv("GIT_TRACE"); + char *trace = getenv(key); if (!trace || !strcmp(trace, "") || !strcmp(trace, "0") || !strcasecmp(trace, "false")) @@ -50,10 +50,10 @@ static int get_trace_fd(int *need_close) return fd; } - fprintf(stderr, "What does '%s' for GIT_TRACE mean?\n", trace); + fprintf(stderr, "What does '%s' for %s mean?\n", trace, key); fprintf(stderr, "If you want to trace into a file, " - "then please set GIT_TRACE to an absolute pathname " - "(starting with /).\n"); + "then please set %s to an absolute pathname " + "(starting with /).\n", key); fprintf(stderr, "Defaulting to tracing on stderr...\n"); return STDERR_FILENO; @@ -62,12 +62,12 @@ static int get_trace_fd(int *need_close) static const char err_msg[] = "Could not trace into fd given by " "GIT_TRACE environment variable"; -void trace_vprintf(const char *fmt, va_list ap) +void trace_vprintf(const char *key, const char *fmt, va_list ap) { struct strbuf buf = STRBUF_INIT; int fd, need_close = 0; - fd = get_trace_fd(&need_close); + fd = get_trace_fd(key, &need_close); if (!fd) return; @@ -80,11 +80,19 @@ void trace_vprintf(const char *fmt, va_list ap) close(fd); } +void trace_printf_key(const char *key, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + trace_vprintf(key, fmt, ap); + va_end(ap); +} + void trace_printf(const char *fmt, ...) { va_list ap; va_start(ap, fmt); - trace_vprintf(fmt, ap); + trace_vprintf("GIT_TRACE", fmt, ap); va_end(ap); } @@ -94,7 +102,7 @@ void trace_argv_printf(const char **argv, const char *fmt, ...) va_list ap; int fd, need_close = 0; - fd = get_trace_fd(&need_close); + fd = get_trace_fd("GIT_TRACE", &need_close); if (!fd) return; -- cgit v1.2.1 From 39bc5e4680a1ed7192968fbe9f5784ad56ecbd36 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Thu, 24 Feb 2011 09:28:59 -0500 Subject: trace: factor out "do we want to trace" logic As we add more tracing areas, this will avoid repeated code. Technically, trace_printf already checks this and will avoid printing if the trace key is not set. However, callers may want to find out early whether or not tracing is enabled so they can avoid doing work in the common non-trace case. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- trace.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'trace.c') diff --git a/trace.c b/trace.c index 1d0e17e01..ca0ab0448 100644 --- a/trace.c +++ b/trace.c @@ -148,10 +148,8 @@ void trace_repo_setup(const char *prefix) { const char *git_work_tree; char cwd[PATH_MAX]; - char *trace = getenv("GIT_TRACE"); - if (!trace || !strcmp(trace, "") || - !strcmp(trace, "0") || !strcasecmp(trace, "false")) + if (!trace_want("GIT_TRACE")) return; if (!getcwd(cwd, PATH_MAX)) @@ -168,3 +166,13 @@ void trace_repo_setup(const char *prefix) trace_printf("setup: cwd: %s\n", quote_crnl(cwd)); trace_printf("setup: prefix: %s\n", quote_crnl(prefix)); } + +int trace_want(const char *key) +{ + const char *trace = getenv(key); + + if (!trace || !strcmp(trace, "") || + !strcmp(trace, "0") || !strcasecmp(trace, "false")) + return 0; + return 1; +} -- cgit v1.2.1 From 94b3b3746456949d834ec7bf454da3db4eb439cf Mon Sep 17 00:00:00 2001 From: Jeff King Date: Thu, 24 Feb 2011 09:29:50 -0500 Subject: trace: add trace_strbuf If you happen to have a strbuf, it is a little more readable and a little more efficient to be able to print it directly instead of jamming it through the trace_printf interface. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- trace.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'trace.c') diff --git a/trace.c b/trace.c index ca0ab0448..9f39eab02 100644 --- a/trace.c +++ b/trace.c @@ -65,19 +65,14 @@ static const char err_msg[] = "Could not trace into fd given by " void trace_vprintf(const char *key, const char *fmt, va_list ap) { struct strbuf buf = STRBUF_INIT; - int fd, need_close = 0; - fd = get_trace_fd(key, &need_close); - if (!fd) + if (!trace_want(key)) return; set_try_to_free_routine(NULL); /* is never reset */ strbuf_vaddf(&buf, fmt, ap); - write_or_whine_pipe(fd, buf.buf, buf.len, err_msg); + trace_strbuf(key, &buf); strbuf_release(&buf); - - if (need_close) - close(fd); } void trace_printf_key(const char *key, const char *fmt, ...) @@ -96,6 +91,20 @@ void trace_printf(const char *fmt, ...) va_end(ap); } +void trace_strbuf(const char *key, const struct strbuf *buf) +{ + int fd, need_close = 0; + + fd = get_trace_fd(key, &need_close); + if (!fd) + return; + + write_or_whine_pipe(fd, buf->buf, buf->len, err_msg); + + if (need_close) + close(fd); +} + void trace_argv_printf(const char **argv, const char *fmt, ...) { struct strbuf buf = STRBUF_INIT; -- cgit v1.2.1 From 7e2342d0987f7f78bc8582e97bbfbafd80d327ef Mon Sep 17 00:00:00 2001 From: Jeff King Date: Thu, 24 Feb 2011 09:30:30 -0500 Subject: trace: give repo_setup trace its own key You no longer get this output with GIT_TRACE=1; instead, you can do GIT_TRACE_SETUP=1. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- trace.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'trace.c') diff --git a/trace.c b/trace.c index 9f39eab02..8390bf7cb 100644 --- a/trace.c +++ b/trace.c @@ -155,10 +155,11 @@ static const char *quote_crnl(const char *path) /* FIXME: move prefix to startup_info struct and get rid of this arg */ void trace_repo_setup(const char *prefix) { + static const char *key = "GIT_TRACE_SETUP"; const char *git_work_tree; char cwd[PATH_MAX]; - if (!trace_want("GIT_TRACE")) + if (!trace_want(key)) return; if (!getcwd(cwd, PATH_MAX)) @@ -170,10 +171,10 @@ void trace_repo_setup(const char *prefix) if (!prefix) prefix = "(null)"; - trace_printf("setup: git_dir: %s\n", quote_crnl(get_git_dir())); - trace_printf("setup: worktree: %s\n", quote_crnl(git_work_tree)); - trace_printf("setup: cwd: %s\n", quote_crnl(cwd)); - trace_printf("setup: prefix: %s\n", quote_crnl(prefix)); + trace_printf_key(key, "setup: git_dir: %s\n", quote_crnl(get_git_dir())); + trace_printf_key(key, "setup: worktree: %s\n", quote_crnl(git_work_tree)); + trace_printf_key(key, "setup: cwd: %s\n", quote_crnl(cwd)); + trace_printf_key(key, "setup: prefix: %s\n", quote_crnl(prefix)); } int trace_want(const char *key) -- cgit v1.2.1 From c2e86addb86689306b992065328ec52aa2479658 Mon Sep 17 00:00:00 2001 From: Stephen Boyd Date: Tue, 22 Mar 2011 00:51:05 -0700 Subject: Fix sparse warnings Fix warnings from 'make check'. - These files don't include 'builtin.h' causing sparse to complain that cmd_* isn't declared: builtin/clone.c:364, builtin/fetch-pack.c:797, builtin/fmt-merge-msg.c:34, builtin/hash-object.c:78, builtin/merge-index.c:69, builtin/merge-recursive.c:22 builtin/merge-tree.c:341, builtin/mktag.c:156, builtin/notes.c:426 builtin/notes.c:822, builtin/pack-redundant.c:596, builtin/pack-refs.c:10, builtin/patch-id.c:60, builtin/patch-id.c:149, builtin/remote.c:1512, builtin/remote-ext.c:240, builtin/remote-fd.c:53, builtin/reset.c:236, builtin/send-pack.c:384, builtin/unpack-file.c:25, builtin/var.c:75 - These files have symbols which should be marked static since they're only file scope: submodule.c:12, diff.c:631, replace_object.c:92, submodule.c:13, submodule.c:14, trace.c:78, transport.c:195, transport-helper.c:79, unpack-trees.c:19, url.c:3, url.c:18, url.c:104, url.c:117, url.c:123, url.c:129, url.c:136, thread-utils.c:21, thread-utils.c:48 - These files redeclare symbols to be different types: builtin/index-pack.c:210, parse-options.c:564, parse-options.c:571, usage.c:49, usage.c:58, usage.c:63, usage.c:72 - These files use a literal integer 0 when they really should use a NULL pointer: daemon.c:663, fast-import.c:2942, imap-send.c:1072, notes-merge.c:362 While we're in the area, clean up some unused #includes in builtin files (mostly exec_cmd.h). Signed-off-by: Stephen Boyd Signed-off-by: Junio C Hamano --- trace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'trace.c') diff --git a/trace.c b/trace.c index 8390bf7cb..d95341693 100644 --- a/trace.c +++ b/trace.c @@ -75,7 +75,7 @@ void trace_vprintf(const char *key, const char *fmt, va_list ap) strbuf_release(&buf); } -void trace_printf_key(const char *key, const char *fmt, ...) +static void trace_printf_key(const char *key, const char *fmt, ...) { va_list ap; va_start(ap, fmt); -- cgit v1.2.1