Introduction ot Android system log system. The content is based on Android Honeycomb 3.2.
Size: 808.72 KB
Language: en
Added: Aug 24, 2013
Slides: 29 pages
Slide Content
AnroidLogging System
William.L
Date: 2011-07-21
Outline
O
Overview of Android LogginSystem
O
Log from Java program
O
Log from Native C/C++ program
O
How to read log?
O
Tips
Overview of Android
Logging System
What is Android Logging System?
O
Provide a mechanism for collecting and viewing
system debug output
O
Logs from various applications and portions of
the system are collected in a series of circular
buffers, which then can be viewed and filtered by
the logcatcommand
Overview of Logging System
[Android Device]
[HOST/PC
]
U
S
B
c
ab
le
Introduction
O
The logging system consists of u
A kernel driverand kernel buffersfor storing log messages O
HoneycombMR2Src
/kernel/drivers/staging/android/ logger.c
O
Create “/dev/log” folder in handle_device_event()of
AndroidSrc/system/core/init/devices.c
u
C/C++/Java APIs and classes O
For making log messages
O
For accessing the log messages
u
logcat, the command for viewing log messages O
AndroidSrc/system/core/logcat/
u
Ability to view and filter the log messages from th e host
machine (via Eclipse-ADTor DDMS)
Log device files
O
4 channels, each have a
Ring/Circular Buffer
u
/dev/log/radio– radio&phone-related messages (64KB)
u
/dev/log/events– system/hardware events (256KB)
u
/dev/log/system–framwork or low-level system messages
(64KB)
u
/dev/log/main– everything else (64KB)
u
The maximum log message size of each channel is specified in
kernel driver(logger.c)
O
File permission of each(radio/events/system/main) i s 0662 (rw-rw-w) u
owner/group RW,
other Write only
u
owner=root, group=log
u
Anyone can Write logs, root or log group can Read t hem
A
ndroid
D
ebug
B
ridge
A
ADB client D
Runs on your development machine(Host).
D
You can invoke a client from a shell by issuing an “adb”
command. D
Other Android tools such as the ADTplugin and DDMSalso
create adb clients.
A
ADB server (adbserver) D
Runs on your development machine(Host).
D
The server manages communication between the client and the
adb daemon running on an emulator or device.
A
ADB daemon (adbd) D
Runs on each Android emulator or Android device instance.
Log from Java program
Classes used for Logging
O
android.util.Logclass
O
System.out/ System.err
android.util.Log
O
Static methods u
AndroidSrc/frameworks/base/core/java/android/util/L og.
java
E
rror message Log.e(String tag, String msg)
W
arrning message Log.w(String tag, String msg)
I
nfo message Log.i(String tag, String msg)
D
ebugging message Log.d(String tag, String msg)
V
erbose message Log.v(String tag, String msg)
Example :
/* In Javacode, add the following codes */
import android.util.Log; class CCLLAASS {
static String TAG=“tagName”; public MethodXX() {
Log.v(TAG, “Debugging messages you want”);
}
}
System.out/ System.err
(1/2)
O
System.out/System.erroutput to Android log u
zygoteInit
() {
System.setOut(AndroidPrintStream);
System.setErr(AndroidPrintStream); }
O
AndroidSrc/frameworks/base/core/java/com/android/internal/o
s/
RuntimeInit.java
u
com.android.internal.os.
AndroidPrintStream
(which derives
from LoggingPrintStreamwhich derives from
PrintStream)
O
AndroidSrc/frameworks/base/core/java/com/android/internal/o
s/
AndroidPrintStream.java
System.out/ System.err
(2/2)
O
How to identify instance of System.out/System.err? u
System.out.println(”System.out=”+System.out.toStrin g())
u
System.err.println(”System.err=”+System.err.toStrin g()) Example:
/*
Add the System.out and System.err statements in the constructor of MountService.java
*/
class MountService {
MountService() {
….
System.out.println(”System.out’s instance is ”+System.out.toString());
System.err.println(”System.err’s instance is ”+System.err.toString());
….
}
}
Log from Native C/C++
program
Library for Logging
O
Use
liblog
library
O
Include
<android/log.h>
header
O
<cutils/log.h>(libcutils) header could be used u
This header includes <android/log.h> eventually
O
__android_log_printmacro(definedin liblog) is the
actual worker behind LOGI[V/D/W/E] functions Example:
#define LOGI(...) \ __android_log_print
(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
Usage :
LOGI(”i=%d, name=%s\n”, i, name);
Log From Native Program
(1/2)
O
Log functions
E
rror message LOGE (String msg)
W
arrning message LOGW (String msg)
I
nfo message LOGI (String msg)
D
ebugging message LOGD (String msg)
V
erbose message. LOGV (String msg)
Example: /* In C/C++ code, add the following codes*/ #define LOG_TAG
“
tagName
”
#include <cutils/log.h>
LOGV(“Debugging messages you want”);
Log From Native Program
(2/2)
O
It must add following definition
BEFORE
the header
"
#include <cutils/log.h>
"
u
#undef NDEBUG
: Enable LOGV/LOGI/LOGD
u
#define LOG_NDEBUG 0
: Enable LOGV
u
#define LOG_NIDEBUG 0
: Enable LOGI
u
#define LOG_NDDEBUG 0
: Enable LOGD
u
#define LOG_TAG “String-You-Want"
O
Because all the above are defined in <cutils/log.h>,
if the defineis put after the header including
statment, it will show “
redefined
”compile warning
and definewill
not take effect
How to read log?
LogcatCommand
O
“
logcat
”command runs on Android device
O
Use the command to run ‘logcat’command on
the remoteAndroid device : “
adbshell logcat
”
ADB Logcat
O
Command :
adblogcat
Logcatpane in ADT, Eclipse
Tips
O
Dumping stack trace
O
logwrapper
O
Log at ‘init’process
O
Support Non built-in ADB USB VID
Dumping stack trace
(1/2)
O
3 arguments methods in android.util.Logclass u
Ex: Log.e(String tag, String msg,
new Throwable()
)
O
Throwable.printStacktrace
() also works
u
Dump to System.err
Dumping stack trace
(2/2)
Examplefor Throwable.printStackTrace(MountService.java) :
class MountService extends IMountService.Stub implements INativeDaemonConnectorCallbacks
{
public static void NewException() throws Throwable
{
throw new Throwable("New Exception...");
}
public MountSerivce(Context context) {
…
try {
NewException();
} catch (Throwablee) {
// Prints this throwable and its backtrace to the
// standard error stream.
e.printStackTrace();
}
...
}
…
}
logwrapper
O
Redirects stdout(likeprintf)/stderrto Android
Logging system O
Usage u
“logwrapper Executable”
, and use “logcat” to watch logs as
usual
u
Ex : “
logwrapper ObbFile_test”
Executing without ‘logwrapper’ Executing with ‘logwrapper’
Log at init process
O
The first process, 'init‘, does not use Android
Logging System.
O
‘init’writes log to (the same node as) '/dev/kmsg' u
The same way as 'printk()'
O
Add a command in
init.rc
to write Android logs to
kernel logging file, /dev/kmsg
u
Command :
u
Watch logs : run “adb shell dmesg” on the host
u
Shortpoint : duplicated store of Android log
O
To save output messages of logcat u
logcat -f fileName
service logcat /system/bin/logcat -f /dev/kmsg
oneshot
Support Non built-in ADB USB VID
(1/2)
O
ADB built-in USB VID u
http://developer.android.com/guide/developing/devic e.html #VendorIds
O
Solution-1 : Append the new USB VID into the adb_usb.ini
file
u
Commands (executing on the host, e.g.PC/NB) : O
Create the folder/file ‘ ~/.android/adb_usb.ini’if it does not
exist
u
‘adb’ command
reads and checks content of this file
each timeit
is executed
O
echo "New-Vendor-ID" >> ~/.android/adb_usb.ini
O
sudo -s "adb kill-server;adb start-server“
Example(LenovoVID : 0x17EF) : /* It can watch the VID of an Android device
using ‘
lsusb
’ command under the host */
#> echo "0x17EF" >> ~/.android/adb_usb.ini
#> sudo -s "adb kill-server;adb start-server
"
Support Non built-in ADB USB VID
(2/2)
O
Solution-2 : Build a new ‘adb’tool supporting new
VID
u
In
AndroidSrc/system/core/adb/usb_vendors.c
u
#define
VENDOR-NAME
Vendor-ID
u
Add an entry with new
VENDOR-NAME
in variable
builtInVendorIds[]
and then compile ‘adb’sources
u
Built new ‘adb’exectuable is under the folder : out/host/linux-x86/bin/ Ex - InAndroidSrc/system/core/adb/usb_vendors.c: // Lenovo's USB Vendor ID #define VENDOR_ID_LENOVO
0x17EF
/** built-in vendor list */ int builtInVendorIds[] = {
....... ,
VENDOR_ID_LENOVO
};
Reference
O
http://elinux.org/Android_Logging_System
O
Android Logging system slide - http://blog.kmckk.com/archives/2936958.html
O
logwrapper- http://blog.kmckk.com/archives/2918551.html
O
Print Call Stack - http://blog.kmckk.com/archives/2902690.html