Wednesday, 10 July 2013

Trouble Figuring Android NDK stack trace ?

Have you ever faced a bizarre stack trace  containing only memory locations, and praying god if you could find the code lines around/at crash is happening.

Here is a typical example of a stack trace generated by Android NDK when crash occurs in native code:

Lets try to understand what it is.
The PC stands for the program counter and #0 is the stack pointer where the crash happened.

There are several methods we can un- obfuscate  this stack trace.

1. Addr2line : This tool can be found in toolchains/. In ndkr5
 You can simply provide the addr at which the crash happened and it will generate the corresponding line number.

2 ndk-stack : This tool is available from revision 7 onwards. It takes two arguments

A. The library to look out for reference.
B. The crash dump. A plain txt file which contains the stack trace.

While coping the stack trace you should copy from the starting line which starts from "***** *****" like pattern. Because the current tool looks for this line as starting of the trace. It will be handled in the future versions.

3. Generate a Map file : you can always generate a map file which contains all addresses of classes and methods.



Thats it! Thanks for reading. Comments are solicited.



Autoboxing and Unboxing in Java

Hey all, So today we have a very interesting and quick topic to share about.

Its called Autoboxing and Unboxing in JAVA.

What it is ?

Autoboxing - As the name suggest its some sort of stuff done implicitly i.e automatically by Java compiler. So when write a code line like this:

eg 1: Integer a = 10;

Though our inner soul knows that it will work. but have you ever tried to give a second thought, i.e Why it works ?

Here the Autoboxing comes to play. So what Java does at runtime is it automatically convert the primitive type(int here.) into a object of its wrapper class by invoking  Integer.valueOf(int) method. So after auto conversion at runtime it will become:

runtime : Integer a = Integer.valueOf(10);


Another example where it would play more natural usage:

eg 2: 
 
List<Integer> li = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
    li.add(i);
 
at runtime it become:
 
List<Integer> li = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
    li.add(Integer.valueOf(i));
 

Fact File : If any of you is thinking of why didn't this fellow just do something like List<int> instead of List<Integer>, then lemme tell you primitive types are not allowed in Java Generics.


Now comes the big question, When Java compiler does AutoBoxing ?

There are only two situations:

1. When a primitive type is assigned to its corresponding wrapper class variable.
As we did in first example.

2. When a primitive type is passed to a method which expects a wrapper class object type as a parameter.
As we did in second example


Unboxing - Yeah, you guess it right! its the opposite of boxing. Conversion of Wrapper class to the primitive types is called unboxing.
eg 1: int b = a.intValue();  {a is object of Integer class from the above code}

Another natural example:
 
eg 2:
 
public static int sumEven(List<Integer> li) {
    int sum = 0;
    for (Integer i: li)
        if (i % 2 == 0)
            sum += i;
        return sum;
}

As you know  the % and + operators doesn't operate on objects. So why compiler didn't complained about it at compile time ? Its because at runtime they are automatically unboxed by invoking valueOf() method and are converted to primitive types.

runtime:

public static int sumEven(List<Integer> li) {
    int sum = 0;
    for (Integer i : li)
        if (i.intValue() % 2 == 0)
            sum += i.intValue();
        return sum;
}


Now comes the easiest question of you life, When Java compiler does unboxing ?

There are only two situations:

1. When a wrapper class object is assigned to its corresponding primitive type.
As we did in first example.

2. When the wrapper class object is passed as a parameter to a method that expects a value of the corresponding primitive type.
As we did in second example

Thats all Cheers !! Thanks for reading my first blog.