• 下载解压 bash-3.2
  • 编译bash
export LFS=/my/soft/mylfs
tar xvf $LFS/sources/bash-3.2.tar.gz -C $LFS/sources/
cd $LFS/sources/bash-3.2/ patch -Np1 -i ../bash-3.2-fixes-.patch ./configure --prefix=$LFS/builds --without-bash-malloc
make
make install
  • 常用的调用堆栈
在读取命令之前执行一些命令,如PS1的打印
bash [C/C++ Application]
bash [] [cores: ]
Thread # [core: ] (Suspended : Step)
execute_builtin_or_function() at execute_cmd.c:, 0x42bf86
execute_simple_command() at execute_cmd.c:, 0x42bf86
execute_command_internal() at execute_cmd.c: 0x42952e
parse_and_execute() at evalstring.c: 0x45dc63
execute_variable_command() at parse.y:, 0x41e157
parse_command() at eval.c: 0x41b1c2
read_command() at eval.c: 0x41b2a6
reader_loop() at eval.c: 0x41b4e4
main() at shell.c: 0x41ac53
gdb (7.2)
执行用户输入命令的堆栈
bash [C/C++ Application]
bash [] [cores: ]
Thread # [core: ] (Suspended : Step)
cd_builtin() at cd.def: 0x45a830
execute_builtin() at execute_cmd.c:, 0x4283d1
execute_builtin_or_function() at execute_cmd.c:, 0x42bf60
execute_simple_command() at execute_cmd.c:, 0x42bf60
execute_command_internal() at execute_cmd.c: 0x42952e
execute_command() at execute_cmd.c: 0x429ffe
reader_loop() at eval.c: 0x41b412
main() at shell.c: 0x41ac53
gdb (7.2)
execute_builtin_or_function 方法有一个分支,分为执行内建命令和执行函数
if (builtin)
result = execute_builtin (builtin, words, flags, );
else
result = execute_function (var, words, flags, fds_to_close, , ); 如果是执行函数,如何找到执行的函数呢?
func = find_function (words->word->word); /* Return the pointer to the function implementing builtin command NAME. */
sh_builtin_func_t *
find_shell_builtin (name)
char *name;
{
current_builtin = builtin_address_internal (name, );
return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL);
}
  • 执行内置命令的方法如下:
static int
execute_builtin (builtin, words, flags, subshell)
sh_builtin_func_t *builtin;
WORD_LIST *words;
int flags, subshell;
{
int old_e_flag, result, eval_unwind;
int isbltinenv; old_e_flag = exit_immediately_on_error;
/* The eval builtin calls parse_and_execute, which does not know about
the setting of flags, and always calls the execution functions with
flags that will exit the shell on an error if -e is set. If the
eval builtin is being called, and we're supposed to ignore the exit
value of the command, we turn the -e flag off ourselves, then
restore it when the command completes. */
if (subshell == && builtin == eval_builtin && (flags & CMD_IGNORE_RETURN))
{
begin_unwind_frame ("eval_builtin");
unwind_protect_int (exit_immediately_on_error);
exit_immediately_on_error = ;
eval_unwind = ;
}
else
eval_unwind = ; /* The temporary environment for a builtin is supposed to apply to
all commands executed by that builtin. Currently, this is a
problem only with the `unset', `source' and `eval' builtins. */ isbltinenv = (builtin == source_builtin || builtin == eval_builtin || builtin == unset_builtin); if (isbltinenv)
{
if (subshell == )
begin_unwind_frame ("builtin_env"); if (temporary_env)
{
push_scope (VC_BLTNENV, temporary_env);
if (subshell == )
add_unwind_protect (pop_scope, (flags & CMD_COMMAND_BUILTIN) ? : "");
temporary_env = (HASH_TABLE *)NULL;
}
} /* `return' does a longjmp() back to a saved environment in execute_function.
If a variable assignment list preceded the command, and the shell is
running in POSIX mode, we need to merge that into the shell_variables
table, since `return' is a POSIX special builtin. */
if (posixly_correct && subshell == && builtin == return_builtin && temporary_env)
{
begin_unwind_frame ("return_temp_env");
add_unwind_protect (merge_temporary_env, (char *)NULL);
} /*
执行执行的语句,会调用不同的内置方法执行具体的命令,如cd命令会执行cd.def中的cd_builtin方法,内置方法的文件都在builtins目录下
*/
result = ((*builtin) (words->next)); /* This shouldn't happen, but in case `return' comes back instead of
longjmp'ing, we need to unwind. */
if (posixly_correct && subshell == && builtin == return_builtin && temporary_env)
discard_unwind_frame ("return_temp_env"); if (subshell == && isbltinenv)
run_unwind_frame ("builtin_env"); if (eval_unwind)
{
exit_immediately_on_error += old_e_flag;
discard_unwind_frame ("eval_builtin");
} return (result);
}

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. 【清华集训】楼房重建 BZOJ 2957

    Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题,我们考虑这些 ...

  2. shell 脚本中的运算

    #!/bin/bash read -p "please Insert two integer values: " a b if [ ! -n "$a" -o ! ...

  3. erlang 故障排查工具

    系统级别perf top, dstat -tam, vtune 都能很好分析beam 瓶颈,本文主要erlang 级别排查: 1. 反编译 确认线上运行代码是否正确,reltools没掌握好,升级偶尔 ...

  4. Docker - Dockerizing applications: A “Hello world”

    1. 在Docker容器(container)运行Hello world $ docker run ubuntu /bin/echo 'Hello world' docker run - 运行一个容器 ...

  5. python 使用pymssql连接sql server数据库

    python 使用pymssql连接sql server数据库   #coding=utf-8 #!/usr/bin/env python#------------------------------ ...

  6. NIO及Reactor模式

    关于Nio Java NIO即Java Non-blocking IO(Java非阻塞I/O),是Jdk1.4之后增加的一套操作I/O工具包,又被叫做Java New IO. Nio要去解决的问题 N ...

  7. iOS 改变App状态栏颜色为白色

    默认状态栏为黑色,对于某些App不是很美观,变成白色很简单,只需要两个步骤. 1.在Info.plist中添加新项目,View controller-based status bar appearan ...

  8. DL论文

    题目:Accurate Image Super-Resolution Using Very Deep Convolutional Networks(2016CVPR) 摘要:文中提出了一种高精度处理单 ...

  9. code first提示已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭解决方法

    使用codefirst查询当然是必不可少的,但有时不小心可能很简单的查询也会导致异常. 下面用codefirst做个示例简单演示下异常发生的场景: var attendlist = db.Databa ...

  10. [Python数据分析]新股破板买入,赚钱几率如何?

    这是本人一直比较好奇的问题,网上没搜到,最近在看python数据分析,正好自己动手做一下试试.作者对于python是零基础,需要从头学起. 在写本文时,作者也没有完成这个小分析目标,边学边做吧. == ...