wiki:jazz/09-02-01

2009-02-01

favicon.ico

Programming : Source Code Tracing

  • GNU Global - trace source code 的工具 - 把程式原始碼變 HTML 網頁 ,有點類似 LXR

GNU Toolchain

OpenCSL

CGDB

  • [參考] http://www.cmlab.csie.ntu.edu.tw/~daniel/linux/cgdb.html
    基本操作方法
    
        * ESC -- switch to source window
        * i -- switch to GDB mode
        * spacebar -- 在目前的地方設一個中斷點 (source window mode)
        * t -- 在目前的地方設一個暫時中斷點 (source window mode)
        * '-' -- 縮減 source window
        * '=' -- 增加 source window
    
  • [參考] ~/.cgdb/cgdbrc 參考設定
        set arrowstyle=highlight
        set autosourcereload
        set shortcut
        set tabstop=4
        set winsplit=top_big
        hi Statement cterm=bold ctermfg=6
        hi PreProc cterm=bold ctermfg=4
        hi IncSearch ctermfg=2
        hi LineHighlight cterm=bold ctermfg=3 ctermbg=4
    
  • [參考] http://blogkrogh.blogspot.com/2008/05/gdb-xxgdb-ddd.html
    • kgdb 的介面看起來不錯。cgdb 有中文支援問題?! gdb 也有 ui 界面:
      $ gdb -tui 程式碼
      

BUGFIX: jfbterm (5)

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>       // include frame buffer header files
#include <sys/ioctl.h>

int main(void)
{
  int fbfd = 0 ;            // frame buffer file description

  /* Get device independent unchangeable information about a frame buffer
   * device and a specific video mode by FBIOGET_FSCREENINFO ioctl. */
  struct  fb_fix_screeninfo finfo;

  fbfd = open( "/dev/fb0" , O_RDWR );
  if ( !fbfd )
  {
    printf( "Error: cannot open framebuffer device.\n" );
    exit(1);
  }
  printf("The framebuffer device was opened successfully.\n");

  /* Get fixed screen information */
  if ( ioctl ( fbfd ,  FBIOGET_FSCREENINFO , &finfo )  )
  {
    printf("Error reading fixed screen information.\n");
    exit(2);
  } else {
    printf("Succesfully Read the Fixed Screen Information\n");
  }

  printf("fb_fix_screeninfo.smem_len = %d\n", finfo.smem_len);
  printf("fb_fix_screeninfo.mmio_len = %d\n", finfo.mmio_len);
}
  • 在 Ubuntu Intrepid 上執行結果
    root@intrepid:~# uname -a
    Linux intrepid 2.6.27-7-server #1 SMP Tue Nov 4 20:16:57 UTC 2008 x86_64 GNU/Linux
    root@intrepid:~# ./fbinfo
    The framebuffer device was opened successfully.
    Succesfully Read the Fixed Screen Information
    fb_fix_screeninfo.smem_len = 16777216
    fb_fix_screeninfo.mmio_len = 0
    
  • 以下是在 Debian Sarge 上執行結果,值得一提的是 jfbterm 在 2.6.8 核心上可以正常執行。(不過還有字型問題需要調整)
    root@wsu-admin:~# uname -a
    Linux wsu-admin 2.6.8-2-386 #1 Tue Aug 16 12:46:35 UTC 2005 i686 GNU/Linux
    root@wsu-admin:~# ./fbinfo
    The framebuffer device was opened successfully.
    Succesfully Read the Fixed Screen Information
    fb_fix_screeninfo.smem_len = 65536
    fb_fix_screeninfo.mmio_len = 0
    
    root@wsu-admin:~# jfbterm
    jfbterm - Kanji on framebuffer console Version 0.4.7
           Copyright (C) 2003 Fumitoshi UKAI
            Copyright (C) 1999-2000 Noritoshi Masuichi
    This program is based on KON2
            Copyright (C) 1992-1996 Takashi MANABE
    
    (**) : Configuration file `/etc/jfbterm.conf'
    .... 略 ....
    exec : /bin/bash ioctl FBIOGET_CON2FBMAP: Invalid argument
    color 0 : 0, 0
    color 1 : 2a, 2a
    color 2 : 2a, 2a
    color 3 : 2a, 2a
    color 4 : 2a, 2a
    color 5 : 2a, 2a
    color 6 : 2a, 2a
    color 7 : 2a, 2a
    color 8 : 15, 15
    color 9 : 3f, 3f
    color 10 : 3f, 3f
    color 11 : 3f, 3f
    color 12 : 3f, 3f
    color 13 : 3f, 3f
    color 14 : 3f, 3f
    color 15 : 3f, 3f
    
  • 以下是在 Debian Etch 上執行結果,有趣的是 jfbterm 0.4.7-7 在 2.6.18-4 上執行,縱使有 cannot mmap(mmio) : Invalid argument 的錯誤訊息,但仍舊可以正常進入 jfbterm 的環境。
    root@etch:~# uname -a
    Linux etch 2.6.18-4-686 #1 SMP Wed May 9 23:03:12 UTC 2007 i686 GNU/Linux
    root@etch:~# ./fbinfo
    The framebuffer device was opened successfully.
    Succesfully Read the Fixed Screen Information
    fb_fix_screeninfo.smem_len = 1920000
    fb_fix_screeninfo.mmio_len = 0
    
    root@etch:~# jfbterm
    jfbterm - Kanji on framebuffer console Version 0.4.7
           Copyright (C) 2003 Fumitoshi UKAI
            Copyright (C) 1999-2000 Noritoshi Masuichi
    This program is based on KON2
            Copyright (C) 1992-1996 Takashi MANABE
    
    (**) : Configuration file `/etc/jfbterm.conf'
    ... 略 ...
    exec : /bin/bash color 0 : 0, 0
    color 1 : 15, 15
    color 2 : 540, 540
    color 3 : 555, 555
    color 4 : a800, a800
    color 5 : a815, a815
    color 6 : ad40, ad40
    color 7 : ad55, ad55
    color 8 : 52aa, 52aa
    color 9 : 52bf, 52bf
    color 10 : 57ea, 57ea
    color 11 : 57ff, 57ff
    color 12 : faaa, faaa
    color 13 : fabf, fabf
    color 14 : ffea, ffea
    color 15 : ffff, ffff
    cannot mmap(mmio) : Invalid argument
    
  • 為了測試 jfbterm 的中文相容性,我們寫了一隻簡單的 script
    #!/bin/bash
    echo " TEST 中文測試 "
    echo "Press Any Key to continue!"
    read
    
  • 在 Ubutnu intriped 的 tty1 上執行 jfbterm -f /etc/jfbterm.conf -e /root/test.sh 可以正常看到" TEST 中文測試 "字樣,但卻無法正常跳出,因此我們再來 DEBUG 一下。
    file jfbterm
    set args -f /etc/jfbterm.conf -e /root/test.sh
    show args
    break fbcommon.c:500
    break fbcommon.c:557
    break fbcommon.c:566
    break term.c:103
    break term.c:284
    r
    c
    c
    c
    bt
    c
    bt
    p utp->ut_time
    p utp->ut_tv
    
    Breakpoint 4, tterm_final (p=0x6147c0) at term.c:103
    (gdb) bt
    #0  tterm_final (p=0x6147c0) at term.c:103
    #1  0x0000000000408adf in sigchld (sig=<value optimized out>) at term.c:82
    #2  <signal handler called>
    #3  0x00007f5dfc00f433 in select () from /lib/libc.so.6
    #4  0x0000000000408873 in tterm_start (p=0x6147c0, tn=<value optimized out>, en=<value optimized out>) at term.c:192
    #5  0x0000000000402cf2 in main (argc=<value optimized out>, argv=0x7fff049477f8) at main.c:438
    (gdb) c
    Continuing.
    
    Breakpoint 5, tterm_reset_utmp (p=<value optimized out>) at term.c:284
    (gdb) bt
    #0  tterm_reset_utmp (p=<value optimized out>) at term.c:284
    #1  0x0000000000408a50 in tterm_final (p=0xe) at term.c:103
    #2  0x0000000000408adf in sigchld (sig=<value optimized out>) at term.c:82
    #3  <signal handler called>
    #4  0x00007fa268131433 in select () from /lib/libc.so.6
    #5  0x0000000000408873 in tterm_start (p=0x6147c0, tn=<value optimized out>, en=<value optimized out>) at term.c:192
    #6  0x0000000000402cf2 in main (argc=<value optimized out>, argv=0x7fff70a69328) at main.c:438
    (gdb) p utp->ut_time
    There is no member named ut_time.
    
  • [註] utp->ut_time = utp->ut_tv.tv_sec
    < /usr/include/bits/utmp.h >
    
     59 /* The structure describing an entry in the user accounting database.  */
     60 struct utmp
     61 {
     62   short int ut_type;            /* Type of login.  */
     63   pid_t ut_pid;                 /* Process ID of login process.  */
     64   char ut_line[UT_LINESIZE];    /* Devicename.  */
     65   char ut_id[4];                /* Inittab ID.  */
     66   char ut_user[UT_NAMESIZE];    /* Username.  */
     67   char ut_host[UT_HOSTSIZE];    /* Hostname for remote login.  */
     68   struct exit_status ut_exit;   /* Exit status of a process marked
     69                                    as DEAD_PROCESS.  */
     70 /* The ut_session and ut_tv fields must be the same size when compiled
     71    32- and 64-bit.  This allows data files and shared memory to be
     72    shared between 32- and 64-bit applications.  */
     73 #if __WORDSIZE == 64 && defined __WORDSIZE_COMPAT32
     74   int32_t ut_session;           /* Session ID, used for windowing.  */
     75   struct
     76   {
     77     int32_t tv_sec;             /* Seconds.  */
     78     int32_t tv_usec;            /* Microseconds.  */
     79   } ut_tv;                      /* Time entry was made.  */
     80 #else
     81   long int ut_session;          /* Session ID, used for windowing.  */
     82   struct timeval ut_tv;         /* Time entry was made.  */
     83 #endif
     84
     85   int32_t ut_addr_v6[4];        /* Internet address of remote host.  */
     86   char __unused[20];            /* Reserved for future use.  */
     87 };
    
     94 # define ut_time        ut_tv.tv_sec
    
  • GDB 線上除錯練習
    [tty1]
    
    root@etch:~# apt-get build-dep jfbterm
    root@etch:~# apt-get source jfbterm
    root@etch:~# apt-get install jfbterm
    root@etch:~# cd jfbterm-0.4.7
    root@etch:~/jfbterm-0.4.7# ./configure --enable-debug
    root@etch:~/jfbterm-0.4.7# make
    root@etch:~/jfbterm-0.4.7# ./jfbterm -f /etc/jfbterm.conf -e /root/test.sh
    
    [tty2]
    
    root@etch:~# gdb /root/jfbterm-0.4.7/jfbterm `pidof jfbterm`
    GNU gdb 6.4.90-debian
    Copyright (C) 2006 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i486-linux-gnu"...Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".
    
    Attaching to program: /root/jfbterm-0.4.7/jfbterm, process 7480
    Reading symbols from /lib/tls/i686/cmov/libutil.so.1...done.
    Loaded symbols for /lib/tls/i686/cmov/libutil.so.1
    Reading symbols from /lib/tls/i686/cmov/libm.so.6...done.
    Loaded symbols for /lib/tls/i686/cmov/libm.so.6
    Reading symbols from /lib/tls/i686/cmov/libc.so.6...done.
    Loaded symbols for /lib/tls/i686/cmov/libc.so.6
    Reading symbols from /lib/ld-linux.so.2...done.
    Loaded symbols for /lib/ld-linux.so.2
    Reading symbols from /lib/tls/i686/cmov/libnss_compat.so.2...done.
    Loaded symbols for /lib/tls/i686/cmov/libnss_compat.so.2
    Reading symbols from /lib/tls/i686/cmov/libnsl.so.1...done.
    Loaded symbols for /lib/tls/i686/cmov/libnsl.so.1
    Reading symbols from /lib/tls/i686/cmov/libnss_nis.so.2...done.
    Loaded symbols for /lib/tls/i686/cmov/libnss_nis.so.2
    Reading symbols from /lib/tls/i686/cmov/libnss_files.so.2...done.
    Loaded symbols for /lib/tls/i686/cmov/libnss_files.so.2
    Failed to read a valid object file image from memory.
    0xb7fea410 in ?? ()
    (gdb) break term.c:284
    Breakpoint 1 at 0x804f552: file term.c, line 284.
    (gdb) c
    Continuing.
    
    Breakpoint 1, tterm_final (p=0x8058be0) at term.c:284
    284             time(&(utp->ut_time));
    (gdb) c
    Continuing.
    
    Breakpoint 1, tterm_final (p=0x8058be0) at term.c:284
    284             time(&(utp->ut_time));
    (gdb) c
    Continuing.
    
    Program exited normally.
    
  • GDB 遠端除錯練習
    [tty1]
    gdbserver 127.0.0.1:1234 /root/jfbterm-0.4.7/jfbterm
    [pts1]
    (gdb) target remote 127.0.0.1:1234
    
  • 在 DEBUG 的時候偶爾會看到 <value optimized out>,但是該怎麼去掉呢??
    • [參考] http://www.cs.utk.edu/~vose/c-stuff/onlinedocs/gdb_7.html - 不要最佳化 ( 查 gcc manpage 得知 -O0 可以取消最佳化 )
      The values of arguments that were not saved in their stack frames are shown as `<value optimized out>'.
      
      If you need to display the values of such optimized-out arguments, either deduce that from other variables 
      whose values depend on the one you are interested in, or recompile without optimizations.
      
    • [作法] 在 configure 的時候加上 CFLAGS
      • 註: 因為如果有設定 CFLAGS 的話,在 configure 裡是會直接採用我們提供的 CFLAGS。因此縱使加上 --enable-debug 也沒有用,configure 不會幫你加 -g 進去。
        root@intrepid:~/jfbterm-0.4.7-dev# CFLAGS="-g -O0" ./configure
        
  • 繼續追為何結束時會收到 SIGSEGV 的主因
    < 直接在 tty1 或在 screen 中執行 >
    root@intrepid:~/jfbterm-0.4.7-dev# ./jfbterm -f /etc/jfbterm.conf -e /root/test.sh
    
    root@intrepid:~/jfbterm-0.4.7-dev# gdb ./jfbterm `pidof jfbterm`
    break term.c:103
    break term.c:284
    c
    #### 直接在 tty1 不透過 screen 執行 的結果 #####
    Breakpoint 1, tterm_final (p=0x617a00) at term.c:103
    103             tterm_reset_utmp(p);
    (gdb) s
    tterm_reset_utmp (p=0x617a00) at term.c:271
    271     {
    (gdb) s
    275             tn = rindex(p->name, '/') + 4;
    (gdb) p p->name
    $1 = "/dev/pts/0", '\0' <repeats 53 times>
    #### 直接在 tty1 有透過 screen 執行 的結果 #####
    
    Breakpoint 1, tterm_final (p=0x617a00) at term.c:103
    103             tterm_reset_utmp(p);
    (gdb) s
    tterm_reset_utmp (p=0x617a00) at term.c:271
    271     {
    (gdb) s
    275             tn = rindex(p->name, '/') + 4;
    (gdb) p p->name
    $1 = "/dev/pts/1", '\0' <repeats 53 times>
    
  • manpage of getutid(3)
    SYNOPSIS
    
           struct utmp *getutid(struct utmp *ut);
    
    DESCRIPTION
    
           getutid() searches forward from the current file position in  the  utmp
           file  based  upon  ut.   If  ut->ut_type  is one of RUN_LVL, BOOT_TIME,
           NEW_TIME, or OLD_TIME,  getutid()  will  find  the  first  entry  whose
           ut_type  field matches ut->ut_type.  If ut->ut_type is one of INIT_PRO-
           CESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS, getutid() will find
           the first entry whose ut_id field matches ut->ut_id.
    
    RETURN VALUE
           getutent(), getutid(), getutline() and pututline() return a pointer  to
           a  struct  utmp  on  success, and NULL on failure.  This struct utmp is
           allocated in static storage,  and  may  be  overwritten  by  subsequent
           calls.
    
    FILES
           /var/run/utmp  database of currently logged-in users
           /var/log/wtmp  database of past user logins
    
    NOTES
       Glibc Notes
           The above functions are not thread-safe.  Glibc adds reentrant versions
    
           #define _GNU_SOURCE    /* or _SVID_SOURCE or _BSD_SOURCE */
           #include <utmp.h>
    
           int getutent_r(struct utmp *ubuf, struct utmp **ubufp);
    
           int getutid_r(struct utmp *ut,
                         struct utmp *ubuf, struct utmp **ubufp);
    
           int getutline_r(struct utmp *ut,
                           struct utmp *ubuf, struct utmp **ubufp);
    
           These  functions  are  GNU  extensions, analogs of the functions of the
           same name without the _r suffix.  The ubuf parameter gives these  func-
           tions  a  place to store their result.  On success they return 0, and a
           pointer to the result is written in *ubufp.  On error  these  functions
           return -1.
    
    
    root@intrepid:~# w
     02:02:14 up 6 min,  5 users,  load average: 0.35, 0.78, 0.39
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    :tty1:S.0        01:58    1:46m  0.24s  0.21s ./jfbterm -f /etc/jfbterm.conf -e /root/test.sh ### screen 0
    root     pts/1    :tty1:S.1        01:58    3:55m  0.01s  0.01s /bin/bash                                       ### screen 1
    root     pts/3    172.21.253.1     01:59    1:19m  0.12s  0.10s gdb ./jfbterm 3833                              ### from ssh for gdb
    root     pts/2    -                01:59    1:46m  0.00s  0.21s ./jfbterm -f /etc/jfbterm.conf -e /root/test.sh ### fb created by jfbterm
    root     pts/4    172.21.253.1     02:01    0.00s  0.02s  0.00s w                                               ### from ssh for this command `w'
    
Last modified 15 years ago Last modified on Feb 2, 2009, 2:22:03 AM