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