~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
20
#include <my_global.h>
21
#include <m_ctype.h>
22
#include <my_sys.h>
23
#include <m_string.h>
77.1.39 by Monty Taylor
More mysql->drizzle renaming.
24
#include <drizzle_version.h>
1 by brian
clean slate
25
#include <errno.h>
26
#include <my_getopt.h>
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"
32
#define HEX_INVALID  (uchar)255
33
34
typedef ulong my_long_addr_t ; /* at some point, we need to fix configure
35
				* to define this for us  
36
				*/
37
38
typedef struct sym_entry
39
{
40
  char symbol[MAX_SYM_SIZE];
41
  uchar* addr;
42
} SYM_ENTRY;
43
44
45
static char* dump_fname = 0, *sym_fname = 0;
46
static DYNAMIC_ARRAY sym_table; /* how do you like this , static DYNAMIC ? */
47
static FILE* fp_dump, *fp_sym = 0, *fp_out; 
48
49
static struct my_option my_long_options[] =
50
{
51
  {"help", 'h', "Display this help and exit.",
52
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
53
  {"version", 'V', "Output version information and exit.",
54
   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.
55
  {"symbols-file", 's', "Use specified symbols file.", (char**) &sym_fname,
56
   (char**) &sym_fname, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1 by brian
clean slate
57
  {"numeric-dump-file", 'n', "Read the dump from specified file.",
77.1.78 by Monty Taylor
One last bunch of warnings edits.
58
   (char**) &dump_fname, (char**) &dump_fname, 0, GET_STR, REQUIRED_ARG,
1 by brian
clean slate
59
   0, 0, 0, 0, 0, 0},
60
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
61
};
62
63
77.1.44 by Monty Taylor
Fixed warnings in test
64
static void verify_sort(void);
1 by brian
clean slate
65
66
static void print_version(void)
67
{
68
  printf("%s  Ver %s Distrib %s, for %s (%s)\n",my_progname,DUMP_VERSION,
69
	 MYSQL_SERVER_VERSION,SYSTEM_TYPE,MACHINE_TYPE);
70
}
71
72
77.1.44 by Monty Taylor
Fixed warnings in test
73
static void usage(void)
1 by brian
clean slate
74
{
75
  print_version();
76
  printf("MySQL AB, by Sasha Pachev\n");
77
  printf("This software comes with ABSOLUTELY NO WARRANTY\n\n");
78
  printf("Resolve numeric stack strace dump into symbols.\n\n");
79
  printf("Usage: %s [OPTIONS] symbols-file [numeric-dump-file]\n",
80
	 my_progname);
81
  my_print_help(my_long_options);
82
  my_print_variables(my_long_options);
83
  printf("\n\
77.1.39 by Monty Taylor
More mysql->drizzle renaming.
84
The symbols-file should include the output from:  'nm --numeric-sort drizzled'.\n\
85
The numeric-dump-file should contain a numeric stack trace from drizzled.\n\
1 by brian
clean slate
86
If the numeric-dump-file is not given, the stack trace is read from stdin.\n");
87
}
88
89
static void die(const char* fmt, ...)
90
{
91
  va_list args;
92
  va_start(args, fmt);
93
  fprintf(stderr, "%s: ", my_progname);
94
  vfprintf(stderr, fmt, args);
95
  fprintf(stderr, "\n");
96
  va_end(args);
97
  exit(1);
98
}
99
100
146 by Brian Aker
my_bool cleanup.
101
static bool
1 by brian
clean slate
102
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
103
	       char *argument __attribute__((unused)))
104
{
105
  switch(optid) {
106
  case 'V':
107
    print_version();
108
    exit(0);
109
  case '?':
110
    usage();
111
    exit(0);
112
  }
113
  return 0;
114
}
115
116
117
static int parse_args(int argc, char **argv)
118
{
119
  int ho_error;
120
146 by Brian Aker
my_bool cleanup.
121
  if ((ho_error= handle_options(&argc, &argv, my_long_options, get_one_option)))
1 by brian
clean slate
122
    exit(ho_error);
123
124
  /*
125
    The following code is to make the command compatible with the old
126
    version that required one to use the -n and -s options
127
  */
128
129
  if (argc == 2)
130
  {
131
    sym_fname= argv[0];
132
    dump_fname= argv[1];
133
  }
134
  else if (argc == 1)
135
  {
136
    if (!sym_fname)
137
      sym_fname = argv[0];
138
    else if (!dump_fname)
139
      dump_fname = argv[0];
140
    else
141
    {
142
      usage();
143
      exit(1);
144
    }
145
  }
146
  else if (argc != 0 || !sym_fname)
147
  {
148
    usage();
149
    exit(1);
150
  }
151
  return 0;
152
}
153
154
77.1.44 by Monty Taylor
Fixed warnings in test
155
static void open_files(void)
1 by brian
clean slate
156
{
157
  fp_out = stdout;
158
  fp_dump = stdin;
159
160
  if (dump_fname && !(fp_dump = my_fopen(dump_fname, O_RDONLY, MYF(MY_WME))))
161
      die("Could not open %s", dump_fname);
162
  /* if name not given, assume stdin*/
163
164
  if (!sym_fname)
77.1.39 by Monty Taylor
More mysql->drizzle renaming.
165
    die("Please run nm --numeric-sort on drizzled binary that produced stack \
1 by brian
clean slate
166
trace dump and specify the path to it with -s or --symbols-file");
167
  if (!(fp_sym = my_fopen(sym_fname, O_RDONLY, MYF(MY_WME))))
168
    die("Could not open %s", sym_fname);
169
170
}
171
172
static uchar hex_val(char c)
173
{
174
  uchar l;
175
  if (my_isdigit(&my_charset_latin1,c))
176
    return c - '0';
177
  l = my_tolower(&my_charset_latin1,c);
178
  if (l < 'a' || l > 'f')
179
    return HEX_INVALID; 
180
  return (uchar)10 + ((uchar)c - (uchar)'a');
181
}
182
183
static my_long_addr_t read_addr(char** buf)
184
{
185
  uchar c;
186
  char* p = *buf;
187
  my_long_addr_t addr = 0;
188
189
  while((c = hex_val(*p++)) != HEX_INVALID)
190
      addr = (addr << 4) + c;
191
192
  *buf = p; 
193
  return addr;
194
}
195
196
static int init_sym_entry(SYM_ENTRY* se, char* buf)
197
{
198
  char* p, *p_end;
199
  se->addr = (uchar*)read_addr(&buf);
200
201
  if (!se->addr)
202
    return -1;
203
  while (my_isspace(&my_charset_latin1,*buf++))
204
    /* empty */;
205
206
  while (my_isspace(&my_charset_latin1,*buf++))
207
    /* empty - skip more space */;
208
  --buf;
209
  /* now we are on the symbol */
210
  for (p = se->symbol, p_end = se->symbol + sizeof(se->symbol) - 1;
211
       *buf != '\n' && *buf && p < p_end; ++buf,++p)
212
    *p = *buf;
213
  *p = 0;
214
  if (!strcmp(se->symbol, "gcc2_compiled."))
215
    return -1;
216
  return 0;
217
}
218
77.1.44 by Monty Taylor
Fixed warnings in test
219
static void init_sym_table(void)
1 by brian
clean slate
220
{
221
  char buf[512];
222
  if (my_init_dynamic_array(&sym_table, sizeof(SYM_ENTRY), INIT_SYM_TABLE,
223
			    INC_SYM_TABLE))
224
    die("Failed in my_init_dynamic_array() -- looks like out of memory problem");
225
226
  while (fgets(buf, sizeof(buf), fp_sym))
227
  {
228
    SYM_ENTRY se;
229
    if (init_sym_entry(&se, buf))
230
      continue;
231
    if (insert_dynamic(&sym_table, (uchar*)&se))
232
      die("insert_dynamic() failed - looks like we are out of memory");
233
  }
234
235
  verify_sort();
236
}
237
77.1.44 by Monty Taylor
Fixed warnings in test
238
static void clean_up(void)
1 by brian
clean slate
239
{
240
  delete_dynamic(&sym_table);
241
}
242
243
static void verify_sort()
244
{
245
  uint i;
246
  uchar* last = 0;
247
248
  for (i = 0; i < sym_table.elements; i++)
249
  {
250
    SYM_ENTRY se;
251
    get_dynamic(&sym_table, (uchar*)&se, i);
252
    if (se.addr < last)
253
      die("sym table does not appear to be sorted, did you forget \
254
--numeric-sort arg to nm? trouble addr = %p, last = %p", se.addr, last);
255
    last = se.addr;
256
  }
257
}
258
259
260
static SYM_ENTRY* resolve_addr(uchar* addr, SYM_ENTRY* se)
261
{
262
  uint i;
263
  get_dynamic(&sym_table, (uchar*)se, 0);
264
  if (addr < se->addr)
265
    return 0;
266
267
  for (i = 1; i < sym_table.elements; i++)
268
  {
269
    get_dynamic(&sym_table, (uchar*)se, i);
270
    if (addr < se->addr)
271
    {
272
      get_dynamic(&sym_table, (uchar*)se, i - 1);
273
      return se;
274
    }
275
  }
276
277
  return se;
278
}
279
280
77.1.44 by Monty Taylor
Fixed warnings in test
281
static void do_resolve(void)
1 by brian
clean slate
282
{
283
  char buf[1024], *p;
284
  while (fgets(buf, sizeof(buf), fp_dump))
285
  {
286
    p = buf;
287
    /* skip space */
288
    while (my_isspace(&my_charset_latin1,*p))
289
      ++p;
290
291
    if (*p++ == '0' && *p++ == 'x')
292
    {
293
      SYM_ENTRY se ;
294
      uchar* addr = (uchar*)read_addr(&p);
295
      if (resolve_addr(addr, &se))
296
	fprintf(fp_out, "%p %s + %d\n", addr, se.symbol,
297
		(int) (addr - se.addr));
298
      else
299
	fprintf(fp_out, "%p (?)\n", addr);
300
301
    }
302
    else
303
    {
304
      fputs(buf, fp_out);
305
      continue;
306
    }
307
  }
308
}
309
310
311
int main(int argc, char** argv)
312
{
313
  MY_INIT(argv[0]);
314
  parse_args(argc, argv);
315
  open_files();
316
  init_sym_table();
317
  do_resolve();
318
  clean_up();
319
  return 0;
320
}