### Berkeley DB Reference Guide: Debugging Applications |

# Reviewing Berkeley DB log files

If you are running with transactions and logging, the db_printlog utility can be a useful debugging aid. The db_printlog utility will display the contents of your log files in a human readable (and machine-processable) format.

The db_printlog utility will attempt to display any and all logfiles present in a designated db_home directory. For each log record, db_printlog will display a line of the form:

>     [22][28]db_big: rec: 43 txnid 80000963 prevlsn [21][10483281]

The opening numbers in square brackets are the log sequence number (LSN) of the log record being displayed. The first number indicates the log file in which the record appears, and the second number indicates the offset in that file of the record.

The first character string identifies the particular log operation being reported. The log records corresponding to particular operations are described below. The rest of the line consists of name/value pairs.

The rec field indicates the record type (this is used to dispatch records in the log to appropriate recovery functions).

The txnid field identifies the transaction for which this record was written. A txnid of 0 means that the record was written outside the context of any transaction. You will see these most frequently for checkpoints.

Finally, the prevlsn contains the LSN of the last record for this transaction. By following prevlsn fields, you can accumulate all the updates for a particular transaction. During normal abort processing, this field is used to quickly access all the records for a particular transaction.

After the initial line identifying the record type, each field of the log record is displayed, one item per line. There are several fields that appear in many different records and a few fields that appear only in some records.

The list below presents each log record type currently produced with a brief description of the operation they describe.

Log Record Type | Description
--- | ---
bam_adj | Used when we insert/remove an index into/from the page header of a Btree page.
bam_cadjust | Keeps track of record counts in a Btree or Recno database.
bam_cdel | Used to mark a record on a page as deleted.
bam_pg_alloc | Indicates that we allocated a page to a Btree.
bam_pg_free | Indicates that we freed a page in the Btree (freed pages are added to a freelist and reused).
bam_repl | Describes a replace operation on a record.
bam_root | Describes an assignment of a root page.
bam_rsplit | Describes a reverse page split.
bam_split | Describes a page split.
crdel_delete | Describes the removal of a Berkeley DB file.
crdel_fileopen | Describes a Berkeley DB file create attempt.
crdel_metapage | Describes the creation of a meta-data page for a new file.
crdel_metasub | Describes the creation of a meta data page for a subdatabase.
crdel_rename | Describes a file rename operation.
db_addrem | Add or remove an item from a page of duplicates.
db_big | Add an item to an overflow page (overflow pages contain items too large to place on the main page)
db_debug | Log debugging message.
db_noop | This marks an operation that did nothing but update the LSN on a page.
db_ovref | Increment or decrement the reference count for a big item.
db_relink | Fix prev/next chains on duplicate pages because a page was added or removed.
ham_copypage | Used when we empty a bucket page, but there are overflow pages for the bucket; one needs to be copied back into the actual bucket.
ham_groupalloc | Allocate some number of contiguous pages to the Hash database.
ham_insdel | Insert/Delete an item on a Hash page.
ham_metagroup | Update the metadata page to reflect the allocation of a sequence of contiguous pages.
ham_newpage | Adds or removes overflow pages from a Hash bucket.
ham_replace | Handle updates to records that are on the main page.
ham_splitdata | Record the page data for a split.
log_register | Records an open of a file (mapping the file name to a log-id that is used in subsequent log operations).
qam_add | Describes the actual addition of a new record to a Queue.
qam_del | Delete a record in a Queue.
qam_inc | Increments the maximum record number allocated in a Queue indicating that we've allocated another space in the file.
qam_incfirst | Increments the record number that refers to the first record in the database.
qam_mvptr | Indicates that we changed the reference to either or both of the first and current records in the file.
txn_child | Commit a child transaction.
txn_ckp | Transaction checkpoint.
txn_regop | Logs a regular (non-child) transaction commit.
txn_xa_regop | Logs a prepare message.

### Augmenting the Log for Debugging

When debugging applications, it is sometimes useful to log, not only the actual operations that modify pages, but also the underlying Berkeley DB functions being executed. This form of logging can add significant bulk to your log, but can permit debugging application errors that are almost impossible to find any other way. To turn on these log messages, specify the --enable-debug_rop and --enable-debug_wop configuration options when configuring Berkeley DB. See Configuring Berkeley DB for more information.

db_debug
  Presents a debugging message or describes an API call into the DB library.

### Extracting Committed Transactions and Transaction Status

Sometimes it is useful to use the human-readable log output to determine which transactions committed and aborted. The awk script, commit.awk, found in the db_printlog directory of the Berkeley DB distribution allows you to do just that. The command:

>     awk -f commit.awk log_output

where log_output is the output of db_printlog will display a list of the transaction IDs of all committed transactions found in the log.

If you need a complete list of both committed and aborted transactions, then the script status.awk will produce that. The syntax is:

>     awk -f status.awk log_output

### Extracting Transaction Histories

Another useful debugging aid is to print out the complete history of a transaction. The awk script txn.awk, allows you to do that. The command line:

>     awk -f txn.awk TXN=txnlist log_output

where log_output is the output of db_printlog and txnlist is a comma-separated list of transaction IDs, will display all log records associated with the designated transaction ids.

### Extracting Page Histories

The awk script pgno.awk, allows you to extract all log records that affect particular pages. As currently designed, however, it will extract records of all files with the designated page number, so this script is most useful in conjunction with the fileid script discussed below.

The syntax for the pgno.awk script is:

>     awk -f pgno.awk PGNO=pgnolist log_output

where log_output is the output of db_printlog and pgnolist is a comma-separated list of page numbers. The script will output all log records that reference the designated page numbers.

### Extracting File Histories

The awk script fileid.awk, allows you to extract all log records that affect particular files. The syntax for the fileid.awk script is:

>     awk -f fileid.awk PGNO=fids log_output

where log_output is the output of db_printlog and fids is a comma-separated list of fileids. The script will output all log records that reference the designated file.

### Other log processing tools

The awk script count.awk will print out the number of log records encountered that belonged to some transaction (that is the number of log records excluding those for checkpoints and non-transaction protected operations).

The script range.awk will extract a subset of a log. This is useful when the output of db_printlog is too large to be reasonably manipulated with an editor or other tool.

The syntax for range.awk is:

>     awk -f range.awk START_FILE=sf START_OFFSET=so END_FILE=ef END_OFFSET=eo log_output

where the tuple [sf,so] represents the log sequence number (LSN) of the beginning of the sublog you wish to extract and [ef, eo] represents the LSN of the end of the sublog you wish to extract.

Copyright Sleepycat Software