⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gexmap.cpp

📁 内存管理工具Exmap。该工具比 ps 或 top 更精确
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    AllProcList::AllProcList()    : ProcList("All Processes"){}void AllProcList::set_data(const list<ProcessPtr> &procs){    list<Exmap::ProcessPtr>::const_iterator it;    if (procs.empty()) {	show_label("No processes found");	return;    }    show_list();    for (it = procs.begin(); it != procs.end(); ++it) {	SizesPtr sizes = (*it)->sizes();	add_row((*it)->pid(),		(*it)->cmdline(),		sizes);    }    SizesPtr totals = calc_totals(procs);    add_row(0, "TOTALS", totals);}SizesPtr AllProcList::calc_totals(const list<ProcessPtr> &procs){    list<ProcessPtr>::const_iterator it;    SizesPtr totals (new Sizes);    for (it = procs.begin(); it != procs.end(); ++it) {	SizesPtr sizes = (*it)->sizes();	totals->add(sizes);    }    return totals;}// ------------------------------------------------------------    PerFileProcList::PerFileProcList()    : ProcList("Sizes for file in processes"){    show_label("No file selected");}void PerFileProcList::set_data(const Exmap::FilePtr &file){    list<ProcessPtr> procs(file->procs());        if (procs.empty()) {	string txt = "No processes for file " + file->name();	show_label(txt);	return;    }    list<Exmap::ProcessPtr>::iterator it;    show_list();    for (it = procs.begin(); it != procs.end(); ++it) {	SizesPtr sizes = (*it)->sizes(file);	add_row((*it)->pid(),		(*it)->cmdline(),		sizes);    }}	// ------------------------------------------------------------PerProcFileList::PerProcFileList()    : SizeListView("Sizes for files in selected process"){    _columns.add(_filename);    init_store_and_view();    _treeview.append_column("Filename", _filename);    init_columns();    show_label("No process selected");}void PerProcFileList::set_data(const Exmap::ProcessPtr &proc){    list<Exmap::FilePtr> files = proc->files();    list<Exmap::FilePtr>::const_iterator it;    if (files.empty()) {	show_label("No files for process");	return;    }    show_list();        for (it = files.begin(); it != files.end(); ++it) {	Gtk::TreeModel::Row row = *(_store->append());	row[_filename] = (*it)->name();	Exmap::SizesPtr sizes = proc->sizes(*it);	add_row_sizes(row, sizes);    }}string PerProcFileList::currently_selected(){    Glib::RefPtr<Gtk::TreeSelection> sel = listwin().get_selection();    Glib::RefPtr<Gtk::TreeModel> model = listwin().get_model();    string fname;    if (sel->count_selected_rows() == 1) {	Gtk::TreeModel::iterator iter = sel->get_selected(model);	if (iter) {	    Gtk::TreeModel::Row row = *iter;	    fname = row.get_value(_filename);	}    }    return fname;}// ------------------------------------------------------------AllFileList::AllFileList()    : SizeListView("All Files"){    _columns.add(_filename);    _columns.add(_nprocs);    init_store_and_view();    _treeview.append_column("Filename", _filename);    _treeview.append_column("Number of procs", _nprocs);    init_columns();}void AllFileList::set_data(const list<FilePtr> &files){    list<Exmap::FilePtr>::const_iterator it;    if (files.empty()) {	show_label("No files found");	return;    }    show_list();        for (it = files.begin(); it != files.end(); ++it) {	Gtk::TreeModel::Row row = *(_store->append());	row[_filename] = (*it)->name();	row[_nprocs] = (*it)->procs().size();	Exmap::SizesPtr sizes = (*it)->sizes();	add_row_sizes(row, sizes);    }}string AllFileList::currently_selected(){    Glib::RefPtr<Gtk::TreeSelection> sel = listwin().get_selection();    Glib::RefPtr<Gtk::TreeModel> model = listwin().get_model();    string fname;    if (sel->count_selected_rows() == 1) {	Gtk::TreeModel::iterator iter = sel->get_selected(model);	if (iter) {	    Gtk::TreeModel::Row row = *iter;	    fname = row.get_value(_filename);	}    }    return fname;}// ------------------------------------------------------------ElfSectionList::ElfSectionList()    : SizeListView("ELF Sections for selected file and process"){    _columns.add(_name);    _columns.add(_file_offset);    init_store_and_view();    _treeview.append_column("Section name", _name);    _treeview.append_column("File offset", _file_offset);    init_columns();    show_label("Process and file not selected");}void ElfSectionList::set_data(const Exmap::ProcessPtr &proc,			      const Exmap::FilePtr &file){    list<Elf::SectionPtr> sections;    list<Elf::SectionPtr>::const_iterator it;    Elf::FilePtr elf = file->elf();    if (!elf) {	string txt = file->name() + " is not an elf file";	show_label(txt);	return;    }    show_list();    sections = elf->mappable_sections();    for (it = sections.begin(); it != sections.end(); ++it) {	Gtk::TreeModel::Row row = *(_store->append());	row[_name] = (*it)->name();	row[_file_offset] = (*it)->file_range()->start();	Exmap::SizesPtr sizes = proc->sizes(file, (*it)->mem_range());	add_row_sizes(row, sizes);    }}string ElfSectionList::currently_selected(){    Glib::RefPtr<Gtk::TreeSelection> sel = listwin().get_selection();    Glib::RefPtr<Gtk::TreeModel> model = listwin().get_model();    string section_name;    if (sel->count_selected_rows() == 1) {	Gtk::TreeModel::iterator iter = sel->get_selected(model);	if (iter) {	    Gtk::TreeModel::Row row = *iter;	    section_name = row.get_value(_name);	}    }    return section_name;}// ------------------------------------------------------------ElfSymbolList::ElfSymbolList()    : SizeListView("ELF symbols within selected section"){    _columns.add(_name);    init_store_and_view();    _treeview.append_column("Symbol name", _name);    init_columns();    show_label("Process, file and section not selected");}void ElfSymbolList::set_data(const Exmap::ProcessPtr &proc,			     const Exmap::FilePtr &file,			     const Elf::SectionPtr &section){    list<Elf::SymbolPtr> symbols;    list<Elf::SymbolPtr>::const_iterator it;    Elf::FilePtr elf = file->elf();    if (!elf) {	string txt = file->name() + " is not an elf file";	show_label(txt);	return;    }    symbols = elf->symbols_in_section(section);    if (symbols.empty()) {	string txt = "No symbols found in " + file->name();	show_label(txt);	return;    }    show_list();    for (it = symbols.begin(); it != symbols.end(); ++it) {	Gtk::TreeModel::Row row = *(_store->append());	row[_name] = (*it)->name();	Exmap::SizesPtr sizes = proc->sizes(file, (*it)->range());	add_row_sizes(row, sizes);    }} // ------------------------------------------------------------ExmapTab::ExmapTab(){    add1(_top_half);    add2(_bottom_half);    _bottom_half.add1(_sectionlist);    _bottom_half.add2(_symlist);    int height = TopWin::HEIGHT;    set_position(2 * height / 3);    _top_half.set_position(height / 3);    _bottom_half.set_position(height / 6);}// ------------------------------------------------------------ProcTab::ProcTab(){    _top_half.add1(_allproclist);    _top_half.add2(_filelist);    add1(_top_half);    add2(_bottom_half);    _allproclist.listwin().get_selection()->signal_changed()	.connect(sigc::mem_fun(*this, &ProcTab::proclist_changed_cb));    _filelist.listwin().get_selection()->signal_changed()    	.connect(sigc::mem_fun(*this, &ProcTab::filelist_changed_cb));    _sectionlist.listwin().get_selection()->signal_changed()    	.connect(sigc::mem_fun(*this, &ProcTab::sectionlist_changed_cb));}void ProcTab::set_data(SnapshotPtr &snapshot){    _snapshot = snapshot;    _allproclist.set_data(_snapshot->procs());}void ProcTab::proclist_changed_cb(){    pid_t pid = _allproclist.currently_selected();    if (pid != 0) {	Exmap::ProcessPtr proc = _snapshot->proc(pid);	_filelist.set_data(proc);    }}void ProcTab::filelist_changed_cb(){    pid_t pid = _allproclist.currently_selected();    string fname = _filelist.currently_selected();    if (pid != 0 && !fname.empty()) {	Exmap::ProcessPtr proc = _snapshot->proc(pid);	Exmap::FilePtr file = _snapshot->file(fname);	if (proc && file) {	    _sectionlist.set_data(proc, file);	}    }}	void ProcTab::sectionlist_changed_cb(){    pid_t pid = _allproclist.currently_selected();    string fname = _filelist.currently_selected();    string section_name = _sectionlist.currently_selected();    if (pid != 0 && !fname.empty()) {	Exmap::ProcessPtr proc = _snapshot->proc(pid);	Exmap::FilePtr file = _snapshot->file(fname);	Elf::FilePtr elf = file->elf();	if (!elf) { return; }	Elf::SectionPtr section = elf->section(section_name);	if (proc && file && section) {	    _symlist.set_data(proc, file, section);	}    }}	// ------------------------------------------------------------FileTab::FileTab(){    _top_half.add1(_allfilelist);    _top_half.add2(_proclist);    _allfilelist.listwin().get_selection()->signal_changed()    	.connect(sigc::mem_fun(*this, &FileTab::filelist_changed_cb));    _proclist.listwin().get_selection()->signal_changed()	.connect(sigc::mem_fun(*this, &FileTab::proclist_changed_cb));    _sectionlist.listwin().get_selection()->signal_changed()    	.connect(sigc::mem_fun(*this, &FileTab::sectionlist_changed_cb));}void FileTab::set_data(SnapshotPtr &snapshot){    _snapshot = snapshot;    _allfilelist.set_data(_snapshot->files());}void FileTab::filelist_changed_cb(){    string fname = _allfilelist.currently_selected();    if (!fname.empty()) {	Exmap::FilePtr file = _snapshot->file(fname);	_proclist.set_data(file);    }}void FileTab::proclist_changed_cb(){    string fname = _allfilelist.currently_selected();    pid_t pid = _proclist.currently_selected();    if (pid != 0 && !fname.empty()) {	Exmap::ProcessPtr proc = _snapshot->proc(pid);	Exmap::FilePtr file = _snapshot->file(fname);	if (proc && file) {	    _sectionlist.set_data(proc, file);	}    }}	void FileTab::sectionlist_changed_cb(){    string fname = _allfilelist.currently_selected();    pid_t pid = _proclist.currently_selected();    string section_name = _sectionlist.currently_selected();        if (pid != 0 && !fname.empty()) {	Exmap::ProcessPtr proc = _snapshot->proc(pid);	Exmap::FilePtr file = _snapshot->file(fname);	Elf::FilePtr elf = file->elf();	if (!elf) { return; }	Elf::SectionPtr section = elf->section(section_name);	if (proc && file && section) {	    _symlist.set_data(proc, file, section);	}    }}        // ------------------------------------------------------------int main(int argc, char *argv[]){    Gtk::Main kit(argc, argv);    // If you change the scale you may want to change SIZES_PRINTF_FORMAT    Sizes::scale_kbytes();    SysInfoPtr sysinfo(new LinuxSysInfo);    SnapshotPtr snapshot(new Snapshot(sysinfo));    if (!snapshot->load()) {	cerr << "Failed to load snapshot - aborting" << endl;	return -1;    }    TopWin topwin(snapshot);        if (argc == 1) {	Gtk::Main::run(topwin);    }    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -