Sunday, April 22, 2007

Upgrade to Ubuntu 7.04

Yesterday, I follow the instruction here to upgrade my Ubuntu to 7.04. Unfortunately, the Update Manager crashes during package installation. To solve the problem, I use the following command:
dpkg --configure -a

Then, I reboot the Linux box. Some errors occurs. It fails to mount all partitions, excepts "/" partition. After some investigation, under the new kernel (2.6.20), IDE harddisks/CD-ROM are considered as SCSI. The following is the dmesg output:
ata1: PATA max UDMA/100 cmd 0x000101f0 ctl 0x000103f6 bmdma 0x0001f000 irq 14
ata2: PATA max UDMA/100 cmd 0x00010170 ctl 0x00010376 bmdma 0x0001f008 irq 15
scsi0 : ata_piix
ata1.00: ata_hpa_resize 1: sectors = 156301488, hpa_sectors = 156301488
ata1.00: ATA-6: WDC WD800BB-55HEA0, 13.03G13, max UDMA/100
ata1.00: 156301488 sectors, multi 16: LBA
ata1.00: ata_hpa_resize 1: sectors = 156301488, hpa_sectors = 156301488
ata1.00: configured for UDMA/100
scsi1 : ata_piix
ATA: abnormal status 0x7F on port 0x00010177
ata2.01: ATAPI, max UDMA/33
ata2.01: configured for UDMA/33
scsi 0:0:0:0: Direct-Access ATA WDC WD800BB-55HE 13.0 PQ: 0 ANSI: 5
scsi 1:0:1:0: CD-ROM RW-481248 1.00 PQ: 0 ANSI: 5


Therefore, the entries in /etc/fstab should be changed from "/dev/hdan" to "/dev/sdan".

Thursday, April 19, 2007

Know more about a process

Today, I would like to introduce some ways to get more information of a process, and hope those can help in trouble-shooting and debugging.

List environment variables


To list the environment variables of a process you can issue the command:
ps ewww pid
E.g.:

ahlam@oxygen:~/test/malloc$ ps ewww 4752
PID TTY STAT TIME COMMAND
4752 pts/2 Ss+ 0:00 bash USER=ahlam HOME=/home/ahlam DESKTOP_SESSION=default GDM_XSERVER_LOCATION=loca
l GTK_IM_MODULE=gcin LOGNAME=ahlam USERNAME=ahlam GDM_LANG=en_HK.UTF-8 PATH=/usr/local/sbin:/usr/local/bin:/u
sr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games DISPLAY=:0.0 LANG=en_HK.UTF-8 XMODIFIERS=@im=gcin XAUTHOR
ITY=/home/ahlam/.Xauthority SHELL=/bin/bash GDMSESSION=default QT_IM_MODULE=gcin PWD=/home/ahlam SSH_AUTH_SOC
K=/tmp/ssh-nRdglP4521/agent.4521 SSH_AGENT_PID=4563 DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-ix9My5o6
DJ,guid=8abd2646dc897f03d1cfb07e3a3d4b00 GTK_RC_FILES=/etc/gtk/gtkrc:/home/ahlam/.gtkrc-1.2-gnome2 SESSION_MA
NAGER=local/oxygen:/tmp/.ICE-unix/4521 GNOME_KEYRING_SOCKET=/tmp/keyring-fXmYul/socket GNOME_DESKTOP_SESSION_
ID=Default TERM=xterm COLORTERM=gnome-terminal WINDOWID=31457489



List out the threads of a process


ps ms pid
E.g:

ahlam@oxygen:~/test/malloc$ ps ms 4752
UID PID PENDING BLOCKED IGNORED CAUGHT STAT TTY TIME COMMAND
1000 4752 0000000000000000 - - - - pts/2 0:00 bash
1000 - 0000000000000000 0000000000000000 0000000000384004 000000004b813efb Ss+ - 0:00 -



Print the memory map


To list the memory map of a process, you can use the pmap command. It lists out the mapped memory regions and the purpose of the memory regions. It also lists out the .so files loaded by the process.
pmap pid
E.g:

ahlam@oxygen:~/test/malloc$ pmap 4752
4752: bash
08048000 644K r-x-- /bin/bash
080e9000 20K rw--- /bin/bash
080ee000 1804K rw--- [ anon ]
b7c94000 36K r-x-- /lib/tls/i686/cmov/libnss_files-2.4.so
b7c9d000 8K rw--- /lib/tls/i686/cmov/libnss_files-2.4.so
b7c9f000 32K r-x-- /lib/tls/i686/cmov/libnss_nis-2.4.so
b7ca7000 8K rw--- /lib/tls/i686/cmov/libnss_nis-2.4.so
b7ca9000 72K r-x-- /lib/tls/i686/cmov/libnsl-2.4.so
b7cbb000 8K rw--- /lib/tls/i686/cmov/libnsl-2.4.so
b7cbd000 8K rw--- [ anon ]
b7cbf000 28K r-x-- /lib/tls/i686/cmov/libnss_compat-2.4.so
b7cc6000 8K rw--- /lib/tls/i686/cmov/libnss_compat-2.4.so
b7cd7000 204K r---- /usr/lib/locale/en_HK.utf8/LC_CTYPE
b7d0a000 4K r---- /usr/lib/locale/en_HK.utf8/LC_NUMERIC
b7d0b000 4K r---- /usr/lib/locale/en_HK.utf8/LC_TIME
b7d0c000 860K r---- /usr/lib/locale/en_HK.utf8/LC_COLLATE
b7de3000 8K rw--- [ anon ]
b7de5000 1204K r-x-- /lib/tls/i686/cmov/libc-2.4.so
b7f12000 8K r---- /lib/tls/i686/cmov/libc-2.4.so
b7f14000 8K rw--- /lib/tls/i686/cmov/libc-2.4.so
b7f16000 12K rw--- [ anon ]
b7f19000 8K r-x-- /lib/tls/i686/cmov/libdl-2.4.so
b7f1b000 8K rw--- /lib/tls/i686/cmov/libdl-2.4.so
b7f1d000 220K r-x-- /lib/libncurses.so.5.5
b7f54000 32K rw--- /lib/libncurses.so.5.5
b7f5c000 4K rw--- [ anon ]
b7f5d000 4K r---- /usr/lib/locale/en_HK.utf8/LC_MONETARY
b7f5e000 4K r---- /usr/lib/locale/en_HK.utf8/LC_MESSAGES/SYS_LC_MESSAGES
b7f5f000 4K r---- /usr/lib/locale/en_HK.utf8/LC_PAPER
b7f60000 4K r---- /usr/lib/locale/en_HK.utf8/LC_NAME
b7f61000 4K r---- /usr/lib/locale/en_HK.utf8/LC_ADDRESS
b7f62000 4K r---- /usr/lib/locale/en_HK.utf8/LC_TELEPHONE
b7f63000 4K r---- /usr/lib/locale/en_HK.utf8/LC_MEASUREMENT
b7f64000 28K r--s- /usr/lib/gconv/gconv-modules.cache
b7f6b000 4K r---- /usr/lib/locale/en_HK.utf8/LC_IDENTIFICATION
b7f6c000 8K rw--- [ anon ]
b7f6e000 100K r-x-- /lib/ld-2.4.so
b7f87000 8K rw--- /lib/ld-2.4.so
bfa1c000 88K rw--- [ stack ]
ffffe000 4K ----- [ anon ]
total 5528K



/proc/pid


In proc/pid directory, it contains much information of a process.

ahlam@oxygen:~/test/malloc$ ls -l /proc/4752
total 0
dr-xr-xr-x 2 ahlam ahlam 0 2007-04-19 20:17 attr
-r-------- 1 ahlam ahlam 0 2007-04-19 20:17 auxv
-r--r--r-- 1 ahlam ahlam 0 2007-04-19 19:57 cmdline
-r--r--r-- 1 ahlam ahlam 0 2007-04-19 20:17 cpuset
lrwxrwxrwx 1 ahlam ahlam 0 2007-04-19 18:20 cwd -> /home/ahlam/test/malloc
-r-------- 1 ahlam ahlam 0 2007-04-19 19:58 environ
lrwxrwxrwx 1 ahlam ahlam 0 2007-04-19 20:17 exe -> /bin/bash
dr-x------ 2 ahlam ahlam 0 2007-04-19 19:57 fd
-r--r--r-- 1 ahlam ahlam 0 2007-04-19 20:06 maps
-rw------- 1 ahlam ahlam 0 2007-04-19 20:17 mem
-r--r--r-- 1 ahlam ahlam 0 2007-04-19 20:17 mounts
-r-------- 1 ahlam ahlam 0 2007-04-19 20:17 mountstats
-rw-r--r-- 1 ahlam ahlam 0 2007-04-19 20:17 oom_adj
-r--r--r-- 1 ahlam ahlam 0 2007-04-19 20:17 oom_score
lrwxrwxrwx 1 ahlam ahlam 0 2007-04-19 20:17 root -> /
-rw------- 1 ahlam ahlam 0 2007-04-19 20:17 seccomp
-r--r--r-- 1 ahlam ahlam 0 2007-04-19 20:17 smaps
-r--r--r-- 1 ahlam ahlam 0 2007-04-19 19:57 stat
-r--r--r-- 1 ahlam ahlam 0 2007-04-19 20:17 statm
-r--r--r-- 1 ahlam ahlam 0 2007-04-19 19:57 status
dr-xr-xr-x 3 ahlam ahlam 0 2007-04-19 20:17 task
-r--r--r-- 1 ahlam ahlam 0 2007-04-19 20:17 wchan


For details, please man proc.

Sunday, April 15, 2007

Optimistic memory allocation strategy in Linux

If you know C programming language, you must know what is malloc. malloc is for dynamic memory allocation. In colleges, we learned that malloc should return NULL in case of out-of-memory. However, it is not the case in Linux. By default, Linux uses optimistic memory allocation strategy. Under this strategy, Linux assumes there always exists free memory. The memory region returns by malloc is not actually allocated until the process touches the memory region. This means the memory region returns by malloc may not be available. In case of out-of-memory, the OOM Killer in Linux will pick up one or more process to kill. This sounds strange!

Reference:
man malloc
http://linux-mm.org/OOM_Killer

Friday, April 13, 2007

Change the title of xterm

In most modern Linux distribution, the title of terminal follows the working directory. The mechanism behind this feature is not simple. You can even customize the title. Please note that, in this article, it is assumed to use xterm and bash.

In xterm, the following escape sequence can change the title of terminal windows:
ESC]0;title_stringBEL
where ESC and BEL is 033 and 007 (in octal ASCII) respectively. For example, we can use the following echo command to set the terminal title as "Hello World.":
echo -ne "\033]0;Hello World.\007"

However, if you issue the above command in your terminal, the title may not be changed. It is because the PROMPT_COMMAND environment variable have already been defined for changing the terminal title. The value in PROMPT_COMMAND environment variable will be executed as command prior to issuing each prompt. In my terminal, the value of PROMPT_COMMAND is:
echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"

In the above command, ${USER} and ${HOSTNAME} is the username and hostname respectively, while the ${PWD/$HOME/~} is the current working directory with $HOME abbreviated as ~. Therefore, in my terminal, the title will be changed to show username, hostname and current working directory, e.g.:
ahlam@oxygen: ~/download

To customize your terminal title, you should update the value of PROMPT_COMMAND, e.g.:
export PROMPT_COMMAND='echo -ne "\033]0;Hello World.\007"'


Reference:
How to change the title of an xterm
man bash