While its true that fragmentation among Bluetooth implementations in general are hurting wider adapation of latest bluetooth profiles or features in Android, there is little Google can do to stem the tide. As we all know, Android uses Bluez stack as the native stack and provides implementations and adaptations on top of it, quite a lot depends on how the lower layers in bluez support such features in form and type. For ex. Bluetooth Low Energy is totally supported by Bluez(I mean in the Bluez 4.93 version that ICeCream sandwitch uses) while the relevant Java API’s are still pending in Android 4.0…

If we analyse a bit deeper on the level of support offered in Bluez for Bluetooth Low Energy, quite a few conclusions may emerge. FindMe and Proximity profiles are supported in Bluez(Thermometer profile API’s appear in Bluez 4.99 too) with even API’s at DBus layer to configure these. But essentially both these profiles and all upcoming ones shall be supported below DBUS layer itself giving very little freedom to application developers above Dbus viz Android framework, to play around with it.  This leaves very little scope of proprietary profile implementations as well.

Secondly ATT is totally eclipsed at DBus layer in current Bluez implementation. So if one intends to write proximity profile in Android Java framework, it would be quite difficult with minimal GATT api’s currently exposed from Bluez over Dbus and none from ATT.

Managing connection parameters could be another issue at Dbus layer as these API’s dont exist. same is the case with Connection data for LE.

@Carles[1] : The security part of Bluez SMP has been evolving for last year and a half  and MITM patches to SMP are already under phased review from November last[2]. Unlike other close communities viz. iOS or Windows etc.. where adding such a support is just a matter of few weeks coding effort, Open source discussions and development cycle is generally slower with multiple iterations of patches, reviews etc.

If you may recall, that Bluez was probably the first stack to have basic Low Energy support since a year or so starting with Just Works implementation in SMP. There has been a total overhaul in security architecture of bluez ever since due to Bluetooth Low energy wherein the existing hciops interface is being discarded solely due to its handicap in handling SMP keys and their distribution.(note that there are no hci events passed on to the host once the SMP keys arrive at controller since these are over a dedicated L2CAP channel). Hence mgmt ops interface is introduced and naturally things may be bit slower in re-implementing the entire security apparatus of even BR/EDR. Not to mention the additional

IO capability introduced in LE being KeyboardDisplay which warrants at least one side to input 6 digits numeric passkey for MITM. bluez being a dual mode stack needs to accommodate existing BR/EDR IO capabilities(4 in number) which makes mainlining of these even slower.

The point is given the holistic nature of  Open source development which tends to make it bit slow, Bluez still has an advanced implementation of Bluetooth Low Energy stack which can be leveraged by stacks above it like MEego or Android.  For Android, yes the implementation of Java API’s should be straightforward and I believe Google guys themselves are aware of this. The bigger question for them may be whether to re-use the existing bluez profiles or let them be developed in android space which may be more complex than just porting Java API’s across JNI..

Refrences

[1] http://www.linkedin.com/groupItem?view=&gid=3196502&type=member&item=99282684&qid=7c0dec06-2ef0-4e42-9d6b-fb5a43dc1aa3&trk=group_most_popular-0-b-cmr&goback=%2Egmp_3196502

[2]http://marc.info/?l=linux-bluetooth&m=132105750728946&w=2

March 3rd, 2012 | Categories: Android, Bluez, Linux | Tags:

——this post is still under construction :-) —————————–

Android supports insecure RFCOMM sockets from AOSP 2.1 onwards(if i remember correctly). What it means that you now have an option to bypass security requirements mandated by Bluetooth agent registered to Bluez on startup which registers the IO capabilties. So even though the local adapter may be keyboardDisplay enabled, it still is possible for an android application to open an insecure RFCOMM socket(one where authentication is disabled). and this is possible because the RFCOMM socket abstraction bypasses the JNI and other layer that rely on local adapter capabilties and security requirements registered for local adapter. This thus allows an Android device to connect to both a pre 2.1 specs device and one compatible with its later releases by giving an option to the user to open an insecure RFCOMM socket.

 

January 5th, 2011 | Categories: GNU, Linux | Tags:

In this post, I will describe some useful tips on how to debug under Linux. Linux offers advanced tools  suitable for profiling, debugging and crash analysis. Below, I shall describe few of the most common and useful techniques for debugging. Namely dynamic debugging, Oops analysis, strace etc..
Debugging via printing: printk

The printk function, similar to printf for user space programs, writes messages into a circular buffer that is LOG_BUF_LEN (defined in kernel/printk.c) bytes long. It then wakes any process that is waiting for messages, that is, any process that is sleeping in the syslog system call or that is reading /proc/kmsg. These two interfaces{syslog and kmsg} to the logging engine are almost equivalent.

One of the unique features that printk offers is that it lets you classify messages according to their severity by associating different loglevels, or priorities, with the messages. Usually such log-level associate with a macro and an integer value. For example, KERN_INFO, is one of the possible log-levels of the message has an integer value 6. The log-level macro expands to a string, which is concatenated to the message text at compile time; that’s why there is no comma between the priority and the format string in the following examples. Here are two examples of printk commands, a debug message and a critical message:

printk(KERN_DEBUG “Here I am: %s:%i\n”, __FILE__, __LINE_&_);

printk(KERN_CRIT “I’m trashed; giving up on %p\n”, ptr);

 

There are eight possible log-level strings, defined in the header <linux/kernel.h>. Integers range from 0 to 7, with smaller values representing higher priorities.

A printk statement with no specified priority defaults to DEFAULT_MESSAGE_LOGLEVEL, specified in kernel/printk.c as an integer. The default loglevel value has changed several times during Linux development and is currently 4. Based on the loglevel, the kernel prints the message to current console, be it a text-mode terminal, a serial line printer, or a parallel printer. If the priority is less than the integer variable console_loglevel, the message is displayed. If both klogd and syslogd are running on the system, kernel messages are appended to /var/log/messages , independent of console_loglevel. If klogd is not running, the message won’t reach user space unless you read /proc/kmsg. Messages in /var/log/messages can be read via dmesg utility.

 

The variable console_loglevel is initialized to DEFAULT_CONSOLE_LOGLEVEL and can be modified. From version 2.1.31 on it is possible to read and modify the console log-level using the text file /proc/sys/kernel/printk. The file hosts four integer values, the current console log-level and the default level for messages. If we do a look at the current console loglevel in 2.6.36 kernel, it defaults to 4. Meaning all messages from KERN_ERR[3] to KERN_EMERG[0] would get printed.

root@arunks-PC1# less /proc/sys/kernel/printk

4     4     1     7

Setting 8 as the current console value ensures, all info and debug messages appear on console. Thus by doing as below:

root@arunks-PC1# echo 8 > /proc/sys/kernel/printk

root@arunks-PC1# less /proc/sys/kernel/printk

8     4     1     7

ensures that all messages with priority lower than 8 get printed to console.

A massive use of printk can slow down the system noticeably, because syslogd

keeps syncing its output files; thus, every line that is printed causes a disk operation.

This is the right implementation from syslogd ’s perspective. It tries to write everything to disk in case the system crashes right after printing the message. To overcome this bottleneck, kernel offers the technique of Dynamic Debugging as described below.

 

Dynamic Debugging

Even though, debugging via printk is the most common form of debugging kernel code, it comes with its own inefficiencies and complications. Though printk offers a verbose mode for developers to classify the prints as per priority and also to define whether they should hit console or syslogd, yet processing of printk’s involve extensive rendering by kernel slowing down the performance.

Not only performance, there are other issues as well with use is printk in kernel architecture. Existing kernel debug architecture involves use of pr_debug and various other macros. 2.6.29 kernel had 3058 pr_debug() calls, 3158 dev_dbg() calls, 5618 dprintk() calls, 206 DEBUGP() calls, and countless additional debugging statements. Some subsystems have developed sophisticated debug statement frameworks, allowing fine grain control via bit flags and debug levels. As may be noticed, the kernel is sprinkled with debug statements that are only available by individually re-compiling the various subsystems of the kernel. So, the big question is how do we enable debugs in any kernel module without editing and recompiling the kernel?Before 2.6.28, the only way seemed to be to edit and recompile the specific kernel module after enabling the compile time debug flags either in the Makefile or in the headers etc. From kernel 2.8.28 onwards, the concept of Dynamic debug has been introduced. So now we can enable/disable any kernel debug statements without recompiling and that too on the fly. Even subsystems that have implemented complex and more granular logs could be also be controlled via dynamic debug.So does Dynamic debugging help in any other way apart from enabling/disabling debugs on the runtime? Alternatively it seems to be handy too with different version of debug macros and facilities. And the developer is offered great flexibility in terms. Consider this: printk messages have different levels of verbosity attached. with KERN_EMERG being most severe to KERN_DEBUG with least severe. Such severity control where eventually these printks should land up or may be not land at all. But even if a printk/debug messages doesn’t end up either in syslog or on consoled, the kernel needs to render the same. Stats point out that there is more than 86% degradation of performance when enabling all debug statements but not logging these messages to system logs or console. Amazing but in the current kernel debug infrastructure, enabling logs via makefiles etc do incur heavy penalties in terms of kernel size and performance.Dynamic debug addresses these two concerns. i.e ease of enabling/verbosity and performance. The verbosity part is tackled using a unique language which allows fine grain control of debug messages as well as at a coarse level. The performance part is taken care by introduction of a new technique called Bloom filters. Essentially all info regarding debug statements is stored as meta data which then is organized as multiple and efficient hashes to speed up performance and speed. On the flip-side enabling dynamic debug option in kernel results in a penalty of increasing  kernel text size by 2%.

 Usage and Examples

Dynamic debugging could be enabled under Kernel Hacking -> Enable Dynamic printk support[CONFIG_DYNAMIC_DEBUG]. It provides a debugfs control file: <debugfs>/dynamic_debug/control which can be read to display the complete list of known debug statements, to help guide the programmer.The behaviour of pr_debug() and dev_debug() are controlled by writing to a control file in the debugfs filesystem. Here is how a typical control file looks like

Subsequently, we refer to the control file as: <debugfs> /dynamic_debug/control. In my Ubuntu 10.10 setup, the debugfs is mounted in </sys/kernel/debug>.

Now if we want to enable BT_DBG on line 91 in source file l2cap.c, the command would be like,

root@arunks-PC1: echo ’file l2cap.c line 91 +p’ > /sys/kernel/debug/dynamic_debug/control

root@arunks-PC1:echo -n ‘func svc_process +p’ > /sys/kernel/debug/dynamic_debug/control

Debugging via straceUser space programs can be watched under strace. Strace command is a powerful tool that shows all the system calls issued by a user space program. Not only does it shows the calls, but it can also show arguments to the calls, as well as return values in symbolic form. When a system call fails, both the symbolic value of the error and the corresponding string are displayed. Strace has many command line options too : -T being most notable displaying time spent in each call. By default, strace prints tracing information on stderr.What is more wonderful of strace is that it can trace a program regardless of whether the program was compiled with debugging support[-g option of gcc] or whether such information is stripped or not. Strace could also be attached to a running process pretty much like a debugger. In summary strace program intercepts and records the system calls which are called by a process and the signals which are received by a process. The name of each system call, its arguments and its return value are printed on standard error or to the file specified with the -o option.Consider below dump from bluetoothd.

root@arunks-PC1:/home/arunks/bluez-4.79/src# strace bluetoothdexecve(“/usr/local/sbin/bluetoothd”, ["bluetoothd"], [/* 25 vars */]) = 0brk(0)                                  = 0xb9424000access(“/etc/ld.so.nohwcap”, F_OK)      = -1 ENOENT (No such file or directory)

mmap2(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7832000

access(“/etc/ld.so.preload”, R_OK)      = -1 ENOENT (No such file or directory)

open(“/etc/ld.so.cache”, O_RDONLY)      = 3

fstat64(3, {st_mode=S_IFREG|0644, st_size=60803, …}) = 0

mmap2(NULL, 60803, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7823000

close(3)                                = 0

access(“/etc/ld.so.nohwcap”, F_OK)      = -1 ENOENT (No such file or directory)

open(“/usr/local/lib/libbluetooth.so.3″, O_RDONLY) = 3

read(3, “\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\300C\0\0004\0\0\0″…, 512) = 512

fstat64(3, {st_mode=S_IFREG|0755, st_size=100888, …}) = 0

mmap2(NULL, 90264, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb780c000

mmap2(0xb7820000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0×13) = 0xb7820000

close(3)                                = 0

mmap2(NULL, 12408, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb76e2000

mmap2(0xb76e4000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0×1) = 0xb76e4000

close(3)                                = 0

access(“/etc/ld.so.nohwcap”, F_OK)      = -1 ENOENT (No such file or directory)

open(“/lib/librt.so.1″, O_RDONLY)       = 3

read(3, “\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\300\30\0\0004\0\0\0″…, 512) = 512

fstat64(3, {st_mode=S_IFREG|0644, st_size=30684, …}) = 0

mmap2(NULL, 33364, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb76d9000

mmap2(0xb76e0000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0×6) = 0xb76e0000

close(3)                                = 0

access(“/etc/ld.so.nohwcap”, F_OK)      = -1 ENOENT (No such file or directory)

open(“/lib/libc.so.6″, O_RDONLY)        = 3

read(3, “\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0@n\1\0004\0\0\0″…, 512) = 512

fstat64(3, {st_mode=S_IFREG|0755, st_size=1421892, …}) = 0

mmap2(NULL, 1431976, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb757b000

mprotect(0xb76d2000, 4096, PROT_NONE)   = 0

mmap2(0xb76d3000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0×157) = 0xb76d3000

mmap2(0xb76d6000, 10664, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb76d6000

close(3)                                = 0

access(“/etc/ld.so.nohwcap”, F_OK)      = -1 ENOENT (No such file or directory)

open(“/lib/libpcre.so.3″, O_RDONLY)     = 3

read(3, “\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\300\20\0\0004\0\0\0″…, 512) = 512

fstat64(3, {st_mode=S_IFREG|0644, st_size=210244, …}) = 0

mmap2(NULL, 213080, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7546000

mmap2(0xb7579000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0×32) = 0xb7579000

close(3)                                = 0

mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7545000

mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7544000

set_thread_area({entry_number:-1 -> 6, base_addr:0xb75446c0, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0

mprotect(0xb7579000, 4096, PROT_READ)   = 0

mprotect(0xb76d3000, 8192, PROT_READ)   = 0

set_robust_list(0xb7544730, 0xc)        = 0

futex(0xbfd7a8b0, FUTEX_WAKE_PRIVATE, 1) = 0

futex(0xbfd7a8b0, FUTEX_WAIT_BITSET_PRIVATE|FUTEX_CLOCK_REALTIME, 1, NULL, bfd7a8c0) = -1 EAGAIN (Resource temporarily unavailable)

rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0

getrlimit(RLIMIT_STACK, {rlim_cur=8192*1024, rlim_max=RLIM_INFINITY}) = 0

uname({sys=”Linux”, node=”arunks-PC1″, …}) = 0

brk(0)                                  = 0xb9424000

brk(0xb9445000)                         = 0xb9445000

uname({sys=”Linux”, node=”arunks-PC1″, …}) = 0

gettimeofday({1291871335, 768433}, NULL) = 0

clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0xb7544728) = 4579

exit_group(0)                           = ?

this dump has been truncated at places to fit in here but the output is self evident.

As we can see the bluetooth daemon program tried to open series of shared libraries, mmaping them to the process address space of bluetoothd, setting access writes to these memory pages and further. You can also figure the path where it tried to search for specific shared libs. Throughout the trace bluetoothd seems to be locating (“/etc/ld.so.nohwcap) and meeting with successive failures. Sequence of reads/ writes and any memory protection errors etc could be easily tracked and debugged via strace.

 

Oops Analysis.

An Oops is triggered by some exception and is a dump of CPU state and kernel stack at that point. In short if exceptions such as segmentation fault or invalid memory access happen in kernel code, lead to panics/Oops. Addresses used by processor are virtual and are mapped to physical addresses through a complex structure of so-called page tables. When an invalid pointer is de-referenced, the paging mechanism fails to map the pointer to a physical address and the processor signals a page fault to the operating system. If the address is not valid, the kernel is not able to “page in” the missing address; it generates an oops. This happens when the CPU is in supervisor mode. Sometimes, if the oops makes it into /var/log/messages, the syslogd daemon might disassemble the oops automatically.  This is usually handy, unless you don’t have the correct System.map file on the system.  Else you may generate the stack trace via a utility called ksymoops feeding it the correct system.map and Oops dump. To run ksymoops, do the following:root@arunks-PC1# ksymoops -K -L -O -m System.map < oops.txt > oops.outwhere the oops data above is in a file oops.txt and the corresponding disassembled oops will be written to the file oops.out.  Now lets look at an actual Oops on my x86 machine hosting Ubuntu Linux distribution.Unable to handle kernel NULL pointer dereference at virtual address 00000004printing eip:80289912*pde = 591be001Oops: 0002

Modules linked in: deadman multipath md bonding1 bonding cpqci cpqhealth cpqrom e100 lpfcdd

CPU:    1

EIP:    0010:[<80289912>]    Not tainted

EFLAGS: 00010202

eax: 00000000   ebx: f8f37138   ecx: f8b36d98   edx: 00000000

esi: 00619380   edi: f504ec00   ebp: 00000900   esp: d91bdcf8

ds: 0018   es: 0018   ss: 0018

Process spew (pid: 1230, stackpage=d91bd000)

Stack:

f504ec00 f504ed70 f587f370 00bb8000 00000080 00000001 00000050 00000000

80286341 d91bdd56 d91bdd58 00610180 f504ec00 00003a01 daa9c320 00609250

00bb8000 00000020 00000200 f587f200 f5a2e000 00610180 00000600 09001000

Call Trace: [<80286341>]  [<80286427>]  [<80219fdc>]  [<8021a051>]

[<8013b78c>]  [<8013b94b>]  [<80139b70>]  [<8012fd22>]  [<80130020>]  [<8013008c>][<80130b1292ff>]  [<8012919c>]  [<801d8425>]  [<801d34d9>]  [<80137717>][<80106f27>]

Code: 89 42 04 89 10 c7 01 00 00 00 00 c7 41 04 00 00 00 00 8b 03

Decoding Oops message

The first line displays reason for the panic, obviously. There is a null pointer access somewhere. Next the EIP or the Extended Instruction Pointer’s value is printed. An Oops count is also dumped which in this case is 2. Generally one Oops triggers more oops and only the first oops message is reliable.

Next is the EIP displaying code segment and instruction address. Next are the Program status, control, general purpose and more segment registers.

The stack trace is a trail of stack frames of subroutines that the system is executing at any point of time. A snap shot of same on crash time would yield valuable info on the function trail that lead to crash. Often its not the last routine executed that is a source of problem or crash rather some subroutine in middle of stack trace is usually the culprit.

The call trace dump is said to be the trail of return addresses, usually extracted from the stack trace for relevant info.

Below is a an extract after subjecting above oops thru ksymoops with appropriate system.map file. Trace; 80286341 <lvm_map+3b9/490>

Trace; 80286427 <lvm_make_request_fn+f/1c>

Trace; 80219fdc <generic_make_request+11c/12c>

Trace; 8021a051 <submit_bh+65/80>

Trace; 8013b78c <sync_page_buffers+94/ac>

Trace; 8013b94b <try_to_free_buffers+1a7/1d4>

Trace; 80139b70 <try_to_release_page+44/48>

Trace; 8012fd22 <shrink_cache+24a/3c8>

Trace; 80130020 <shrink_caches+78/a8>

Trace; 8013008c <try_to_free_pages_zone+3c/5c>

All it does is translate the hex addresses to equivalent function or symbolic names handy for the developer to correlate.

Now we will walk the call stack and verify function chain.

Now that we have a translated call trace for a particular process, we’ll need to verify the call trace.  Unfortunately, the call trace isn’t always accurate.  Somehow phantom function calls get inserted in the trace, yet there is no code path to those functions.  To follow a call trace, you’ll need a disassembled kernel.  The kernel build process will generate a uncompressed kernel image called “vmlinux”, and it will be in the top level kernel directory after the compilation process.  To disassemble the vmlinux file:

% objdump -s -d vmlinux > vmlinux.disas

It is usually very helpful to re-compile the kernel with symbols and generate a new vmlinux file with line number information.

So with Ksymoops and call trace with vmlinux, it becomes easier to track the offending function often leading to exact offending instruction. But essentially, there may be incorrect parameters passed to a system call from user space or incorrect processing within kernel which leads to oops.

December 4th, 2010 | Categories: Uncategorized | Tags:

At first this sounds to be a brilliant use case to me to govern-limit/allow/control Bluetooth access at runtime on Android based systems. And dont know whether its fully realisable but definitely worth exploring.  Since Android uses Bluez stack and extends it at framework, it would be really difficult or detect any application/module accessing or trying to access BT baseband.

Consider that there are more than four layers in android BT stack which may access BT baseband :-

Kernel across HCI.

Kernel via ALSA[certain chips dont support sco over hci]

bluez utils like hcitool

bluetoothd

Android/JNI layers.

This use case is borrowed from one of the suggestions made on Android-platform reflector from earlenceferns@gmail.com

September 19th, 2010 | Categories: Linux | Tags:

Technologies for Measuring Bootup Time

Printktimes – simple system for showing timing information for each printk
KernelFunctionTrace – system for reporting function timings in the kernel
LinuxTraceToolkit – system for reporting timing data for certain kernel and process events

Techniques for reducing bootup time

DisableConsole – Avoid overhead of console output during system startup.

RTCNoSync – Avoid delay to synchronize system time with RTC clock edge, on startup

ShortIDEDelays – Reduce duration of IDE startup delays (This is effective but possibly dangerous.)

KernelXIP – Allow kernel to be executed in-place in ROM or FLASH

IDENoProbe – Force kernel to observe the ide<x>=noprobe option

PresetLPJ – Allow the use of a preset loops_per_jiffy value

ApplicationXIP – Allow programs and libraries to be executed in-place in ROM or FLASH

PreLinking – Avoid cost of runtime linking on first program load

OptimizeRCScripts – Reduce overhead of running RC scripts
DmaCopyOfKernelOnStartup – Copy kernel from Flash to RAM using DMA
ParallelRCScripts – Run RC scripts in parallel instead of sequentially
ApplicationInitOptimizations – Improvements in program load and init time via
use of mmap vs. read and ii) control over page mapping characteristics

1.1 Introduction( Printk Times)

Printk times” is a simple technology which adds some code to the standard kernel printkroutine, to output timing data with each message. While crude, this can be used to get an overview of the areas of kernel initalization which take a relatively long time. This feature is used by the Bootup Time Working Group to identify areas of the Linux kernel requiring work to improve bootup time,and to measure the improvements of changes made by the working group.
The technology for this feature consists of a patch and a utility program. The patch alters the printk code in the kernel to emit the timing data. The patch was incorporated into the mainline kernel as of version 2.6.11! Both the feature, and the utility program are now part of mainline Linux.

With printk-times turned on, the system emits the timing data as a floating point number of seconds (to microsecond resolution) for the time at which the printk started. The utility program shows the time between calls, or it can show the times relative to a specific message. This makes it easier to see the timing for specific segments of kernel code.

How To Use (2.6 version of kernel)

If using a kernel version prior to 2.6.11, apply the appropriate patch to your kernel
When booting the kernel, add the option “time” on the kernel command line
You should see extra data at the beginning of each printk line

Collect the kernel printk data with dmesg
save the results to a file, like so:  dmesg >/tmp/bootup_printks

To see the time spent between successive printks, use the show_delta command.
scripts/show_delta /tmp/bootup_printks

To avoid the overhead of writing out the kernel messages during bootup, try booting the kernel with the “quiet” kernel command line option.

If you want to configure the kernel to always boot with timing information turned on,  and/or  you want to see timing information for the kernel BEFORE it parses the kernel command line:

Configure your kernel with “Show timing information on printks” turned on.
This option is on the the “Kernel hacking” menu of the kernel configuration program.
Compile and boot your kernel

Kernel Function tracer

Kernel Function Trace (KFT) is a kernel function tracing system, which uses the
“-finstrument-functions” capability of the gcc compiler to add instrumentation  callouts to every function entry and exit.

The KFT system provides for capturing these callouts and generating a trace of events,with timing details. KFT is excellent at providing a good timing overview of kernel procedures, allowing you to see where time is spent in functions and sub-routines in the kernel.

The main mode of operation with KFT is to use the system with a dynamic trace configuration.That is, you can set a trace configuration after kernel startup, using the /proc/kft interface,and retrieve trace data immediately.

However, another (special) mode of operation is available, called STATIC_RUN mode, where the configuration for a KFT run is configured and compiled statically into the kernel. This mode is useful for getting a trace of kernel operation during system bootup (before user space is running).

The KFT configuration lets you specify how to automatically start and stop a trace, whether to include interrupts as part of the trace, and whether to filter the trace data by various criteria (for minimum function duration, only certain listed functions, etc.) KFT trace data is retrieved by reading from /proc/kft_data after the trace is complete.

August 23rd, 2010 | Categories: Bluez | Tags:

Finally I try to summarize the flow of info and packets via HCI to and fro Bluetooth controller. The flow of packets in core is namely for Events, commands and ACL data. It happens via tasklets registered when a new transport device is registered with hci. Namely hci_rx_task that handles incoming acl, sco and event packets, hci_tx_task  takes care of transmitting everything other than hci commands and hci_cmd_task which takes care of  sending of hci commands

These tasklets are registered when in hci_register_dev() of hci_core.c whenever a new HCI transport is registered with Bluez.  HCI events were existingly being handled via in hci_rx_task that besides copying of these frames to raw sockets also updates hci_event.c which takes of processing of these events and other intelligence at kernel-hci layer itself.

hci_dev is the struct that characterises a hci_device. which has representations for 3 tasklets apart from 3 skb queues for these packets. HCI  requests and hci notifications are handled via hci_send_cmd, hci_sched_acl, hci_sched_sco, hci_tx_task all ulitmately use hci_send_frame to do the needful. hci_tx_task a common fucntion of tx tasklet that sends acl, sco, esco packets on these queues and flushes them down to the hci driver.

But as Bluez evolves on lines of Bluetooth/SIG, tasklets are not viewed as the proper way of handling transmissions and receptions. As per new architecture proposed by Marcel and Bluez maintainers: hci event processing needs to go via workqueues rather than the existing rx_task. Why?  with the introduction of AMP support in bluez for HS, there would be bunch more of events and tasklets cound be a problem. But is it really between a better implementation via wq or rather that event rx path is coupled with ACL data? If former there have to be compelling reasons to have rx events to need sleep or execution in kernel process context and if latter than wont spawning another tasklet be a solution. Whatever the reason might be, there is definitely a movement in existing information flow within kernel and later in user Bluez.

Marcel has already submitted a patch to this effect but i guess the debate is still on …..

Comments Off
August 18th, 2010 | Categories: Android | Tags: , , , , ,

Android Build system has a two-phase build:

1. Read all Android.mk files, and compile source.
2. Link all object files, including static libs.

This means it is only for exe or shared object, that we are to state link dependency to static libs. We have some examples where one static lib uses another. We dont have to state those dependencies other makefiles. Call below seems to find the path for a module:

LOCAL_C_INCLUDES := $(call include-path-for, module) also seen examples of, and seems it by default search at build root.

LOCAL_C_INCLUDES := external/…

This explains why $(LOCAL_PATH) appears in many makefiles. We can replace all += with := for include paths, because nothing is set to include path variables per default.

Android make all below:

include $(call all-subdir-makefiles)
Output a message:

$(info Message for you)
=> “Message for you” appears in build log output

Configuration for linking with other modules/libs- It is enough to have a config variable set in makefile of a module  for static link with other libraries. No other module needs to know which we build for. We can remove all Android.mk files that makes nothing, e.g in the top dir.  But then mm will not work from there as it must find a Android.mk file in the current dir. I replaced the contents of ($MYMODULE)/Android.mk with

include $(call all-subdir-makefiles) instead to allow this.

Setting LOCAL_REQUIRED_MODULES, is a no-brainer. mm build *only* local module; it never considers local required modules. Note that clear_vars clears out a certain set of LOCAL_* variables that Android defines. It will not clear out any LOCAL_* that you define.

Most parts of build system is in android/build/core and most is in the file: base_rules.mk

There is a build-system.html, but it may be outdated. It is not inline with what we see in the build dir. There are numerous examples of Android.mk files in the external dir for instance.  You may browse through them and see what is being done with these Android.mk files!

August 17th, 2010 | Categories: Android | Tags: , , , , , , , ,

Well, to increase the thruput of your bluetooth OPP aplication under Android, you need to tweak at multiple layers. This is apart from the inherent limitations imposed by the remote entity during a bluetooth link setup.Essentially prior to an OPP transfer Bluetooth acl communication happens and there are capabilities exchangeduring same. So l2cap mtu and other capabilites are exchanged which to much extent have dependency on the opp transfers etc ..this is typically set in the bluez kernel l2cap.h file and can be set to max at our end. On the obex side , it is dependent on the settings in ObexHelper.javafile of Obex protocol. But effectively this size has to be tuned up to l2cap MTU size which should be set to max. I have seen difference in thruput

speeds while measuring opp thruput with different commercial phones. This has much to do with default l2cap MTU side.


If you do a hciconfig -a on your android shell
# hciconfig -a
hci0: Type: VIRTUAL
BD Address: 11:22:33:44:55:66 ACL MTU: 1021:4 SCO MTU: 0:0
UP RUNNING PSCAN
RX bytes:3014 acl:112 sco:0 events:246 errors:0
TX bytes:3545 acl:57 sco:0 commands:52 errors:0
Features: 0xff 0xee 0x8d 0xfe 0x9b 0xff 0×79 0×87
Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV3
Link policy: RSWITCH SNIFF
Link mode: SLAVE ACCEPT
Name: ‘test_opp’
Class: 0x58020c
Service Classes: Capturing, Object Transfer, Telephony
Device Class: Phone, Smart phone

ACL MTU size refer to default MTU size as set in l2cap/bluez. you may try to play around same for thruput observations …
All the above references are based on Eclairs 2.1 Android source code.

August 15th, 2010 | Categories: Android | Tags: ,

Everytime a process crashes under Android, a so called Tombstone is written for the process. A tombstone is a file containing important information about the process when it crashed. A slimmed core dump of sorts.

It is printed out in the log (can be seen in the printout using adb shell logcat), but the tombstones are also saved and stored on target under /data/tombstones/ and are called tombstone_XX where XX is a number increased by one with each crash.

To get a stacktrace for the crashed process containing file and line information we need to cross reference
the tombstone with the debugging symbols located on the host.  generally the debug symbols are stripped when the libraries are loaded in the rootfs to save space.  Hence you may need to dig the appropriate /out/product/xxx/symbols/system/libxx to get the unstripped libraries. Once that you can extract the line number and function name etc from a uitility called addr2line. this is generally found in the prebuilt directory of android source distribution.
$(android-root)prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin is the normal location.

hence once you have located the unstripped libraries and location of addr2line tool on your host its as simple as using the addr2line command.

$(android-root)prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/addr2line -f -e /out/product/xxx/symbols/system/libc.so 0xbe8c1630 if 0xbe8c1630 is one of the symbols in the strack trace from library libc.so. is a typical dump for a system that underwent 6 crashes on my target.

# ls -l /data/tombstones

-rw——- system   system      24216 2000-01-01 00:04 tombstone_02
-rw——- system   system      22684 2000-01-01 00:03 tombstone_01
-rw——- system   system      21913 2000-01-01 00:02 tombstone_06
-rw——- system   system      24216 2000-01-01 00:04 tombstone_03
-rw——- system   system      24322 2000-01-01 00:10 tombstone_05
-rw——- system   system      22612 2000-01-01 00:02 tombstone_04
-rw——- system   system      24665 2000-01-01 00:02 tombstone_00

# cat tombstone_01
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: ‘generic/myboard_xxx/xxx/myplatform:2.1/ERD79/eng.arunks.20100726.14330
8:eng/test-keys’
pid: 1138, tid: 1138  >>> /system/bin/bluetoothd <<<
signal 11 (SIGSEGV), fault addr deadbaad
r0 00000000  r1 afe13369  r2 00000027  r3 00000054
r4 afe3ae08  r5 00000000  r6 00000000  r7 0000a000
r8 00000000  r9 00000000  10 00000000  fp 00000000
ip 00002ed8  sp bec782d8  lr deadbaad  pc afe10a20  cpsr 60000030
#00  pc 00010a20  /system/lib/libc.so
#01  pc 0000b332  /system/lib/libc.so
#02  pc 0000ca62  /system/lib/bluez-plugin/audio.so
#03  pc 0000d1ce  /system/lib/bluez-plugin/audio.so
#04  pc 0000e0ba  /system/lib/bluez-plugin/audio.so
#05  pc 0002f9a2  /system/lib/libbluetoothd.so
#06  pc 00026806  /system/lib/libbluetoothd.so
#07  pc 00026986  /system/lib/libbluetoothd.so
#08  pc 0002800c  /system/lib/libbluetoothd.so
#09  pc 00028b72  /system/lib/libbluetoothd.so
#10  pc 0001891a  /system/lib/libbluetoothd.so
#11  pc 0000c228  /system/lib/libc.so

code around pc:
afe10a10 f8442001 4798000c e054f8df 26002227
afe10a20 2000f88e ef2cf7fb f7fd2106 f04fe80a
afe10a30 91035180 460aa901 96012006 f7fc9602

code around lr:
deadba9c ffffffff ffffffff ffffffff ffffffff
deadbaac ffffffff ffffffff ffffffff ffffffff
deadbabc ffffffff ffffffff ffffffff ffffffff

stack:
bec78298  bec78334  [stack]

bec7829c  bec7838b  [stack]

bec782a0  afe3b02c  /system/lib/libc.so

is a typical tombstone dump from my system….

August 14th, 2010 | Categories: Uncategorized | Tags:

Here, I bring supplemental information on Android and Linux developers. Besides interests in Bluez, I would bring misc info on ARM,  kernel profiling, debugging and other performance measurement tools for kernel. you are invited to leave your feedbacks on how I may do better.

Also I am interested in providing documentation for kernel and Android-something rare to hear these days :-)

So watch out these pages for exciting stuff and stay tuned!

TOP