跳转至

Linux 研发

1 环境变量

1.1 添加系统全局 C/C++头文件、库路径

系统默认include目录是/usr/include目录。默认的lib库加载路径是/usr/local/bin目录

1.2 CPATH

修改CPATH环境变量(在/etc/profile或~/.bash_profile里面),添加新的头文件包含目录,重新连接终端生效,gcc编译器会在此目录下查找头文件。 - 示例:export CPATH=$CPATH:/opt/rocksdb-7.6/include - 查看头文件搜索目录:使用此方式可以查到刚刚添加的/opt/rocksdb-7.6/include的目录

# 查看gcc预处理C时的的搜索目录:
[rocksdb@centos7 ~]$ echo | gcc -x c -v -E -
# 查看gcc预处理C++时的的搜索目录:
[rocksdb@centos7 ~]$ echo | gcc -x c++ -v -E -
# 查看clang预处理C++时的搜索目录:
[rocksdb@centos7 ~]$ echo | clang -x c++ -v -E -

1.3 LD_LIBRARY_PATH

修改LD_LIBRARY_PATH环境变量,添加新的库目录,重新连接终端生效。 - 示例:export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/rocksdb-7.6/lib

1.4 LD_PRELOAD 影响程序运行时动态库的链接

  • 动态库加载顺序:LD_PRELOAD>LD_LIBRARY_PATH>/etc/ld.so.cache>/lib>/usr/lib
  • 使用实例:LD_PRELOAD=动态库路径 待执行程序路径和名,如LD_PRELOAD=/home/user01/libxxx.so /home/user01/test,这时test程序会优先加载/home/user01/libxxx.so这个动态库

2 c++filt

  • 功能:翻译c++或java符号到标准可读名称
  • 语法:c++filt [option] symbol
  • -p:当处理的symbol时函数时,不打印函数参数
  • -_:删除名称前的下划线。On some systems, both the C and C++ compilers put an underscore in front of every name. For example, the C name "foo" gets the low-level name "_foo". This option removes the initial underscore.
  • -n:不删除名称前的下划线
  • 示例
$ c++filt _ZN2ns3fooIdbEEvT_T0_
void ns::foo<double, bool>(double, bool)
$ c++filt -_ _ZN2ns3fooIdbEEvT_T0_
_ZN2ns3fooIdbEEvT_T0_
$ c++filt -n _ZN2ns3fooIdbEEvT_T0_
void ns::foo<double, bool>(double, bool)
$ c++filt -p _ZN2ns3fooIdbEEvT_T0_
ns::foo<double, bool>

3 nm

  • 功能:查看obj文件符号信息,包括lib,so,o
  • 语法形式:nm [option] filename
  • 参考资料

3.1 查看导出符号(包含函数参数信息)

nm -n -C xxx.so

3.2 查看.so文件导出函数

nm -D xxx.so | awk '{if($2=="T"){print $3}}'

4 objdump

  • 功能:objdump工具用来显示二进制文件的信息。

4.1 常用参数说明

  • -f 显示文件头信息
  • -D 反汇编所有section (-d反汇编特定section)
  • -h 显示目标文件各个section的头部摘要信息
  • -x 显示所有可用的头信息,包括符号表、重定位入口。-x 等价于 -a -f -h -r -t 同时指定。
  • -i 显示对于 -b 或者 -m 选项可用的架构和目标格式列表。
  • -r 显示文件的重定位入口。如果和-d或者-D一起使用,重定位部分以反汇编后的格式显示出来。
  • -R 显示文件的动态重定位入口,仅仅对于动态目标文件有意义,比如某些共享库。
  • -S 尽可能反汇编出源代码,尤其当编译的时候指定了-g这种调试参数时,效果比较明显。隐含了-d参数。
  • -t 显示文件的符号表入口。类似于nm -s提供的信息

5 readelf

  • 参考博客 ELF文件有三种类型:
  • REL (Relocatable file)可重定位的对象文件,由汇编器汇编生成的 .o 文件
  • EXEC (Executable file)可执行的对象文件,如window上.exe文件
  • DYN (Shared object file)可被共享的对象文件,.so文件
  • 示例
[root@centos7 perf_example]# readelf -h /lib64/libm.so.6
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 03 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - GNU
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x5350
  Start of program headers:          64 (bytes into file)
  Start of section headers:          1134704 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         7
  Size of section headers:           64 (bytes)
  Number of section headers:         35
  Section header string table index: 34

6 strings

  • 功能:打印文件里面的可打印字符。(可以打印普通文本文件,但一般用于二进制文件中)
  • 语法:string [option] file
    • -a:扫描整个文件,无论它包含哪些section,或者这些section是否已加载或初始化。(默认选项)
    • -d:仅仅打印在文件中已加载或初始化的section的可打印字符
    • -n num:指定打印的字符序列最小长度(默认是4byte)
    • -f:在输出的每行字符前打印所属的文件
    • -t radix:在每个字符串之前打印在文件中的偏移量
  • 示例
[root@centos7 ~]# strings -fd -t x a.out 
a.out:     238 /lib64/ld-linux-x86-64.so.2
a.out:     31f |k       C
a.out:     5c9 libstdc++.so.6
a.out:     5d8 __gmon_start__
a.out:     5e7 __cxa_demangle
a.out:     5f6 libm.so.6
a.out:     600 libgcc_s.so.1
a.out:     60e libc.so.6
a.out:     618 puts
a.out:     61d printf
a.out:     624 malloc
...中间省略...
a.out:     975 UH-h `
a.out:     9a5 UH-h `
a.out:     e2a []A\A]A^A_
a.out:     e63 %s:%s+%s
a.out:     e6d %s:%s()+%s
a.out:     e79 ----------done----------
a.out:     f57 ;*3$"

7 代码中打印函数调用栈信息

8 修改 core 文件配置

8.1 修改 core 文件生成位置

/proc/sys/kernel/core_pattern 用来配置 core dump 文件生成位置、文件名。 - 示例:echo "/tmp/core-%e-%p-%t" > /proc/sys/kernel/core_pattern,其中/tmp是生成文件所在的文件夹(任意有权限的位置),core-%e-%p-%t是生成core文件名的格式 - 文件名格式如下

%p - insert pid into filename 添加pid
%u - insert current uid into filename 添加当前uid
%g - insert current gid into filename 添加当前gid
%s - insert signal that caused the coredump into the filename 添加导致产生core的信号
%t - insert UNIX time that the coredump occurred into filename 添加core文件生成时的unix时间
%h - insert hostname where the coredump happened into filename 添加主机名
%e - insert coredumping executable name into filename 添加命令名

8.2 修改 core 文件大小

  • 临时修改:(当前 shell 及子 shell 有效)使用 ulimit -c unlimited 使 core 文件大小无限制。
  • 修改 /etc/security/limits.conf 配置文件, 编辑如下
*       soft core      unlimited
*       hard core      unlimited

9 修改系统限制资源

/etc/security/limits.d下按照字母顺序排列的配置文件会覆盖 /etc/security/limits.conf中的 domain相同的的配置

limits.conf 文件的全路径是 /etc/security/limits.conf,是 conf-pamlimits 模块的配置文件。 每一行内容格式如下:

<domain>      <type>  <item>         <value>
  • <domain> :表示的是作用范围,可以是用户、用户组
  • user_name :用户名
  • @group_name :用户组名
  • * :所用用户
  • % :(比较复杂,不解释)
  • <type> :有软限制和硬限制区别,软限制不能超过硬限制,软限制可以有普通用户修改,而硬限制只能由 root 用户修改。可选值如下
  • soft :软限制
  • hard :硬限制
  • - :软限制和硬限制
  • <item> :资源项   - core - limits the core file size (KB)   - data - max data size (KB)   - fsize - maximum filesize (KB)   - memlock - max locked-in-memory address space (KB)   - nofile - max number of open file descriptors   - rss - max resident set size (KB)   - stack - max stack size (KB)   - cpu - max CPU time (MIN)   - nproc - max number of processes   - as - address space limit (KB)   - maxlogins - max number of logins for this user   - maxsyslogins - max number of logins on the system   - priority - the priority to run user process with   - locks - max number of file locks the user can hold   - sigpending - max number of pending signals   - msgqueue - max memory used by POSIX message queues (bytes)   - nice - max nice priority allowed to raise to values: [-20, 19]   - rtprio - max realtime priority
  • <value> :可以由无限制或无限大,或具体的值。priority and nice不能使用 unlimited
  • unlimited : 无限制
  • 其它数值
  • 示例
*               soft    core            0
*               hard    nofile          512
@student        hard    nproc           20
@faculty        soft    nproc           20
@faculty        hard    nproc           50
ftp             hard    nproc           0
@student        -       maxlogins       4
:123            hard    cpu             5000
@500:           soft    cpu             10000
600:700         hard    locks           10