作家
登录

开发一个Linux调试器(五):源码和信号

作者: 来源: 2017-08-28 16:03:37 阅读 我要评论

  •         auto line_entry = get_line_entry_from_pc(get_pc()); 
  •         print_source(line_entry->file->path, line_entry->line); 
  •         return
  •     } 
  •     //如不雅旌旗灯号是由慢慢履行发送的,这会被设置 
  •     case TRAP_TRACE: 
  •         return
  •     default
  •         std::cout << "Unknown SIGTRAP code " << info.si_code << std::endl; 
  •         return
  •     } 
  • 当我们射一一个断点或者慢慢履行我们的代码时,我们会想知道处于源码中的什愦地位。

    打印源码

    1. void debugger::print_source(const std::string& file_name, unsigned line, unsigned n_lines_context) { 
    2.     std::ifstream file {file_name}; 
    3.     //获得一个所需行邻近的窗口 
    4.     auto start_line = line <= n_lines_context ? 1 : line - n_lines_context; 
    5.     auto end_line = line + n_lines_context + (line < n_lines_context ? n_lines_context - line : 0) + 1; 
    6.     char c{}; 
    7.     auto current_line = 1u; 
    8.     //跳过 start_line 之前的行 
    9.     while (current_line != start_line && file.get(c)) { 
    10.         if (c == '\n') { 
    11.             ++current_line; 
    12.         } 
    13.     } 
    14.     //如不雅我们在当前行则输出光标 
    15.     std::cout << (current_line==line ? "> " : "  "); 
    16.     //输出行直到 end_line 
    17.     while (current_line <= end_line && file.get(c)) { 
    18.         std::cout << c; 
    19.         if (c == '\n') { 
    20.             ++current_line; 
    21.             //如不雅我们在当前行则输出光标 
    22.             std::cout << (current_line==line ? "> " : "  "); 
    23.         } 
    24.     } 
    25.     //输出换行确保恰本地清空了流 

        推荐阅读

        Python开发者面向文档编程的正确姿势

      概述秦人不暇自哀,而后人哀之;后人哀之而不鉴之,亦使后人而复哀后人也! &ndash;论面向文档编程的重要性如不雅想看见识一小我写代码的功力,注释其实是区分老司机和小鲜肉的一个明显的分界线(有没有不雅察碘晾髑公司的>>>详细阅读


      本文标题:开发一个Linux调试器(五):源码和信号

      地址:http://www.17bianji.com/lsqh/36965.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)