2015年1月24日 星期六

[Embedded] Why no core dump after segmentation fault?

1. Follow below command to set core dump generated in embedded Linux.
$ ulimit -c 1024
$ echo '/tmp/core_%e.%p' | tee /proc/sys/kernel/core_pattern

2. Why no core dump?
  • no write access to the directory 
  • use "ls" to check
  • the program changes the working directory 
  • how to check??
  • look for the core in other places too 
  • use "sysctl -a | grep core_pattern" to check
  • disk is full 
  • use "df ." to check
  • most Unix systems do not allow a setuid process to dump core 
  • use "cat /proc/sys/fs/suid_dumpable" to check,  
  • use "echo 1 > /proc/sys/fs/suid_dumpable" to change setting 
  • ulimit is set in one shell and the program is started in a different shell or environment
  • check /proc/pid/environ for current shell
  • note that only the program executed after "ulimit -c 1024" can generate core

3. We can use "kill -ABRT  pid_of_process" to generate core manually.

4. Check /proc/pid/limits for system limit
Limit                     Soft Limit           Hard Limit           Units
Max cpu time              unlimited            unlimited            seconds
Max file size             unlimited            unlimited            bytes
Max data size             unlimited            unlimited            bytes
Max stack size            8388608              unlimited            bytes
Max core file size        0                    unlimited            bytesMax resident set          unlimited            unlimited            bytes
Max processes             748                  748                  processes
Max open files            1024                 1024                 files
Max locked memory         65536                65536                bytes
Max address space         unlimited            unlimited            bytes
Max file locks            unlimited            unlimited            locks
Max pending signals       748                  748                  signals
Max msgqueue size         819200               819200               bytes
Max nice priority         0                    0
Max realtime priority     0                    0
Max realtime timeout      unlimited            unlimited            us

Reference:
  1. http://stackoverflow.com/questions/15272817/segmentation-fault-no-core-dump
  2. http://www.idimmu.net/2013/06/21/enable-linux-core-dump/
  3. http://www.gnu.org/software/bash/manual/bashref.html#Bourne-Shell-Builtins
  4. http://blog.csdn.net/pro_or_gram/article/details/15813879
  5. http://linuxxperts.com/enabling-core-dumps-in-embedded-systems/
  6. http://tieba.baidu.com/p/2365639005

2015年1月18日 星期日

[Embedded] Ubuntu的核心編譯

Below is the procedures to compile a linux kernel for ubuntu 

1.  get kernel source code 
$ sudo apt-get install linux-source
$ cp /usr/src/linux-source-3.13.0.tar.bz2 ~/ubuntu
$ cd ~/ubuntu
$ tar jxvf linux-source-3.13.0.tar.bz2

2. compile kernel
$ cd linux-source-3.13.0
$ make menuconfig
$ make

3. install new kernel
$ make modules_install
$ make install
$ update-initramfs -c -k

5. make sure kernel is build correctly
$ sudo ls /boot/*

6. check current kernel versuib
$ uname -r
// The original version is The new version is linux-source-3.13.0

7. After reboot
$ uname -r
// The new version is linux-source-3.13.0

2015年1月2日 星期五

用中文字找注音念法

小時候中文沒學好,常常看到某些複雜的字卻不會念,前陣子去吃了家火鍋,叫做溱洧陶養,看到店名,還跟老婆一起猜,到底是唸作溱(ㄓㄣ)洧(ㄨㄟˇ),還是唸作溱(ㄓㄣ)洧(一ㄡˇ)

為了要應付這種狀況,當下便想找個 app 幫忙查一下注音,可是沒想到 iOS 上面並沒有這種應用,我想可能是太簡單了,所以沒人想寫。所以只好自己來弄囉。以下針對這個 app 實作,作個記錄。

1. 輸入
預設的使用情境是看到某個字,卻不會念,因此想要查這個字的注音,所以提供使用者的輸入方式應該會有兩種,分別是
  • 透過手寫輸入法輸入,
  • 透過相機,拍下字,以此圖片當做輸入

2. 手寫輸入法輸入
以 iOS 而言,系統內本來便提供中文手寫輸入法,因此我們所需要的便是幫使用者把這個輸入法叫出來,讓使用者方便輸入。邏輯如下:
// 判斷 iPhone 上是否已經安裝中文手寫輸入法 
NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; 
if ([lang isEqualToString:@"zh-Hant"]) { // 繁體中文輸入   // iPhone 上已安裝中文手寫輸入法   ;
}
else {
   // iPhone 上並無安裝中文手寫輸入法
   // 提醒使用者需安裝中文手寫輸入法才能使用,
   // 並且取消手寫輸入功能,只支援用圖片當成輸入來源。

   ;
}

3. 辨識圖片,找出中文字
圖片變成中文字的過程叫做OCR(Optical character recognition),此部份可以直接參考此Tesseract 的使用範例 
以下簡介此範例使用步驟,
1. Download and install
$ git clone https://github.com/pablosproject/iPhone-OCR-Tesseract-and-OpenCV.git
$ sudo gem install cocoapods
$ cd iPhone-OCR-Tesseract-and-OpenCV
$ pod setup
$ pod install 
2. setup
download Chinese (Traditional) language data for Tesseract 3.02 from https://code.google.com/p/tesseract-ocr/downloads/list
$ tar xvf tesseract-ocr-3.02.chi_tra.tar
$ cp tesseract-ocr/tessdata/* iPhone-OCR-Tesseract-and-OpenCV/infojobOCR/tessdata
$ open TestOCR.xcworkspace

4. 找注音
基本的想法很簡單,透過使用者的輸入,找到對應的中文字碼(BIG5 or UNICODE),到注音的字庫內找到對應的碼,在螢幕上顯示注音。 
iOS內的每個字元都是使用 UNICODE (UTF-16),中文碼的範圍介於4E00 ~ 9FFF之間(細節請參考全字庫),因此透過手寫輸入法所取得字,將其轉成 UTF-16 即可,查詢的方式可分為離線查詢,或線上查詢兩種

線上查詢 
可以直接登入此網頁,輸入對應的UNICODE即可,用法舉例如下:
 
若APP要支援此方法,便需要簡化操作與顯示頁面,當使用者輸入之後,使用CGI請此伺服器查詢資料,然後解析XML,直接取出第三頁內的部分資料提供使用者檢視,例如只取出發音子頁面。 
當然,如果要直接作個網頁介面連到此網站也是一種方式啦。 
離線查詢 
要用此方法,首先得需要有一個 unicode 與注音的對照表格,在iOS的注音輸入法內一定有個類似的表格,只是該如何取得對應資料。 
個人的想法是兩種方式都提供,預設使用離線查詢,若使用者想懂多一個對應的資訊,再幫打開全字庫的網頁。 

註:後來詢問老闆的結果是唸作(ㄓㄣ  ㄨㄟˇ ㄊㄠˊ 一ㄤˇ)

參考資料:
  1. 注音字母 與 Unicode 對照表
  2. U全字庫Unicode 字碼查詢
  3. 教育部終身教育司- 中文譯音轉換系統
  4. ios Managing the Keyboard

[Assembly] Relocation,以靜態聯結為範例

C code

/* a.c */
extern int shared;

int main()
{
    int a = 100;
    swap( &a, &shared );
}

/* b.c */
int shared = 1;

void swap( int* a, int* b )
{
    *a ^= *b ^= *a ^= *b;
}


Object file


$ gcc -c a.c b.c
$ ld a.o b.o -e main -o ab
$ objdump -h ab


$ objdump -t ab

Assembly code


 a.o 有兩個符號需要重新定位,shared 與 swap,其位置分別位於 0x15 與 0x21處。此處
  • R_386_32 表示 絕對定址修正(位址長度為 32bit,4 bytes)
  • R_386_PC32 表示 相對定址修正

    shared 與 swap 對應的程式碼對應如下
    • extern int shared;  --> line 11,暫時位址為 "0x00000000"
    • swap( &a, &shared ); --> line 20,暫時位址為 "0xFFFFFFFC"

    參考下述執行檔 ab,已經分別針對 shared 與 swap 重新定位
    • extern int shared; ,位址由 "0x00000000" 改為 "0x080490f8"
    • swap( &a, &shared ); ,位址由 "0xFFFFFFFC"  改為 "0x00000003",表示 swap函數的實際位址為  下個指令位址 + Offset = 0x080450b9 + 0x00000003 = 0x008480bc


    參考資料:
    1. 程式設計師的自我修養
    2. https://github.com/samuelololol/self-knowledgeable/tree/master/4