Professional Documents
Culture Documents
Utilities for
2005-2006 Strategy
Debugging in Unix
Srinivas Adyapak
Wipro confidential
1
Common Utilities for Developers in Unix
•Common Utilities required by Developers to improve their code and enhance their productivity
• These utilities will be available in most of the Linux installations.
Debugging cscope, ctags – Code browsing
strace (truss in Solaris) – Debugging a running process
pstack - Stack trace of a running process
pmap - Memory used by a process
nm - Symbol names in an object or exe
ddd – A GUI for Command line debugging
File Management lsof - Open files and Processes opening the files
df, du – available disk space, disk usage
Networking netstat - displays the contents of various network related data structures
ifconfig – configuring a network device
ping - Sends ICMP ECHO_REQUEST packets to network hosts
• Not limited to C –> C++, Python, Perl, hardware designed languages such
as Verilog.
• Builds the cross-references on all the source files to examine – includes .c, .h
file extensions
• cscope vsftpd-2.0.5/* - builds the cross-references for all files under the
directory vsftpd-2.0.5
• cscope rebuilds the cross-reference only if a source file has changed or
the list of source files is different.
• Can directly navigate to the occurrence of the source.
Wipro confidential 4
cscope
Cscope version 15.5 Press the ? key for help
Wipro confidential 5
cscope
Functions called by this function: str_netfd_alloc
Wipro confidential 6
cscope
Functions calling this function: str_netfd_alloc
Wipro confidential 7
strace
Traces the system calls used by program
Lets you know what’s happening even if there is no debugger or source code
Wipro confidential 8
strace
Each line in the trace contains the system call name, followed by its arguments
in parentheses and its return value.
• open("/dev/null", O_RDONLY) = 3
Errors (typically a return value of -1) have the errno symbol and error string
appended.
• open("/foo/bar", O_RDONLY) = -1 ENOENT (No such file or directory)
Usage
• strace –c –p 2155 gives the count time, calls and errors for each system call and
report summary for the process with pid 2155
• strace –o strace.out –p 2155
• strace <program>
Wipro confidential 9
strace
For eg. If a normal user tries to delete the services file (owned by root)
• strace rm /etc/services
lstat64("/etc/services", {st_mode=S_IFREG|0644, st_size=362031, ...}) = 0
access("/etc/services", W_OK) = -1 EACCES (Permission denied)
unlink("/etc/services") = -1 EACCES (Permission denied)
write(2, "cannot remove `/etc/services\'", 29) = 29
Wipro confidential 10
pmap
pmap reports the memory map of a process
Amount of memory used by the process
Details of the map are also available in the /proc
• pmap –x 2352
2352: ./server
Address Kbytes RSS Anon Locked Mode Mapping
00111000 1200 - - - r-x-- libc-2.4.so
0023d000 12 - - - r-x-- libc-2.4.so
00240000 4 - - - rwx-- libc-2.4.so
00241000 12 - - - rwx-- [ anon ]
006dc000 100 - - - r-x-- ld-2.4.so
006f5000 4 - - - r-x-- ld-2.4.so
006f6000 4 - - - rwx-- ld-2.4.so
00769000 4 - - - r-x-- [ anon ]
08048000 4 - - - r-x-- server
08049000 4 - - - rw--- server
b7f5d000 4 - - - rw--- [ anon ]
b7f6e000 4 - - - rw--- [ anon ]
bfe59000 88 - - - rw--- [ stack ]
-------- ------- ------- ------- ----------------------------------------------------------------
total kB 1444 - - -
Wipro confidential 11
pstack
pstack
• prints the stack trace of the running process – quite useful along with strace.
• Attaches to the active process given on the command line
• The output is read bottom up.
Wipro confidential 12
nm
Used to retrieve information on symbol names inside an object file or
executable file.
Helps in resolving problems due to name conflicts
• If there are poorly defined headers tracking down the offending module becomes
easier using nm.
Wipro confidential 13
ps
ps
Wipro confidential 14
ps
ps
ps -eo start,etime,state,pid,cmd
Wipro confidential 15
top
top is a simple text based system monitoring tool
Displays system summary and details, status of individual tasks (memory, cpu, pid and
more)
The top line prints the current time, uptime since the last reboot, users logged in, and the
load average.
Wipro confidential 16
top
top - 10:57:09 up 2:02, 3 users, load average: 0.07, 0.12, 0.60
Tasks: 96 total, 1 running, 95 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.3% us, 2.4% sy, 0.0% ni, 97.2% id, 0.0% wa, 0.0% hi, 0.0% si
Mem: 126252k total, 118640k used, 7612k free, 4148k buffers
Swap: 1048568k total, 16084k used, 1032484k free, 52500k cached
Wipro confidential 17
lsof
Reports a list of all
• open files and
• processes that opened them.
Wipro confidential 18
lsof
To list the internet ports/files opened by guest user
• lsof –i | grep guest
• lsof –u guest –a -i
Wipro confidential 19
lsof
Sometimes you need to track down the user or process that's blocking you from
unmounting a disk.
• Umount /opt will give -> umount: /opt: device is busy
• kill `lsof -t /opt` will kill all the users on the /opt
Wipro confidential 20
netstat
Netstat gets the information about the status of network connections
• netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
10.116.3.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 10.116.3.1 0.0.0.0 UG 0 0 0 eth0
Wipro confidential 21
netstat
Processes accepting connections for TCP
• netstat -tpln
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:1353 0.0.0.0:* LISTEN 2352/server
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:50000 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:50002 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:52340 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN -
Wipro confidential 22
ifconfig
Allows to configure network interfaces (bring interfaces up or down)
Displays the currently active and inactive network interfaces.
Supports a variety of address families and hardware types
• Address famililes – inet (ipv4), inet6(ipv6), unix ..
• Hardware types – ethernet, x.25, frame-relay ..
• ifconfig eth0 - View the network settings on the first Ethernet adapter installed in the computer.
eth0 Link encap:Ethernet HWaddr 00:03:FF:E3:08:1C
inet addr:10.116.3.80 Bcast:10.116.3.255 Mask:255.255.255.0
inet6 addr: fe80::203:ffff:fee3:81c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1640 errors:0 dropped:0 overruns:0 frame:0
TX packets:434 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:163039 (159.2 KiB) TX bytes:108893 (106.3 KiB)
Interrupt:11 Base address:0x6000
Wipro confidential 23
sar
Collect, report, or save system activity information.
Displays the CPU activity
Memory usage can be monitored
Disk I/O activity
Number of context switches - a good indication of how much time a process is
wasting
• sar –u -> cpu utilisation
Linux 2.6.15-1.2054_FC5 (localhost.localdomain) 12/05/2006
Wipro confidential 24
sar
sar –r -> prints the memory usage
10:10:02 AM kbmemfree kbmemused %memused kbbuffers kbcached kbswpfree kbswpused %swpused kbswpcad
10:20:01 AM 3500 122752 97.23 4108 45604 1032052 16516 1.58 1952
10:30:02 AM 2904 123348 97.70 2572 49116 1032052 16516 1.58 980
Average: 3202 123050 97.46 3340 47360 1032052 16516 1.58 1466
10:10:02 AM cswch/s
10:20:01 AM 1749.10
10:30:02 AM 1997.61
10:40:01 AM 1269.78
Average: 1688.47
Wipro confidential 25
iostat
Reports the CPU statistics and Input/Output statistics for devices and partitions
Can be used with sar and vmstat
CPU utilization gives the percentage of idle time with/with out any outstanding
I/Os requests (iowait, idle).
Blocks read and written is given for device utilization
Wipro confidential 26
ddd
DDD – Data Display Debugger is a popular GUI for command line
debugger
Debugs executable binaries using GDB, DBX command line debuggers
Debugs programs in
• Java (DDD with JDB Java debugger)
• Python (DDD with PYDB, Python debugger)
• Perl (Perl debugger)
Advantages
• User need not know much about the underlying command line debugger used with
DDD
• Available freely
Wipro confidential 27