GNU_make

"gcc -M sepo.c" will generate depends.

debug makefile:

remake -x
remake -X

variable in make:

a = d #comment

now variable a is “d “

Where do variables come from:

1.file: via "include" command
2.command line: user decide [makefile/command_line] when conflicts
3.env: user decide [env/makefile] when conflicts
(1)When make is invoked recursively, some variables from the parent make are passed through the environment to the child make. By default, only those variables that originally came from the environment are exported to the child's environment, but any variable can be exported to the environment by using the export directive.
(2)The export and unexport directives work the same way their counterparts in sh work.
    export -- export
    unexport -- unset
4.auto

OUTPUT_DIR ?= out #OUTPUT_DIR default to out if undefined

======================
target_a : | build_log
REQUIRED_DIRS += build_log
$(REQUIRED_DIRS):
        mkdir $@
======================
build_log should exist

.DEFAULT_GOAL := apply_product_patch

Multicast_with_VLC

VLC Streaming

Multicast Address Range:
224.0.0.1-239.255.255.255

practice 1

========== RTP/MPEG Transport Stream==========
Multicast Streaming
1.File->Stream
* Source:Choose src
* Destination Setup:
Add "RTP/MPEG Transport Stream", check "Display locally"
Address: 239.1.1.1, Port: 5004
* Option Setup:
TTL: 10

---------- Client ----------
Media->Open Network Stream->rtp://239.1.1.1:5004

practice 2

========== RTP/MPEG Transport Stream==========
VOD
1.File->Stream
* Source:Choose src
* Destination Setup:
Add "HTTP", check "Display locally"
Port: 8080 
File Path: /1.ts
* Option Setup:
TTL: 10

---------- Client ----------
Media->Open Network Stream->http://10.37.135.142:8080/1.ts

practice 3

=================================
            RTP
=================================
Destinations:
RTP/MPEG Transpot Stream <--add
Display locally <--uncheck

Transcodeing options:
Active Transcoding <--check
Video - H.264+MP3(MP4)

Miscellaneous Options:
uncheck all



:sout=#transcode{vcodec=h264,vb=0,scale=0,acodec=mpga,ab=128,channels=2,samplerate=44100}:duplicate{dst=std{access=udp{ttl=2},mux=ts,dst=239.239.239.2:1234},dst=display} :sout-keep


vlc --sout "#duplicate{dst=rtp{dst=224.5.6.7,port=5004,mux=ts,ttl=1},dst=display}" --sout-keep

vlc --sout "#rtp{dst=224.5.6.7,port=5004,mux=ts,ttl=1}" --sout-keep

vlc --sout "#transcode{vcodec=h264,vb=0,scale=0,acodec=mpga,ab=128,channels=2,samplerate=44100}:duplicate{dst=rtp{dst=224.5.6.7,port=5004,mux=ts,ttl=1},dst=display}" --sout-keep --keep 

vlc --sout "#transcode{vcodec=h264,vb=0,scale=0,acodec=mpga,ab=128,channels=2,samplerate=44100}:duplicate{dst=rtp{dst=224.5.6.7,port=5004,mux=ts,ttl=3},dst=display}" --sout-all --sout-keep --loop 

vlc $1 --sout "#transcode{vcodec=h264,vb=800,scale=1,acodec=mpga,ab=128,channels=2,samplerate=44100}" --sout-all --sout-keep --loop

debugging (still work in progress)

android Boot Time Analysis & logging & debugging

Enable bootchart

http://elinux.org/Using_Bootchart_on_Android

  1. 准备
    Host
    apt-get install bootchart pybootchartgui
    Target
    设置环境变量INIT_BOOTCHART=true, 重编init

  2. 打开
    adb shell ‘echo 120 > /data/bootchart-start’
    或者使用kernel commandline:
    androidboot.bootchart=120

  3. 抓取日志
    system/core/init/grab-bootchart.sh

  4. 生成png
    bootchart bootchart.tgz

Lollipop上的hack:
如果bootchart_init时/data分区未mount,需要hack代码:把/data/bootchart改为/dev/bootchart (同时修改bootchart.c和grab-bootchart.sh)
此时只能用kenel commandline传递timeout.

logging

kernel logging

kernel has a ring buffer which can be dumped via command dmesg. Default loglevel in init.rc is 3, you can change it to 8(higest).
loglevel 8

android logging

The Android application framework has a built-in logging system – logcat, which goes through a special driver in the kernel.

tracing

strace : trace system calls and signals

android has strace in external/strace, strace can be enabled in init.rc or hot attach.

init.rc
service xxx /system/xbin/strace -tt -o/data/boot.strace /system/bin/vold

hot attach
strace -tt -p <PID>

ltrace : A library call tracer

debian has this package

debugging

advanced method

start gdbserver on board: find PID of vold, then
    gdbserver :1234 --attach 1264
map port on PC: map 1234 on PC to 123 on board
    adb forward tcp:1234 tcp:1234
start arm-eabi-gdb
    arm-eabi-gdb
    (gdb) target remote :1234
    (gdb) file out/target/product/$TARGET_PRODUCT/symbols/system/bin/vold
    (gdb) set solib-search-path out/target/product/$TARGET_PRODUCT/symbols/system/lib
    (gdb) bt
    (gdb) info threads
    (gdb) thread 3
    (gdb) p <var>

quick method

gdbclient <PID>

debuggerd

Android crash logcat can be fed to development/scripts/stack to get more detailed info.

Run program inside:
gdbserver
gdbserver :5039 /system/bin/my_test_app
valgrind

attach crash programs:
$ adb shell setprop debug.db.uid 999999 # <= M
$ adb shell setprop debug.debuggerd.wait_for_gdb true # > M

AddressSanitizer (ASan)
dumpsys: provides information about the status of system services.
DDMS - native memory tracking: libc.debug.malloc

procstatsanalyze application memory usage over time
meminfo: analyze mem usage in time
$ adb shell dumpsys procstats –hours 3
network use

lsof usage

1. lsof intro

1.1 lsof can check opened files:

  • /dev file
  • socket files(unix/network)
  • lib files
  • executables
  • .etc

lsof page on debian: https://packages.debian.org/stable/lsof

a cool webpage on lsof from daniel

1.2 output interpretation

**COMMAND   PID USER   FD      TYPE             DEVICE SIZE/OFF     NODE NAME**
adb     15516   yu  cwd       DIR                8,1     4096 12845057 /home/yu/workspace/q
adb     15516   yu  rtd       DIR                8,1     4096        2 /
adb     15516   yu  txt       REG                8,1  4894227 11272415 /home/yu/.bin/adb
adb     15516   yu  mem       REG                8,1    71488 14420702 /lib/i386-linux-gnu/i686/cmov/libresolv-2.13.so
adb     15516   yu  mem       REG                8,1    22088 14420706 /lib/i386-linux-gnu/i686/cmov/libnss\_dns-2.13.so
adb     15516   yu  mem       REG                8,1    42628 14420694 /lib/i386-linux-gnu/i686/cmov/libnss\_files-2.13.so
adb     15516   yu  mem       REG                8,1  1437864 14420700 /lib/i386-linux-gnu/i686/cmov/libc-2.13.so
adb     15516   yu  mem       REG                8,1   114788 14420559 /lib/i386-linux-gnu/libgcc\_s.so.1

row of “FD”

+----+-------+-----------------------------------+
| No | item  |              note                 |
+----+-------+-----------------------------------+
| 1  | cwd   |  current workding directory       |
+----+-------+-----------------------------------+
| 2  | rtd   |  root directory                   |
+----+-------+-----------------------------------+
| 3  | txt   |  program text (code and data)     |
+----+-------+-----------------------------------+
| 4  | mem   |  memory-mapped file               |
+----+-------+-----------------------------------+

row of “TYPE”

+----+-------+-----------------------------------+
| No | item  |              note                 |
+----+-------+-----------------------------------+
| 1  | DIR   |  Directory                        |
+----+-------+-----------------------------------+
| 2  | REG   |  Regular file                     |
+----+-------+-----------------------------------+
| 3  | CHR   |  Character special file           |
+----+-------+-----------------------------------+
| 4  | FIFO  |  First In First Out               |
+----+-------+-----------------------------------+

1.3 practice

Given PID:

$ lsof -p <pid>
$ ls /proc/<pid>/fd

Given port/protocol

$ lsof -i :5037

COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
adbd    939 root    5u  IPv4     93      0t0  TCP localhost:5037 (LISTEN)

$ lsof -i @localhost

COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
adbd    939 root    5u  IPv4     93      0t0  TCP localhost:5037 (LISTEN)

$ lsof -i TCP
$ lsof -i TCP:80
$ lsof -i TCP@192.168.0.2:636
$ lsof -i --> show all network connections
$ lsof -i 4 --> show only IPv4 connections
$ lsof -i 6 --> show only IPv6 connections
$ lsof -i TCP:1-1024 --> List Open Files of TCP Port ranges 1-1024

Given UID

$ lsof -u <UID>
$ lsof -u ^<UID>

Given category

$ lsof -d txt
$ lsof -d ^mem

Given files/directries

$ lsof <file>
$ lsof <directory>

only show PID

$ lsof -t <file>

show network port

$ lsof -i -P

2. checking opening fd limits

use ulimit -n to check limits, use ls /proc/*/fd/ to count all fd

3. check server risk

lsof +L1 shows you all open files that have a link count less than 1
This is often (but not always) indicative of an attacker trying to hide file content by unlinking it.

$ lsof +L1

memory usage monitor

memdump: This  program  dumps system memory to the standard output stream, skipping over holes in memory maps.  By default, the program dumps the contents of physical memory (/dev/mem).
memstat: Identify what's using up virtual memory.

ps aux --sort:-rss

dmverity

dm-verity on android

1. Keys needed for dm-verity

android default keys are located at build/target/product/security. They have 4 paris of keys
{platform/testkey/media/shared}.x509.pem and {platform/testkey/media/shared}.pk8.

What are they:

key name type
xx.x509.pem x509 PEM Certificate
xx.pk8 PKCS#8 PrivateKeyInfo

dm-verity will use build/target/product/security/verity.pk8 by default

2. How to generate root/system image

system.img is combined of 3 part:

raw ext4 system.img
    standard ext4 image with all system files
verity.img : 
    sha256 hashtree, 
    generated from google's build_verity_tree
    source: system/extras/verity/build_verity_tree.cpp
verity_metadata.img : 
    dm-verity metadata, 
    generated from google's build_verity_metadata.py
    source: system/extras/verity/build_verity_metadata.py

misc

prop:

  • partition.system.verified == 1
  • partition.vendor.verified == 1

when system or vendor partition is dm-verity enabled, user can use “adb disable-verity” to disable verity.

Optimizing dm-verity performance:

https://source.android.com/devices/tech/security/verifiedboot/dm-verity.html

To get the best performance out of dm-verity, you should:

In the kernel, turn on NEON SHA-2 for ARMv7 and the SHA-2 extensions for ARMv8.
Experiment with different read-ahead and prefetch_cluster settings to find the best configuration for your device.

Hello Markdown

Basic Demo of Markdown

1 italic and bold

italic

Just add surround words with underscore(_) make it italic,

bold

Just add surround words with 2 underscore(__) or 2 asterisks(**) make it bold.

2 url

You’re really, really going to want to see this.

img url

Here’s a link to something else.
Here’s yet another link.
And now back to the first link.

blockquote

examples on LOGD/LOGV in C style

#define LOGW(...) fprintf(stdout, "W:" __VA_ARGS__)

#define LOGI(...) fprintf(stdout, "I:" __VA_ARGS__)

#define LOGV(...) do {} while (0)

#define LOGD(...) do {} while (0)

A beginner’s guide:
<a href=http://www.ruanyifeng.com/blog/2012/08/blogging_with_jekyll.html>http://www.ruanyifeng.com/blog/2012/08/blogging_with_jekyll.html

github flavored mkd table:

Left-Aligned Center Aligned Right Aligned
col 3 is some wordy text $1600
col 2 is centered $12
zebra stripes are neat $1




















a b c
d e f
g h i