打印“可执行程序”或者是“共享库”的符号信息,例如:
root@VM-Ubuntu203001:/home # nm -D /lib/tls/i686/cmov/libc.so.6
root@VM-Ubuntu203001:/home # nm -A /usr/lib/lib*.so 2> /dev/null | grep ’ crypt$’
/usr/lib/libcrypt.so:00007080 W crypt
The –A option to nm specifies that the library name should be listed at the start of
each line displaying a symbol. This is necessary because, by default, nm lists the
library name once, and then, on subsequent lines, all of the symbols it contains,
which isn’t useful for the kind of filtering shown in the above example. In addition,
we discard standard error output in order to hide error messages about files in formats
unrecognized by nm. From the above output, we can see that crypt() is defined
in the libcrypt library
BSD系统,打印object file信息
访问ELF文件信息。
打印目标文件“符号信息”,也就是symtab区间的信息。
root@VM-Ubuntu203001:~/test# readelf -s virt_fun_impl.o
打印目标文件“区间信息”,也就是所有区间(section)
root@VM-Ubuntu203001:~/test# readelf -S virt_fun_impl.o
将编译连接后可执行程序的地址转换为文件名称和行号。
要求程序编译必须加上 –g选项,生成符号信息。
-e <二进制程序文件名>
-f 生成函数名称
例如:
root@VM-Ubuntu203001:~/test# addr2line -e a.out 0x80484c8 -f
main
/root/test/main.c:7
显示可执行文件或共享库text , bss , data段的大小。
例如,
root@VM-Ubuntu203001:~/test# size a.out
text data bss dec hex filename
1511 284 8 1803 70b a.out
Assuming you have no file foo.h, the command:
touch foo.h; cpp -dM foo.h
will show all the predefined macros.
例如:
root@VM-Ubuntu203001:~/work/tcp# cat a.dmp
root@VM-Ubuntu203001:~/work/tcp# cpp -dM a.dmp
pmap
pmap结果中,包含:
1、 每个线程的堆栈
2、 共享库,readonly 是代码部分,所有进程共享,rw部分是每个进程独有的数据部分
3、 堆空间
4、 可执行文件,readonly 是代码部分,所有进程共享,rw部分是每个进程独有的数据部分
virt: 虚拟内存
rss:是物理内存
查看内存使用情况
Usage: ldd [OPTION]… FILE…
例如:
root@VM-Ubuntu203001:/home/xxx# ldd /bin/ls
1、 用ldd获得glibc共享库路径。如下,
root@VM-Ubuntu203001:/home # ldd /bin/ls
linux-gate.so.1 => (0xb7f33000)
librt.so.1 => /lib/tls/i686/cmov/librt.so.1 (0xb7f28000)
libselinux.so.1 => /lib/libselinux.so.1 (0xb7f0f000)
libacl.so.1 => /lib/libacl.so.1 (0xb7f07000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7db8000)
libpthread.so.0 => /lib/tls/i686/cmov/libpthread.so.0 (0xb7da0000)
/lib/ld-linux.so.2 (0xb7f34000)
libdl.so.2 => /lib/tls/i686/cmov/libdl.so.2 (0xb7d9c000)
libattr.so.1 => /lib/libattr.so.1 (0xb7d98000)
2、 执行/lib/tls/i686/cmov/libc.so.6。如下,
root@VM-Ubuntu203001:/home # /lib/tls/i686/cmov/libc.so.6
GNU C Library stable release version 2.7, by Roland McGrath et al.
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.2.4 (Ubuntu 4.2.4-1ubuntu3).
Compiled on a Linux >>2.6.24-25-server<< system on 2011-01-11.
Available extensions:
crypt add-on version 2.1 by Michael Glad and others
GNU Libidn by Simon Josefsson
Native POSIX Threads Library by Ulrich Drepper et al
BIND-8.2.3-T5B
For bug reporting instructions, please see:
.
1、 把共享库的路径加入LD_LIBRARY_PATH环境变量
2、 把共享库的路径加入/etc/ld.so.conf文件中,有时/etc/ld.so.conf include了另一个路径,我们只需要在这个路径下面创建一个 *.conf文件,然后把库路径加入这个conf文件。即可。例如,
root@VM-Ubuntu203001:/usr/lib# cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf
root@VM-Ubuntu203001:/usr/lib# cd /etc/ld.so.conf.d
root@VM-Ubuntu203001:/etc/ld.so.conf.d# ls
i486-linux-gnu.conf libc.conf
root@VM-Ubuntu203001:/etc/ld.so.conf.d# cat libc.conf
# libc default configuration
/usr/local/lib
3、 在链接的时候,通过设置-rpath选项来完成。例如,
$ cd /home/mtk/pdir/d1
$ gcc -g -c -Wall -fPIC modx1.c
$ gcc -g -shared -o libx1.so modx1.o -Wl,-rpath,/home/mtk/pdir/d2 \
-L/home/mtk/pdir/d2 -lx2
-rpath选项可以通过设置环境变量LD_RUN_PATH来代替。
$ cat /proc/$$/status
Name: bash
State: S (sleeping)
Tgid: 3515
Pid: 3515
PPid: 3452
TracerPid: 0
Uid: 1000 1000 1000 1000
Gid: 100 100 100 100
FDSize: 256
Groups: 16 33 100
VmPeak: 9136 kB
VmSize: 7896 kB
VmLck: 0 kB
VmHWM: 7572 kB
VmRSS: 6316 kB
VmData: 5224 kB
VmStk: 88 kB
VmExe: 572 kB
VmLib: 1708 kB
VmPTE: 20 kB
Threads: 1
SigQ: 0/3067
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000010000
SigIgn: 0000000000384004
SigCgt: 000000004b813efb
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: ffffffffffffffff
Cpus_allowed: 00000001
Cpus_allowed_list: 0
Mems_allowed: 1
Mems_allowed_list: 0
voluntary_ctxt_switches: 150
nonvoluntary_ctxt_switches: 545
The fields are as follows:
Name: Command run by this process.
State: Current state of the process. One of "R (running)", "S (sleeping)", "D (disk sleep)", "T (stopped)", "T (tracing stop)", "Z (zombie)", or "X (dead)".
Tgid: Thread group ID (i.e., Process ID).
Pid: Thread ID (see gettid(2)).
TracerPid: PID of process tracing this process (0 if not being traced).
Uid, Gid: Real, effective, saved set, and file system UIDs (GIDs).
FDSize: Number of file descriptor slots currently allocated.
Groups: Supplementary group list.
VmPeak: Peak virtual memory size.
VmSize: Virtual memory size.
VmLck: Locked memory size (see mlock(3)).
VmHWM: Peak resident set size ("high water mark").
VmRSS: Resident set size.
VmData, VmStk, VmExe: Size of data, stack, and text segments.
VmLib: Shared library code size.
VmPTE: Page table entries size (since Linux 2.6.10).
Threads: Number of threads in process containing this thread.
SigPnd, ShdPnd: Number of signals pending for thread and for process as a whole (see pthreads(7) and signal(7)).
SigBlk, SigIgn, SigCgt: Masks indicating signals being blocked, ignored, and caught (see signal(7)).
CapInh, CapPrm, CapEff: Masks of capabilities enabled in inheritable, permitted, and effective sets (see capabilities(7)).
CapBnd: Capability Bounding set (since kernel 2.6.26, see capabilities(7)).
Cpus_allowed: Mask of CPUs on which this process may run (since Linux 2.6.24, see cpuset(7)).
Cpus_allowed_list: Same as previous, but in "list format" (since Linux 2.6.26, see cpuset(7)).
Mems_allowed: Mask of memory nodes allowed to this process (since Linux 2.6.24, see cpuset(7)).
Mems_allowed_list: Same as previous, but in "list format" (since Linux 2.6.26, see cpuset(7)).
voluntary_context_switches, nonvoluntary_context_switches: Number of voluntary and involuntary context switches (since Linux 2.6.23).
[user@server ~]$ cat /proc/7278/stat
7278 (postgres) S 1 7257 7257 0 -1 4202496 36060376 10845160168 0 749 20435 137212 158536835 39143290 15 0 1 0 50528579 3763298304 20289 18446744073709551615 4194304 7336916 140734091375136 18446744073709551615 225773929891 0 0 19935232 84487 0 0 0 17 2 0 0 12
The fields, in order, are:
pid: The process ID.
comm: The filename of the executable, in parentheses. This is visible whether or not the executable is swapped out.
state: One character from the string "RSDZTW" where R is running, S is sleeping in an interruptible wait, D is waiting in uninterruptible disk sleep, Z is zombie, T is traced or stopped (on a signal), and W is paging.
ppid: The PID of the parent.
pgrp: The process group ID of the process.
session: The session ID of the process.
tty_nr: The controlling terminal of the process. (The minor device number is contained in the combination of bits 31 to 20 and 7 to 0; the major device number is in bits 15 to 8.)
tpgid: The ID of the foreground process group of the controlling terminal of the process.
flags: The kernel flags word of the process. For bit meanings, see the PF_* defines in . Details depend on the kernel version.
minflt: The number of minor faults the process has made which have not required loading a memory page from disk.
cminflt: The number of minor faults that the process's waited-for children have made.
majflt: The number of major faults the process has made which have required loading a memory page from disk.
cmajflt: The number of major faults that the process's waited-for children have made.
utime: Amount of time that this process has been scheduled in user mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK). This includes guest time, guest_time (time spent running a virtual CPU, see below), so that applications that are not aware of the guest time field do not lose that time from their calculations.
stime: Amount of time that this process has been scheduled in kernel mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK).
cutime: Amount of time that this process's waited-for children have been scheduled in user mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK). (See also times(2).) This includes guest time, cguest_time (time spent running a virtual CPU, see below).
cstime: Amount of time that this process's waited-for children have been scheduled in kernel mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK).
priority: (Explanation for Linux 2.6) For processes running a real-time scheduling policy (policy below; see sched_setscheduler(2)), this is the negated scheduling priority, minus one; that is, a number in the range -2 to -100, corresponding to real-time priorities 1 to 99. For processes running under a non-real-time scheduling policy, this is the raw nice value (setpriority(2)) as represented in the kernel. The kernel stores nice values as numbers in the range 0 (high) to 39 (low), corresponding to the user-visible nice range of -20 to 19. Before Linux 2.6, this was a scaled value based on the scheduler weighting given to this process.
nice: The nice value (see setpriority(2)), a value in the range 19 (low priority) to -20 (high priority).
num_threads: Number of threads in this process (since Linux 2.6). Before kernel 2.6, this field was hard coded to 0 as a placeholder for an earlier removed field.
itrealvalue: The time in jiffies before the next SIGALRM is sent to the process due to an interval timer. Since kernel 2.6.17, this field is no longer maintained, and is hard coded as 0.
starttime: The time in jiffies the process started after system boot.
vsize: Virtual memory size in bytes.
rss: Resident Set Size: number of pages the process has in real memory. This is just the pages which count toward text, data, or stack space. This does not include pages which have not been demand-loaded in, or which are swapped out.
rsslim: Current soft limit in bytes on the rss of the process; see the description of RLIMIT_RSS in getpriority(2).
startcode: The address above which program text can run.
endcode: The address below which program text can run.
startstack: The address of the start (i.e., bottom) of the stack.
kstkesp: The current value of ESP (stack pointer), as found in the kernel stack page for the process.
kstkeip: The current EIP (instruction pointer).
signal: The bitmap of pending signals, displayed as a decimal number. Obsolete, because it does not provide information on real-time signals; use /proc/[pid]/status instead.
blocked: The bitmap of blocked signals, displayed as a decimalnumber. Obsolete, because it does not provide information on real-time signals; use /proc/[pid]/status instead.
sigignore: The bitmap of ignored signals, displayed as a decimal number. Obsolete, because it does not provide information on real-time signals; use /proc/[pid]/status instead.
sigcatch: The bitmap of caught signals, displayed as a decimal number. Obsolete, because it does not provide information on real-time signals; use /proc/[pid]/status instead.
wchan: This is the "channel" in which the process is waiting. It is the address of a system call, and can be looked up in a namelist if you need a textual name. (If you have an up-to-date /etc/psdatabase, then try ps -l to see the WCHAN field in action.)
nswap: Number of pages swapped (not maintained).
cnswap: Cumulative nswap for child processes (not maintained).
exit_signal: (since Linux 2.1.22) Signal to be sent to parent when we die.
processor: (since Linux 2.2.8) CPU number last executed on.
rt_priority: (since Linux 2.5.19; was: before Linux 2.6.22) Real-time scheduling priority, a number in the range 1 to 99 for processes scheduled under a real-time policy, or 0, for non-real-time processes (see sched_setscheduler(2)).
policy: (since Linux 2.5.19; was: before Linux 2.6.22) Scheduling policy (see sched_setscheduler(2)). Decode using the SCHED_* constants in linux/sched.h.
delayacct_blkio_ticks: (since Linux 2.6.18) Aggregated block I/O delays, measured in clock ticks (centiseconds).
guest_time: (since Linux 2.6.24) Guest time of the process (time spent running a virtual CPU for a guest operating system), measured in clock ticks (divide by sysconf(_SC_CLK_TCK).
cguest_time:ld (since Linux 2.6.24) Guest time of the process's children, measured in clock ticks (divide by sysconf(_SC_CLK_TCK).
查看某个进程虚拟内存使用,布局情况
linux内核启动的时候会将初始化信息保存到ring-buffer中,可以通过dmesg查看启动信息。
例如,如下命令可以查看可用内存信息。
root@ubuntu:~# dmesg |grep kernel
[ 356.152405] Memory: 1016716k/1037888k available (2260k kernel code, 20464k reserved, 1035k data, 384k init, 120384k highmem)
[ 356.152411] virtual kernel memory layout:
[ 356.922512] Booting paravirtualized kernel on bare hardware
[ 358.474805] Freeing unused kernel memory: 384k freed
[ 358.474846] Write protecting the kernel read-only data: 824k