Saturday, June 17, 2017

How to apply a patch made by repo diff?

Source: https://groups.google.com/forum/#!topic/repo-discuss/43juvD1qGIQ

## Script to patch up diff reated by `repo diff`

if [ -z "$1" ] || [ ! -e "$1" ]; then
    echo "Usages: $0 <repo_diff_file>";
    exit 0;
fi

rm -fr _tmp_splits*
cat $1 | csplit -qf '' -b "_tmp_splits.%d.diff" - '/^project.*\/$/' '{*}'

working_dir=`pwd`

for proj_diff in `ls _tmp_splits.*.diff`
do
    chg_dir=`cat $proj_diff | grep '^project.*\/$' | cut -d " " -f 2`
    echo "FILE: $proj_diff $chg_dir"
    if [ -e $chg_dir ]; then
        ( cd $chg_dir; \
            cat $working_dir/$proj_diff | grep -v '^project.*\/$' | patch -Np1;);
    else
        echo "$0: Project directory $chg_dir don't exists.";
    fi
done

Android 調試.so庫文件

資料來源: http://www.blogjava.net/anymobile/articles/314685.html

作者:徐建祥(netpirate@gmail.com)
日期:2010/03/06
網址:http://www.anymobile.org

調試步驟如下:
1/ 將異常的地址信息存到文本文件,並存放在項目根目錄;
2/ 使用panic.py解析該文件;
3/ 根據返回的信息(文件名;行號;方法名)調試程序。

以操作第三方的Touch input 輸入法為例,過程如下:

# 該輸入法的英文和手寫OK,操作中文輸入法時,出現異常,軟鍵盤消失,異常日誌如下
02-05 06:41:19.834 D/dalvikvm(  751): Trying to load lib /system/lib/libimezi.so 0x43e2a788
02-05 06:41:19.874 D/dalvikvm(  751): Added shared lib /system/lib/libimezi.so 0x43e2a788
02-05 06:41:20.035 I/DEBUG   (  551): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
02-05 06:41:20.035 I/DEBUG   (  551): Build fingerprint: 'unknown'
02-05 06:41:20.035 I/DEBUG   (  551): pid: 751, tid: 751  >>> com.htc.android.cime <<<
02-05 06:41:20.045 I/DEBUG   (  551): signal 11 (SIGSEGV), fault addr 00000003
02-05 06:41:20.045 I/DEBUG   (  551):  r0 00000000  r1 00000000  r2 807248ec  r3 807248ec
02-05 06:41:20.055 I/DEBUG   (  551):  r4 00252d80  r5 00000004  r6 00000001  r7 00000000
02-05 06:41:20.055 I/DEBUG   (  551):  r8 beff8550  r9 41459d28  10 41459d18  fp 00000000
02-05 06:41:20.055 I/DEBUG   (  551):  ip 8072430c  sp beff84f0  lr 80716b4f  pc 80717bce cpsr 60000030
02-05 06:41:20.135 I/DEBUG   (  551):          #00  pc 00017bce  /system/lib/libimezi.so
02-05 06:41:20.135 I/DEBUG   (  551):          #01  pc 00010f80  /system/lib/libimezi.so
02-05 06:41:20.145 I/DEBUG   (  551): stack:
02-05 06:41:20.155 I/DEBUG   (  551):     beff84b0  ad083e10 
02-05 06:41:20.155 I/DEBUG   (  551):     beff84b4  ad05d44f  /system/lib/libdvm.so
02-05 06:41:20.155 I/DEBUG   (  551):     beff84b8  ad06b8a0  /system/lib/libdvm.so
02-05 06:41:20.155 I/DEBUG   (  551):     beff84bc  ad083e10 
02-05 06:41:20.167 I/DEBUG   (  551):     beff84c0  43e2aa90 
02-05 06:41:20.175 I/DEBUG   (  551):     beff84c4  00000003 
02-05 06:41:20.175 I/DEBUG   (  551):     beff84c8  00000014 
02-05 06:41:20.185 I/DEBUG   (  551):     beff84cc  80716b4f  /system/lib/libimezi.so

# 保存異常日誌,存為文件: 20100205_ime.txt
02-05 06:41:20.045 I/DEBUG   (  551):  r0 00000000  r1 00000000  r2 807248ec  r3 807248ec
02-05 06:41:20.055 I/DEBUG   (  551):  r4 00252d80  r5 00000004  r6 00000001  r7 00000000
02-05 06:41:20.055 I/DEBUG   (  551):  r8 beff8550  r9 41459d28  10 41459d18  fp 00000000
02-05 06:41:20.055 I/DEBUG   (  551):  ip 8072430c  sp beff84f0  lr 80716b4f  pc 80717bce  cpsr 60000030
02-05 06:41:20.135 I/DEBUG   (  551):          #00  pc 00017bce  /system/lib/libimezi.so
02-05 06:41:20.135 I/DEBUG   (  551):          #01  pc 00010f80  /system/lib/libimezi.so

# 執行腳本
xujianxiang@wingdroid-clone:~/workspace/xujx/wing15$ ./panic.py 20100205_ime.txt
read file ok
/home/xujianxiang/workspace/xujx/wing15/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-addr2line: '/home/xujianxiang/workspace/xujx/wing15/out/target/product/generic/symbols/system/lib/libimezi.so': No such file
Traceback (most recent call last):
  File "./panic.py", line 69, in <module>
    print "%-30s%s" % (list[1],list[0])
IndexError: list index out of range

# 沒找到庫文件,因為這個文件是第三方的,只存放在/out/target/product/generic/system/lib /libimezi.so,拷貝一份存到/out/target/product/generic/symbols/system/lib /libimezi.so,再次執行腳本
xujianxiang@wingdroid-clone:~/workspace/xujx/wing15$ ./panic.py 20100205_ime.txt
read file ok
zi81keyd.c:0                  Zi8GetTableData
zi81keyd.c:0                  Zi8InitializeDynamic

# 成功解析異常,沒有zi8庫文件所致,更新如下庫文件,輸入法支持中文拼音。
/system/lib/zi/Zi8DatPYP_CN.z8d
/system/lib/zi/Zi8DatPYS_CN.z8d
/system/lib/zi/Zi8DatZHA_CN.z8d
/system/lib/zi/Zi8DatZHA_HK.z8d
/system/lib/zi/Zi8DatZHA_TW.z8d
/system/lib/zi/Zi8DatZYP_TW.z8d
/system/lib/zi/Zi8DatZYS_TW.z8d


附 panic.py:

#!/usr/bin/python
# stack symbol parser

import os
import string
import sys

#define android product name
#ANDROID_PRODUCT_NAME = 'generic'
ANDROID_PRODUCT_NAME = 'smdk6410'

ANDROID_WORKSPACE = os.getcwd()+"/"

# addr2line tool path and symbol path
addr2line_tool = ANDROID_WORKSPACE + 'prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-addr2line'
symbol_dir = ANDROID_WORKSPACE + 'out/target/product/' + ANDROID_PRODUCT_NAME +'/symbols'
symbol_bin = symbol_dir + '/system/bin/'
symbol_lib = symbol_dir + '/system/lib/'

class ReadLog:
    def __init__(self,filename):
        self.logname = filename
    def parse(self):
        f = file(self.logname,'r')
        lines = f.readlines()
        if lines != []:
            print 'read file ok'
        else:
            print 'read file failed'
        result =[]
        for line in lines:
            if line.find('stack') != -1:
                print 'stop search'
                break
            elif line.find('system') != -1:
                #print 'find one item' + line
                result.append(line)
        return result

class ParseContent:
    def __init__(self,addr,lib):
            self.address = addr # pc address
            self.exename = lib  # executable or shared library
    def addr2line(self):
        cmd = addr2line_tool + " -C -f -s -e " + symbol_dir + self.exename + " " + self.address
        #print cmd
        stream = os.popen(cmd)
        lines = stream.readlines();
        list = map(string.strip,lines)
        return list
    
inputarg = sys.argv
if len(inputarg) < 2:
    print 'Please input panic log'
    exit()

filename = inputarg[1]
readlog = ReadLog(filename)
inputlist = readlog.parse()

for item in inputlist:
    itemsplit = item.split()
    test = ParseContent(itemsplit[-2],itemsplit[-1])
    list = test.addr2line()
    print "%-30s%s" % (list[1],list[0])

Tuesday, February 07, 2017

[轉] 關於refs/for/

資料來源: http://lishicongli.blog.163.com/blog/static/146825902013213439500/

1. 這個不是git的規則,而是gerrit的規則,
2. Branches, remote-tracking branches, and tags等等都是對commite的引用(reference),引用都以 「refs/……」表示. 比如remote branch: origin/git_int(=refs/remotes/origin/git_int), local tag: v2.0(=refs/tags/v2.0), local branch: git_int(=refs/heads/git_int)…
3. 簡單點說,就是refs/for/mybranch需要經過code review之後才可以提交;refs/heads/mybranch不需要code review。

(since you want to directly push into the branch, rather than create code reviews. Pushing to refs/for/* creates code reviews which must be approved and then submitted. Pushing to refs/heads/* bypasses review entirely, and just enters the commits directly into the branch. The latter does not check committer identity, making it appropriate for importing past project history)
如果需要code review,直接push
$git push origin master
那麼就會有「! [remote rejected] master -> master (prohibited by Gerrit)」的錯誤信息
而這樣push就沒有問題,
$git push origin HEAD:refs/for/mybranch

下面一段是對「refs/for」更詳細的描述:
The documentation for Gerrit explains that you push to the "magical refs/for/'branch' ref using any Git client tool".



This image is taken from the Intro to Gerrit. When you push to Gerrit, you do git push gerrit HEAD:refs/for/. This pushes your changes to the staging area (in the diagram, "Pending Changes"). Gerrit doesn't actually have a branch called ; it lies to the git client.
Internally, Gerrit has it's own implementation for the Git and SSH stacks. This allows it to provide the "magical" refs/for/ refs.
When a push request is received to create a ref in one of these namespaces Gerrit performs its own logic to update the database, and then lies to the client about the result of the operation. A successful result causes the client to believe that Gerrit has created the ref, but in reality Gerrit hasn't created the ref at all.
After a successful patch (i.e, the patch has been pushed to Gerrit, [putting it into the "Pending Changes" staging area], reviewed, and the review has passed), Gerrit pushes the change from the "Pending Changes" into the "Authoritative Repository", calculating which branch to push it into based on the magic it did when you pushed to refs/for/. This way, successfully reviewed patches can be pulled directly from the correct branches of the Authoritative Repository.

Saturday, November 19, 2016

How to binarize image.

* Binary image has following steps:
** To convert image to gray scale.
** To check every pixel if greater than threshold, set value to maximum(255), otherwise minimum(0).

#include <cv.hpp>

using namespace cv;
#define THRESHOLD 100

int main( int argc, char** argv )
{
  Mat image;
  image = imread( "resource/lena.png", IMREAD_COLOR );
  Mat Output( image.rows, image.cols, CV_8UC3, Scalar(0, 0, 0) );
  int height = image.rows;
  int width = image.cols;

  for(int row = 0; row < height; row++) {
      for(int col = 0; col < width; col++) {
          uchar val = (image.at<Vec3b>(row, col)[0] * 0.2999 + image.at<Vec3b>(row, col)[1] * 0.587 + image.at<Vec3b>(row, col)[2] * 0.114);

          Output.at<Vec3b>(row, col)[0] = (val > THRESHOLD ? 255 : 0);
          Output.at<Vec3b>(row, col)[1] = (val > THRESHOLD ? 255 : 0);
          Output.at<Vec3b>(row, col)[2] = (val > THRESHOLD ? 255 : 0);
      }
  }

  namedWindow("out", WINDOW_NORMAL);
  imshow("out", Output);

  waitKey(0);

  return 0;
}
Result:

Friday, November 18, 2016

How to convert grayscale image.

#include <cv.hpp>

using namespace cv;

int main( int argc, char** argv )
{
  Mat image;
  image = imread( "resource/lena.png", IMREAD_COLOR );
  Mat Output( image.rows, image.cols, CV_8UC3, Scalar(0, 0, 0) );
  int height = image.rows;
  int width = image.cols;

  for(int row = 0; row < height; row++) {
      for(int col = 0; col < width; col++) {
          uchar val = (image.at<Vec3b>(row, col)[0] * 0.2999 + image.at<Vec3b>(row, col)[1] * 0.587 + image.at<Vec3b>(row, col)[2] * 0.114);
          Output.at<Vec3b>(row, col)[0] = val;
          Output.at<Vec3b>(row, col)[1] = val;
          Output.at<Vec3b>(row, col)[2] = val;
      }
  }

  namedWindow("out", WINDOW_NORMAL);
  imshow("out", Output);

  waitKey(0);

  return 0;
}

Result:


Thursday, November 17, 2016

To get RGB channel and output.

#include <cv.hpp>

using namespace cv;

int main( int argc, char** argv )
{
  Mat image;
  image = imread( "resource/lena.png", IMREAD_COLOR );
  Mat OutputR( image.rows, image.cols, CV_8UC3, Scalar(0, 0, 0) );
  Mat OutputG( image.rows, image.cols, CV_8UC3, Scalar(0, 0, 0) );
  Mat OutputB( image.rows, image.cols, CV_8UC3, Scalar(0, 0, 0) );
  int height = image.rows;
  int width = image.cols;

  for(int row = 0; row < height; row++) {
   for(int col = 0; col < width; col++) {

    OutputB.at<Vec3b>(row, col)[0] = image.at<Vec3b>(row, col)[0];
    OutputB.at<Vec3b>(row, col)[1] = 0;
    OutputB.at<Vec3b>(row, col)[2] = 0;
    OutputG.at<Vec3b>(row, col)[0] = 0;
    OutputG.at<Vec3b>(row, col)[1] = image.at<Vec3b>(row, col)[1];
    OutputG.at<Vec3b>(row, col)[2] = 0;
    OutputR.at<Vec3b>(row, col)[0] = 0;
    OutputR.at<Vec3b>(row, col)[1] = 0;
    OutputR.at<Vec3b>(row, col)[2] = image.at<Vec3b>(row, col)[2];
   }
  }

  namedWindow("outr", WINDOW_NORMAL);
  imshow("outr", OutputR);

  namedWindow("outg", WINDOW_NORMAL);
  imshow("outg", OutputG);

  namedWindow("outb", WINDOW_NORMAL);
  imshow("outb", OutputB);

  waitKey(0);

  return 0;
}


Results:

Using opencv to inverse Image.


#include <cv.hpp>

using namespace cv;

int main( int argc, char** argv )
{
  Mat image;
  int height, width;
  // load lena picture and convert to grayscale
  image = imread("resource/lena.png", IMREAD_GRAYSCALE);
  // store height and width
  height = image.rows;
  width = image.cols;
  // show image
  imshow("lena1", image);
  // inverse image
  for(int i = 0; i < height; i++) {
      for(int j = 0; j < width; j++) {
          image.at<uchar>(i, j) = 255 - image.at<uchar>(i,j);
      }
  }
  // show image again to compare
  imshow("lena2", image);

  waitKey(0);

  return 0;
}
Result:

Eclipse mars.2 opencv configuration for mac

How to configure opencv on eclipse mars.2 Release(4.5.2).
You have to install opencv3 by issue command "brew install opencv3"
* Elipse->Projects(right click)->Properties->C/C++ General->Paths and Symbols->Includes->GNU C++ -> Add
** /usr/local/Cellar/opencv3/3.1.0_2/include/opencv
** /usr/local/Cellar/opencv3/3.1.0_2/include

*  Elipse->Projects(right click)->Properties->C/C++ General->Paths and Symbols->Libraries -> Add
** opencv_core (required)
** opencv_imgproc (required)
** opencv_highgui (required)
** opencv_imgcodecs (required)
** opencv_flann
** opencv_video
** opencv_features2d
** opencv_calib3d
** opencv_ml

 *  Elipse->Projects(right click)->Properties->C/C++ General->Paths and Symbols->Library Paths->Add
** /usr/local/Cellar/opencv3/3.1.0_2/lib

Saturday, June 27, 2015

如何在 Android 各 level ( 包含 user space 與 kernel space ) 使用dump call stack的方法

資料來源: 如何在 Android 各 level ( 包含 user space 與 kernel space ) 使用dump call stack的方法

 

dump call stack


[文章重點]

了解 Android 各 level ( UI, framework 與 HAL) 與 kernel 間, 如何印出 call stack, 方便追 code 與 debug

[文章目錄]
  1. kernel call stack
  2. Android Java layer
  3. Android framework ( written by c++)
  4. Android HAL ( written by c )
  5. Call Stack 沒有出現 function name

kernel call stack

如果想知道call stack,也就是說, 我們想知道是誰call到func_foo(). 此時,我們可以利用 dump_stack(),放在你想dump back trace的地方就OK囉.
 
void func_foo(void){
 
  int a=3;
  ...
  
  dump_stack();

  ...

}

Java layer call stack
在Java檔案, 可以使用下述方法得到dump call stack


public void foo(boolean state, int flags) {
 ...
 Log.d(TAG,"xxxx", new Throwable());
 ...
}


C++ layer call stack

在C/C++ 檔案, Android 已經有寫了frameworks/native/libs/utils/CallStack.cpp 供我們使用


#include <utils/CallStack.h>
...
void foo(void) {
...
   android::CallStack stack;
   stack.update();
   stack.dump("XXX");

...
}


如果你所使用是Android 4.4 之後
請改用


#include <utils/CallStack.h>
...
void foo(void) {
...
   android::CallStack stack;
   stack.update( );
   stack.log("XXX");

...
}

在Android.mk 記得要加


LOCAL_SHARED_LIBRARIES += libutils


C layer call stack

由於C去call C++需要做一些宣告, 所以將它獨立出來方便使用(dump_stack.cpp與 dump_stack.h)


dump_stack.h

#ifdef __cplusplus
extern "C" {
#endif

 void dump_stack_android(void);
 
#ifdef __cplusplus
}
#endif


dump_stack.cpp


#include "dump_stack.h"
#include <utils/CallStack.h>

using namespace android;
extern "C"{
 void dump_stack_android(void)
 {
CallStack stack;
stack.update();
stack.dump("XXX");
 }
}


如果你所使用是Android 4.4 之後
請改用


#include "dump_stack.h"
#include <utils/CallStack.h>

using namespace android;
extern "C"{
 void dump_stack_android(void)
 {
CallStack stack;
stack.update();
stack.log("XXX");
 }
}


同樣地, Android.mk也需要修改


LOCAL_SRC_FILES := \
        …... \
        dump_stack.cpp

LOCAL_SHARED_LIBRARIES += libutils


接下來在C file中要使用時只要


extern void dump_stack_android();


void function_a()
{
 …
 dump_stack_android();
 …
}


[ Call Stack 沒有出現 function name]
有時我們會發現在C++ 或 C 語言中使用 CallStack , 在 call dump 中並沒有出現 function name


D/XXX (  147): #00  pc 00001b90  /system/lib/hw/audio.primary.mrvl.so (dump_stack_android+19)
D/XXX (  147): #01  pc 00004b56  /system/lib/hw/audio.primary.mrvl.so
D/XXX (  147): #02  pc 0001f828  /system/lib/libaudioflinger.so
D/XXX (  147): #03  pc 00019138  /system/lib/libaudioflinger.so
D/XXX (  147): #04  pc 00023bb6  /system/lib/libaudioflinger.so
D/XXX (  147): #05  pc 0000e9fe  /system/lib/libutils.so (android::Thread::_threadLoop(void*)+213)
D/XXX (  147): #06  pc 0000e530  /system/lib/libutils.so
D/XXX (  147): #07  pc 0000d208  /system/lib/libc.so (__thread_entry+72)
D/XXX (  147): #08  pc 0000d3a4  /system/lib/libc.so (pthread_create+240)

我們追一下 CallStack 是如何被實作
先回顧一下 CallStack 是如何被使用 (以 Android 4.4 為例)
 CallStack stack;  
 stack.update();  
 stack.log();  

先看一下 update( ) function 的定義 ( it is under system/core/include/utils/CallStack.h)
   // Immediately collect the stack traces for the specified thread.  
   void update(int32_t ignoreDepth=1, int32_t maxDepth=MAX_DEPTH, pid_t tid=CURRENT_THREAD);  
所以透過 update( ) function, 我們可以設定想看哪一個 thread 並 dump 出多少層的 call stack, 如果都沒寫, 就是以當前的 thread 去做 call stack dump, update( ) function 會將實際可以 dump 多少的 frame 給抓出來, 其中 frame 的數量記錄在 mCount 變數, 各 frame 的資訊則記錄在 mStack[ ] 裡面, 接下來再透過 log( ) function 把 call stack 裡的 program counter 所記載的記憶體位址去把相對應的 function name 給解析出來.

 log( )  
 |--> print( )  
    |--> get_backtrace_symbols( )  

看一下 get_backtrace_symbols( ) 在做些什麼
void get_backtrace_symbols(const backtrace_frame_t* backtrace, size_t frames,
    backtrace_symbol_t* backtrace_symbols) {

   ... 
for (size_t i = 0; i < frames; i++) {
       ...
           Dl_info info;
           if (dladdr((const void*)frame->absolute_pc, &info) && info.dli_sname) {
            symbol->relative_symbol_addr = (uintptr_t)info.dli_saddr
                    - (uintptr_t)info.dli_fbase;
            symbol->symbol_name = strdup(info.dli_sname);
            symbol->demangled_name =
                       demangle_symbol_name(symbol->symbol_name);
           }
      ...
}
release_my_map_info_list(milist);
}


這是因為它是使用 dladdr() 去讀取該share lib的 dynamic symbol 而獲取 function name

但是如果該 function 是宣告成 static, 該 function name 就不會出現在 dynamic symbol 裡 (你可以使用 arm-linux-androideabi-nm -D xxxx.so | grep the_function_name , 如果沒有出現, 就表示該 funciton name 並不在 dynamic symbol 裡),  遇到這情況就只好使用 add2line 指令去讀 out folder 下的 symbol 了, 各位可以參考我另一篇文章 http://janbarry0914.blogspot.tw/2011/07/android-crash-tombstone.html . 感謝.