Commit 749b9887 authored by Monty's avatar Monty

Fixed memory leaks in resolve_stack_dump

- Remove memory leaks reported by safemalloc
- Changed that all 0x strings are converted. This is needed
  to easily be able to resolve safemalloc backtraces
parent 48eda61c
...@@ -53,7 +53,7 @@ static struct my_option my_long_options[] = ...@@ -53,7 +53,7 @@ static struct my_option my_long_options[] =
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
{"version", 'V', "Output version information and exit.", {"version", 'V', "Output version information and exit.",
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
{"symbols-file", 's', "Use specified symbols file.", &sym_fname, {"symbols-file", 's', "Use specified symbols file", &sym_fname,
&sym_fname, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, &sym_fname, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{"numeric-dump-file", 'n', "Read the dump from specified file.", {"numeric-dump-file", 'n', "Read the dump from specified file.",
&dump_fname, &dump_fname, 0, GET_STR, REQUIRED_ARG, &dump_fname, &dump_fname, 0, GET_STR, REQUIRED_ARG,
...@@ -63,7 +63,7 @@ static struct my_option my_long_options[] = ...@@ -63,7 +63,7 @@ static struct my_option my_long_options[] =
static void verify_sort(); static void verify_sort();
static void clean_up();
static void print_version(void) static void print_version(void)
{ {
...@@ -97,9 +97,18 @@ static void die(const char* fmt, ...) ...@@ -97,9 +97,18 @@ static void die(const char* fmt, ...)
vfprintf(stderr, fmt, args); vfprintf(stderr, fmt, args);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
va_end(args); va_end(args);
clean_up();
my_end(0);
exit(1); exit(1);
} }
void local_exit(int error)
{
clean_up();
my_end(0);
exit(error);
}
static my_bool static my_bool
get_one_option(int optid, const struct my_option *opt __attribute__((unused)), get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
...@@ -108,10 +117,12 @@ get_one_option(int optid, const struct my_option *opt __attribute__((unused)), ...@@ -108,10 +117,12 @@ get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
switch(optid) { switch(optid) {
case 'V': case 'V':
print_version(); print_version();
exit(0); local_exit(0);
break;
case '?': case '?':
usage(); usage();
exit(0); local_exit(0);
break;
} }
return 0; return 0;
} }
...@@ -122,7 +133,7 @@ static int parse_args(int argc, char **argv) ...@@ -122,7 +133,7 @@ static int parse_args(int argc, char **argv)
int ho_error; int ho_error;
if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option))) if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
exit(ho_error); local_exit(ho_error);
/* /*
The following code is to make the command compatible with the old The following code is to make the command compatible with the old
...@@ -143,13 +154,13 @@ static int parse_args(int argc, char **argv) ...@@ -143,13 +154,13 @@ static int parse_args(int argc, char **argv)
else else
{ {
usage(); usage();
exit(1); local_exit(1);
} }
} }
else if (argc != 0 || !sym_fname) else if (argc != 0 || !sym_fname)
{ {
usage(); usage();
exit(1); local_exit(1);
} }
return 0; return 0;
} }
...@@ -242,6 +253,10 @@ static void init_sym_table() ...@@ -242,6 +253,10 @@ static void init_sym_table()
static void clean_up() static void clean_up()
{ {
delete_dynamic(&sym_table); delete_dynamic(&sym_table);
if (fp_dump && fp_dump != stdin)
my_fclose(fp_dump, MYF(0));
if (fp_sym)
my_fclose(fp_sym, MYF(0));
} }
static void verify_sort() static void verify_sort()
...@@ -283,7 +298,7 @@ static SYM_ENTRY* resolve_addr(uchar* addr, SYM_ENTRY* se) ...@@ -283,7 +298,7 @@ static SYM_ENTRY* resolve_addr(uchar* addr, SYM_ENTRY* se)
/* /*
Resolve anything that starts with [0x or (+0x or start of line and 0x Resolve anything that starts with [0x or (+0x or 0x
Skip '_end' as this is an indication of a wrong symbol (stack?) Skip '_end' as this is an indication of a wrong symbol (stack?)
*/ */
...@@ -299,9 +314,7 @@ static void do_resolve() ...@@ -299,9 +314,7 @@ static void do_resolve()
found= 3; found= 3;
if (p[0] == '(' && p[1] == '+' && p[2] == '0' && p[3] == 'x') if (p[0] == '(' && p[1] == '+' && p[2] == '0' && p[3] == 'x')
found= 4; found= 4;
if (p[0] == '0' && p[1] == 'x')
/* For stdin */
if (p == buf && p[0] == '0' && p[1] == 'x')
found= 2; found= 2;
if (found) if (found)
...@@ -312,14 +325,15 @@ static void do_resolve() ...@@ -312,14 +325,15 @@ static void do_resolve()
addr= (uchar*)read_addr(&tmp); addr= (uchar*)read_addr(&tmp);
if (resolve_addr(addr, &se) && strcmp(se.symbol, "_end")) if (resolve_addr(addr, &se) && strcmp(se.symbol, "_end"))
{ {
fprintf(fp_out, "%c%p %s + %d", *p, addr, se.symbol, found-= 2; /* Don't print 0x as it's added by %p */
(int) (addr - se.addr)); while (found--)
fputc(*p++, stdout);
fprintf(fp_out, "%p %s + %d", addr,
se.symbol, (int) (addr - se.addr));
p= tmp-1; p= tmp-1;
} }
else else
{
fputc(*p, stdout); fputc(*p, stdout);
}
} }
else else
fputc(*p, stdout); fputc(*p, stdout);
...@@ -336,5 +350,6 @@ int main(int argc, char** argv) ...@@ -336,5 +350,6 @@ int main(int argc, char** argv)
init_sym_table(); init_sym_table();
do_resolve(); do_resolve();
clean_up(); clean_up();
my_end(0);
return 0; return 0;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment