2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 2012 Oracle and/or its affiliates. All rights reserved.
14 static const char copyright[] =
15 "Copyright (c) 1996, 2012 Oracle and/or its affiliates. All rights reserved.\n";
18 typedef enum { T_NOTSET, T_DB,
19 T_ENV, T_LOCK, T_LOG, T_MPOOL, T_MUTEX, T_REP, T_TXN } test_t;
21 int db_init __P((DB_ENV *, char *, test_t, u_int32_t, int *));
22 int main __P((int, char *[]));
23 int usage __P((void));
24 int version_check __P((void));
38 u_int32_t cache, flags;
40 int nflag, private, resize, ret;
41 char *db, *home, *p, *passwd, *subdb;
43 if ((progname = __db_rpath(argv[0])) == NULL)
48 if ((ret = version_check()) != 0)
55 exitval = flags = nflag = private = 0;
56 db = home = passwd = subdb = NULL;
58 while ((ch = getopt(argc,
59 argv, "aC:cd:Eefgh:L:lM:mNP:R:rs:tVxX:Z")) != EOF)
62 LF_SET(DB_STAT_ALLOC);
65 if (ttype != T_NOTSET && ttype != T_LOCK)
69 for (p = optarg; *p; ++p)
75 LF_SET(DB_STAT_LOCK_CONF);
78 LF_SET(DB_STAT_LOCK_LOCKERS);
80 case 'm': /* Backward compatible. */
83 LF_SET(DB_STAT_LOCK_OBJECTS);
86 LF_SET(DB_STAT_LOCK_PARAMS);
93 if (ttype != T_NOTSET && ttype != T_DB)
99 if (ttype != T_NOTSET && ttype != T_ENV)
102 LF_SET(DB_STAT_SUBSYSTEM);
107 if (ttype != T_NOTSET && ttype != T_DB)
110 LF_SET(DB_FAST_STAT);
116 if (ttype != T_NOTSET && ttype != T_LOG)
120 for (p = optarg; *p; ++p)
130 if (ttype != T_NOTSET && ttype != T_MPOOL)
134 for (p = optarg; *p; ++p)
140 LF_SET(DB_STAT_MEMP_HASH);
142 case 'm': /* Backward compatible. */
152 if (passwd != NULL) {
153 fprintf(stderr, DB_STR("5139",
154 "Password may not be specified twice"));
156 return (EXIT_FAILURE);
158 passwd = strdup(optarg);
159 memset(optarg, 0, strlen(optarg));
160 if (passwd == NULL) {
161 fprintf(stderr, DB_STR_A("5005",
162 "%s: strdup: %s\n", "%s %s\n"),
163 progname, strerror(errno));
164 return (EXIT_FAILURE);
168 if (ttype != T_NOTSET && ttype != T_REP)
172 for (p = optarg; *p; ++p)
182 if (ttype != T_NOTSET && ttype != T_DB)
188 if (ttype != T_NOTSET) {
189 argcombo: fprintf(stderr, DB_STR_A("5006",
190 "%s: illegal option combination\n",
197 printf("%s\n", db_version(NULL, NULL, NULL));
198 return (EXIT_SUCCESS);
200 if (ttype != T_NOTSET && ttype != T_MUTEX)
204 for (p = optarg; *p; ++p)
214 LF_SET(DB_STAT_CLEAR);
240 if (LF_ISSET(DB_STAT_ALL | DB_STAT_ALLOC) == DB_STAT_ALLOC)
243 /* Handle possible interruptions. */
247 * Create an environment object and initialize it for error
250 retry: if ((ret = db_env_create(&dbenv, 0)) != 0) {
252 "%s: db_env_create: %s\n", progname, db_strerror(ret));
256 dbenv->set_errfile(dbenv, stderr);
257 dbenv->set_errpfx(dbenv, progname);
260 if ((ret = dbenv->set_flags(dbenv, DB_NOLOCKING, 1)) != 0) {
261 dbenv->err(dbenv, ret, "set_flags: DB_NOLOCKING");
264 if ((ret = dbenv->set_flags(dbenv, DB_NOPANIC, 1)) != 0) {
265 dbenv->err(dbenv, ret, "set_flags: DB_NOPANIC");
270 if (passwd != NULL &&
271 (ret = dbenv->set_encrypt(dbenv, passwd, DB_ENCRYPT_AES)) != 0) {
272 dbenv->err(dbenv, ret, "set_passwd");
276 /* Initialize the environment. */
277 if (db_init(dbenv, home, ttype, cache, &private) != 0)
282 /* Create the DB object and open the file. */
283 if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
284 dbenv->err(dbenv, ret, "db_create");
289 * We open the database for writing so we can update the cached
290 * statistics, but it's OK to fail, we can open read-only and
293 * Turn off error messages for now -- we can't open lots of
294 * databases read-write (for example, master databases and
295 * hash databases for which we don't know the hash function).
297 dbenv->set_errfile(dbenv, NULL);
298 ret = dbp->open(dbp, NULL, db, subdb, DB_UNKNOWN, 0, 0);
299 dbenv->set_errfile(dbenv, stderr);
301 /* Handles cannot be reused after a failed DB->open. */
302 (void)dbp->close(dbp, 0);
303 if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
304 dbenv->err(dbenv, ret, "db_create");
308 if ((ret = dbp->open(dbp,
309 NULL, db, subdb, DB_UNKNOWN, DB_RDONLY, 0)) != 0) {
310 dbenv->err(dbenv, ret, "DB->open: %s", db);
315 /* Check if cache is too small for this DB's pagesize. */
317 if ((ret = __db_util_cache(dbp, &cache, &resize)) != 0)
320 (void)dbp->close(dbp, DB_NOSYNC);
323 (void)dbenv->close(dbenv, 0);
329 if (dbp->stat_print(dbp, flags))
333 if (dbenv->stat_print(dbenv, flags))
337 if (dbenv->lock_stat_print(dbenv, flags))
341 if (dbenv->log_stat_print(dbenv, flags))
345 if (dbenv->memp_stat_print(dbenv, flags))
349 if (dbenv->mutex_stat_print(dbenv, flags))
353 #ifdef HAVE_REPLICATION_THREADS
354 if (dbenv->repmgr_stat_print(dbenv, flags))
357 if (dbenv->rep_stat_print(dbenv, flags))
361 if (dbenv->txn_stat_print(dbenv, flags))
365 dbenv->errx(dbenv, DB_STR("5007",
366 "Unknown statistics flag"));
373 if (dbp != NULL && (ret = dbp->close(dbp, DB_NOSYNC)) != 0) {
375 dbenv->err(dbenv, ret, DB_STR("5008", "close"));
377 if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
380 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
386 /* Resend any caught signal. */
387 __db_util_sigresend();
389 return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
394 * Initialize the environment.
397 db_init(dbenv, home, ttype, cache, is_private)
408 * If our environment open fails, and we're trying to look at a
409 * shared region, it's a hard failure.
411 * We will probably just drop core if the environment we join does
412 * not include a memory pool. This is probably acceptable; trying
413 * to use an existing environment that does not contain a memory
414 * pool to look at a database can be safely construed as operator
418 if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) == 0)
420 if (ret == DB_VERSION_MISMATCH)
422 if (ttype != T_DB && ttype != T_LOG) {
423 dbenv->err(dbenv, ret, "DB_ENV->open%s%s",
424 home == NULL ? "" : ": ", home == NULL ? "" : home);
429 * We're looking at a database or set of log files and no environment
430 * exists. Create one, but make it private so no files are actually
431 * created. Declare a reasonably large cache so that we don't fail
432 * when reporting statistics on large databases.
434 * An environment is required to look at databases because we may be
435 * trying to look at databases in directories other than the current
438 if ((ret = dbenv->set_cachesize(dbenv, 0, cache, 1)) != 0) {
439 dbenv->err(dbenv, ret, "set_cachesize");
443 oflags = DB_CREATE | DB_PRIVATE | DB_USE_ENVIRON;
445 oflags |= DB_INIT_MPOOL;
447 oflags |= DB_INIT_LOG;
448 if ((ret = dbenv->open(dbenv, home, oflags, 0)) == 0)
451 /* An environment is required. */
452 err: dbenv->err(dbenv, ret, "DB_ENV->open");
459 fprintf(stderr, "usage: %s %s\n", progname,
460 "-d file [-fN] [-h home] [-P password] [-s database]");
461 fprintf(stderr, "usage: %s %s\n\t%s\n", progname,
462 "[-cEelmrtVx] [-C Aclop]",
463 "[-h home] [-L A] [-M A] [-P password] [-R A] [-X A] [-aNZ]");
464 return (EXIT_FAILURE);
470 int v_major, v_minor, v_patch;
472 /* Make sure we're loaded with the right version of the DB library. */
473 (void)db_version(&v_major, &v_minor, &v_patch);
474 if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
475 fprintf(stderr, DB_STR_A("5009",
476 "%s: version %d.%d doesn't match library version %d.%d\n",
477 "%s %d %d %d %d\n"), progname,
478 DB_VERSION_MAJOR, DB_VERSION_MINOR,
480 return (EXIT_FAILURE);