2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 2012 Oracle and/or its affiliates. All rights reserved.
12 #include "dbinc/db_page.h"
13 #include "dbinc/btree.h"
14 #include "dbinc/fop.h"
15 #include "dbinc/hash.h"
17 #include "dbinc/heap.h"
19 #include "dbinc/qam.h"
20 #include "dbinc/txn.h"
23 static const char copyright[] =
24 "Copyright (c) 1996, 2012 Oracle and/or its affiliates. All rights reserved.\n";
27 int db_printlog_print_app_record __P((DB_ENV *, DBT *, DB_LSN *, db_recops));
28 int env_init_print __P((ENV *, u_int32_t, DB_DISTAB *));
29 int env_init_print_42 __P((ENV *, DB_DISTAB *));
30 int env_init_print_43 __P((ENV *, DB_DISTAB *));
31 int env_init_print_47 __P((ENV *, DB_DISTAB *));
32 int env_init_print_48 __P((ENV *, DB_DISTAB *));
33 int lsn_arg __P((char *, DB_LSN *));
34 int main __P((int, char *[]));
35 int open_rep_db __P((DB_ENV *, DB **, DBC **));
36 int usage __P((void));
37 int version_check __P((void));
55 DB_LSN key, start, stop, verslsn;
57 u_int32_t logcflag, newversion, version;
58 int ch, cmp, exitval, i, nflag, rflag, ret, repflag;
59 char *data_len, *home, *passwd;
61 if ((progname = __db_rpath(argv[0])) == NULL)
66 if ((ret = version_check()) != 0)
76 exitval = nflag = rflag = repflag = 0;
77 data_len = home = passwd = NULL;
79 memset(&dtab, 0, sizeof(dtab));
80 memset(&dblog, 0, sizeof(dblog));
82 while ((ch = getopt(argc, argv, "b:D:e:h:NP:rRV")) != EOF)
85 /* Don't use getsubopt(3), not all systems have it. */
86 if (lsn_arg(optarg, &start))
93 /* Don't use getsubopt(3), not all systems have it. */
94 if (lsn_arg(optarg, &stop))
104 if (passwd != NULL) {
105 fprintf(stderr, DB_STR("5138",
106 "Password may not be specified twice"));
108 return (EXIT_FAILURE);
110 passwd = strdup(optarg);
111 memset(optarg, 0, strlen(optarg));
112 if (passwd == NULL) {
113 fprintf(stderr, DB_STR_A("5010",
114 "%s: strdup: %s\n", "%s %s\n"),
115 progname, strerror(errno));
116 return (EXIT_FAILURE);
122 case 'R': /* Undocumented */
126 printf("%s\n", db_version(NULL, NULL, NULL));
127 return (EXIT_SUCCESS);
138 /* Handle possible interruptions. */
142 * Create an environment object and initialize it for error
145 if ((ret = db_env_create(&dbenv, 0)) != 0) {
147 "%s: db_env_create: %s\n", progname, db_strerror(ret));
151 dbenv->set_errfile(dbenv, stderr);
152 dbenv->set_errpfx(dbenv, progname);
153 dbenv->set_msgfile(dbenv, stdout);
155 if (data_len != NULL)
156 (void)dbenv->set_data_len(dbenv, (u_int32_t)atol(data_len));
159 if ((ret = dbenv->set_flags(dbenv, DB_NOLOCKING, 1)) != 0) {
160 dbenv->err(dbenv, ret, "set_flags: DB_NOLOCKING");
163 if ((ret = dbenv->set_flags(dbenv, DB_NOPANIC, 1)) != 0) {
164 dbenv->err(dbenv, ret, "set_flags: DB_NOPANIC");
169 if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
170 passwd, DB_ENCRYPT_AES)) != 0) {
171 dbenv->err(dbenv, ret, "set_passwd");
176 * Set up an app-specific dispatch function so that we can gracefully
177 * handle app-specific log records.
179 if ((ret = dbenv->set_app_dispatch(
180 dbenv, db_printlog_print_app_record)) != 0) {
181 dbenv->err(dbenv, ret, "app_dispatch");
186 * An environment is required, but as all we're doing is reading log
187 * files, we create one if it doesn't already exist. If we create
188 * it, create it private so it automatically goes away when we're done.
189 * If we are reading the replication database, do not open the env
190 * with logging, because we don't want to log the opens.
193 if ((ret = dbenv->open(dbenv, home,
194 DB_INIT_MPOOL | DB_USE_ENVIRON, 0)) != 0 &&
195 (ret == DB_VERSION_MISMATCH ||
196 (ret = dbenv->open(dbenv, home,
197 DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0))
199 dbenv->err(dbenv, ret, "DB_ENV->open");
202 } else if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) != 0 &&
203 (ret == DB_VERSION_MISMATCH ||
204 (ret = dbenv->open(dbenv, home,
205 DB_CREATE | DB_INIT_LOG | DB_PRIVATE | DB_USE_ENVIRON, 0)) != 0)) {
206 dbenv->err(dbenv, ret, "DB_ENV->open");
211 /* Allocate a log cursor. */
213 if ((ret = open_rep_db(dbenv, &dbp, &dbc)) != 0)
215 } else if ((ret = dbenv->log_cursor(dbenv, &logc, 0)) != 0) {
216 dbenv->err(dbenv, ret, "DB_ENV->log_cursor");
220 if (IS_ZERO_LSN(start)) {
221 memset(&keydbt, 0, sizeof(keydbt));
222 logcflag = rflag ? DB_PREV : DB_NEXT;
227 memset(&data, 0, sizeof(data));
230 * If we're using the repflag, we're immediately initializing
231 * the print table. Use the current version. If we're printing
232 * the log then initialize version to 0 so that we get the
233 * correct version right away.
236 version = DB_LOGVERSION;
241 /* Initialize print callbacks if repflag. */
243 (ret = env_init_print(env, version, &dtab)) != 0) {
244 dbenv->err(dbenv, ret, DB_STR("5011",
245 "callback: initialization"));
248 for (; !__db_util_interrupted(); logcflag = rflag ? DB_PREV : DB_NEXT) {
250 ret = dbc->get(dbc, &keydbt, &data, logcflag);
252 key = ((__rep_control_args *)keydbt.data)->lsn;
254 ret = logc->get(logc, &key, &data, logcflag);
256 if (ret == DB_NOTFOUND)
259 ret, repflag ? "DBC->get" : "DB_LOGC->get");
264 * We may have reached the end of the range we're displaying.
266 if (!IS_ZERO_LSN(stop)) {
267 cmp = LOG_COMPARE(&key, &stop);
268 if ((rflag && cmp < 0) || (!rflag && cmp > 0))
271 if (!repflag && key.file != verslsn.file) {
273 * If our log file changed, we need to see if the
274 * version of the log file changed as well.
275 * If it changed, reset the print table.
277 if ((ret = logc->version(logc, &newversion, 0)) != 0) {
278 dbenv->err(dbenv, ret, "DB_LOGC->version");
281 if (version != newversion) {
282 version = newversion;
283 if ((ret = env_init_print(env, version,
285 dbenv->err(dbenv, ret, DB_STR("5012",
286 "callback: initialization"));
292 ret = __db_dispatch(env,
293 &dtab, &data, &key, DB_TXN_PRINT, (void*)&dblog);
297 * Just in case the underlying routines don't flush.
299 (void)fflush(stdout);
302 dbenv->err(dbenv, ret, DB_STR("5013",
312 if (dtab.int_dispatch != NULL)
313 __os_free(env, dtab.int_dispatch);
314 if (dtab.ext_dispatch != NULL)
315 __os_free(env, dtab.ext_dispatch);
317 * Call __db_close to free the dummy DB handles that were used
318 * by the print routines.
320 for (i = 0; i < dblog.dbentry_cnt; i++)
321 if (dblog.dbentry[i].dbp != NULL)
322 (void)__db_close(dblog.dbentry[i].dbp, NULL, DB_NOSYNC);
323 if (env != NULL && dblog.dbentry != NULL)
324 __os_free(env, dblog.dbentry);
325 if (logc != NULL && (ret = logc->close(logc, 0)) != 0)
328 if (dbc != NULL && (ret = dbc->close(dbc)) != 0)
331 if (dbp != NULL && (ret = dbp->close(dbp, 0)) != 0)
334 if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
337 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
343 /* Resend any caught signal. */
344 __db_util_sigresend();
346 return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
353 env_init_print(env, version, dtabp)
361 * We need to prime the print table with the current print
362 * functions. Then we overwrite only specific entries based on
363 * each previous version we support.
365 if ((ret = __bam_init_print(env, dtabp)) != 0)
367 if ((ret = __crdel_init_print(env, dtabp)) != 0)
369 if ((ret = __db_init_print(env, dtabp)) != 0)
371 if ((ret = __dbreg_init_print(env, dtabp)) != 0)
373 if ((ret = __fop_init_print(env, dtabp)) != 0)
376 if ((ret = __ham_init_print(env, dtabp)) != 0)
380 if ((ret = __heap_init_print(env, dtabp)) != 0)
384 if ((ret = __qam_init_print(env, dtabp)) != 0)
387 #ifdef HAVE_REPLICATION_THREADS
388 if ((ret = __repmgr_init_print(env, dtabp)) != 0)
391 if ((ret = __txn_init_print(env, dtabp)) != 0)
395 * There are no log differences between 5.0 and 5.2, but 5.2
396 * is a superset of 5.0. Patch 2 of 4.8 added __db_pg_trunc
397 * but didn't alter any log records so we want the same
400 if (version > DB_LOGVERSION_48p2)
402 if ((ret = env_init_print_48(env, dtabp)) != 0)
404 if (version >= DB_LOGVERSION_48)
406 if ((ret = env_init_print_47(env, dtabp)) != 0)
408 if (version == DB_LOGVERSION_47)
411 * There are no log record/recovery differences between 4.4 and 4.5.
412 * The log version changed due to checksum. There are no log recovery
413 * differences between 4.5 and 4.6. The name of the rep_gen in
414 * txn_checkpoint changed (to spare, since we don't use it anymore).
416 if (version >= DB_LOGVERSION_44)
418 if ((ret = env_init_print_43(env, dtabp)) != 0)
420 if (version == DB_LOGVERSION_43)
422 if (version != DB_LOGVERSION_42) {
423 __db_errx(env, DB_STR_A("5014",
424 "Unknown version %lu", "%lu"), (u_long)version);
428 ret = env_init_print_42(env, dtabp);
434 env_init_print_42(env, dtabp)
440 if ((ret = __db_add_recovery_int(env, dtabp,
441 __db_relink_42_print, DB___db_relink_42)) != 0)
443 if ((ret = __db_add_recovery_int(env, dtabp,
444 __db_pg_alloc_42_print, DB___db_pg_alloc_42)) != 0)
446 if ((ret = __db_add_recovery_int(env, dtabp,
447 __db_pg_free_42_print, DB___db_pg_free_42)) != 0)
449 if ((ret = __db_add_recovery_int(env, dtabp,
450 __db_pg_freedata_42_print, DB___db_pg_freedata_42)) != 0)
453 if ((ret = __db_add_recovery_int(env, dtabp,
454 __ham_metagroup_42_print, DB___ham_metagroup_42)) != 0)
456 if ((ret = __db_add_recovery_int(env, dtabp,
457 __ham_groupalloc_42_print, DB___ham_groupalloc_42)) != 0)
460 if ((ret = __db_add_recovery_int(env, dtabp,
461 __txn_ckp_42_print, DB___txn_ckp_42)) != 0)
468 env_init_print_43(env, dtabp)
474 if ((ret = __db_add_recovery_int(env, dtabp,
475 __bam_relink_43_print, DB___bam_relink_43)) != 0)
478 * We want to use the 4.2-based txn_regop record.
480 if ((ret = __db_add_recovery_int(env, dtabp,
481 __txn_regop_42_print, DB___txn_regop_42)) != 0)
489 * env_init_print_47 --
493 env_init_print_47(env, dtabp)
499 if ((ret = __db_add_recovery_int(env, dtabp,
500 __bam_split_42_print, DB___bam_split_42)) != 0)
502 if ((ret = __db_add_recovery_int(env, dtabp,
503 __db_pg_sort_44_print, DB___db_pg_sort_44)) != 0)
505 if ((ret = __db_add_recovery_int(env, dtabp,
506 __db_pg_sort_44_print, DB___db_pg_sort_44)) != 0)
508 if ((ret = __db_add_recovery_int(env, dtabp,
509 __fop_create_42_print, DB___fop_create_42)) != 0)
511 if ((ret = __db_add_recovery_int(env, dtabp,
512 __fop_write_42_print, DB___fop_write_42)) != 0)
514 if ((ret = __db_add_recovery_int(env, dtabp,
515 __fop_rename_42_print, DB___fop_rename_42)) != 0)
517 if ((ret = __db_add_recovery_int(env, dtabp,
518 __fop_rename_42_print, DB___fop_rename_noundo_46)) != 0)
520 if ((ret = __db_add_recovery_int(env, dtabp,
521 __txn_xa_regop_42_print, DB___txn_xa_regop_42)) != 0)
529 env_init_print_48(env, dtabp)
534 if ((ret = __db_add_recovery_int(env, dtabp,
535 __db_pg_sort_44_print, DB___db_pg_sort_44)) != 0)
537 if ((ret = __db_add_recovery_int(env, dtabp,
538 __db_addrem_42_print, DB___db_addrem_42)) != 0)
540 if ((ret = __db_add_recovery_int(env, dtabp,
541 __db_big_42_print, DB___db_big_42)) != 0)
543 if ((ret = __db_add_recovery_int(env, dtabp,
544 __bam_split_48_print, DB___bam_split_48)) != 0)
547 if ((ret = __db_add_recovery_int(env, dtabp,
548 __ham_insdel_42_print, DB___ham_insdel_42)) != 0)
550 if ((ret = __db_add_recovery_int(env, dtabp,
551 __ham_replace_42_print, DB___ham_replace_42)) != 0)
562 fprintf(stderr, "usage: %s %s\n", progname,
563 "[-NrV] [-b file/offset] [-e file/offset] [-h home] [-P password]");
564 return (EXIT_FAILURE);
570 int v_major, v_minor, v_patch;
572 /* Make sure we're loaded with the right version of the DB library. */
573 (void)db_version(&v_major, &v_minor, &v_patch);
574 if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
575 fprintf(stderr, DB_STR_A("5015",
576 "%s: version %d.%d doesn't match library version %d.%d\n",
577 "%s %d %d %d %d\n"), progname,
578 DB_VERSION_MAJOR, DB_VERSION_MINOR,
580 return (EXIT_FAILURE);
585 /* Print an unknown, application-specific log record as best we can. */
587 db_printlog_print_app_record(dbenv, dbt, lsnp, op)
593 u_int32_t i, rectype;
596 DB_ASSERT(dbenv->env, op == DB_TXN_PRINT);
598 COMPQUIET(dbenv, NULL);
599 COMPQUIET(op, DB_TXN_PRINT);
602 * Fetch the rectype, which always must be at the beginning of the
603 * record (if dispatching is to work at all).
605 memcpy(&rectype, dbt->data, sizeof(rectype));
608 * Applications may wish to customize the output here based on the
609 * rectype. We just print the entire log record in the generic
610 * mixed-hex-and-printable format we use for binary data.
612 printf(DB_STR_A("5016",
613 "[%lu][%lu]application specific record: rec: %lu\n",
614 "%lu %lu %lu"), (u_long)lsnp->file, (u_long)lsnp->offset,
616 printf(DB_STR("5017", "\tdata: "));
617 for (i = 0; i < dbt->size; i++) {
618 ch = ((u_int8_t *)dbt->data)[i];
619 printf(isprint(ch) || ch == 0x0a ? "%c" : "%#x ", ch);
627 open_rep_db(dbenv, dbpp, dbcp)
638 if ((ret = db_create(dbpp, dbenv, 0)) != 0) {
639 dbenv->err(dbenv, ret, "db_create");
644 if ((ret = dbp->open(dbp, NULL,
645 REPDBNAME, NULL, DB_BTREE, DB_RDONLY, 0)) != 0) {
646 dbenv->err(dbenv, ret, "DB->open");
650 if ((ret = dbp->cursor(dbp, NULL, dbcp, 0)) != 0) {
651 dbenv->err(dbenv, ret, "DB->cursor");
657 err: if (*dbpp != NULL)
658 (void)(*dbpp)->close(*dbpp, 0);
664 * Parse a LSN argument.
675 * Expected format is: lsn.file/lsn.offset.
677 if ((p = strchr(arg, '/')) == NULL)
681 if (__db_getulong(NULL, progname, arg, 0, UINT32_MAX, &uval))
684 if (__db_getulong(NULL, progname, p + 1, 0, UINT32_MAX, &uval))