SlideShare a Scribd company logo
Android internals
    Egor Elizarov
    SPbSU 2012
Legal info
   Android internals by Egor Elizarov is licensed under a
    Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

   You are free to
      – copy, distribute, display, and perform the work
      – make derivative works
   Under the following conditions
       – Attribution. You must give the original author credit
       – Share Alike. If you alter, transform, or build upon this work, you may
         distribute the resulting work only under a license identical to this one
   All pictures and trademarks are the property of their respective owners. Use of
    these trademarks and pictures is subject to owners permissions.
   Corrections, suggestions, contributions and translations are welcome!



                                          2                                 Egor Elizarov SPbSU 2012
Lecture 4



                          “Androidized” kernel,
                            Bionic C library,
                            Logging system

                                                  yegor.yelizarov(at)gmail.com
Rev: 1.1
Last update: 05/30/2012                         http://vk.com/android_internals


                                    3                              Egor Elizarov SPbSU 2012
Previous time

    Makefile structure

    Android.mk structure

    Build system stages

    Steps to add new module

    Emulator; Qemu; KVM; ADB

    Target file system structure

                             4     Egor Elizarov SPbSU 2012
Kernel types


    Monolithic kernels

    Microkernels

    Hybrid kernels

    Nanokernels

    Exokernels


                         5        Egor Elizarov SPbSU 2012
Linux kernel


    First release 5 October 1991

    Vanilla – repository of Linus Torvalds

    Monolithic kernel with loadable modules
    support

    Languages: C, ASM


                          6                   Egor Elizarov SPbSU 2012
Linux drivers


    Code in drivers/ directory

    Can be build-in or modules (*.ko)

    Devices usually have corresponding element
    in /dev directory

    Network devices doesn't have /dev/* elements


                           7                  Egor Elizarov SPbSU 2012
“Androidized” kernel


    Based on Vanilla

    Not in mainline until 3.x kernels

    Usually board specific

    Have android features



                             8          Egor Elizarov SPbSU 2012
System calls




     9         Egor Elizarov SPbSU 2012
SysFS & DevFS




     10         Egor Elizarov SPbSU 2012
Android / kernel interaction


    System calls (Ex: Bionic routines)

    SysFS (Ex: Power management)

    DevFS (Ex: Volume deamon)

    ProcFS (Ex: Debug system)

    Netlink (Ex: Event hub)


                          11             Egor Elizarov SPbSU 2012
Wakelocks

    Early suspend (Go to suspend as often as
    possible)

    /sys/power/wake_lock, /sys/power/wake_unlock

    Could not suspend if wakelock occupied

    Wake locks have labels (Ex:
    PowerManagerService)


                          12                   Egor Elizarov SPbSU 2012
Low memory killer

   Behind Linux Out Of Memory logic

    Allow to set different OOM thresholds for different kinds
    of processes

    Set up user-space policies during init
   Policies are adjusted in runtime by Activity Manager
   kernel/drivers/staging/android/lowmemorykiller.c



                                13                         Egor Elizarov SPbSU 2012
Binder


    RPC/IPC akin to COM

    Derived from OpenBinder project

    Transmit “parcels” between processes

    /dev/binder & /proc/binder

    Code: kernel/drivers/staging/android/binder.c


                          14                    Egor Elizarov SPbSU 2012
Anonymous SHared MEMory

    Derived from POSIX SHM part of SysV IPC

    Named memory block that is shared between
    processes

    Have reference counting to destroy memory
    regions

    Shrink memory regions if system has lack of
    memory

                          15                    Egor Elizarov SPbSU 2012
Alarm

    Based on Linux High-Resolution Timer & Real-
    Time Clock

    Allow set system time and adjust alarms

    Use RTC to exit suspend and HRT to get precise
    alarm

    /dev/alarm & /dev/rtc/ & /sys/class/rtc


                           16                 Egor Elizarov SPbSU 2012
Logger


    Light weight logger

    Use three ring buffer queues inside kernel

    No file writes or context switching operations

    /dev/log/*

    Code: kernel/drivers/staging/android/logger.c


                           17                    Egor Elizarov SPbSU 2012
More android features


    Paranoid networking

    Ram console

    Physical memory (pmem)

    Timed output / timed gpio

    etc.


                          18       Egor Elizarov SPbSU 2012
Getting & building kernel
   git clone https://android.googlesource.com/kernel/goldfish.git
   cd goldfish
   git checkout -b android-goldfish-2.6.29 origin/android-goldfish-
    2.6.29
   make goldfish_defconfig
   make menuconfig
   make ARCH=arm
    CROSS_COMPILE=$ANDROID_ROOT/prebuilt/linux-
    x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- uImage -j4


                                    19                               Egor Elizarov SPbSU 2012
Bionic


    Small LibC implementation

    Partially POSIX compatible

    BSD license

    Support for x86, ARM, ARM thumb



                        20            Egor Elizarov SPbSU 2012
Bionic features


    Not compatible with GNU libc

    Fast pthread implementation (based on futexes)

    Access to property values

    User/group management



                          21                   Egor Elizarov SPbSU 2012
Not in Bionic


    Wide chars

    C++ exceptions (limited implementation since
    NDKr5)

    C++ STL

    Full pthread


                         22                  Egor Elizarov SPbSU 2012
Adding syscall


    ./bionic/libc/README.TXT

    Find syscall number in kernel (unistd.h)

    Add line to ./bionic/libc/SYSCALLS.TXT

    Run bionic/libc/tools/gensyscalls.py

    Add wrapper to bionic if needed


                           23                  Egor Elizarov SPbSU 2012
Logging system




      24         Egor Elizarov SPbSU 2012
Logging in code

    Kernel: printk()





    Java: Log.*, SLog.*, EventLog.*




                           25         Egor Elizarov SPbSU 2012
Logging in code (2)


    Native code: LOG* (LOGE)

    Define for native code: LOG_TAG,
    LOG_NDEBUG




                        26             Egor Elizarov SPbSU 2012
Obtaining logs



    dmesg

    logcat

    adb logcat




                       27         Egor Elizarov SPbSU 2012
Next time



    Dalvik VM

    Android application live
    cycle

                               Source: http://android-developers.blogspot.com/




                          28                                        Egor Elizarov SPbSU 2012
Useful links
   http://vk.com/android_internals

   K. Yaghmour. Embedded Android. Early Release, O'Reilly, October 2011

   http://wiki.osdev.org/Kernels

   http://www.kernel.org/

   http://cs736-android.pbworks.com/w/page/5834465/ASHMEM

   http://elinux.org/Android_Kernel_Features

   R. Love. Linux kernel development. 3rd edition, Addison-Westley, June 2010

   http://source.android.com/source/building-kernels.html

   $ANDROID_ROOT/bionic/libc/README

   http://codingrelic.geekhold.com/2008/11/six-million-dollar-libc.html



                                                29                               Egor Elizarov SPbSU 2012
Thanks to


    Sergey Matyukevich for review and advices
    (www.linkedin.com/pub/sergey-matyukevich/31/889/769)


    Nikolay F. Fominykh for review and advices


    Nikita Shulga for advices and notes
    (http://www.linkedin.com/pub/nikita-shulga/8/582/287)


    Grigory Tolstolytkin for advices and notes
    (http://www.linkedin.com/pub/grigory-
    tolstolytkin/2a/b41/74)


                                30                      Egor Elizarov SPbSU 2012

More Related Content

PDF
Android internals 05 - Dalvik VM (rev_1.1)
PDF
Android internals 09 - Sensors, Power Management, Input subsystem, Data stora...
PDF
Android internals 06 - Binder, Typical subsystem (rev_1.1)
PDF
Android internals 03 - Build system, emulator (rev_1.1)
PDF
Android internals 08 - System start up, Media subsystem (rev_1.1)
PDF
Android internals 10 - Debugging/Profiling, Bluetooth/WiFI/RIL (rev_1.1)
PDF
Android internals 01 - Basic concepts of mobile platforms (rev_1.1)
PDF
Android internals 02 - High-level architecture, version control system (rev_1.1)
Android internals 05 - Dalvik VM (rev_1.1)
Android internals 09 - Sensors, Power Management, Input subsystem, Data stora...
Android internals 06 - Binder, Typical subsystem (rev_1.1)
Android internals 03 - Build system, emulator (rev_1.1)
Android internals 08 - System start up, Media subsystem (rev_1.1)
Android internals 10 - Debugging/Profiling, Bluetooth/WiFI/RIL (rev_1.1)
Android internals 01 - Basic concepts of mobile platforms (rev_1.1)
Android internals 02 - High-level architecture, version control system (rev_1.1)

What's hot (7)

PDF
Android internals 00 - Introduction (rev_1.1)
PDF
Android internals 07 - Android graphics (rev_1.1)
PPT
Learning AOSP - Android Linux Device Driver
PPTX
Exoplayer 2
PDF
Deep Dive into the AOSP
PDF
PDF
Multimedia on android
Android internals 00 - Introduction (rev_1.1)
Android internals 07 - Android graphics (rev_1.1)
Learning AOSP - Android Linux Device Driver
Exoplayer 2
Deep Dive into the AOSP
Multimedia on android
Ad

Viewers also liked (17)

PDF
Making Linux do Hard Real-time
PDF
Effortless network response logging on Android
PPTX
Game controlling via android
PDF
Smart Phone CPU
PPTX
Samsung processors: Exynos family
PDF
Android Sensors
PDF
Review Multicore processing based on ARM architecture
PDF
Introduction to Android Window System
PDF
Performance Comparison Between x86 and ARM Assembly
PDF
Motion recognition with Android devices
PDF
Improve Android System Component Performance
PDF
Android Logging System
PPTX
Android location and sensors API
PPTX
CPU Architectures for Mobile Phone Devices
PPTX
Arm corrected ppt
PPT
Risc and cisc eugene clewlow
PPTX
Intel & ARM: Strategic Comparison
Making Linux do Hard Real-time
Effortless network response logging on Android
Game controlling via android
Smart Phone CPU
Samsung processors: Exynos family
Android Sensors
Review Multicore processing based on ARM architecture
Introduction to Android Window System
Performance Comparison Between x86 and ARM Assembly
Motion recognition with Android devices
Improve Android System Component Performance
Android Logging System
Android location and sensors API
CPU Architectures for Mobile Phone Devices
Arm corrected ppt
Risc and cisc eugene clewlow
Intel & ARM: Strategic Comparison
Ad

Similar to Android internals 04 - “Androdized” kernel, Bionic, Logging subsystem (rev_1.1) (20)

PDF
olibc: Another C Library optimized for Embedded Linux
PDF
Android OS Porting: Introduction
PDF
Android Internals
PDF
Reusing your existing software on Android
PDF
Android is NOT just 'Java on Linux'
PDF
Android Internals at Linaro Connect Asia 2013
PDF
Android Attacks
PDF
PDF
Android Variants, Hacks, Tricks and Resources presented at AnDevConII
PPTX
Zeelogic android-training-2013
PDF
Porting Android
PDF
Porting Android ABS 2011
PDF
Is Android the New King of Embedded OSes at Embedded World 2014
PDF
Android ROM cooking: A practical Tutorial (DroidCon Torino 2014)
PDF
Linux on System z debugging with Valgrind
PDF
Embedded Android Workshop part I ESC SV 2012
PDF
Porting Android
PDF
Is Android the New Embedded Embedded Linux? at Embedded World 2013
PDF
Discover System Facilities inside Your Android Phone
PDF
Android Hacks, Variants, Tricks and Resources ESC SV 2012
olibc: Another C Library optimized for Embedded Linux
Android OS Porting: Introduction
Android Internals
Reusing your existing software on Android
Android is NOT just 'Java on Linux'
Android Internals at Linaro Connect Asia 2013
Android Attacks
Android Variants, Hacks, Tricks and Resources presented at AnDevConII
Zeelogic android-training-2013
Porting Android
Porting Android ABS 2011
Is Android the New King of Embedded OSes at Embedded World 2014
Android ROM cooking: A practical Tutorial (DroidCon Torino 2014)
Linux on System z debugging with Valgrind
Embedded Android Workshop part I ESC SV 2012
Porting Android
Is Android the New Embedded Embedded Linux? at Embedded World 2013
Discover System Facilities inside Your Android Phone
Android Hacks, Variants, Tricks and Resources ESC SV 2012

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Institutional Correction lecture only . . .
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
master seminar digital applications in india
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Cell Structure & Organelles in detailed.
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pharma ospi slides which help in ospi learning
Microbial diseases, their pathogenesis and prophylaxis
GDM (1) (1).pptx small presentation for students
human mycosis Human fungal infections are called human mycosis..pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Computing-Curriculum for Schools in Ghana
Final Presentation General Medicine 03-08-2024.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Institutional Correction lecture only . . .
Chinmaya Tiranga quiz Grand Finale.pdf
A systematic review of self-coping strategies used by university students to ...
master seminar digital applications in india
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Final Presentation General Medicine 03-08-2024.pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS

Android internals 04 - “Androdized” kernel, Bionic, Logging subsystem (rev_1.1)

  • 1. Android internals Egor Elizarov SPbSU 2012
  • 2. Legal info  Android internals by Egor Elizarov is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License  You are free to – copy, distribute, display, and perform the work – make derivative works  Under the following conditions – Attribution. You must give the original author credit – Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one  All pictures and trademarks are the property of their respective owners. Use of these trademarks and pictures is subject to owners permissions.  Corrections, suggestions, contributions and translations are welcome! 2 Egor Elizarov SPbSU 2012
  • 3. Lecture 4 “Androidized” kernel, Bionic C library, Logging system yegor.yelizarov(at)gmail.com Rev: 1.1 Last update: 05/30/2012 http://vk.com/android_internals 3 Egor Elizarov SPbSU 2012
  • 4. Previous time  Makefile structure  Android.mk structure  Build system stages  Steps to add new module  Emulator; Qemu; KVM; ADB  Target file system structure 4 Egor Elizarov SPbSU 2012
  • 5. Kernel types  Monolithic kernels  Microkernels  Hybrid kernels  Nanokernels  Exokernels 5 Egor Elizarov SPbSU 2012
  • 6. Linux kernel  First release 5 October 1991  Vanilla – repository of Linus Torvalds  Monolithic kernel with loadable modules support  Languages: C, ASM 6 Egor Elizarov SPbSU 2012
  • 7. Linux drivers  Code in drivers/ directory  Can be build-in or modules (*.ko)  Devices usually have corresponding element in /dev directory  Network devices doesn't have /dev/* elements 7 Egor Elizarov SPbSU 2012
  • 8. “Androidized” kernel  Based on Vanilla  Not in mainline until 3.x kernels  Usually board specific  Have android features 8 Egor Elizarov SPbSU 2012
  • 9. System calls 9 Egor Elizarov SPbSU 2012
  • 10. SysFS & DevFS 10 Egor Elizarov SPbSU 2012
  • 11. Android / kernel interaction  System calls (Ex: Bionic routines)  SysFS (Ex: Power management)  DevFS (Ex: Volume deamon)  ProcFS (Ex: Debug system)  Netlink (Ex: Event hub) 11 Egor Elizarov SPbSU 2012
  • 12. Wakelocks  Early suspend (Go to suspend as often as possible)  /sys/power/wake_lock, /sys/power/wake_unlock  Could not suspend if wakelock occupied  Wake locks have labels (Ex: PowerManagerService) 12 Egor Elizarov SPbSU 2012
  • 13. Low memory killer  Behind Linux Out Of Memory logic  Allow to set different OOM thresholds for different kinds of processes  Set up user-space policies during init  Policies are adjusted in runtime by Activity Manager  kernel/drivers/staging/android/lowmemorykiller.c 13 Egor Elizarov SPbSU 2012
  • 14. Binder  RPC/IPC akin to COM  Derived from OpenBinder project  Transmit “parcels” between processes  /dev/binder & /proc/binder  Code: kernel/drivers/staging/android/binder.c 14 Egor Elizarov SPbSU 2012
  • 15. Anonymous SHared MEMory  Derived from POSIX SHM part of SysV IPC  Named memory block that is shared between processes  Have reference counting to destroy memory regions  Shrink memory regions if system has lack of memory 15 Egor Elizarov SPbSU 2012
  • 16. Alarm  Based on Linux High-Resolution Timer & Real- Time Clock  Allow set system time and adjust alarms  Use RTC to exit suspend and HRT to get precise alarm  /dev/alarm & /dev/rtc/ & /sys/class/rtc 16 Egor Elizarov SPbSU 2012
  • 17. Logger  Light weight logger  Use three ring buffer queues inside kernel  No file writes or context switching operations  /dev/log/*  Code: kernel/drivers/staging/android/logger.c 17 Egor Elizarov SPbSU 2012
  • 18. More android features  Paranoid networking  Ram console  Physical memory (pmem)  Timed output / timed gpio  etc. 18 Egor Elizarov SPbSU 2012
  • 19. Getting & building kernel  git clone https://android.googlesource.com/kernel/goldfish.git  cd goldfish  git checkout -b android-goldfish-2.6.29 origin/android-goldfish- 2.6.29  make goldfish_defconfig  make menuconfig  make ARCH=arm CROSS_COMPILE=$ANDROID_ROOT/prebuilt/linux- x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- uImage -j4 19 Egor Elizarov SPbSU 2012
  • 20. Bionic  Small LibC implementation  Partially POSIX compatible  BSD license  Support for x86, ARM, ARM thumb 20 Egor Elizarov SPbSU 2012
  • 21. Bionic features  Not compatible with GNU libc  Fast pthread implementation (based on futexes)  Access to property values  User/group management 21 Egor Elizarov SPbSU 2012
  • 22. Not in Bionic  Wide chars  C++ exceptions (limited implementation since NDKr5)  C++ STL  Full pthread 22 Egor Elizarov SPbSU 2012
  • 23. Adding syscall  ./bionic/libc/README.TXT  Find syscall number in kernel (unistd.h)  Add line to ./bionic/libc/SYSCALLS.TXT  Run bionic/libc/tools/gensyscalls.py  Add wrapper to bionic if needed 23 Egor Elizarov SPbSU 2012
  • 24. Logging system 24 Egor Elizarov SPbSU 2012
  • 25. Logging in code  Kernel: printk()  Java: Log.*, SLog.*, EventLog.* 25 Egor Elizarov SPbSU 2012
  • 26. Logging in code (2)  Native code: LOG* (LOGE)  Define for native code: LOG_TAG, LOG_NDEBUG 26 Egor Elizarov SPbSU 2012
  • 27. Obtaining logs  dmesg  logcat  adb logcat 27 Egor Elizarov SPbSU 2012
  • 28. Next time  Dalvik VM  Android application live cycle Source: http://android-developers.blogspot.com/ 28 Egor Elizarov SPbSU 2012
  • 29. Useful links  http://vk.com/android_internals  K. Yaghmour. Embedded Android. Early Release, O'Reilly, October 2011  http://wiki.osdev.org/Kernels  http://www.kernel.org/  http://cs736-android.pbworks.com/w/page/5834465/ASHMEM  http://elinux.org/Android_Kernel_Features  R. Love. Linux kernel development. 3rd edition, Addison-Westley, June 2010  http://source.android.com/source/building-kernels.html  $ANDROID_ROOT/bionic/libc/README  http://codingrelic.geekhold.com/2008/11/six-million-dollar-libc.html 29 Egor Elizarov SPbSU 2012
  • 30. Thanks to  Sergey Matyukevich for review and advices (www.linkedin.com/pub/sergey-matyukevich/31/889/769)  Nikolay F. Fominykh for review and advices  Nikita Shulga for advices and notes (http://www.linkedin.com/pub/nikita-shulga/8/582/287)  Grigory Tolstolytkin for advices and notes (http://www.linkedin.com/pub/grigory- tolstolytkin/2a/b41/74) 30 Egor Elizarov SPbSU 2012