-
Notifications
You must be signed in to change notification settings - Fork 1
/
thin_debug.cc
345 lines (282 loc) · 8.7 KB
/
thin_debug.cc
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright (C) 2012 Red Hat, Inc. All rights reserved.
//
// This file is part of the thin-provisioning-tools source.
//
// thin-provisioning-tools is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// thin-provisioning-tools is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with thin-provisioning-tools. If not, see
// <http://www.gnu.org/licenses/>.
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/variant.hpp>
#include <getopt.h>
#include <iostream>
#include <libgen.h>
#include <map>
#include <string>
#include <vector>
#include "btree.h"
#include "metadata.h"
#include "metadata_checker.h"
#include "version.h"
using namespace boost;
using namespace persistent_data;
using namespace std;
using namespace thin_provisioning;
namespace {
typedef vector<string> strings;
class formatter {
public:
typedef shared_ptr<formatter> ptr;
virtual ~formatter() {}
typedef optional<string> maybe_string;
void field(string const &name, string const &value) {
fields_.push_back(field_type(name, value));
}
void child(string const &name, formatter::ptr t) {
fields_.push_back(field_type(name, t));
}
virtual void output(ostream &out, int depth = 0) = 0;
protected:
typedef variant<string, ptr> value;
typedef tuple<string, value> field_type;
vector<field_type> fields_;
};
template <typename T>
void
field(formatter &t, string const &name, T const &value) {
t.field(name, lexical_cast<string>(value));
}
//--------------------------------
class xml_formatter : public formatter {
public:
virtual void output(ostream &out, int depth) {
indent(depth, out);
out << "<fields>" << endl;
vector<field_type>::const_iterator it;
for (it = fields_.begin(); it != fields_.end(); ++it) {
if (string const *s = get<string>(&it->get<1>())) {
indent(depth + 1, out);
out << "<field key=\""
<< it->get<0>()
<< "\" value=\""
<< *s
<< "\"/>"
<< endl;
} else {
formatter::ptr f = get<formatter::ptr>(it->get<1>());
f->output(out, depth + 1);
}
}
indent(depth, out);
out << "</fields>" << endl;
}
private:
void indent(int depth, ostream &out) const {
for (int i = 0; i < depth * 2; i++)
out << ' ';
}
};
//--------------------------------
class command {
public:
typedef boost::shared_ptr<command> ptr;
virtual ~command() {}
virtual void exec(strings const &args, ostream &out) = 0;
};
class command_interpreter {
public:
command_interpreter(istream &in, ostream &out)
: in_(in),
out_(out) {
}
void register_command(string const &str, command::ptr cmd) {
commands_.insert(make_pair(str, cmd));
}
void enter_main_loop() {
while (true)
do_once();
}
private:
strings read_input() {
using namespace boost::algorithm;
string input;
getline(in_, input);
strings toks;
split(toks, input, is_any_of(" \t"), token_compress_on);
return toks;
}
void do_once() {
if (in_.eof())
throw runtime_error("input closed");
out_ << "> ";
strings args = read_input();
map<string, command::ptr>::iterator it;
it = commands_.find(args[0]);
if (it == commands_.end())
out_ << "Unrecognised command" << endl;
else
it->second->exec(args, out_);
}
istream &in_;
ostream &out_;
map <string, command::ptr> commands_;
};
//--------------------------------
class hello : public command {
virtual void exec(strings const &args, ostream &out) {
out << "Hello, world!" << endl;
}
};
class show_superblock : public command {
public:
explicit show_superblock(metadata::ptr md)
: md_(md) {
}
virtual void exec(strings const &args, ostream &out) {
xml_formatter f;
superblock const &sb = md_->sb_;
field(f, "csum", sb.csum_);
field(f, "flags", sb.flags_);
field(f, "blocknr", sb.blocknr_);
field(f, "uuid", sb.uuid_); // FIXME: delimit, and handle non-printable chars
field(f, "magic", sb.magic_);
field(f, "version", sb.version_);
field(f, "time", sb.time_);
field(f, "trans id", sb.trans_id_);
field(f, "metadata snap", sb.metadata_snap_);
field(f, "data mapping root", sb.data_mapping_root_);
field(f, "device details root", sb.device_details_root_);
field(f, "data block size", sb.data_block_size_);
field(f, "metadata block size", sb.metadata_block_size_);
field(f, "metadata nr blocks", sb.metadata_nr_blocks_);
field(f, "compat flags", sb.compat_flags_);
field(f, "compat ro flags", sb.compat_ro_flags_);
field(f, "incompat flags", sb.incompat_flags_);
f.output(out, 0);
}
private:
metadata::ptr md_;
};
class device_details_show_traits : public device_details_traits {
public:
static void show(formatter &f, string const &key, device_details const &value) {
field(f, "mapped blocks", value.mapped_blocks_);
field(f, "transaction id", value.transaction_id_);
field(f, "creation time", value.creation_time_);
field(f, "snap time", value.snapshotted_time_);
}
};
class uint64_show_traits : public uint64_traits {
public:
static void show(formatter &f, string const &key, uint64_t const &value) {
field(f, key, lexical_cast<string>(value));
}
};
class block_show_traits : public block_traits {
public:
static void show(formatter &f, string const &key, block_time const &value) {
field(f, "block", value.block_);
field(f, "time", value.time_);
}
};
template <typename ValueTraits>
class show_btree_node : public command {
public:
explicit show_btree_node(metadata::ptr md)
: md_(md) {
}
virtual void exec(strings const &args, ostream &out) {
using namespace persistent_data::btree_detail;
if (args.size() != 2)
throw runtime_error("incorrect number of arguments");
block_address block = lexical_cast<block_address>(args[1]);
block_manager<>::read_ref rr = md_->tm_->read_lock(block);
node_ref<uint64_show_traits> n = btree_detail::to_node<uint64_show_traits>(rr);
if (n.get_type() == INTERNAL)
show_node<uint64_show_traits>(n, out);
else {
node_ref<ValueTraits> n = btree_detail::to_node<ValueTraits>(rr);
show_node<ValueTraits>(n, out);
}
}
private:
template <typename VT>
void show_node(node_ref<VT> n, ostream &out) {
xml_formatter f;
field(f, "csum", n.get_checksum());
field(f, "blocknr", n.get_location());
field(f, "type", n.get_type() == INTERNAL ? "internal" : "leaf");
field(f, "nr entries", n.get_nr_entries());
field(f, "max entries", n.get_max_entries());
field(f, "value size", n.get_value_size());
for (unsigned i = 0; i < n.get_nr_entries(); i++) {
formatter::ptr f2(new xml_formatter);
field(*f2, "key", n.key_at(i));
VT::show(*f2, "value", n.value_at(i));
f.child("child", f2);
}
f.output(out, 0);
}
metadata::ptr md_;
};
//--------------------------------
int debug(string const &path) {
try {
metadata::ptr md(new metadata(path, metadata::OPEN));
command_interpreter interp(cin, cout);
interp.register_command("hello", command::ptr(new hello));
interp.register_command("superblock", command::ptr(new show_superblock(md)));
interp.register_command("m1_node", command::ptr(new show_btree_node<uint64_show_traits>(md)));
interp.register_command("m2_node", command::ptr(new show_btree_node<block_show_traits>(md)));
interp.register_command("detail_node", command::ptr(new show_btree_node<device_details_show_traits>(md)));
interp.enter_main_loop();
} catch (std::exception &e) {
cerr << e.what();
return 1;
}
return 0;
}
void usage(string const &cmd) {
cerr << "Usage: " << cmd << " {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-V|--version}" << endl;
}
}
int main(int argc, char **argv)
{
int c;
const char shortopts[] = "hV";
const struct option longopts[] = {
{ "help", no_argument, NULL, 'h'},
{ "version", no_argument, NULL, 'V'},
{ NULL, no_argument, NULL, 0 }
};
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch(c) {
case 'h':
usage(basename(argv[0]));
return 0;
case 'V':
cerr << THIN_PROVISIONING_TOOLS_VERSION << endl;
return 0;
}
}
if (argc == optind) {
usage(basename(argv[0]));
exit(1);
}
return debug(argv[optind]);
}