分类 Linux 相关 下的文章

Linux core dump

什么是 core dump

core dump 是一个进程的内存瞬时映像. core dump 这个名字来自于最早的计算机的内存技术. 使用 gdb 等工具可以读取 core dump, 然后分析进程当时的运行状态和内存信息.
https://man7.org/linux/man-pages/man5/core.5.html

如何获得 core dump

  1. 给进程发各种能产生 core dump 的 signal, 比如 SIGQUIT, SIGKILL 等.
  2. 使用 gdb 等 debug 工具对正在运行的进程发送 signal, 产生 core dump.

由于 1) core dump 会占用大量的磁盘空间, 2) core dump 可能包含内存的里面某些敏感数据, 所以 Linux 尽管在得到要 trigger core dump 的信号情况下, 也由于 ulimit 的控制, 默认不产生 core dump. 因此, 如果要捕获 core dump, 首先要选择打开 core dump limit 设置.
使用 ulimit -a 命令查看当前 ulimit 设置值:

eric@host:~$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 31660
max locked memory       (kbytes, -l) 16384
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 31660
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
修改 ulimit 设置

可以使用 ulimit -c unlimited 命令, 但是可能非 root 用户没有权限.
在 /etc/security/ 目录下, 有个 limits.conf 和 limits.d 目录(如果有 limits.d 则 limits.conf 不生效, 可以认为 limits.conf 是模板). 在 limits.d 目录下新建一个以 .conf 结尾的文本文件, 添加要改的一行.

eric@host:~$ ls /etc/security/ | grep limit
limits.conf
limits.d

core dump 的文件名

正常情况下 core dump 的文件名没有后缀, 文件名就是 core. 这个名字可以通过 /proc/sys/kernel/core_pattern 来改. 这个名字还可以使用一些模板字符来替换. 比如 %h: 主机名; %p: 进程号 %t: Epoch 毫秒数, %u: user id;

如何分析 core dump

tmp 目录文件无法执行, 报 Permission denied

今天在执行 aysnc-profiler 的时候, 遇到无法执行的问题: 为了方便文件清除, 把解压后的文件放到了 /tmp 目录, 然后把 owner 和 权限都加好, 之后切换java 进程的用户去执行, 报 Permission denied.

mkdir /tmp/profiler
tar -C /tmp/profiler -xvf async-profiler-1.7.1-linux-x64.tar.gz
sudo chmod -R 755 /tmp/profiler/*
sudo chown -R appuser /tmp/profiler
sudo su appuser
/tmp/profiler/profile.sh -d 60 -o tree -e cpu -f profile0717.log.html 
/tmp/profile/profiler.sh: Permission denied

如果使用 bash 去执行, 则报:

bash /tmp/profiler/profile.sh -d 60 -o tree -e cpu -f profile0717.log.html 
/tmp/profile/profiler.sh: line 67: /tmp/profile/build/jattach: Permission denied

既然文件 owner 和 读和执行权限都加好了, 为什么还报错呢?
原来在 /tmp 目录的挂载方式:

appsuer@test-host:/home/appuser$ mount | grep /tmp
tmpfs on /tmp type tmpfs (rw,nosuid,nodev,noexec)

它设置了 noexec 属性. 所以问题就知道出在那里了, 既然这样, 换个目录就解决了.

Linux namespaces

namespace 表示一种对全局资源使用的隔离方式, 同一 namespace 下的进程能看到一样的资源, 不同的 namespace 不能看到其他 namespace 的变动.
ls -l /proc//ns/
UTS IPC PID User Net Cgroup Mount Time

  1. UTS (Unix Time Sharing) namespace
    隔离跟主机名称相关的数据, 其内容包括: hostname, domain name, OS name, OS version, kernel 版本, 处理器信息等信息. 隔离之后使用 uname 这个系统调用就返回不同的值. 参看 uname 系统条用和 Linux uname 命令

    ~$ uname --help
    Usage: uname [OPTION]...
    Print certain system information.  With no OPTION, same as -s.
    
    -a, --all                print all information, in the following order,
                            except omit -p and -i if unknown:
    -s, --kernel-name        print the kernel name
    -n, --nodename           print the network node hostname
    -r, --kernel-release     print the kernel release
    -v, --kernel-version     print the kernel version
    -m, --machine            print the machine hardware name
    -p, --processor          print the processor type (non-portable)
    -i, --hardware-platform  print the hardware platform (non-portable)
    -o, --operating-system   print the operating system
     --help     display this help and exit
     --version  output version information and exit
  2. IPC
    隔离跟 IPC 相关的资源: System V IPC objects & POSIX message queue. 每个 IPC namespace 都有自己的 System V IPC identifiers 和它自己的 POSIX message queue filesystem. 系统必须设置 CONFIG_IPC_NS选项.
  3. PID
    用来隔离 PID 数字的命名空间, 不同 PID 的进程号可以重复, 新的 PID namespace 的进程号从 1 开始.
  4. User
  5. Net
  6. Time

参考:

  1. https://www.kernel.org/doc/html/latest/admin-guide/namespaces/compatibility-list.html
  2. https://windsock.io/uts-namespace/
  3. https://www.informit.com/articles/article.aspx?p=23618&seqNum=16#:~:text=The%20uname%20system%20call%20fills,h%3E%20if%20you%20use%20uname.
  4. http://www.linfo.org/uname.html
  5. https://man7.org/linux/man-pages/man7/namespaces.7.html
  6. https://man7.org/linux/man-pages/man7/pid_namespaces.7.html

Linux 模块学习笔记

  1. 使用 lsmod 命令查看已经load 的内核模块, 它其实读取的是 /proc/modules 的信息
  2. 当系统需要某个内核模块的功能的时候, 如果该模块还么有加载, 作为 daemon 的 kmod 进程就会使用 modprobe 命令去加载该模块;
  3. modprobe 可以接受模块的名称, 如: softdog, 或者该模块的别名, 如: char-major-10-30. 标准名称和别名的映射在文件 /etc/modprobe.conf 中, 如: alias char-major-10-30 softdog;
  4. 模块的依赖关系在 /lib/modules/version/modules.dep 中;
  5. depmod 会创建新的依赖关系;

以上内容可能过时, 来源: https://tldp.org/LDP/lkmpg/2.6/html/x44.html

Linux kprobes & uprobes, static tracepoint knowledge links

  1. Kernel Probes: https://www.kernel.org/doc/Documentation/kprobes.txt#:~:text=Kprobes%20enables%20you%20to%20dynamically,when%20the%20breakpoint%20is%20hit.
  2. Uprobe: https://www.kernel.org/doc/Documentation/trace/uprobetracer.txt
  3. https://kernelnewbies.org/Linux_3.5#Uprobes:_userspace_probes
  4. https://lwn.net/Articles/499190/
  5. static tracepoint and how it works: https://www.kernel.org/doc/Documentation/trace/tracepoints.txt