asp.net个人网站模板,有没有专门做布料的网站,什么是企业,照片视频制作软件1. 前言
限于作者能力水平#xff0c;本文可能存在谬误#xff0c;因此而给读者带来的损失#xff0c;作者不做任何承诺。
2. 背景
本文分析基于 linux-4.14.132 上游代码。 运行环境为#xff1a; Ubuntu 16.04.4 LTS QEMU Arm vexpress-a9 rootfs 基于 ubuntu-base-…1. 前言
限于作者能力水平本文可能存在谬误因此而给读者带来的损失作者不做任何承诺。
2. 背景
本文分析基于 linux-4.14.132 上游代码。 运行环境为 Ubuntu 16.04.4 LTS QEMU Arm vexpress-a9 rootfs 基于 ubuntu-base-16.04-core-armhf.tar.gz 制作。
3. 问题场景和分析过程
交叉编译了 perf 后将 perf 放到 rootfs 的 /usr/bin/perf 路径用QEMU启动系统后执行 perf 系统提示 No such file or directory 。通过 ls 命令发现文件 /usr/bin/perf 确实存在所以No such file or directory信息提示的并不是文件 /usr/bin/perf 不存在而应该是提示 perf 执行过程中需要加载的其它文件不存在。由于 perf 程序是通过动态链接方式编译的所以 perf 执行过程中需加载相关的解释器程序动态链接器和动态链接库。事情到了这里我们通常可能会怀疑是编译链接过程出了差错。用file命令对比了不能正常运行的 perf 程序和能正常运行的 make 程序的基本执行信息
$ file /usr/bin/perf
/usr/bin/perf: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.3, for GNU/Linux 3.2.0, BuildID[sha1]b33d1b50a0a7636b8f5c48cc111e0c816c691fc9, not stripped
$ file /usr/bin/make
/usr/bin/make: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.32, BuildID[sha1]0b89bb2e75e4a812e398a069ad88628f6f535dd5, stripped
通过上面两条信息我们看到关键差异在于perf 和 make 指向的解释器程序即动态链接器不同分别是/lib/ld-linux.so.3和/lib/ld-linux-armhf.so.3。由于根文件系统基于ubuntu-base-16.04-core-armhf.tar.gz构建 该基础包包含的解释器程序是/lib/ld-linux-armhf.so.3而不是/lib/ld-linux.so.3导致系统在执行 perf 程序时找不到需要的解释器程序/lib/ld-linux.so.3从而输出 No such file or directory 提示信息。在将编译器由arm-linux-gnueabi-gcc通过 apt 安装的用来编译内核的编译器更换为arm-linux-gnueabihf-gcc后运行如下命令序列
cd tools # 切换到内核代码 tools 目录
make perf_clean # 清空 perf 目录下旧的编译文件或 make clean 清空 tools 的所有子目录
make ARCHarm CROSS_COMPILEarm-linux-gnueabihf- perf # 编译 perf
重新编译 perf 后我们看到程序可以正常运行了
$ perfusage: perf [--version] [--help] [OPTIONS] COMMAND [ARGS]...
$ file which perf
/usr/bin/perf: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.16, BuildID[sha1]fa482c30f51a3b34231c9a6011681a7bb43679bd, not stripped
问题已经解决了但我还是从代码层面进一步分析了出错的原因。我们是从 bash 启动 perf 程序的而 bash 启动程序的过程是通过系统调用fork() execve() 发起的。在我们的场景下报错代码路径执行的简要流程如下
sys_execve()do_execve(getname(filename), argv, envp).../* 打开程序文件 /usr/bin/perf */file do_open_execat(fd, filename, flags);.../* 执行程序 /usr/bin/perf */exec_binprm(bprm)...load_elf_binary() /* 执行 ELF 格式程序 */.../* 读取ELF文件的所有phdr */elf_phdata load_elf_phdrs(loc-elf_ex, bprm-file);...for (i 0; i loc-elf_ex.e_phnum; i) {if (elf_ppnt-p_type PT_INTERP) { /* 解释器程序路径, 如 /lib/ld-linux-armhf.so.3 */.../* 如果找不到解释器程序将返回错误码 ENOENT对应的错误信息为 No such file or directory */interpreter open_exec(elf_interpreter);retval PTR_ERR(interpreter);if (IS_ERR(interpreter))goto out_free_interp;}...}
当然在 sys_execve() 系统调用的代码路径中并非只有一处返回 ENOENT错误码在这里是通过 ftrace 来确认在内核代码哪一处返回ENOENT的。以下是确认上述调用路径的操作步骤
# cd /sys/kernel/debug/tracing
# echo 0 tracing_on
# echo trace
# echo function_graph current_tracer
# echo do_execveat_common set_graph_function
# echo 4 max_graph_depth
# echo 1 tracing_on
# perf
# echo 0 tracing_on
# cat trace
cat trace 命令用来查看 ftrace 追踪记录我们截取 perf 执行过程追踪部分如下
# tracer: function_graph
#
# CPU DURATION FUNCTION CALLS
# | | | | | | |2) | do_execveat_common() {2) | unshare_files() {2) 2.917 us | unshare_fd();2) 20.791 us | }2) 12.500 us | kmem_cache_alloc_trace();2) | prepare_bprm_creds() {2) 3.250 us | mutex_lock_interruptible();2) | prepare_exec_creds() {2) 17.834 us | prepare_creds();2) 46.042 us | }2) 89.666 us | }2) 2.875 us | _raw_spin_lock();2) | do_open_execat() {2) | do_filp_open() {2) ! 125.542 us | path_openat();2) 3.500 us | restore_nameidata();2) ! 174.833 us | }2) 2.792 us | __fsnotify_parent();2) 3.625 us | fsnotify();2) ! 233.875 us | }2) | sched_exec() {2) 5.709 us | _raw_spin_lock_irqsave();2) 30.708 us | select_task_rq_fair();2) 3.917 us | _raw_spin_unlock_irqrestore();2) ! 105.833 us | }2) | mm_alloc() {2) 4.375 us | kmem_cache_alloc();2) | mm_init() {2) 1.541 us | __init_rwsem();2) 68.000 us | pgd_alloc();2) ! 105.708 us | }2) ! 141.500 us | }2) 5.000 us | kmem_cache_alloc();2) 3.125 us | down_write_killable();2) 2.458 us | vm_get_page_prot();2) | insert_vm_struct() {2) | cap_vm_enough_memory() {2) 3.291 us | cap_capable();2) 17.708 us | }2) 4.292 us | __vm_enough_memory();2) | vma_link() {2) 1.042 us | __vma_link_list();2) 5.083 us | __vma_link_rb();2) 0.833 us | __vma_link_file();2) 26.875 us | }2) ! 494.417 us | }2) 1.084 us | up_write();2) | count.constprop.4() {2) 13.625 us | get_user_arg_ptr();2) 1.416 us | _cond_resched();2) 9.334 us | get_user_arg_ptr();2) 63.291 us | }2) | count.constprop.4() {2) | get_user_arg_ptr() {2) 25.542 us | do_page_fault();2) 68.500 us | }2) 1.083 us | _cond_resched();2) 9.083 us | get_user_arg_ptr();2) 0.958 us | _cond_resched();2) 8.916 us | get_user_arg_ptr();2) 0.958 us | _cond_resched();2) 8.916 us | get_user_arg_ptr();2) 0.917 us | _cond_resched();2) 8.750 us | get_user_arg_ptr();2) 0.959 us | _cond_resched();2) 8.833 us | get_user_arg_ptr();2) 0.959 us | _cond_resched();2) 8.875 us | get_user_arg_ptr();2) 0.916 us | _cond_resched();2) 8.792 us | get_user_arg_ptr();2) 0.916 us | _cond_resched();2) 8.792 us | get_user_arg_ptr();2) 0.917 us | _cond_resched();2) 35.958 us | get_user_arg_ptr();2) 28.083 us | _cond_resched();2) 19.833 us | get_user_arg_ptr();2) 1.917 us | _cond_resched();2) 43.042 us | get_user_arg_ptr();2) 1.875 us | _cond_resched();2) 16.500 us | get_user_arg_ptr();2) 1.708 us | _cond_resched();2) 16.208 us | get_user_arg_ptr();2) 2.334 us | _cond_resched();2) 18.333 us | get_user_arg_ptr();2) ! 850.917 us | }2) | prepare_binprm() {2) 3.416 us | mnt_may_suid();2) | cap_bprm_set_creds() {2) 1.500 us | mnt_may_suid();2) 11.542 us | get_vfs_caps_from_disk();2) 46.334 us | }2) | kernel_read() {2) 64.667 us | vfs_read();2) 81.792 us | }2) ! 174.084 us | }2) | copy_strings_kernel() {2) | copy_strings() {2) 16.916 us | get_user_arg_ptr();2) 2.667 us | _cond_resched();2) ! 120.792 us | get_user_pages_remote();2) 2.625 us | acct_arg_size();2) 1.458 us | flush_cache_page();2) 1.625 us | flush_kernel_dcache_page();2) 1.375 us | put_arg_page();2) ! 413.833 us | }2) ! 423.500 us | }2) | copy_strings() {2) 9.583 us | get_user_arg_ptr();2) 1.375 us | _cond_resched();2) | get_user_pages_remote() {2) 13.209 us | __get_user_pages();2) 20.666 us | }2) 0.875 us | acct_arg_size();2) 0.792 us | flush_cache_page();2) 9.292 us | get_user_arg_ptr();2) | do_page_fault() {2) 1.458 us | down_read_trylock();2) 2.875 us | find_vma();2) 12.875 us | handle_mm_fault();2) 0.959 us | up_read();2) 46.542 us | }2) 1.333 us | _cond_resched();2) 9.125 us | get_user_arg_ptr();2) 1.292 us | _cond_resched();2) 9.083 us | get_user_arg_ptr();2) 1.250 us | _cond_resched();2) 9.083 us | get_user_arg_ptr();2) 1.250 us | _cond_resched();2) 9.084 us | get_user_arg_ptr();2) | do_page_fault() {2) 1.125 us | down_read_trylock();2) 2.334 us | find_vma();2) 9.042 us | handle_mm_fault();2) 0.875 us | up_read();2) 42.917 us | }2) 1.375 us | _cond_resched();2) 9.167 us | get_user_arg_ptr();2) 1.250 us | _cond_resched();2) 9.042 us | get_user_arg_ptr();2) | do_page_fault() {2) 1.167 us | down_read_trylock();2) 2.292 us | find_vma();2) 8.750 us | handle_mm_fault();2) 0.833 us | up_read();2) 43.375 us | }2) |2) | gic_handle_irq() {2) ! 151.958 us | __handle_domain_irq();2) ! 170.791 us | }2) |2) 1.625 us | _cond_resched();2) 10.167 us | get_user_arg_ptr();2) 1.625 us | _cond_resched();2) 17.500 us | get_user_arg_ptr();2) 2.292 us | _cond_resched();2) 16.625 us | get_user_arg_ptr();2) 2.250 us | _cond_resched();2) 16.542 us | get_user_arg_ptr();2) 2.208 us | _cond_resched();2) 16.500 us | get_user_arg_ptr();2) 2.250 us | _cond_resched();2) 16.541 us | get_user_arg_ptr();2) | do_page_fault() {2) 3.042 us | down_read_trylock();2) 5.209 us | find_vma();2) 22.166 us | handle_mm_fault();2) 1.583 us | up_read();2) 92.500 us | }2) 2.458 us | _cond_resched();2) 2.584 us | flush_kernel_dcache_page();2) 2.459 us | put_arg_page();2) # 2887.709 us | }2) | copy_strings() {2) 25.167 us | get_user_arg_ptr();2) 1.334 us | _cond_resched();2) | get_user_pages_remote() {2) 16.250 us | __get_user_pages();2) 24.042 us | }2) 0.791 us | acct_arg_size();2) 0.792 us | flush_cache_page();2) 1.291 us | flush_kernel_dcache_page();2) 1.042 us | put_arg_page();2) ! 147.708 us | }2) | would_dump() {2) | inode_permission() {2) 2.916 us | __inode_permission();2) 9.792 us | }2) 17.042 us | }2) 2.000 us | task_active_pid_ns();2) 1.792 us | __task_pid_nr_ns();2) | search_binary_handler() {2) 1.417 us | _raw_read_lock();2) 1.500 us | try_module_get();2) 1.458 us | load_script();2) 0.833 us | _raw_read_lock();2) 0.833 us | module_put();2) 0.833 us | try_module_get();2) | load_elf_binary() {2) 3.000 us | kmem_cache_alloc_trace();2) 1.250 us | elf_check_arch();2) 42.041 us | load_elf_phdrs();2) 2.916 us | __kmalloc();2) 13.625 us | kernel_read();2) 59.125 us | open_exec();2) 3.417 us | kfree();2) 1.458 us | kfree();2) 1.333 us | kfree();2) ! 192.750 us | }2) 1.042 us | _raw_read_lock();2) 0.791 us | module_put();2) ! 257.292 us | }2) 1.208 us | acct_arg_size();
通过查看上面的追踪记录同时结合内核代码我们可以判定load_elf_binary() 调用终止于如下代码路径
load_elf_binary()loc kmalloc(sizeof(*loc), GFP_KERNEL);...elf_phdata load_elf_phdrs(loc-elf_ex, bprm-file);...for (i 0; i loc-elf_ex.e_phnum; i) {if (elf_ppnt-p_type PT_INTERP) {elf_interpreter kmalloc(elf_ppnt-p_filesz, GFP_KERNEL);...kernel_read(bprm-file, elf_interpreter, elf_ppnt-p_filesz, pos);interpreter open_exec(elf_interpreter); /* 找不到动态链接器文件返回 ENOENT */retval PTR_ERR(interpreter);if (IS_ERR(interpreter))goto out_free_interp; /* 出错了从这里退出了 load_elf_binary() 函数 *//* 如果没有出错would_dump() 会出现在 load_elf_binary() 函数调用的追踪记录中 */would_dump(bprm, interpreter);...}}...
out_free_interp: /* 我们的场景中出错后跳转到这里 */kfree(elf_interpreter);
out_free_ph:kfree(elf_phdata);goto out;
ftrace 的配置和使用方法在这里不作展开读者可自行查阅相关资料或参考博文 Linux: ftrace使用手册 。
4. 后记
在确认文件/usr/bin/perf存在的情形下仍然提示No such file or directory 确实让人有些困惑但我们相信事出必有因逐步分析抽丝剥茧真相总会浮出水面而在这个过程中我们终有所得。 现实中存在更多文件存在但不能运行的情景如程序路径不对、远程传送拷贝导致程序不完整、运行为其它架构编译的程序等等。
5. 番外
我们经常看到类似如下的编译器
arm-none-eabi-gcc
arm-linux-gnueabi-gcc
arm-linux-gnueabihf-gcc
让人不知所措很是迷茫。下面一个简单的小结可以帮助我们理解它们。
交叉编译工具链的命名规则为arch[-vendor][-os][-(gnu)eabi]
arch: 体系架构如ARMMIPS
vendor: 工具链提供商
os: 目标操作系统如 linux
eabi: 嵌入式应用二进制接口Embedded Application Binary Interface
根据对操作系统的支持与否ARM GCC可分为支持和不支持操作系统如arm-none-eabi这个是没有操作系统的自然不可能支持那些跟操作系统关系密切的函数比如fork(2)它使用的是newlib这个专用于嵌入式系统的C库。而arm-none-linux-eabi是用于Linux的使用glibc。
6. 参考资料和链接
交叉编译器命名规则 文章转载自: http://www.morning.xsymm.cn.gov.cn.xsymm.cn http://www.morning.prznc.cn.gov.cn.prznc.cn http://www.morning.c7501.cn.gov.cn.c7501.cn http://www.morning.eviap.com.gov.cn.eviap.com http://www.morning.czzpm.cn.gov.cn.czzpm.cn http://www.morning.gtkyr.cn.gov.cn.gtkyr.cn http://www.morning.xpqdf.cn.gov.cn.xpqdf.cn http://www.morning.sjwzl.cn.gov.cn.sjwzl.cn http://www.morning.srbbh.cn.gov.cn.srbbh.cn http://www.morning.zjqwr.cn.gov.cn.zjqwr.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.rwlns.cn.gov.cn.rwlns.cn http://www.morning.gthwz.cn.gov.cn.gthwz.cn http://www.morning.rcdmp.cn.gov.cn.rcdmp.cn http://www.morning.prjty.cn.gov.cn.prjty.cn http://www.morning.gbrdx.cn.gov.cn.gbrdx.cn http://www.morning.cwyfs.cn.gov.cn.cwyfs.cn http://www.morning.jxltk.cn.gov.cn.jxltk.cn http://www.morning.sfwd.cn.gov.cn.sfwd.cn http://www.morning.gkfwp.cn.gov.cn.gkfwp.cn http://www.morning.tsqrc.cn.gov.cn.tsqrc.cn http://www.morning.wzwyz.cn.gov.cn.wzwyz.cn http://www.morning.xltdh.cn.gov.cn.xltdh.cn http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.ghfmd.cn.gov.cn.ghfmd.cn http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn http://www.morning.fwzjs.cn.gov.cn.fwzjs.cn http://www.morning.zynjt.cn.gov.cn.zynjt.cn http://www.morning.cniedu.com.gov.cn.cniedu.com http://www.morning.ptysj.cn.gov.cn.ptysj.cn http://www.morning.rpkg.cn.gov.cn.rpkg.cn http://www.morning.fglzk.cn.gov.cn.fglzk.cn http://www.morning.xjnjb.cn.gov.cn.xjnjb.cn http://www.morning.bpmtl.cn.gov.cn.bpmtl.cn http://www.morning.tllws.cn.gov.cn.tllws.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.tqbyw.cn.gov.cn.tqbyw.cn http://www.morning.xxwl1.com.gov.cn.xxwl1.com http://www.morning.gcqs.cn.gov.cn.gcqs.cn http://www.morning.jpqmq.cn.gov.cn.jpqmq.cn http://www.morning.lmpfk.cn.gov.cn.lmpfk.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.yrjxr.cn.gov.cn.yrjxr.cn http://www.morning.skkln.cn.gov.cn.skkln.cn http://www.morning.kksjr.cn.gov.cn.kksjr.cn http://www.morning.jkcnq.cn.gov.cn.jkcnq.cn http://www.morning.nftzn.cn.gov.cn.nftzn.cn http://www.morning.cftkz.cn.gov.cn.cftkz.cn http://www.morning.zkdmk.cn.gov.cn.zkdmk.cn http://www.morning.tjndb.cn.gov.cn.tjndb.cn http://www.morning.mtrfz.cn.gov.cn.mtrfz.cn http://www.morning.kjfqf.cn.gov.cn.kjfqf.cn http://www.morning.tyrlk.cn.gov.cn.tyrlk.cn http://www.morning.jrwbl.cn.gov.cn.jrwbl.cn http://www.morning.rdnjc.cn.gov.cn.rdnjc.cn http://www.morning.ybnzn.cn.gov.cn.ybnzn.cn http://www.morning.pqrhb.cn.gov.cn.pqrhb.cn http://www.morning.hhfwj.cn.gov.cn.hhfwj.cn http://www.morning.fgsct.cn.gov.cn.fgsct.cn http://www.morning.znmwb.cn.gov.cn.znmwb.cn http://www.morning.bdzps.cn.gov.cn.bdzps.cn http://www.morning.mlcnh.cn.gov.cn.mlcnh.cn http://www.morning.fplqh.cn.gov.cn.fplqh.cn http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.fdrb.cn.gov.cn.fdrb.cn http://www.morning.zhoer.com.gov.cn.zhoer.com http://www.morning.nzmhk.cn.gov.cn.nzmhk.cn http://www.morning.bbmx.cn.gov.cn.bbmx.cn http://www.morning.mxnfh.cn.gov.cn.mxnfh.cn http://www.morning.kpqjr.cn.gov.cn.kpqjr.cn http://www.morning.rqfnl.cn.gov.cn.rqfnl.cn http://www.morning.dwyyf.cn.gov.cn.dwyyf.cn http://www.morning.jfjqs.cn.gov.cn.jfjqs.cn http://www.morning.bchfp.cn.gov.cn.bchfp.cn http://www.morning.rcjqgy.com.gov.cn.rcjqgy.com http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.sqqdy.cn.gov.cn.sqqdy.cn http://www.morning.jxhlx.cn.gov.cn.jxhlx.cn http://www.morning.rgfx.cn.gov.cn.rgfx.cn