1 /* Copyright (C) 2007 MySQL AB & Sanja Belkin
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
15 
16 #include "maria_def.h"
17 #include <my_getopt.h>
18 extern void translog_example_table_init();
19 static const char *load_default_groups[]= { "aria_dump_log",0 };
20 static void get_options(int *argc,char * * *argv);
21 #ifndef DBUG_OFF
22 #if defined(__WIN__)
23 const char *default_dbug_option= "d:t:i:O,\\aria_dump_log.trace";
24 #else
25 const char *default_dbug_option= "d:t:i:o,/tmp/aria_dump_log.trace";
26 #endif
27 #endif
28 static ulonglong opt_offset;
29 static ulong opt_pages;
30 static const char *opt_file= NULL;
31 static File handler= -1;
32 static my_bool opt_unit= 0;
33 static struct my_option my_long_options[] =
34 {
35 #ifdef IMPLTMENTED
36   {"body", 'b',
37    "Print chunk body dump",
38    (uchar **) &opt_body, (uchar **) &opt_body, 0,
39    GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
40 #endif
41 #ifndef DBUG_OFF
42   {"debug", '#', "Output debug log. Often the argument is 'd:t:o,filename'.",
43    0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
44 #endif
45   {"file", 'f', "Path to file which will be read",
46     (uchar**) &opt_file, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
47   {"help", '?', "Display this help and exit.",
48    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
49   { "offset", 'o', "Start reading log from this offset",
50     (uchar**) &opt_offset, (uchar**) &opt_offset,
51     0, GET_ULL, REQUIRED_ARG, 0, 0, ~(longlong) 0, 0, 0, 0 },
52   { "pages", 'n', "Number of pages to read",
53     (uchar**) &opt_pages, (uchar**) &opt_pages, 0,
54     GET_ULONG, REQUIRED_ARG, (long) ~(ulong) 0,
55     (long) 1, (long) ~(ulong) 0, (long) 0,
56     (long) 1, 0},
57   {"unit-test", 'U',
58    "Use unit test record table (for logs created by unittests",
59    (uchar **) &opt_unit, (uchar **) &opt_unit, 0,
60    GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
61   {"version", 'V', "Print version and exit.",
62    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
63   { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
64 };
65 
66 
print_version(void)67 static void print_version(void)
68 {
69   printf("%s Ver 1.0 for %s on %s\n",
70               my_progname_short, SYSTEM_TYPE, MACHINE_TYPE);
71 }
72 
73 
usage(void)74 static void usage(void)
75 {
76   print_version();
77   puts("Copyright (C) 2008 MySQL AB");
78   puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,");
79   puts("and you are welcome to modify and redistribute it under the GPL license\n");
80 
81   puts("Dump content of aria log pages.");
82   printf("\nUsage: %s -f file OPTIONS\n", my_progname_short);
83   my_print_help(my_long_options);
84   print_defaults("my", load_default_groups);
85   my_print_variables(my_long_options);
86 }
87 
88 
89 static my_bool
get_one_option(const struct my_option * opt,const char * argument,const char * filename)90 get_one_option(const struct my_option *opt,
91                const char *argument __attribute__((unused)),
92                const char *filename __attribute__((unused)))
93 {
94   switch (opt->id) {
95   case '?':
96     usage();
97     exit(0);
98   case 'V':
99     print_version();
100     exit(0);
101 #ifndef DBUG_OFF
102   case '#':
103     DBUG_SET_INITIAL(argument ? argument : default_dbug_option);
104     break;
105 #endif
106   }
107   return 0;
108 }
109 
110 
get_options(int * argc,char *** argv)111 static void get_options(int *argc,char ***argv)
112 {
113   int ho_error;
114 
115   if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
116     exit(ho_error);
117 
118   if (opt_file == NULL)
119   {
120     usage();
121     exit(1);
122   }
123 }
124 
125 
126 /**
127   @brief maria_dump_log main function.
128 */
129 
main(int argc,char ** argv)130 int main(int argc, char **argv)
131 {
132   char **default_argv;
133   uchar buffer[TRANSLOG_PAGE_SIZE];
134   MY_INIT(argv[0]);
135 
136   load_defaults_or_exit("my", load_default_groups, &argc, &argv);
137   default_argv= argv;
138   get_options(&argc, &argv);
139 
140   if (opt_unit)
141     translog_example_table_init();
142   else
143     translog_table_init();
144   translog_fill_overhead_table();
145 
146   maria_data_root= ".";
147 
148   if ((handler= my_open(opt_file, O_RDONLY, MYF(MY_WME))) < 0)
149   {
150     fprintf(stderr, "Can't open file: '%s'  errno: %d\n",
151             opt_file, my_errno);
152     goto err;
153   }
154   if (my_seek(handler, opt_offset, SEEK_SET, MYF(MY_WME)) !=
155       opt_offset)
156   {
157      fprintf(stderr, "Can't set position %lld  file: '%s'  errno: %d\n",
158              opt_offset, opt_file, my_errno);
159      goto err;
160   }
161   for (;
162        opt_pages;
163        opt_offset+= TRANSLOG_PAGE_SIZE, opt_pages--)
164   {
165     if (my_pread(handler, buffer, TRANSLOG_PAGE_SIZE, opt_offset,
166                  MYF(MY_NABP)))
167     {
168       if (my_errno == HA_ERR_FILE_TOO_SHORT)
169         goto end;
170       fprintf(stderr, "Can't read page at position %lld  file: '%s'  "
171               "errno: %d\n", opt_offset, opt_file, my_errno);
172       goto err;
173     }
174     printf("Page by offset %llu (0x%llx)\n", opt_offset, opt_offset);
175     dump_page(buffer, handler);
176   }
177 
178 end:
179   my_close(handler, MYF(0));
180   free_defaults(default_argv);
181   exit(0);
182   return 0;				/* No compiler warning */
183 
184 err:
185   my_close(handler, MYF(0));
186   fprintf(stderr, "%s: FAILED\n", my_progname_short);
187   free_defaults(default_argv);
188   exit(1);
189 }
190 
191 #include "ma_check_standalone.h"
192 
193