• 下载解压 bash-3.2
  • 编译bash
  1. export LFS=/my/soft/mylfs
  2. tar xvf $LFS/sources/bash-3.2.tar.gz -C $LFS/sources/
  3. cd $LFS/sources/bash-3.2/
  4.  
  5. patch -Np1 -i ../bash-3.2-fixes-.patch
  6.  
  7. ./configure --prefix=$LFS/builds --without-bash-malloc
  8. make
  9. make install
  • 常用的调用堆栈
  1. 在读取命令之前执行一些命令,如PS1的打印
  2. bash [C/C++ Application]
  3. bash [] [cores: ]
  4. Thread # [core: ] (Suspended : Step)
  5. execute_builtin_or_function() at execute_cmd.c:, 0x42bf86
  6. execute_simple_command() at execute_cmd.c:, 0x42bf86
  7. execute_command_internal() at execute_cmd.c: 0x42952e
  8. parse_and_execute() at evalstring.c: 0x45dc63
  9. execute_variable_command() at parse.y:, 0x41e157
  10. parse_command() at eval.c: 0x41b1c2
  11. read_command() at eval.c: 0x41b2a6
  12. reader_loop() at eval.c: 0x41b4e4
  13. main() at shell.c: 0x41ac53
  14. gdb (7.2)
  1. 执行用户输入命令的堆栈
  2. bash [C/C++ Application]
  3. bash [] [cores: ]
  4. Thread # [core: ] (Suspended : Step)
  5. cd_builtin() at cd.def: 0x45a830
  6. execute_builtin() at execute_cmd.c:, 0x4283d1
  7. execute_builtin_or_function() at execute_cmd.c:, 0x42bf60
  8. execute_simple_command() at execute_cmd.c:, 0x42bf60
  9. execute_command_internal() at execute_cmd.c: 0x42952e
  10. execute_command() at execute_cmd.c: 0x429ffe
  11. reader_loop() at eval.c: 0x41b412
  12. main() at shell.c: 0x41ac53
  13. gdb (7.2)
  1. execute_builtin_or_function 方法有一个分支,分为执行内建命令和执行函数
  2. if (builtin)
  3. result = execute_builtin (builtin, words, flags, );
  4. else
  5. result = execute_function (var, words, flags, fds_to_close, , );
  6.  
  7. 如果是执行函数,如何找到执行的函数呢?
  8. func = find_function (words->word->word);
  9.  
  10. /* Return the pointer to the function implementing builtin command NAME. */
  11. sh_builtin_func_t *
  12. find_shell_builtin (name)
  13. char *name;
  14. {
  15. current_builtin = builtin_address_internal (name, );
  16. return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL);
  17. }
  • 执行内置命令的方法如下:
  1. static int
  2. execute_builtin (builtin, words, flags, subshell)
  3. sh_builtin_func_t *builtin;
  4. WORD_LIST *words;
  5. int flags, subshell;
  6. {
  7. int old_e_flag, result, eval_unwind;
  8. int isbltinenv;
  9.  
  10. old_e_flag = exit_immediately_on_error;
  11. /* The eval builtin calls parse_and_execute, which does not know about
  12. the setting of flags, and always calls the execution functions with
  13. flags that will exit the shell on an error if -e is set. If the
  14. eval builtin is being called, and we're supposed to ignore the exit
  15. value of the command, we turn the -e flag off ourselves, then
  16. restore it when the command completes. */
  17. if (subshell == && builtin == eval_builtin && (flags & CMD_IGNORE_RETURN))
  18. {
  19. begin_unwind_frame ("eval_builtin");
  20. unwind_protect_int (exit_immediately_on_error);
  21. exit_immediately_on_error = ;
  22. eval_unwind = ;
  23. }
  24. else
  25. eval_unwind = ;
  26.  
  27. /* The temporary environment for a builtin is supposed to apply to
  28. all commands executed by that builtin. Currently, this is a
  29. problem only with the `unset', `source' and `eval' builtins. */
  30.  
  31. isbltinenv = (builtin == source_builtin || builtin == eval_builtin || builtin == unset_builtin);
  32.  
  33. if (isbltinenv)
  34. {
  35. if (subshell == )
  36. begin_unwind_frame ("builtin_env");
  37.  
  38. if (temporary_env)
  39. {
  40. push_scope (VC_BLTNENV, temporary_env);
  41. if (subshell == )
  42. add_unwind_protect (pop_scope, (flags & CMD_COMMAND_BUILTIN) ? : "");
  43. temporary_env = (HASH_TABLE *)NULL;
  44. }
  45. }
  46.  
  47. /* `return' does a longjmp() back to a saved environment in execute_function.
  48. If a variable assignment list preceded the command, and the shell is
  49. running in POSIX mode, we need to merge that into the shell_variables
  50. table, since `return' is a POSIX special builtin. */
  51. if (posixly_correct && subshell == && builtin == return_builtin && temporary_env)
  52. {
  53. begin_unwind_frame ("return_temp_env");
  54. add_unwind_protect (merge_temporary_env, (char *)NULL);
  55. }
  56.  
  57. /*
  58. 执行执行的语句,会调用不同的内置方法执行具体的命令,如cd命令会执行cd.def中的cd_builtin方法,内置方法的文件都在builtins目录下
  59. */
  60. result = ((*builtin) (words->next));
  61.  
  62. /* This shouldn't happen, but in case `return' comes back instead of
  63. longjmp'ing, we need to unwind. */
  64. if (posixly_correct && subshell == && builtin == return_builtin && temporary_env)
  65. discard_unwind_frame ("return_temp_env");
  66.  
  67. if (subshell == && isbltinenv)
  68. run_unwind_frame ("builtin_env");
  69.  
  70. if (eval_unwind)
  71. {
  72. exit_immediately_on_error += old_e_flag;
  73. discard_unwind_frame ("eval_builtin");
  74. }
  75.  
  76. return (result);
  77. }

bash 源码分析的更多相关文章

  1. 《深入理解Spark:核心思想与源码分析》(前言及第1章)

    自己牺牲了7个月的周末和下班空闲时间,通过研究Spark源码和原理,总结整理的<深入理解Spark:核心思想与源码分析>一书现在已经正式出版上市,目前亚马逊.京东.当当.天猫等网站均有销售 ...

  2. Spark源码分析之Spark Shell(下)

    继上次的Spark-shell脚本源码分析,还剩下后面半段.由于上次涉及了不少shell的基本内容,因此就把trap和stty放在这篇来讲述. 上篇回顾:Spark源码分析之Spark Shell(上 ...

  3. Spark源码分析之Spark-submit和Spark-class

    有了前面spark-shell的经验,看这两个脚本就容易多啦.前面总结的Spark-shell的分析可以参考: Spark源码分析之Spark Shell(上) Spark源码分析之Spark She ...

  4. Elasticsearch源码分析 - 源码构建

    原文地址:https://mp.weixin.qq.com/s?__biz=MzU2Njg5Nzk0NQ==&mid=2247483694&idx=1&sn=bd03afe5a ...

  5. Docker源码分析(九):Docker镜像

    1.前言 回首过去的2014年,大家可以看到Docker在全球刮起了一阵又一阵的“容器风”,工业界对Docker的探索与实践更是一波高过一波.在如今的2015年以及未来,Docker似乎并不会像其他昙 ...

  6. Docker源码分析(八):Docker Container网络(下)

    1.Docker Client配置容器网络模式 Docker目前支持4种网络模式,分别是bridge.host.container.none,Docker开发者可以根据自己的需求来确定最适合自己应用场 ...

  7. kube-scheduler源码分析

    kubernetes集群三步安装 kube-scheduler源码分析 关于源码编译 我嫌弃官方提供的编译脚本太麻烦,所以用了更简单粗暴的方式编译k8s代码,当然官方脚本在编译所有项目或者夸平台编译以 ...

  8. JVM源码分析-JVM源码编译与调试

    要分析JVM的源码,结合资料直接阅读是一种方式,但是遇到一些想不通的场景,必须要结合调试,查看执行路径以及参数具体的值,才能搞得明白.所以我们先来把JVM的源码进行编译,并能够使用GDB进行调试. 编 ...

  9. 鸿蒙内核源码分析(忍者ninja篇) | 都忍者了能不快吗 | 百篇博客分析OpenHarmony源码 | v61.02

    百篇博客系列篇.本篇为: v61.xx 鸿蒙内核源码分析(忍者ninja篇) | 都忍者了能不快吗 | 51.c.h.o 编译构建相关篇为: v50.xx 鸿蒙内核源码分析(编译环境篇) | 编译鸿蒙 ...

随机推荐

  1. 学习 opencv---(6)玩转opencv源代码:生成opencv 工程解决方案与opencv 源码编译

    在这篇中,我们探讨如何通过已安装的opencv选择不同的编译器类型,生成高度还原的OpenCV开发时的解决方案工程文件,欣赏OpenCV新版本中总计 六十六多万行的精妙源代码.我们可以对其源代码进行再 ...

  2. [CodeWars][JS]实现大整数加法

    问题描述 实现‘字符串加法’,即将两个以字符串形式表示的数字相加,得到结果然后返回一个新的字符串. 例如:输入‘123’,‘321’,返回‘444’. 这样在进行两个任意大的整数相加的时候,既不会溢出 ...

  3. 【bb平台刷课记】wireshark结合实例学抓包

    [bb平台刷课记]wireshark结合实例学抓包 背景:本校形势与政策课程课需要在网上观看视频的方式来修得学分,视频网页自带"播放器不可快进+离开窗口自动暂停+看完一集解锁下一集(即不能同 ...

  4. 50. 树的子结构[subtree structure in tree]

    [本文链接] http://www.cnblogs.com/hellogiser/p/subtree-structure-in-tree.html [题目] 输入两棵二叉树A和B,判断B是不是A的子结 ...

  5. MVC 定时执行任务

    环境:.net4.5 需求:需要一个方法定时执行任务 解决: System.Threading.Timer 提供以指定的时间间隔执行方法的机制. 此类不能被继承,有10多种实例化方法,满足多种情况. ...

  6. c++队列基本功能

    #include<string>#include<assert.h>#include<iostream>typedef int status;#define OK ...

  7. vector

    .vector是一个能够存放任意类型的动态数组,能够增加和压缩数据. .vector容器最擅长的工作是: 利用位置索引存储容器中的单个元素. 以任何顺序迭代容器中的元素. 在容器的末尾追加和删除元素. ...

  8. mysql 5.7.14 免安装配置方法教程

    仅供参考 一.下载 1. 进入mysql官网,下载Mysql-5.7.14,下载地址:http://dev.mysql.com/downloads/mysql/ 2.将下载好的文件解压到自定义目录 二 ...

  9. 手把手教你玩转nginx负载均衡(一)----使用vitualBox创建虚拟机

    引言 作为一个web程序员,有时候需要想尽办法来利用有限的资源来产生最大程度的负载,除了提高硬件配置,增加带宽之外,CDN加速,DNS加速,缓存,还可以利用反向代理.但是要说反向代理,就不的不说ngi ...

  10. C#创建文件夹,往里追字符串。

    /// <summary>写文本日志</summary> /// <param name="StrMessage">日志消息</param ...