2011年9月6日 星期二

Linux script : 2>&1

2>&1 表示將stderr導向輸出stdout

0: stdin
1: stdout
2: stderr

exampe: # /tmp/test.sh > /tmp/test.log 2&1

2011年9月5日 星期一

Using printk debug

Using printk debug
There three method
1. KERN_EMERG to KERN_DEBUG
- pr_energe to pr_debug

2. Change the kernel command line
- loglevel = parameter
We can refer Kernel/doc/kernel-parameters.txt

3. Change the printk level after bootup
- /proc/sys/kernel/printk
ex: echo 8 > /proc/sys/kernel/printk
Then read /proc/kmsg

- /proc/sysrq-trigger

You can modify the #DEFAULT_CONSOLE_LOGLEVEL 8
at kernel/prink source code

There are prink tips andd tricks
1. CONFIG_PRINTK_TIME

2. CONFIG_EARLY_PRINTK
- CONFIG_DEBUG_LL and added some patch

3. CONFIG_LOG_BUF_SHIFT
- you can use the dmesg to change the printk buffer
"dmesg -s 128000" this mean we open 128k buffer


You can open the kernel configuration with #make menuconfig
kernel hacking --> <> show timming information as printks
<> debug filesystem
<> kernel low-level debugging function


There are two topic need to study
- debug system
debug system at /sys/kernel/debug
You can mount it by mount -t debugfs none /sys/kernel/debug
Or write it in /etc/fstab
debugfs /sys/kernel/debug debugfs 0 0

- dynamic debug
- CONFIG_DYNAMIC_DEBUG
- Operate on pr_debug or dev_dbg

linux script :Using while loop

http://www.cyberciti.biz/faq/bash-while-loop/

bash while loop syntax


while [ condition ]
do
command1
command2
command3
done
================================
Example:
#!/bin/bash
x=1
while [ $x -le 5 ]
do
echo "Welcome $x times"
x=$(( $x + 1 ))
done



=======================================

#!/bin/bash
while :
do
echo "infinite loops [ hit CTRL+C to stop]"
done
==========================================

Conditional while loop exit with break statement


while [ condition ]
do
statements1 #Executed as long as condition is true and/or, up to a disaster-condition if any.
statements2
if (disaster-condition)
then
break #Abandon the while lopp.
fi
statements3 #While good and, no disaster-condition.
done

2011年9月1日 星期四

linux driver sample : only init

Makefile:
===============================================================
KDIR :=/work/kernel/android-2.6.35
PWD := $(shell pwd)
ARCH=arm
CROSS_COMPILE=/work/tools/arm-2010q1/bin/arm-none-linux-gnueabi-
CC=$(CROSS_COMPILE)gcc
LD=$(CROSS_COMPILE)ld

obj-m := foo.o

all:
$(MAKE) -C $(KDIR) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) M=$(PWD) modules

clean:
rm -rf *.o *~ core.depend .*.cmd *.ko *.mod.c .tmp_versions
===============================================================


source code:
===============================================================

#include "linux/module.h"
#include "linux/kernel.h"
#include "linux/init.h"

static int __init foo_init(void)
{
printk("foo module init !!! \n");
return 0;
}
static void __exit foo_exit(void)
{
printk("foo module exit !!!! \n");
}

module_init(foo_init);
module_exit(foo_exit);

===============================================================

2011年8月31日 星期三

linux and android power management information

@ Normal Linux suspend
- Source code : kernel_src/kernel/main.c
kernel_src/kernel/arch/arm/mach-xxx/pm.c

- userspace interface: /sys/power/state
You can trace kernel_src/kernel/main.c
echo mem > /sys/power/state
echo standby > /sys/power/syste


- Process
1. suspend_prepare()
2. suspend_freeze_processes(): freeze all process.
3. suspend all devices : suppend_device_and_enter() at kernel/power/suspend.c suspend_device_and_enter() {
.......
dpm_suspend_start(PMSG_SUSPEND)
.......
}


dpm_suspend_start(PMSG_SUSPEND)
{
.......
dpm_suspend()
......
}

dpm_suspend()
{
......
;call all device callback function suspend()
get_device(dev);
device_suspend(dev);
......
}



4. Resume:
1. Get wake up event
2. Resume the devices /sys/devices/system
3. call suspend_ops -> finish() and let it start resume
4. call every devices resume in suspend_devices_and_enter()
5. call suspend_ops -> end()
6. call suspend_finish which will thaw the process and enter user mode helper




@ Android suspend : request_suspend_state() in kernel/power/earlysuspend.c
- Early suspend function
- wakelock function

You can study
- kernel_src/kernel/power/main.c
- kernel_src/kernel/power/earlysuspend.c
- kernel-src/kernel/power/wakelock.c



Definition:
http://kzjblog.appspot.com/2010/11/20/suspend-en.html

- Early Suspend
Early suspend is a mechanism that android introduced into linux kernel.
This state is btween really suspend, and trun off screen. After Screen is off,
several device such as LCD backlight, gsensor, touchscreen will stop for battery life and functional requirement.

- Late Resume
Late resume is a mechinism pairs to early suspend, executed after the kernel and system resume finished.
It will resume the devices suspended during early suspend.

- Wake Lock
Wake lock acts as a core member in android power management system.
wake lock is a lock can be hold by kernel space ,system servers and applications with or without timeout.
In an android patched linux kernel (referenced as android kernel below) will timing how many and how long the lock have.
If there isn't any of wake lock prevent suspend(WAKE_LOCK_SUSPEND),
android kernel will call linux suspend (pm_suspend()) to let entire system going to suspend.


Trace code :
Early suspend :
- state_store() /kernel/power/main()
you write "mem > /sys/power/state" and it will call this function
- state_store(){
request_suspend_state(); ---> call early_suspend_work -> early_suspend()
}

- early_suspend()
{
call early_suspend_handler () function in evenry deivces
}



Late Resume:
After all the kernel resume is finished,
the user space process and service is running, the wake up of system for these reasons:

* If In Calling, the modem will send command to rild (RING command),
and rild will send message to WindowManager and Application to deal with in call event,
PowerManagerSerivce also will write "on" to interface to let kernel execute late resume.

* User Key EventWhen system waked by a key event, such as a power key, or menu key,
these key event will send to WindowManager, and it will deal with it, if the key is not the key can wake up system,
such as return key/home key, the WindowManager will drop the wake lock to let system going to suspend again.
if the key is a wake key, the WindowManager will RPC PowerManagerSerivce interface to execute late resume.

* Late Resume will call the resume func in list of early suspend devices.






* Android power management :
reference:http://www.netmite.com/android/mydroid/development/pdk/docs/power_management.html

- Flow:
Application layer : application using wl.newWakeLock(), wl.acquire() and wl.release()

Application framwork: PowerManagement (Android.os.PowerManager)
Power (Android.os.Power)
PowerManagerService (Android.server.PowerManageService)

JNI layer : Power (lib/hardware/power.c)

Linux kernel : Android Power Management (driver/android/power.c)
Linux driver (android_register_early_suspend(), android_register_early_resume())
Linux power Management



- source :
frameworks/base/core/java/android/os/PowerManager.java
frameworks/base/services/java/com/android/server/PowerManagerService.java
frameworks/base/core/java/android/os/Power.java
frameworks/base/core/jni/android_os_power.cpp
hardware/libhardware/power/power.c

2011年8月18日 星期四

change bash when build android source code

build android error
--> dash can't run source
solution 1:

sudo dpkg-reconfigure dash ->No

default sh改成bash

solution2 :
Added "SHELL=/bin/bash" into Makefile

2011年8月7日 星期日

Using search and replace in vi

============================================================================
Using vi to search and replace
This is about as complicated as it gets in vi, since search and replace sytnax is taken from the UNIX sed (stream editor) command.

>> Global search and replace --> :1,$ s/old/new/g
^ ^ ^ ^ ^
In english, this means: | | | | |
| | | | |
From 1 to $ (end of file) | | | |
| | | |
substitute -----------------/ | | |
| | |
occurrences of "old" ----------/ | |
| |
with occurrences of "new" --------/ |
|
globally (i.e., all instances of "old")


========================================================================
Using sed command to replace string in file
#sed -i 's/old/new/g' filename.txt