MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
print_file.cpp
1 /*
2  Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include <ndb_global.h>
19 
20 #include <NdbOut.hpp>
21 #include <UtilBuffer.hpp>
22 #include "diskpage.hpp"
23 #include <ndb_limits.h>
24 #include <dbtup/tuppage.hpp>
25 
26 static void print_usage(const char*);
27 static int print_zero_page(int, void *, Uint32 sz);
28 static int print_extent_page(int, void*, Uint32 sz);
29 static int print_undo_page(int, void*, Uint32 sz);
30 static int print_data_page(int, void*, Uint32 sz);
31 static bool print_page(int page_no)
32 {
33  return false;
34 }
35 
36 int g_verbosity = 1;
37 unsigned g_page_size = File_formats::NDB_PAGE_SIZE;
38 int (* g_print_page)(int count, void*, Uint32 sz) = print_zero_page;
39 
42 
43 int main(int argc, char ** argv)
44 {
45  for(int i = 1; i<argc; i++){
46  if(!strncmp(argv[i], "-v", 2))
47  {
48  int pos= 2;
49  do {
50  g_verbosity++;
51  } while(argv[i][pos++] == 'v');
52  continue;
53  }
54  else if(!strcmp(argv[i], "-q"))
55  {
56  g_verbosity--;
57  continue;
58  }
59  else if(!strcmp(argv[i], "-?") ||
60  !strcmp(argv[i], "--?") ||
61  !strcmp(argv[i], "-h") ||
62  !strcmp(argv[i], "--help"))
63  {
64  print_usage(argv[0]);
65  exit(0);
66  }
67 
68  const char * filename = argv[i];
69 
70  struct stat sbuf;
71  if(stat(filename, &sbuf) != 0)
72  {
73  ndbout << "Could not find file: \"" << filename << "\"" << endl;
74  continue;
75  }
76  //const Uint32 bytes = sbuf.st_size;
77 
78  UtilBuffer buffer;
79 
80  FILE * f = fopen(filename, "rb");
81  if(f == 0){
82  ndbout << "Failed to open file" << endl;
83  continue;
84  }
85 
86  Uint32 sz;
87  Uint32 j = 0;
88  do {
89  buffer.grow(g_page_size);
90  sz = (Uint32)fread(buffer.get_data(), 1, g_page_size, f);
91  if((* g_print_page)(j++, buffer.get_data(), sz))
92  break;
93  } while(sz == g_page_size);
94 
95  fclose(f);
96  continue;
97  }
98  return 0;
99 }
100 
101 void
102 print_usage(const char* prg)
103 {
104  ndbout << prg << " [-v]+ [-q]+ <file>+" << endl;
105 }
106 
107 int
108 print_zero_page(int i, void * ptr, Uint32 sz){
110  if(memcmp(page->m_magic, "NDBDISK", 8) != 0)
111  {
112  ndbout << "Invalid magic: file is not ndb disk data file" << endl;
113  return 1;
114  }
115 
116  if(page->m_byte_order != 0x12345678)
117  {
118  ndbout << "Unhandled byteorder" << endl;
119  return 1;
120  }
121 
122  switch(page->m_file_type)
123  {
124  case File_formats::FT_Datafile:
125  {
126  g_df_zero = (* (File_formats::Datafile::Zero_page*)ptr);
127  ndbout << "-- Datafile -- " << endl;
128  ndbout << g_df_zero << endl;
129  g_print_page = print_extent_page;
130  return 0;
131  }
132  break;
133  case File_formats::FT_Undofile:
134  {
135  g_uf_zero = (* (File_formats::Undofile::Zero_page*)ptr);
136  ndbout << "-- Undofile -- " << endl;
137  ndbout << g_uf_zero << endl;
138  g_print_page = print_undo_page;
139  return 0;
140  }
141  break;
142  default:
143  ndbout << "Unhandled file type: " << page->m_file_type << endl;
144  return 1;
145  }
146 
147  if(page->m_page_size !=g_page_size)
148  {
154  ndbout << "Unhandled page size: " << page->m_page_size << endl;
155  return 1;
156  }
157 
158  return 0;
159 }
160 
161 NdbOut&
162 operator<<(NdbOut& out, const File_formats::Datafile::Extent_header& obj)
163 {
164  if(obj.m_table == RNIL)
165  {
166  if(obj.m_next_free_extent != RNIL)
167  out << " FREE, next free: " << obj.m_next_free_extent;
168  else
169  out << " FREE, next free: RNIL";
170  }
171  else
172  {
173  out << "table: " << obj.m_table
174  << " fragment: " << obj.m_fragment_id << " ";
175  for(Uint32 i = 0; i<g_df_zero.m_extent_size; i++)
176  {
177  char t[2];
178  BaseString::snprintf(t, sizeof(t), "%x", obj.get_free_bits(i));
179  out << t;
180  }
181  }
182  return out;
183 }
184 
185 int
186 print_extent_page(int count, void* ptr, Uint32 sz){
187  if((unsigned)count == g_df_zero.m_extent_pages)
188  {
189  g_print_page = print_data_page;
190  }
191  Uint32 header_words =
192  File_formats::Datafile::extent_header_words(g_df_zero.m_extent_size);
193  Uint32 per_page = File_formats::Datafile::EXTENT_PAGE_WORDS / header_words;
194 
195  int no = count * per_page;
196 
197  const int max = count < int(g_df_zero.m_extent_pages) ?
198  per_page : g_df_zero.m_extent_count - (g_df_zero.m_extent_count - 1) * per_page;
199 
202 
203  ndbout << "Extent page: " << count
204  << ", lsn = [ "
205  << page->m_page_header.m_page_lsn_hi << " "
206  << page->m_page_header.m_page_lsn_lo << "] "
207  << endl;
208  for(int i = 0; i<max; i++)
209  {
210  ndbout << " extent " << no+i << ": "
211  << (* page->get_header(i, g_df_zero.m_extent_size)) << endl;
212  }
213  return 0;
214 }
215 
216 int
217 print_data_page(int count, void* ptr, Uint32 sz){
218 
221 
222  ndbout << "Data page: " << count
223  << ", lsn = [ "
224  << page->m_page_header.m_page_lsn_hi << " "
225  << page->m_page_header.m_page_lsn_lo << "]" ;
226 
227  if(g_verbosity > 1 || print_page(count))
228  {
229  switch(page->m_page_header.m_page_type){
230  case File_formats::PT_Unallocated:
231  break;
232  case File_formats::PT_Tup_fixsize_page:
233  ndbout << " fix ";
234  if(g_verbosity > 2 || print_page(count))
235  ndbout << (* (Tup_fixsize_page*)page);
236  break;
237  case File_formats::PT_Tup_varsize_page:
238  ndbout << " var ";
239  if(g_verbosity > 2 || print_page(count))
240  ndbout << endl << (* (Tup_varsize_page*)page);
241  break;
242  default:
243  ndbout << " unknown page type: %d" << page->m_page_header.m_page_type;
244  }
245  }
246  ndbout << endl;
247  return 0;
248 }
249 
250 #define DBTUP_C
251 #include "dbtup/Dbtup.hpp"
252 
253 int
254 print_undo_page(int count, void* ptr, Uint32 sz){
255  if(count > int(g_uf_zero.m_undo_pages + 1))
256  {
257  ndbout_c(" ERROR to many pages in file!!");
258  return 1;
259  }
260 
263 
264  if(page->m_page_header.m_page_lsn_hi != 0 ||
265  page->m_page_header.m_page_lsn_lo != 0)
266  {
267  ndbout << "Undo page: " << count
268  << ", lsn = [ "
269  << page->m_page_header.m_page_lsn_hi << " "
270  << page->m_page_header.m_page_lsn_lo << "] "
271  << "words used: " << page->m_words_used << endl;
272 
273  Uint64 lsn= 0;
274  lsn += page->m_page_header.m_page_lsn_hi;
275  lsn <<= 32;
276  lsn += page->m_page_header.m_page_lsn_lo;
277  lsn++;
278 
279  if(g_verbosity >= 3)
280  {
281  Uint32 pos= page->m_words_used - 1;
282  while(pos + 1 != 0)
283  {
284  Uint32 len= page->m_data[pos] & 0xFFFF;
285  Uint32 type= page->m_data[pos] >> 16;
286  const Uint32* src= page->m_data + pos - len + 1;
287  Uint32 next_pos= pos - len;
288  if(type & File_formats::Undofile::UNDO_NEXT_LSN)
289  {
290  type &= ~(Uint32)File_formats::Undofile::UNDO_NEXT_LSN;
291  lsn--;
292  }
293  else
294  {
295  lsn = 0;
296  lsn += * (src - 2);
297  lsn <<= 32;
298  lsn += * (src - 1);
299  next_pos -= 2;
300  }
301  if(g_verbosity > 3)
302  printf(" %.4d - %.4d : ", pos - len + 1, pos);
303  switch(type){
304  case File_formats::Undofile::UNDO_LCP_FIRST:
305  case File_formats::Undofile::UNDO_LCP:
306  printf("[ %lld LCP %d tab: %d frag: %d ]", lsn,
307  src[0], src[1] >> 16, src[1] & 0xFFFF);
308  if(g_verbosity <= 3)
309  printf("\n");
310  break;
311  case File_formats::Undofile::UNDO_TUP_ALLOC:
312  if(g_verbosity > 3)
313  {
315  printf("[ %lld A %d %d %d ]",
316  lsn,
317  req->m_file_no_page_idx >> 16,
318  req->m_file_no_page_idx & 0xFFFF,
319  req->m_page_no);
320  }
321  break;
322  case File_formats::Undofile::UNDO_TUP_UPDATE:
323  if(g_verbosity > 3)
324  {
326  printf("[ %lld U %d %d %d gci: %d ]",
327  lsn,
328  req->m_file_no_page_idx >> 16,
329  req->m_file_no_page_idx & 0xFFFF,
330  req->m_page_no,
331  req->m_gci);
332  }
333  break;
334  case File_formats::Undofile::UNDO_TUP_FREE:
335  if(g_verbosity > 3)
336  {
338  printf("[ %lld F %d %d %d gci: %d ]",
339  lsn,
340  req->m_file_no_page_idx >> 16,
341  req->m_file_no_page_idx & 0xFFFF,
342  req->m_page_no,
343  req->m_gci);
344  }
345  break;
346  case File_formats::Undofile::UNDO_TUP_CREATE:
347  {
349  printf("[ %lld Create %d ]", lsn, req->m_table);
350  if(g_verbosity <= 3)
351  printf("\n");
352  break;
353  }
354  case File_formats::Undofile::UNDO_TUP_DROP:
355  {
357  printf("[ %lld Drop %d ]", lsn, req->m_table);
358  if(g_verbosity <= 3)
359  printf("\n");
360  break;
361  }
362  case File_formats::Undofile::UNDO_TUP_ALLOC_EXTENT:
363  {
365  printf("[ %lld AllocExtent tab: %d frag: %d file: %d page: %d ]",
366  lsn,
367  req->m_table,
368  req->m_fragment,
369  req->m_file_no,
370  req->m_page_no);
371  if(g_verbosity <= 3)
372  printf("\n");
373  break;
374  }
375  case File_formats::Undofile::UNDO_TUP_FREE_EXTENT:
376  {
378  printf("[ %lld FreeExtent tab: %d frag: %d file: %d page: %d ]",
379  lsn,
380  req->m_table,
381  req->m_fragment,
382  req->m_file_no,
383  req->m_page_no);
384  if(g_verbosity <= 3)
385  printf("\n");
386  break;
387  }
388  default:
389  ndbout_c("[ Unknown type %d len: %d, pos: %d ]", type, len, pos);
390  if(!(len && type))
391  {
392  pos= 0;
393  while(pos < page->m_words_used)
394  {
395  printf("%.8x ", page->m_data[pos]);
396  if((pos + 1) % 7 == 0)
397  ndbout_c("%s", "");
398  pos++;
399  }
400  }
401  assert(len && type);
402  }
403  pos = next_pos;
404  if(g_verbosity > 3)
405  printf("\n");
406  }
407  }
408  }
409 
410  if((unsigned)count == g_uf_zero.m_undo_pages + 1)
411  {
412  }
413 
414  return 0;
415 }
416 
417 // Dummy implementations
418 Signal::Signal(){}
419 SimulatedBlock::Callback SimulatedBlock::TheEmptyCallback = {0, 0};