File: dump_cache.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (114 lines) | stat: -rw-r--r-- 3,404 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This command-line program dumps the contents of a set of cache files, either
// to stdout or to another set of cache files.

#include <stdio.h>
#include <string>

#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/i18n/icu_util.h"
#include "base/strings/string_util.h"
#include "net/disk_cache/blockfile/disk_format.h"
#include "net/tools/dump_cache/dump_files.h"

enum Errors {
  GENERIC = -1,
  ALL_GOOD = 0,
  INVALID_ARGUMENT = 1,
  FILE_ACCESS_ERROR,
  UNKNOWN_VERSION,
  TOOL_NOT_FOUND,
};

// Dumps the file headers to stdout.
const char kDumpHeaders[] = "dump-headers";

// Dumps all entries to stdout.
const char kDumpContents[] = "dump-contents";

// Dumps the LRU lists(s).
const char kDumpLists[] = "dump-lists";

// Dumps the entry at the given address (see kDumpAt).
const char kDumpEntry[] = "dump-entry";

// The cache address to dump.
const char kDumpAt[] = "at";

// Dumps the allocation bitmap of a file (see kDumpFile).
const char kDumpAllocation[] = "dump-allocation";

// The file to look at.
const char kDumpFile[] = "file";

int Help() {
  printf("dump_cache path_to_files [options]\n");
  printf("Dumps internal cache structures.\n");
  printf("warning: input files may be modified by this tool\n\n");
  printf("--dump-headers: show file headers\n");
  printf("--dump-contents [-v] [--full-key] [--csv]: list all entries\n");
  printf("--dump-lists: follow the LRU list(s)\n");
  printf(
      "--dump-entry [-v] [--full-key] --at=0xf00: show the data stored at"
      " 0xf00\n");
  printf(
      "--dump-allocation --file=data_0: show the allocation bitmap of"
      " data_0\n");
  printf("--csv: dump in a comma-separated-values format\n");
  printf(
      "--full-key: show up to 160 chars for the key. Use either -v or the"
      " key address for longer keys\n");
  printf("-v: detailed output (verbose)\n");
  return INVALID_ARGUMENT;
}

// -----------------------------------------------------------------------

int main(int argc, const char* argv[]) {
  // Setup an AtExitManager so Singleton objects will be destroyed.
  base::AtExitManager at_exit_manager;

  // base::UnlocalizedTimeFormatWithPattern() depends on ICU.
  base::i18n::InitializeICU();

  base::CommandLine::Init(argc, argv);

  const base::CommandLine& command_line =
      *base::CommandLine::ForCurrentProcess();
  base::CommandLine::StringVector args = command_line.GetArgs();
  if (args.size() != 1)
    return Help();

  base::FilePath input_path(args[0]);
  if (input_path.empty())
    return Help();

  if (!CheckFileVersion(input_path)) {
    return FILE_ACCESS_ERROR;
  }

  if (command_line.HasSwitch(kDumpContents))
    return DumpContents(input_path);

  if (command_line.HasSwitch(kDumpLists))
    return DumpLists(input_path);

  if (command_line.HasSwitch(kDumpEntry) && command_line.HasSwitch(kDumpAt))
    return DumpEntryAt(input_path, command_line.GetSwitchValueASCII(kDumpAt));

  if (command_line.HasSwitch(kDumpAllocation) &&
      command_line.HasSwitch(kDumpFile)) {
    base::FilePath name =
        input_path.AppendASCII(command_line.GetSwitchValueASCII(kDumpFile));
    return DumpAllocation(name);
  }

  if (command_line.HasSwitch(kDumpHeaders))
    return DumpHeaders(input_path);

  return Help();
}