1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This command-line program dumps the contents of a set of cache files, either
6 // to stdout or to another set of cache files.
7
8 #include <stdio.h>
9 #include <string>
10
11 #include "base/at_exit.h"
12 #include "base/command_line.h"
13 #include "base/strings/string_util.h"
14 #include "net/disk_cache/blockfile/disk_format.h"
15 #include "net/tools/dump_cache/dump_files.h"
16
17 enum Errors {
18 GENERIC = -1,
19 ALL_GOOD = 0,
20 INVALID_ARGUMENT = 1,
21 FILE_ACCESS_ERROR,
22 UNKNOWN_VERSION,
23 TOOL_NOT_FOUND,
24 };
25
26 // Dumps the file headers to stdout.
27 const char kDumpHeaders[] = "dump-headers";
28
29 // Dumps all entries to stdout.
30 const char kDumpContents[] = "dump-contents";
31
32 // Dumps the LRU lists(s).
33 const char kDumpLists[] = "dump-lists";
34
35 // Dumps the entry at the given address (see kDumpAt).
36 const char kDumpEntry[] = "dump-entry";
37
38 // The cache address to dump.
39 const char kDumpAt[] = "at";
40
41 // Dumps the allocation bitmap of a file (see kDumpFile).
42 const char kDumpAllocation[] = "dump-allocation";
43
44 // The file to look at.
45 const char kDumpFile[] = "file";
46
Help()47 int Help() {
48 printf("dump_cache path_to_files [options]\n");
49 printf("Dumps internal cache structures.\n");
50 printf("warning: input files may be modified by this tool\n\n");
51 printf("--dump-headers: show file headers\n");
52 printf("--dump-contents [-v] [--full-key] [--csv]: list all entries\n");
53 printf("--dump-lists: follow the LRU list(s)\n");
54 printf(
55 "--dump-entry [-v] [--full-key] --at=0xf00: show the data stored at"
56 " 0xf00\n");
57 printf(
58 "--dump-allocation --file=data_0: show the allocation bitmap of"
59 " data_0\n");
60 printf("--csv: dump in a comma-separated-values format\n");
61 printf(
62 "--full-key: show up to 160 chars for the key. Use either -v or the"
63 " key address for longer keys\n");
64 printf("-v: detailed output (verbose)\n");
65 return INVALID_ARGUMENT;
66 }
67
68 // -----------------------------------------------------------------------
69
main(int argc,const char * argv[])70 int main(int argc, const char* argv[]) {
71 // Setup an AtExitManager so Singleton objects will be destroyed.
72 base::AtExitManager at_exit_manager;
73
74 base::CommandLine::Init(argc, argv);
75
76 const base::CommandLine& command_line =
77 *base::CommandLine::ForCurrentProcess();
78 base::CommandLine::StringVector args = command_line.GetArgs();
79 if (args.size() != 1)
80 return Help();
81
82 base::FilePath input_path(args[0]);
83 if (input_path.empty())
84 return Help();
85
86 int version = GetMajorVersion(input_path);
87 if (version != 2)
88 return FILE_ACCESS_ERROR;
89
90 if (command_line.HasSwitch(kDumpContents))
91 return DumpContents(input_path);
92
93 if (command_line.HasSwitch(kDumpLists))
94 return DumpLists(input_path);
95
96 if (command_line.HasSwitch(kDumpEntry) && command_line.HasSwitch(kDumpAt))
97 return DumpEntryAt(input_path, command_line.GetSwitchValueASCII(kDumpAt));
98
99 if (command_line.HasSwitch(kDumpAllocation) &&
100 command_line.HasSwitch(kDumpFile)) {
101 base::FilePath name =
102 input_path.AppendASCII(command_line.GetSwitchValueASCII(kDumpFile));
103 return DumpAllocation(name);
104 }
105
106 if (command_line.HasSwitch(kDumpHeaders))
107 return DumpHeaders(input_path);
108
109 return Help();
110 }
111