当前位置: 首页 > news >正文

设计部联盟网站去了外包公司就毁了吗

设计部联盟网站,去了外包公司就毁了吗,网络营销推广专家,建设游戏网站〇、前言 终于来到令人激动的源代码 level 了,这里将会有一些很有意思的算法,来实现源代码级别的调试,这将会非常有趣。 一、使用 libelfin 库 我们不可能直接去读取整个 .debug info 段来进行设置,这是没有必要的,…

〇、前言

终于来到令人激动的源代码 level 了,这里将会有一些很有意思的算法,来实现源代码级别的调试,这将会非常有趣。

一、使用 libelfin 库

我们不可能直接去读取整个 .debug info 段来进行设置,这是没有必要的,可以使用现成的库。首先初始化 debugger 对象:

class debugger {
public:debugger (std::string prog_name, pid_t pid): m_prog_name{std::move(prog_name)}, m_pid{pid} {auto fd = open(m_prog_name.c_str(), O_RDONLY);m_elf = elf::elf{elf::create_mmap_loader(fd)};m_dwarf = dwarf::dwarf{dwarf::elf::create_loader(m_elf)};}//...private://...dwarf::dwarf m_dwarf;elf::elf m_elf;
};

不必太过关注这里函数的细节,只需要关注它们做了什么。事实上,m_dwarf、m_elf 和 文件名 m_prog_name 关联起来了,然后就交给它们进行处理了。我们还需要知道 load_addr,这非常重要,因为debuf info 只会提供静态的信息,load_addr 取决于运行时,因此得想办法在 /proc 中获取:

void Debugger::initialise_load_address() {if (m_elf.get_hdr().type == elf::et::dyn) {std::ifstream map("/proc/" + std::to_string(m_pid) + "/maps");//Read the first address from the filestd::string addr;std::getline(map, addr, '-');m_load_address = std::stoi(addr, 0, 16);}
}

二、获取信息

通过一个 pc 怎么获取函数名呢?注意这个 pc 是一个 offset addr,传参的时候一定要转换。思路很简单,首先遍历所有的 cu,然后判断 culow_pchigh_pc,如果在这个 cu 符合,那么就通过 cu 拿到 cu.rootcu.root 是一个根 die,通过它可以遍历所有的 die。之后再判断 dietag 是不是一个函数,如果是且包含 pc,那么就是我们要找的函数。实现如下:

dwarf::die Debugger::get_function_from_pc(std::intptr_t pc) {for (auto &cu : m_dwarf.compilation_units()) { // 循环遍历所有cuif (die_pc_range(cu.root()).contains(pc)) {for (const auto &die :cu.root()) { if (die.tag ==dwarf::DW_TAG::subprogram) { if (die_pc_range(die).contains(pc)) {return die;}}}}}throw std::out_of_range{"Cannot find function"};
}

接着通过 pc 来获取 line entry:

dwarf::line_table::iterator Debugger::get_line_entry_from_pc(uint64_t pc) {for (auto &cu : m_dwarf.compilation_units()) {if (die_pc_range(cu.root()).contains(pc)) {auto &lt = cu.get_line_table();auto it = lt.find_address(pc);if (it == lt.end()) {throw std::out_of_range{"Cannot find line entry"};}else {return it;}}}throw std::out_of_range{"Cannot find line entry"};
}

接着我们打印源代码。思路是通过 debug info 中的源代码路径和 line table 来获取,好消息是,我们不必做更多的底层实现:

void Debugger::print_source(const std::string& file_name, unsigned line, unsigned n_lines_context) {std::ifstream file {file_name};auto start_line = line <= n_lines_context ? 1 : line - n_lines_context;auto end_line = line + n_lines_context + (line < n_lines_context ? n_lines_context - line : 0) + 1;char c{};auto current_line = 1u;while (current_line != start_line && file.get(c)) {if (c == '\n') {++current_line;}}std::cout << (current_line==line ? "> " : "  ");while (current_line <= end_line && file.get(c)) {std::cout << c;if (c == '\n') {++current_line;std::cout << (current_line==line ? "> " : "  ");}}std::cout << std::endl;
}

三、测试

minidbg> break 0x555555555191
Set breakpoint at address 0x555555555191
minidbg> conti
Hit breakpoint at adsress 0x555555555191#include <iostream>int main() {
>   std::cerr << "hello,world0.\n";return 0;}

我们确实成功的打印出了源代码。上述基本的信息获取,基本思路就是对 DWARF 的理解,然后利用库函数接口获取我们想要的信息。

http://www.yayakq.cn/news/524366/

相关文章:

  • 建设网站 目标怎么做网页啊
  • 网站制作与网站设计益阳建设厅网站
  • 上海网站搜索优化北京网站建设排名
  • 免费做淘宝客网站石家庄抖音优化
  • 手机好看网站模板咋制作网站
  • 做一个商城网站今天刚刚发生的重大新闻
  • 天津市武清区建设银行网站wordpress哪个版本稳定
  • 好的做网站的公司国美网上商城
  • 网站开发流程6个阶段深圳建筑业网站建设
  • 百度收录哪些网站移动应用开发心得体会
  • 帮你省网站怎么做设计本app下载
  • 网站建设找超速云建站网络优化大师app
  • 网站策划与建设实训心得福州推广企业网站
  • 做网站的基础wordpress 主题 修改
  • 爱做网站软件网站从哪几个方面维护
  • 在百度搜索到自己的网站中国诚乡建设部网站
  • 开封淘宝网站建设想做机械加工和橡胶生意怎么做网站
  • 企业网站外包建设建设职业注册中心网站
  • 用层做的网站展厅设计公司有哪些
  • 陶瓷网站策划书天津市网站建设天津商城建设
  • dz论坛可以做商业网站邢台市有几个区几个县
  • 一起做业网站app大全免费软件排行榜
  • 上海集团网站建设咨询wordpress 七牛视频
  • 腾讯云怎么做网站内江规划建设教育网站
  • 如何做国外的电商网站设计定制头像的网站
  • 做的最好的微电影网站有哪些海南网站建设哪里有
  • 快站 淘宝优惠券杭州新站整站seo
  • 做个素材网网站难做吗河北大城县网站建设公司
  • php网站开发师市工商局网站建设情况
  • 网站服务器搭建教程seo站点