2018年8月25日 星期六

Using gdbserver to debug MDM9xxx


openembedded提供了一個良好的gdb環境,設定步驟如下。
brook@vista:/home/brook/apps_proc/poky/build$ tar zxvf tmp-glibc/deploy/images/mdm9607/machine-image-mdm9607-dbg.tar.gz
./
./var/
./var/lib/
./var/lib/opkg/
./var/lib/opkg/info/
...

brook@vista:/home/brook/apps_proc/poky/build$ mv usr/lib/.debug/* usr/lib/
brook@vista:/home/brook/apps_proc/poky/build$ mv lib/.debug/* lib

brook@vista:/home/brook/apps_proc/poky/build$ ln -s libconfig.so.9.2.0 usr/lib/libconfig.so.9
...

brook@vista:/home/brook/apps_proc/poky/build$ ln -s libpthread-2.22.so lib/libpthread.so.0
brook@vista:/home/brook/apps_proc/poky/build$ ln -s librt-2.22.so lib/librt.so.1
brook@vista:/home/brook/apps_proc/poky/build$ ln -s libm-2.22.so lib/libm.so.6
brook@vista:/home/brook/apps_proc/poky/build$ ln -s libc-2.22.so lib/libc.so.6
brook@vista:/home/brook/apps_proc/poky/build$ ln -s ld-2.22.so lib/ld-linux.so.3
brook@vista:/home/brook/apps_proc/poky/build$ ln -s libdl-2.22.so lib/libdl.so.2
...

brook@vista:/home/brook/apps_proc/poky/build/tmp-glibc/work/armv7a-vfp-neon-oe-linux-gnueabi/myprog/git-r0/build/src$ adb push myprog /usr/bin/myprog

brook@vista:/home/brook/apps_proc/poky/build/tmp-glibc/work/armv7a-vfp-neon-oe-linux-gnueabi/myprog/git-r0/build/src$ adb shell
sh-3.2# gdbserver :2345 /usr/bin/myprog
Process /usr/bin/myprog created; pid = 9035
Listening on port 2345


接著host端就可以開啟GDB來debug了
brook@vista:/home/brook/apps_proc/poky/build/tmp-glibc/work/armv7a-vfp-neon-oe-linux-gnueabi/myprog/git-r0/build/src$ arm-oe-linux-gnueabi-gdb myprog 
GNU gdb (GDB) 7.10.1
Copyright (C) 2015 Free Software Foundation, Inc.
...
Reading symbols from myprog...done.
(gdb) set sysroot /home/brook/apps_proc/poky/build/
(gdb) target remote 10.0.0.1:2345
Remote debugging using 10.0.0.1:2345
Reading symbols from /home/brook/apps_proc/poky/build//lib/ld-linux.so.3...done.
0xb6fceac0 in ?? ()
(gdb) b main
Breakpoint 1 at 0x7f5595ac: file /home/brook/apps_proc/poky/build/tmp-glibc/work/armv7a-vfp-neon-oe-linux-gnueabi/myprog/git-r0/src/main.c, line 207.
(gdb) c
Continuing.

Breakpoint 1, main (argc=1, argv=0xbefffdf4)
    at /home/brook/apps_proc/poky/build/tmp-glibc/work/armv7a-vfp-neon-oe-linux-gnueabi/myprog/git-r0/src/main.c:207
207     {
(gdb) set sysroot /home/brook/apps_proc/poky/build
warning: .dynamic section for "/home/brook/apps_proc/poky/build/lib/libpthread.so.0" is not at the expected address (wrong library or version mismatch?)
warning: .dynamic section for "/home/brook/apps_proc/poky/build/lib/libm.so.6" is not at the expected address (wrong library or version mismatch?)
warning: .dynamic section for "/home/brook/apps_proc/poky/build/lib/libc.so.6" is not at the expected address (wrong library or version mismatch?)
Use the "info sharedlibrary" command to see the complete listing.
Do you need "set solib-search-path" or "set sysroot"?
Reading symbols from /home/brook/apps_proc/poky/build/usr/lib/libconfig.so.9...done.
Reading symbols from /home/brook/apps_proc/poky/build/lib/libpthread.so.0...done.
Reading symbols from /home/brook/apps_proc/poky/build/lib/librt.so.1...done.
Reading symbols from /home/brook/apps_proc/poky/build/lib/libm.so.6...done.
Reading symbols from /home/brook/apps_proc/poky/build/lib/libc.so.6...done.
Reading symbols from /home/brook/apps_proc/poky/build/lib/libdl.so.2...done.
(gdb) info share
From        To          Syms Read   Shared Object Library
0x7473e7c0  0x7475a328  Yes         /home/brook/apps_proc/poky/build/lib/ld-linux.so.3
0xb6fc42d0  0xb6fca6e0  Yes         /home/brook/apps_proc/poky/build/usr/lib/libconfig.so.9
0xb6ebc210  0xb6ecb4e8  Yes         /home/brook/apps_proc/poky/build/lib/libpthread.so.0
0xb6ea2720  0xb6ea6034  Yes         /home/brook/apps_proc/poky/build/lib/librt.so.1
0xb6e28bf0  0xb6e5ac20  Yes         /home/brook/apps_proc/poky/build/lib/libm.so.6
0xb6ced280  0xb6dee638  Yes         /home/brook/apps_proc/poky/build/lib/libc.so.6
0xb6cc3928  0xb6cc4870  Yes         /home/brook/apps_proc/poky/build/lib/libdl.so.2
確認所有symbol都有找到之後, 就可以開始debug之旅了.




2018年8月11日 星期六

note for "The Art of Readable Code" - CH13 Writing Less Code


The most readable code is no code at all.
Programmer最重要的技能之一就是知道哪些code不用寫,因為寫出來就要測試&維護,越小的程式碼越容易被維護,coupling程度越低越好,最好彼此獨立,有幾個方向:
  1. Create as much generic “utility” code as possible to remove duplicated code. (See Chapter 10, Extracting Unrelated Subproblems.)
  2. Remove unused code or useless features. (See the following sidebar.)
  3. Keep your project compartmentalized into disconnected subprojects.
  4. Generally, be conscious of the “weight” of your codebase. Keep it light and nimble.

Be Familiar with the Libraries Around You

每隔一段時間應該要花一下時間,讀一下你的 standard library,目的在於能對standard library的API有概念,以便在coding時能聯想到,並大量且反覆地使用這些library。


    參考資料:
  • The Art of Readable Code



2018年7月29日 星期日

note for "The Art of Readable Code" - CH12 Turning Thoughts into Code


You do not really understand something unless you can explain it to your grandmother. 
— Albert Einstein

先用口語描述演算法後,再轉成程式碼,能讓programmer寫出更自然的code,也有助於找出可以分解的子問題。
 We are reading three row iterators in parallel.
 Whenever the rows' times don't line up, advance the rows so they do line up.
 Then print the aligned rows, and advance the rows again.
 Keep doing this until there are no more matching rows left.

上述口語轉成的code為
def PrintStockTransactions(): 
    stock_iter = ...
    price_iter = ... 
    num_shares_iter = ...

    while True:
        time = AdvanceToMatchingTime(stock_iter, price_iter, num_shares_iter) 
        if time is None:
            return
        # Print the aligned rows.
        print "@", time,
        print stock_iter.ticker_symbol,
        print price_iter.price,
        print num_shares_iter.number_of_shares
        stock_iter.NextRow()
        price_iter.NextRow()
        num_shares_iter.NextRow()


    參考資料:
  • The Art of Readable Code



熱門文章