diff --git a/Makefile.in b/Makefile.in index ea2bdca..949e30a 100644 --- a/Makefile.in +++ b/Makefile.in @@ -126,10 +126,14 @@ TOOLS_SOURCE=\ thin-provisioning/thin_trim.cc DEVTOOLS_SOURCE=\ + base/command_interpreter.cc \ + base/output_formatter.cc \ + caching/cache_debug.cc \ caching/devel_commands.cc \ era/devel_commands.cc \ thin-provisioning/damage_generator.cc \ thin-provisioning/devel_commands.cc \ + thin-provisioning/thin_debug.cc \ thin-provisioning/thin_generate_damage.cc \ thin-provisioning/thin_generate_mappings.cc \ thin-provisioning/thin_generate_metadata.cc \ diff --git a/base/command_interpreter.cc b/base/command_interpreter.cc new file mode 100644 index 0000000..ce7bd97 --- /dev/null +++ b/base/command_interpreter.cc @@ -0,0 +1,88 @@ +#include "base/command_interpreter.h" + +using namespace dbg; +using namespace std; + +//---------------------------------------------------------------- + +namespace { + class command_interpreter_impl : public command_interpreter { + public: + typedef std::shared_ptr ptr; + + command_interpreter_impl(std::istream &in, std::ostream &out) + : in_(in), + out_(out), + exit_(false) { + } + + void register_command(std::string const &str, command::ptr cmd) { + commands_.insert(make_pair(str, cmd)); + } + + void enter_main_loop() { + while (!exit_) + do_once(); + } + + void exit_main_loop() { + exit_ = true; + } + + private: + void do_once(); + + std::istream &in_; + std::ostream &out_; + std::map commands_; + bool exit_; + }; + + //-------------------------------- + + strings read_input(std::istream &in) + { + using namespace boost::algorithm; + + std::string input; + getline(in, input); + + strings toks; + split(toks, input, is_any_of(" \t"), token_compress_on); + + return toks; + } +} + +//---------------------------------------------------------------- + +void command_interpreter_impl::do_once() +{ + if (in_.eof()) + throw runtime_error("input closed"); + + out_ << "> "; + strings args = read_input(in_); + + std::map::iterator it; + it = commands_.find(args[0]); + if (it == commands_.end()) + out_ << "Unrecognised command" << endl; + else { + try { + it->second->exec(args, out_); + } catch (std::exception &e) { + cerr << e.what() << endl; + } + } +} + +//---------------------------------------------------------------- + +command_interpreter::ptr +dbg::create_command_interpreter(std::istream &in, std::ostream &out) +{ + return command_interpreter::ptr(new command_interpreter_impl(in, out)); +} + +//---------------------------------------------------------------- diff --git a/base/command_interpreter.h b/base/command_interpreter.h new file mode 100644 index 0000000..b0646d9 --- /dev/null +++ b/base/command_interpreter.h @@ -0,0 +1,40 @@ +#ifndef DBG_COMMAND_INTERPRETER +#define DBG_COMMAND_INTERPRETER + +#include +#include + +#include +#include +#include +#include + +//---------------------------------------------------------------- + +namespace dbg { + typedef std::vector strings; + + class command { + public: + typedef std::shared_ptr ptr; + + virtual ~command() {} + virtual void exec(strings const &args, std::ostream &out) = 0; + }; + + class command_interpreter { + public: + typedef std::shared_ptr ptr; + + virtual void register_command(std::string const &str, command::ptr cmd) = 0; + virtual void enter_main_loop() = 0; + virtual void exit_main_loop() = 0; + }; + + command_interpreter::ptr + create_command_interpreter(std::istream &in, std::ostream &out); +} + +//---------------------------------------------------------------- + +#endif diff --git a/base/output_formatter.cc b/base/output_formatter.cc new file mode 100644 index 0000000..c62058f --- /dev/null +++ b/base/output_formatter.cc @@ -0,0 +1,86 @@ +#include + +#include "base/output_formatter.h" + +using namespace dbg; +using namespace std; + +//---------------------------------------------------------------- + +namespace { + class abstract_formatter : public formatter { + typedef boost::optional maybe_string; + + void field(std::string const &name, std::string const &value) { + fields_.push_back(field_type(name, value)); + } + + void child(std::string const &name, formatter::ptr t) { + children_.push_back(field_type(name, t)); + } + + protected: + typedef boost::variant value; + typedef boost::tuple field_type; + + std::vector fields_; + std::vector children_; + }; + + class xml_formatter : public abstract_formatter { + public: + void output(std::ostream &out, int depth = 0, + boost::optional name = boost::none); + }; + + void indent(int depth, std::ostream &out) { + for (int i = 0; i < depth * 2; i++) + out << ' '; + } +} + +//---------------------------------------------------------------- + +void xml_formatter::output(std::ostream &out, + int depth, + boost::optional name) { + indent(depth, out); + out << "::const_iterator it; + for (it = fields_.begin(); it != fields_.end(); ++it) { + if (string const *s = boost::get(&it->get<1>())) { + out << " " << it->get<0>() << "=\"" << *s << "\""; + } + } + + if (children_.size() == 0) { + out << " />" << endl; + return; + } + + /* output child fields */ + out << ">" << endl; + for (it = children_.begin(); it != children_.end(); ++it) { + if (!boost::get(&it->get<1>())) { + formatter::ptr f = boost::get(it->get<1>()); + f->output(out, depth + 1, it->get<0>()); + } + } + + indent(depth, out); + out << "" << endl; +} + +//---------------------------------------------------------------- + +formatter::ptr +dbg::create_xml_formatter() +{ + return formatter::ptr(new xml_formatter()); +} + +//---------------------------------------------------------------- diff --git a/base/output_formatter.h b/base/output_formatter.h new file mode 100644 index 0000000..2cc4bc5 --- /dev/null +++ b/base/output_formatter.h @@ -0,0 +1,37 @@ +#ifndef DBG_OUTPUT_FORMATTER_H +#define DBG_OUTPUT_FORMATTER_H + +#include +#include +#include +#include + +#include +#include + +//---------------------------------------------------------------- + +namespace dbg { + class formatter { + public: + typedef std::shared_ptr ptr; + + virtual ~formatter() {} + virtual void field(std::string const &name, std::string const &value) = 0; + virtual void child(std::string const &name, formatter::ptr t) = 0; + virtual void output(std::ostream &out, int depth = 0, + boost::optional name = boost::none) = 0; + }; + + template + void + field(formatter &t, std::string const &name, T const &value) { + t.field(name, boost::lexical_cast(value)); + } + + formatter::ptr create_xml_formatter(); +} + +//---------------------------------------------------------------- + +#endif diff --git a/caching/cache_debug.cc b/caching/cache_debug.cc new file mode 100644 index 0000000..1053560 --- /dev/null +++ b/caching/cache_debug.cc @@ -0,0 +1,331 @@ +// 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 +// . + +#include +#include +#include +#include + +#include "base/command_interpreter.h" +#include "base/output_formatter.h" +#include "persistent-data/file_utils.h" +#include "persistent-data/data-structures/btree.h" +#include "persistent-data/data-structures/simple_traits.h" +#include "persistent-data/space-maps/disk_structures.h" +#include "caching/commands.h" +#include "caching/metadata.h" +#include "version.h" + +using namespace dbg; +using namespace persistent_data; +using namespace std; +using namespace caching; + +//---------------------------------------------------------------- + +namespace { + class hello : public dbg::command { + virtual void exec(strings const &args, ostream &out) { + out << "Hello, world!" << endl; + } + }; + + class help : public dbg::command { + virtual void exec(strings const &args, ostream &out) { + out << "Commands:" << endl + << " superblock" << endl + << " mapping_node " << endl + << " mapping_array " << endl + << " exit" << endl; + } + }; + + class exit_handler : public dbg::command { + public: + exit_handler(command_interpreter::ptr interpreter) + : interpreter_(interpreter) { + } + + virtual void exec(strings const &args, ostream &out) { + out << "Goodbye!" << endl; + interpreter_->exit_main_loop(); + } + + command_interpreter::ptr interpreter_; + }; + + class sm_root_show_traits : public persistent_data::sm_disk_detail::sm_root_traits { + public: + static void show(formatter::ptr f, string const &key, + persistent_data::sm_disk_detail::sm_root const &value) { + field(*f, "nr_blocks", value.nr_blocks_); + field(*f, "nr_allocated", value.nr_allocated_); + field(*f, "bitmap_root", value.bitmap_root_); + field(*f, "ref_count_root", value.ref_count_root_); + } + }; + + class show_superblock : public dbg::command { + public: + explicit show_superblock(metadata::ptr md) + : md_(md) { + } + + virtual void exec(strings const &args, ostream &out) { + formatter::ptr f = create_xml_formatter(); + ostringstream version; + + superblock const &sb = md_->sb_; + + field(*f, "csum", sb.csum); + field(*f, "flags", sb.flags.encode()); + 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, "policy_name", reinterpret_cast(sb.policy_name)); + version << sb.policy_version[0] << "." + << sb.policy_version[1] << "." + << sb.policy_version[2]; + field(*f, "policy_version", version.str().c_str()); + field(*f, "policy_hint_size", sb.policy_hint_size); + + sm_disk_detail::sm_root_disk const *d; + sm_disk_detail::sm_root v; + { + d = reinterpret_cast(sb.metadata_space_map_root); + sm_disk_detail::sm_root_traits::unpack(*d, v); + formatter::ptr f2 = create_xml_formatter(); + sm_root_show_traits::show(f2, "value", v); + f->child("metadata_space_map_root", f2); + } + + field(*f, "mapping_root", sb.mapping_root); + if (sb.version >= 2) + field(*f, "dirty_root", *sb.dirty_root); + field(*f, "hint_root", sb.hint_root); + field(*f, "discard_root", sb.discard_root); + field(*f, "discard_block_size", sb.discard_block_size); + field(*f, "discard_nr_blocks", sb.discard_nr_blocks); + field(*f, "data_block_size", sb.data_block_size); + field(*f, "metadata_block_size", sb.metadata_block_size); + field(*f, "cache_blocks", sb.cache_blocks); + field(*f, "compat_flags", sb.compat_flags); + field(*f, "compat_ro_flags", sb.compat_ro_flags); + field(*f, "incompat_flags", sb.incompat_flags); + field(*f, "read_hits", sb.read_hits); + field(*f, "read_misses", sb.read_misses); + field(*f, "write_hits", sb.write_hits); + field(*f, "write_misses", sb.write_misses); + + f->output(out, 0); + } + + private: + metadata::ptr md_; + }; + + class uint64_show_traits : public uint64_traits { + public: + static void show(formatter::ptr f, string const &key, uint64_t const &value) { + field(*f, key, boost::lexical_cast(value)); + } + }; + + // FIXME: Expose the array ValueTraits parameter or not? + // Since the block_traits isn't related to the ValueTraits. + class block_show_traits : public persistent_data::array::block_traits { + public: + static void show(formatter::ptr f, string const &key, block_address const &value) { + field(*f, "block", value); + } + }; + + class mapping_show_traits : public caching::mapping_traits { + public: + static void show(formatter::ptr f, string const &key, caching::mapping const &value) { + field(*f, "oblock", value.oblock_); + field(*f, "flags", value.flags_); + } + }; + + template + class show_btree_node : public dbg::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 = boost::lexical_cast(args[1]); + block_manager::read_ref rr = md_->tm_->read_lock(block); + + node_ref n = btree_detail::to_node(rr); + if (n.get_type() == INTERNAL) + show_node(n, out); + else { + node_ref n = btree_detail::to_node(rr); + show_node(n, out); + } + } + + private: + template + void show_node(node_ref n, ostream &out) { + formatter::ptr f = create_xml_formatter(); + + 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 = create_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_; + }; + + template + class show_array_block : public dbg::command { + typedef array_block rblock; + public: + explicit show_array_block(metadata::ptr md) + : md_(md) { + } + + virtual void exec(strings const& args, ostream &out) { + if (args.size() != 2) + throw runtime_error("incorrect number of arguments"); + + block_address block = boost::lexical_cast(args[1]); + block_manager::read_ref rr = md_->tm_->read_lock(block); + + rblock b(rr, typename ValueTraits::ref_counter()); + show_array_entries(b, out); + } + + private: + void show_array_entries(rblock const& b, ostream &out) { + formatter::ptr f = create_xml_formatter(); + uint32_t nr_entries = b.nr_entries(); + + field(*f, "max_entries", b.max_entries()); + field(*f, "nr_entries", nr_entries); + field(*f, "value_size", b.value_size()); + + for (unsigned i = 0; i < nr_entries; i++) { + formatter::ptr f2 = create_xml_formatter(); + field(*f2, "index", i); + ValueTraits::show(f2, "value", b.get(i)); + f->child("child", f2); + } + + f->output(out, 0); + } + + metadata::ptr md_; + }; + + //-------------------------------- + + int debug(string const &path) { + using dbg::command; + + try { + block_manager::ptr bm = open_bm(path, block_manager::READ_ONLY); + metadata::ptr md(new metadata(bm)); + command_interpreter::ptr interp = create_command_interpreter(cin, cout); + interp->register_command("hello", command::ptr(new hello)); + interp->register_command("superblock", command::ptr(new show_superblock(md))); + interp->register_command("mapping_node", command::ptr(new show_btree_node(md))); + interp->register_command("mapping_array", command::ptr(new show_array_block(md))); + interp->register_command("help", command::ptr(new help)); + interp->register_command("exit", command::ptr(new exit_handler(interp))); + interp->enter_main_loop(); + + } catch (std::exception &e) { + cerr << e.what(); + return 1; + } + + return 0; + } +} + +//---------------------------------------------------------------- + +cache_debug_cmd::cache_debug_cmd() + : command("cache_debug") +{ +} + +void +cache_debug_cmd::usage(std::ostream &out) const +{ + out << "Usage: " << get_name() << " {device|file}" << endl + << "Options:" << endl + << " {-h|--help}" << endl + << " {-V|--version}" << endl; +} + +int +cache_debug_cmd::run(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(cout); + return 0; + + case 'V': + cerr << THIN_PROVISIONING_TOOLS_VERSION << endl; + return 0; + } + } + + if (argc == optind) { + usage(cerr); + exit(1); + } + + return debug(argv[optind]); +} + +//---------------------------------------------------------------- diff --git a/caching/commands.h b/caching/commands.h index b0546ab..04deac9 100644 --- a/caching/commands.h +++ b/caching/commands.h @@ -70,6 +70,18 @@ namespace caching { virtual int run(int argc, char **argv); }; + //------------------------------------------------------ + + class cache_debug_cmd : public base::command { + public: + cache_debug_cmd(); + + virtual void usage(std::ostream &out) const; + virtual int run(int argc, char **argv); + }; + + //------------------------------------------------------ + void register_cache_commands(base::application &app); } diff --git a/caching/devel_commands.cc b/caching/devel_commands.cc index 21a1188..c63a68d 100644 --- a/caching/devel_commands.cc +++ b/caching/devel_commands.cc @@ -8,6 +8,7 @@ using namespace caching; void caching::register_cache_commands(application &app) { + app.add_cmd(command::ptr(new cache_debug_cmd)); } //---------------------------------------------------------------- diff --git a/thin-provisioning/commands.h b/thin-provisioning/commands.h index 0f603ae..2067970 100644 --- a/thin-provisioning/commands.h +++ b/thin-provisioning/commands.h @@ -73,6 +73,13 @@ namespace thin_provisioning { //------------------------------------------------------ + class thin_debug_cmd : public base::command { + public: + thin_debug_cmd(); + virtual void usage(std::ostream &out) const; + virtual int run(int argc, char **argv); + }; + class thin_generate_damage_cmd : public base::command { public: thin_generate_damage_cmd(); @@ -94,6 +101,13 @@ namespace thin_provisioning { virtual int run(int argc, char **argv); }; + class thin_journal_cmd : public base::command { + public: + thin_journal_cmd(); + virtual void usage(std::ostream &out) const; + virtual int run(int argc, char **argv); + }; + class thin_ll_dump_cmd : public base::command { public: thin_ll_dump_cmd(); @@ -140,13 +154,6 @@ namespace thin_provisioning { virtual int run(int argc, char **argv); }; - class thin_journal_cmd : public base::command { - public: - thin_journal_cmd(); - virtual void usage(std::ostream &out) const; - virtual int run(int argc, char **argv); - }; - void register_thin_commands(base::application &app); } diff --git a/thin-provisioning/devel_commands.cc b/thin-provisioning/devel_commands.cc index 7a12297..e0f0ae5 100644 --- a/thin-provisioning/devel_commands.cc +++ b/thin-provisioning/devel_commands.cc @@ -8,6 +8,7 @@ using namespace thin_provisioning; void thin_provisioning::register_thin_commands(base::application &app) { + app.add_cmd(command::ptr(new thin_debug_cmd())); app.add_cmd(command::ptr(new thin_generate_damage_cmd())); app.add_cmd(command::ptr(new thin_generate_mappings_cmd())); app.add_cmd(command::ptr(new thin_generate_metadata_cmd())); diff --git a/thin-provisioning/thin_debug.cc b/thin-provisioning/thin_debug.cc index 48ad544..e458d7d 100644 --- a/thin-provisioning/thin_debug.cc +++ b/thin-provisioning/thin_debug.cc @@ -16,227 +16,174 @@ // with thin-provisioning-tools. If not, see // . -#include -#include #include -#include -#include #include #include -#include -#include #include -#include +#include "base/command_interpreter.h" +#include "base/math_utils.h" +#include "base/output_formatter.h" #include "persistent-data/data-structures/btree.h" +#include "persistent-data/data-structures/simple_traits.h" +#include "persistent-data/file_utils.h" +#include "persistent-data/space-maps/disk_structures.h" +#include "thin-provisioning/commands.h" #include "thin-provisioning/metadata.h" -#include "thin-provisioning/metadata_checker.h" +#include "thin-provisioning/superblock.h" #include "version.h" -using namespace boost; +using namespace dbg; using namespace persistent_data; using namespace std; using namespace thin_provisioning; namespace { - typedef vector strings; - - class formatter { - public: - typedef shared_ptr ptr; - - virtual ~formatter() {} - - typedef optional 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 value; - typedef tuple field_type; - - vector fields_; - }; - - template - void - field(formatter &t, string const &name, T const &value) { - t.field(name, lexical_cast(value)); - } - - //-------------------------------- - - class xml_formatter : public formatter { - public: - virtual void output(ostream &out, int depth) { - indent(depth, out); - out << "" << endl; - vector::const_iterator it; - for (it = fields_.begin(); it != fields_.end(); ++it) { - if (string const *s = get(&it->get<1>())) { - indent(depth + 1, out); - out << "get<0>() - << "\" value=\"" - << *s - << "\"/>" - << endl; - - } else { - formatter::ptr f = get(it->get<1>()); - f->output(out, depth + 1); - } - } - - indent(depth, out); - out << "" << endl; - } - - private: - void indent(int depth, ostream &out) const { - for (int i = 0; i < depth * 2; i++) - out << ' '; - } - }; - - //-------------------------------- - - class command { - public: - typedef std::shared_ptr 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::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 commands_; - }; - - //-------------------------------- - - class hello : public command { + class hello : public dbg::command { virtual void exec(strings const &args, ostream &out) { out << "Hello, world!" << endl; } }; - class show_superblock : public command { + class help : public dbg::command { + virtual void exec(strings const &args, ostream &out) { + out << "Commands:" << endl + << " superblock" << endl + << " m1_node " << endl + << " m2_node " << endl + << " detail_node " << endl + << " index_block " << endl + << " index_node " << endl + << " exit" << endl; + } + }; + + class exit_handler : public dbg::command { + public: + exit_handler(command_interpreter::ptr interpreter) + : interpreter_(interpreter) { + } + + virtual void exec(strings const &args, ostream &out) { + out << "Goodbye!" << endl; + interpreter_->exit_main_loop(); + } + + command_interpreter::ptr interpreter_; + }; + + class sm_root_show_traits : public persistent_data::sm_disk_detail::sm_root_traits { + public: + static void show(formatter::ptr f, string const &key, + persistent_data::sm_disk_detail::sm_root const &value) { + field(*f, "nr_blocks", value.nr_blocks_); + field(*f, "nr_allocated", value.nr_allocated_); + field(*f, "bitmap_root", value.bitmap_root_); + field(*f, "ref_count_root", value.ref_count_root_); + } + }; + + class show_superblock : public dbg::command { public: explicit show_superblock(metadata::ptr md) : md_(md) { } virtual void exec(strings const &args, ostream &out) { - xml_formatter f; + formatter::ptr f = create_xml_formatter(); - superblock const &sb = md_->sb_; + thin_provisioning::superblock_detail::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_); + 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_); - f.output(out, 0); + sm_disk_detail::sm_root_disk const *d; + sm_disk_detail::sm_root v; + { + d = reinterpret_cast(sb.metadata_space_map_root_); + sm_disk_detail::sm_root_traits::unpack(*d, v); + formatter::ptr f2 = create_xml_formatter(); + sm_root_show_traits::show(f2, "value", v); + f->child("metadata_space_map_root", f2); + } + { + d = reinterpret_cast(sb.data_space_map_root_); + sm_disk_detail::sm_root_traits::unpack(*d, v); + formatter::ptr f2 = create_xml_formatter(); + sm_root_show_traits::show(f2, "value", v); + f->child("data_space_map_root", f2); + } + + 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 { + class device_details_show_traits : public thin_provisioning::device_tree_detail::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_); + typedef thin_provisioning::device_tree_detail::device_details_traits value_trait; + + static void show(formatter::ptr f, string const &key, + thin_provisioning::device_tree_detail::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(value)); + typedef uint64_traits value_trait; + + static void show(formatter::ptr f, string const &key, uint64_t const &value) { + field(*f, key, boost::lexical_cast(value)); } }; - class block_show_traits : public block_traits { + class block_show_traits : public thin_provisioning::mapping_tree_detail::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_); + typedef thin_provisioning::mapping_tree_detail::block_traits value_trait; + + static void show(formatter::ptr f, string const &key, + thin_provisioning::mapping_tree_detail::block_time const &value) { + field(*f, "block", value.block_); + field(*f, "time", value.time_); } }; - template - class show_btree_node : public command { + class index_entry_show_traits : public persistent_data::sm_disk_detail::index_entry_traits { + public: + typedef persistent_data::sm_disk_detail::index_entry_traits value_trait; + + static void show(formatter::ptr f, string const &key, + persistent_data::sm_disk_detail::index_entry const &value) { + field(*f, "blocknr", value.blocknr_); + field(*f, "nr_free", value.nr_free_); + field(*f, "none_free_before", value.none_free_before_); + } + }; + + template + class show_btree_node : public dbg::command { public: explicit show_btree_node(metadata::ptr md) : md_(md) { @@ -248,98 +195,165 @@ namespace { if (args.size() != 2) throw runtime_error("incorrect number of arguments"); - block_address block = lexical_cast(args[1]); - block_manager<>::read_ref rr = md_->tm_->read_lock(block); + block_address block = boost::lexical_cast(args[1]); + block_manager::read_ref rr = md_->tm_->read_lock(block); - node_ref n = btree_detail::to_node(rr); + node_ref n = btree_detail::to_node(rr); if (n.get_type() == INTERNAL) show_node(n, out); else { - node_ref n = btree_detail::to_node(rr); - show_node(n, out); + node_ref n = btree_detail::to_node(rr); + show_node(n, out); } } private: - template - void show_node(node_ref n, ostream &out) { - xml_formatter f; + template + void show_node(node_ref n, ostream &out) { + formatter::ptr f = create_xml_formatter(); - 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()); + field(*f, "csum", n.get_checksum()); + field(*f, "blocknr", n.get_block_nr()); + 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); + formatter::ptr f2 = create_xml_formatter(); field(*f2, "key", n.key_at(i)); - VT::show(*f2, "value", n.value_at(i)); - f.child("child", f2); + ST::show(f2, "value", n.value_at(i)); + f->child(boost::lexical_cast(i), f2); } - f.output(out, 0); + f->output(out, 0); } metadata::ptr md_; }; + class show_index_block : public dbg::command { + public: + explicit show_index_block(metadata::ptr md) + : md_(md) { + } + + virtual void exec(strings const &args, ostream &out) { + if (args.size() != 2) + throw runtime_error("incorrect number of arguments"); + + // manually load metadata_index, without using index_validator() + block_address block = boost::lexical_cast(args[1]); + block_manager::read_ref rr = md_->tm_->read_lock(block); + + sm_disk_detail::sm_root_disk const *d = + reinterpret_cast(md_->sb_.metadata_space_map_root_); + sm_disk_detail::sm_root v; + sm_disk_detail::sm_root_traits::unpack(*d, v); + block_address nr_indexes = base::div_up(v.nr_blocks_, ENTRIES_PER_BLOCK); + + sm_disk_detail::metadata_index const *mdi = + reinterpret_cast(rr.data()); + show_metadata_index(mdi, nr_indexes, out); + } + + private: + void show_metadata_index(sm_disk_detail::metadata_index const *mdi, block_address nr_indexes, ostream &out) { + formatter::ptr f = create_xml_formatter(); + field(*f, "csum", to_cpu(mdi->csum_)); + field(*f, "padding", to_cpu(mdi->padding_)); + field(*f, "blocknr", to_cpu(mdi->blocknr_)); + + sm_disk_detail::index_entry ie; + for (block_address i = 0; i < nr_indexes; i++) { + sm_disk_detail::index_entry_traits::unpack(*(mdi->index + i), ie); + formatter::ptr f2 = create_xml_formatter(); + index_entry_show_traits::show(f2, "value", ie); + f->child(boost::lexical_cast(i), f2); + } + f->output(out, 0); + } + + unsigned const ENTRIES_PER_BLOCK = (MD_BLOCK_SIZE - sizeof(persistent_data::sm_disk_detail::bitmap_header)) * 4; + metadata::ptr md_; + }; + //-------------------------------- - int debug(string const &path) { + int debug_(string const &path, bool ignore_metadata_sm) { + using dbg::command; + 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(md))); - interp.register_command("m2_node", command::ptr(new show_btree_node(md))); - interp.register_command("detail_node", command::ptr(new show_btree_node(md))); - interp.enter_main_loop(); + block_manager::ptr bm = open_bm(path, block_manager::READ_ONLY, 1); + metadata::ptr md(new metadata(bm, false)); + command_interpreter::ptr interp = create_command_interpreter(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(md))); + interp->register_command("m2_node", command::ptr(new show_btree_node(md))); + interp->register_command("detail_node", command::ptr(new show_btree_node(md))); + interp->register_command("index_block", command::ptr(new show_index_block(md))); + interp->register_command("index_node", command::ptr(new show_btree_node(md))); + interp->register_command("help", command::ptr(new help)); + interp->register_command("exit", command::ptr(new exit_handler(interp))); + interp->enter_main_loop(); } catch (std::exception &e) { - cerr << e.what(); + cerr << e.what() << endl; 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) +thin_debug_cmd::thin_debug_cmd() + : command("thin_debug") +{ +} + +void +thin_debug_cmd::usage(std::ostream &out) const +{ + out << "Usage: " << get_name() << " {device|file}" << endl + << "Options:" << endl + << " {-h|--help}" << endl + << " {-V|--version}" << endl; +} + +int +thin_debug_cmd::run(int argc, char **argv) { int c; const char shortopts[] = "hV"; const struct option longopts[] = { { "help", no_argument, NULL, 'h'}, { "version", no_argument, NULL, 'V'}, + { "ignore-metadata-sm", no_argument, NULL, 1}, { NULL, no_argument, NULL, 0 } }; + bool ignore_metadata_sm = false; while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { switch(c) { case 'h': - usage(basename(argv[0])); + usage(cout); return 0; case 'V': cerr << THIN_PROVISIONING_TOOLS_VERSION << endl; return 0; + + case 1: + ignore_metadata_sm = true; + break; } } if (argc == optind) { - usage(basename(argv[0])); + usage(cerr); exit(1); } - return debug(argv[optind]); + return debug_(argv[optind], ignore_metadata_sm); }