summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilliam Cohen <wcohen@redhat.com>2011-03-22 13:07:32 -0400
committerWilliam Cohen <wcohen@redhat.com>2011-03-22 13:07:32 -0400
commit99f78395ec302ce5800c5c6f4739e7d5a193665b (patch)
tree42f6a4bd398e9ca960aa81b2220820ca865926b6
parentea107f58cd9624042aef6581d082c01065391159 (diff)
downloadmemstomp-99f78395ec302ce5800c5c6f4739e7d5a193665b.tar.gz
memstomp-99f78395ec302ce5800c5c6f4739e7d5a193665b.tar.xz
memstomp-99f78395ec302ce5800c5c6f4739e7d5a193665b.zip
Revert "Format output using iovec+writev instead of malloc+copy."
This reverts commit ea107f58cd9624042aef6581d082c01065391159.
-rw-r--r--memstomp.c52
1 files changed, 38 insertions, 14 deletions
diff --git a/memstomp.c b/memstomp.c
index b9dfafa..72817d8 100644
--- a/memstomp.c
+++ b/memstomp.c
@@ -202,9 +202,7 @@ static bool verify_frame(const char *s) {
return true;
}
-#include <sys/uio.h>
-
-static void generate_stacktrace(int const fd)
+static char* generate_stacktrace(void)
{
void *retaddr[frames_max]; /* c99 or gcc extension */
int const n = real_backtrace(retaddr, frames_max);
@@ -213,18 +211,42 @@ static void generate_stacktrace(int const fd)
char **const strings = real_backtrace_symbols(retaddr, n);
assert(strings);
- struct iovec iov[3];
- iov[0].iov_base = "\t";
- iov[0].iov_len = 1;
- iov[2].iov_base = "\n";
- iov[2].iov_len = 1;
-
+ char *ret;
+ {
+ size_t k = 0;
+ int i;
+ for (i = 0; i < n; i++)
+ k += strlen(strings[i]) + 2;
+
+ ret = malloc(k + 1);
+ }
+ assert(ret);
+
+ char *p;
int i;
- for (i = 0; i < n; i++) if (strings[i]) {
- iov[1].iov_base = strings[i];
- iov[1].iov_len = strlen(strings[i]);
- writev(fd, iov, 3);
+ bool b = false;
+ for (i = 0, p = ret; i < n; i++) {
+ if (!b && !verify_frame(strings[i]))
+ continue;
+
+ if (!b && i > 0) {
+ /* Skip all but the first stack frame of ours */
+ *(p++) = '\t';
+ strcpy(p, strings[i-1]);
+ p += strlen(strings[i-1]);
+ *(p++) = '\n';
+ }
+
+ b = true;
+
+ *(p++) = '\t';
+ strcpy(p, strings[i]);
+ p += strlen(strings[i]);
+ *(p++) = '\n';
}
+ *p = 0;
+ free(strings);
+ return ret;
}
int backtrace(void **array, int size)
@@ -268,7 +290,9 @@ static void warn_memcpy(void * dest, const void * src, size_t count)
* then this is not async signal safe. But the write() above will produce
* some evidence before any possible trouble below.
*/
- generate_stacktrace(STDERR_FILENO);
+ char *const info = generate_stacktrace();
+ fprintf(stderr, "%s", info);
+ free(info);
}