~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
77.1.39 by Monty Taylor
More mysql->drizzle renaming.
16
/* Resolve numeric stack dump produced by drizzled 3.23.30 and later
1 by brian
clean slate
17
   versions into symbolic names. By Sasha Pachev <sasha@mysql.com>
18
 */
19
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
20
#include "config.h"
1241.9.61 by Monty Taylor
No more mystrings in drizzled/
21
#include "drizzled/charset_info.h"
1241.9.64 by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal.
22
#include "drizzled/internal/my_sys.h"
23
#include "drizzled/internal/m_string.h"
1 by brian
clean slate
24
#include <errno.h>
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
25
#include "drizzled/my_getopt.h"
590.2.23 by Monty Taylor
Fixed a few things for solaris builds.
26
#include <stdio.h>
1 by brian
clean slate
27
28
#define INIT_SYM_TABLE  4096
29
#define INC_SYM_TABLE  4096
30
#define MAX_SYM_SIZE   128
31
#define DUMP_VERSION "1.4"
481 by Brian Aker
Remove all of uchar.
32
#define HEX_INVALID  (unsigned char)255
1 by brian
clean slate
33
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
34
extern "C" bool get_one_option(int optid, const struct my_option *, char *);
1 by brian
clean slate
35
36
typedef struct sym_entry
37
{
38
  char symbol[MAX_SYM_SIZE];
481 by Brian Aker
Remove all of uchar.
39
  unsigned char* addr;
1 by brian
clean slate
40
} SYM_ENTRY;
41
42
43
static char* dump_fname = 0, *sym_fname = 0;
44
static DYNAMIC_ARRAY sym_table; /* how do you like this , static DYNAMIC ? */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
45
static FILE* fp_dump, *fp_sym = 0, *fp_out;
1 by brian
clean slate
46
47
static struct my_option my_long_options[] =
48
{
49
  {"help", 'h', "Display this help and exit.",
50
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
51
  {"version", 'V', "Output version information and exit.",
52
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
77.1.78 by Monty Taylor
One last bunch of warnings edits.
53
  {"symbols-file", 's', "Use specified symbols file.", (char**) &sym_fname,
54
   (char**) &sym_fname, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1 by brian
clean slate
55
  {"numeric-dump-file", 'n', "Read the dump from specified file.",
77.1.78 by Monty Taylor
One last bunch of warnings edits.
56
   (char**) &dump_fname, (char**) &dump_fname, 0, GET_STR, REQUIRED_ARG,
1 by brian
clean slate
57
   0, 0, 0, 0, 0, 0},
58
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
59
};
60
61
77.1.44 by Monty Taylor
Fixed warnings in test
62
static void verify_sort(void);
1 by brian
clean slate
63
64
static void print_version(void)
65
{
1081.1.1 by Monty Taylor
Whole boat-load of build fixes.
66
  printf("%s  Ver %s Distrib %s, for %s-%s (%s)\n",my_progname,DUMP_VERSION,
67
	 VERSION,HOST_VENDOR,HOST_OS,HOST_CPU);
1 by brian
clean slate
68
}
69
70
77.1.44 by Monty Taylor
Fixed warnings in test
71
static void usage(void)
1 by brian
clean slate
72
{
73
  print_version();
74
  printf("MySQL AB, by Sasha Pachev\n");
75
  printf("This software comes with ABSOLUTELY NO WARRANTY\n\n");
76
  printf("Resolve numeric stack strace dump into symbols.\n\n");
77
  printf("Usage: %s [OPTIONS] symbols-file [numeric-dump-file]\n",
78
	 my_progname);
79
  my_print_help(my_long_options);
80
  my_print_variables(my_long_options);
81
  printf("\n\
77.1.39 by Monty Taylor
More mysql->drizzle renaming.
82
The symbols-file should include the output from:  'nm --numeric-sort drizzled'.\n\
83
The numeric-dump-file should contain a numeric stack trace from drizzled.\n\
1 by brian
clean slate
84
If the numeric-dump-file is not given, the stack trace is read from stdin.\n");
85
}
86
87
static void die(const char* fmt, ...)
88
{
89
  va_list args;
90
  va_start(args, fmt);
91
  fprintf(stderr, "%s: ", my_progname);
92
  vfprintf(stderr, fmt, args);
93
  fprintf(stderr, "\n");
94
  va_end(args);
95
  exit(1);
96
}
97
98
873.2.18 by Monty Taylor
Fixed some solaris warnings.
99
bool get_one_option(int optid, const struct my_option *, char *)
1 by brian
clean slate
100
{
101
  switch(optid) {
102
  case 'V':
103
    print_version();
104
    exit(0);
105
  case '?':
106
    usage();
107
    exit(0);
108
  }
109
  return 0;
110
}
111
112
113
static int parse_args(int argc, char **argv)
114
{
115
  int ho_error;
116
146 by Brian Aker
my_bool cleanup.
117
  if ((ho_error= handle_options(&argc, &argv, my_long_options, get_one_option)))
1 by brian
clean slate
118
    exit(ho_error);
119
120
  /*
121
    The following code is to make the command compatible with the old
122
    version that required one to use the -n and -s options
123
  */
124
125
  if (argc == 2)
126
  {
127
    sym_fname= argv[0];
128
    dump_fname= argv[1];
129
  }
130
  else if (argc == 1)
131
  {
132
    if (!sym_fname)
133
      sym_fname = argv[0];
134
    else if (!dump_fname)
135
      dump_fname = argv[0];
136
    else
137
    {
138
      usage();
139
      exit(1);
140
    }
141
  }
142
  else if (argc != 0 || !sym_fname)
143
  {
144
    usage();
145
    exit(1);
146
  }
147
  return 0;
148
}
149
150
77.1.44 by Monty Taylor
Fixed warnings in test
151
static void open_files(void)
1 by brian
clean slate
152
{
153
  fp_out = stdout;
154
  fp_dump = stdin;
155
910.1.3 by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov)
156
  if (dump_fname && !(fp_dump= fopen(dump_fname, "r")))
1 by brian
clean slate
157
      die("Could not open %s", dump_fname);
158
  /* if name not given, assume stdin*/
159
160
  if (!sym_fname)
77.1.39 by Monty Taylor
More mysql->drizzle renaming.
161
    die("Please run nm --numeric-sort on drizzled binary that produced stack \
1 by brian
clean slate
162
trace dump and specify the path to it with -s or --symbols-file");
910.1.3 by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov)
163
  if (!(fp_sym= fopen(sym_fname, "r")))
1 by brian
clean slate
164
    die("Could not open %s", sym_fname);
165
166
}
167
481 by Brian Aker
Remove all of uchar.
168
static unsigned char hex_val(char c)
1 by brian
clean slate
169
{
481 by Brian Aker
Remove all of uchar.
170
  unsigned char l;
383.1.10 by Brian Aker
More cleanup/test fixup around utf8
171
  if (my_isdigit(&my_charset_utf8_general_ci,c))
1 by brian
clean slate
172
    return c - '0';
383.1.10 by Brian Aker
More cleanup/test fixup around utf8
173
  l = my_tolower(&my_charset_utf8_general_ci,c);
1 by brian
clean slate
174
  if (l < 'a' || l > 'f')
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
175
    return HEX_INVALID;
481 by Brian Aker
Remove all of uchar.
176
  return (unsigned char)10 + ((unsigned char)c - (unsigned char)'a');
1 by brian
clean slate
177
}
178
299 by brian
Reverted ulong back in resolve stack. Reverted MAX (issue for OSX, possibly
179
static unsigned long read_addr(char** buf)
1 by brian
clean slate
180
{
481 by Brian Aker
Remove all of uchar.
181
  unsigned char c;
1 by brian
clean slate
182
  char* p = *buf;
299 by brian
Reverted ulong back in resolve stack. Reverted MAX (issue for OSX, possibly
183
  unsigned long addr = 0;
1 by brian
clean slate
184
185
  while((c = hex_val(*p++)) != HEX_INVALID)
186
      addr = (addr << 4) + c;
187
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
188
  *buf = p;
1 by brian
clean slate
189
  return addr;
190
}
191
192
static int init_sym_entry(SYM_ENTRY* se, char* buf)
193
{
194
  char* p, *p_end;
481 by Brian Aker
Remove all of uchar.
195
  se->addr = (unsigned char*)read_addr(&buf);
1 by brian
clean slate
196
197
  if (!se->addr)
198
    return -1;
383.1.10 by Brian Aker
More cleanup/test fixup around utf8
199
  while (my_isspace(&my_charset_utf8_general_ci,*buf++))
1 by brian
clean slate
200
    /* empty */;
201
383.1.10 by Brian Aker
More cleanup/test fixup around utf8
202
  while (my_isspace(&my_charset_utf8_general_ci,*buf++))
1 by brian
clean slate
203
    /* empty - skip more space */;
204
  --buf;
205
  /* now we are on the symbol */
206
  for (p = se->symbol, p_end = se->symbol + sizeof(se->symbol) - 1;
207
       *buf != '\n' && *buf && p < p_end; ++buf,++p)
208
    *p = *buf;
209
  *p = 0;
210
  if (!strcmp(se->symbol, "gcc2_compiled."))
211
    return -1;
212
  return 0;
213
}
214
77.1.44 by Monty Taylor
Fixed warnings in test
215
static void init_sym_table(void)
1 by brian
clean slate
216
{
217
  char buf[512];
218
  if (my_init_dynamic_array(&sym_table, sizeof(SYM_ENTRY), INIT_SYM_TABLE,
219
			    INC_SYM_TABLE))
220
    die("Failed in my_init_dynamic_array() -- looks like out of memory problem");
221
222
  while (fgets(buf, sizeof(buf), fp_sym))
223
  {
224
    SYM_ENTRY se;
225
    if (init_sym_entry(&se, buf))
226
      continue;
481 by Brian Aker
Remove all of uchar.
227
    if (insert_dynamic(&sym_table, (unsigned char*)&se))
1 by brian
clean slate
228
      die("insert_dynamic() failed - looks like we are out of memory");
229
  }
230
231
  verify_sort();
232
}
233
77.1.44 by Monty Taylor
Fixed warnings in test
234
static void clean_up(void)
1 by brian
clean slate
235
{
236
  delete_dynamic(&sym_table);
237
}
238
239
static void verify_sort()
240
{
482 by Brian Aker
Remove uint.
241
  uint32_t i;
481 by Brian Aker
Remove all of uchar.
242
  unsigned char* last = 0;
1 by brian
clean slate
243
244
  for (i = 0; i < sym_table.elements; i++)
245
  {
246
    SYM_ENTRY se;
481 by Brian Aker
Remove all of uchar.
247
    get_dynamic(&sym_table, (unsigned char*)&se, i);
1 by brian
clean slate
248
    if (se.addr < last)
249
      die("sym table does not appear to be sorted, did you forget \
250
--numeric-sort arg to nm? trouble addr = %p, last = %p", se.addr, last);
251
    last = se.addr;
252
  }
253
}
254
255
481 by Brian Aker
Remove all of uchar.
256
static SYM_ENTRY* resolve_addr(unsigned char* addr, SYM_ENTRY* se)
1 by brian
clean slate
257
{
482 by Brian Aker
Remove uint.
258
  uint32_t i;
481 by Brian Aker
Remove all of uchar.
259
  get_dynamic(&sym_table, (unsigned char*)se, 0);
1 by brian
clean slate
260
  if (addr < se->addr)
261
    return 0;
262
263
  for (i = 1; i < sym_table.elements; i++)
264
  {
481 by Brian Aker
Remove all of uchar.
265
    get_dynamic(&sym_table, (unsigned char*)se, i);
1 by brian
clean slate
266
    if (addr < se->addr)
267
    {
481 by Brian Aker
Remove all of uchar.
268
      get_dynamic(&sym_table, (unsigned char*)se, i - 1);
1 by brian
clean slate
269
      return se;
270
    }
271
  }
272
273
  return se;
274
}
275
276
77.1.44 by Monty Taylor
Fixed warnings in test
277
static void do_resolve(void)
1 by brian
clean slate
278
{
279
  char buf[1024], *p;
280
  while (fgets(buf, sizeof(buf), fp_dump))
281
  {
282
    p = buf;
283
    /* skip space */
383.1.10 by Brian Aker
More cleanup/test fixup around utf8
284
    while (my_isspace(&my_charset_utf8_general_ci,*p))
1 by brian
clean slate
285
      ++p;
286
287
    if (*p++ == '0' && *p++ == 'x')
288
    {
289
      SYM_ENTRY se ;
481 by Brian Aker
Remove all of uchar.
290
      unsigned char* addr = (unsigned char*)read_addr(&p);
1 by brian
clean slate
291
      if (resolve_addr(addr, &se))
292
	fprintf(fp_out, "%p %s + %d\n", addr, se.symbol,
293
		(int) (addr - se.addr));
294
      else
295
	fprintf(fp_out, "%p (?)\n", addr);
296
297
    }
298
    else
299
    {
300
      fputs(buf, fp_out);
301
      continue;
302
    }
303
  }
304
}
305
306
307
int main(int argc, char** argv)
308
{
309
  MY_INIT(argv[0]);
310
  parse_args(argc, argv);
311
  open_files();
312
  init_sym_table();
313
  do_resolve();
314
  clean_up();
315
  return 0;
316
}