Tuesday, January 29, 2013

Analyze memory fragmentation with VMMap

VMMap is a process virtual and physical memory analysis utility. It shows a breakdown of a process's committed virtual memory types as well as the amount of physical memory (working set) assigned by the operating system to those types. Besides graphical representations of memory usage, VMMap also shows summary information and a detailed process memory map

 

If you have an application that runs out of memory and you only see 800MB of memory used you probably have fragmentation problems. You can use VMMap to see how the memory is allocated. If you use VirtualAlloc for less than 64 KB you will generate unusable memory regions. This is because the address must be aligned to 64KB. By allocating 10 chunks of 1KB you will have 10KB of used memory and 630KB of unusable memory. If you have 32-bit application you may run out of memory (you only have 2 GB of virtual address space available).

References:
http://technet.microsoft.com/en-us/sysinternals/dd535533.aspx

Monday, January 28, 2013

Patching with git

In this post I will show you how to make a patch with git and then apply it.
The first thing we will need is a test project.
We will begin by cloning a repository from github.com, with read only access.
git clone git://github.com/gulyan/gulyan.github.git test
cd test
This will clone a very simple git project.
The first thing we will do is make a new branch. It's considered best-practice to commit your fixes in a separate branch:
git checkout -b my_fix_branch
To view your current branch, you can type:
> git branch
master
* my_fix_branch
Now you can play around and add a couple of commits to this branch.
When you are read to make a patch, you will use the format-patch command.
If you run it without the --stdout argument, it will create a patch file for every commit.
We want to put all the commits in a single file so we use --stdout and we redirect it to a file like this:
git format-patch master..HEAD --stdout > my_fix.patch

Now let's try and apply this patch. First we go back to the master branch:
git checkout master

Before we apply the path, we want to make sure it will not cause problems.
The first thing we should do is run git apply with the --stat argument. This command will show us what the patch will modify:
> git apply --stat my_fix.patch
README |    4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
After this we run git apply with the --check argument. This will not apply the patch. If we don't get any errors we can safely apply the patch.
git apply --check my_fix.patch
In order to apply the patch we will use the git am command:
git am my_fix.patch
This will apply the patch and the commit messages.
If we use git am, we can also signoff on the commits by adding the --signoff argument.

You could also apply the patch with git apply:
git apply my_fix.patch
This will apply the patch to the files, but it will not commit the changed to the branch.
You need to add the files and commit them manually.
For most use cases, I recommend you use git am.

If you have any problems, with git am you can fix the files, add them to the index and then run git am --resolved. To abort a patch use git am --abort.

Reset linux root password

This post will show you how to reset a linux root password. I assume that you are using GRUB 2 as the bootloader.
  1. Select the linux boot option from the grub menu for which you want to reset the password.
  2. Press e to edit your boot option entry. Note that the editing is not persistent, the next time you boot all these changes will be lost so you don't have to worry about reverting them.
  3. In the line starting with something like this: linux /boot/vmlinux-3.5.0-22-generic ... ro quiet slpash replace ro with wr. This will give you write access instead of read only.
  4. Also add init=/bin/bash at the end of the line. This will start a bash process instead of your desktop environment.
  5. When the line looks like this: linux /boot/vmlinux-3.5.0-22-generic ... rw quiet slpash init=/bin/boot press Ctrl+X to boot.
  6. The system will boot and will give a terminal with root access. You can use this terminal to change the password of any users or run any command you want with root access.
  7. To change the password for user gulyan type: passwd gulyan
  8. After you type the new password 2 times, you need to reboot.
Done :)

Sunday, January 27, 2013

C preprocessor

I will mention 3 useful features of the c preprocessor that can be used to make a program more customizable:
  1. Stringification
  2. Concatenation
  3. Varadic macros
Stringification can be used to turn an expression into a string, surrounded by double quotes.
To do this we use # in front of the macro argument that we want to stringify:
#define STRINGIFY(S) #S
This will turn the expression STRINGIFY(2+3) into "2+3".
You can use it for something even more complex like this:
#define STRINGIFY(S) #S
const char str[] = STRINGIFY(int main()
{
  return 0;
});
It should be noted that all the whites-paces will be trimmed intro a single space character.

Concatenation is useful when used with the stringify feature. We can use it to merge 2 strings into a single one by using ## like so:
#define CONCAT(A,B) A ## B
This will turn the expression CONCAT("The result is ", STRINGIFY(2+3)) into "The result is 2+3".

Varadic macros are macros with a variable number of arguments.
These can be useful is we want a custom debug function:
#ifndef DEBUG
  #define dprintf(...) do{}while(0)
#else
  #define dprintf(fmt, ...) \
    fprintf(stderr, fmt, __VA_ARGS__)
#endif
In this example, __VA_ARGS__ will represent the parameters given to the dprintf macro. Based on the definition of DEBOG, calls to dprintf will either be converted to fprintf or be completely removed by the compiler.
We can go a step further with our debug function by including the line and file of the call.
#ifndef DEBUG
  #define dprintf(...) do{}while(0)
#else
  #define dprintf(fmt, ...) \
    fprintf(stderr, "[ERROR %s at %d] " fmt, __FILE__, __LINE__, __VA_ARGS__)
#endif
The __LINE__ and __FILE__ macros are expanded to represent the file and line number of the call of dprintf, NOT the declaration. It should also be noted that there is no coma before the fmt argument. This is done so that the C compiler will concatenate the 2 strings.

Global variables in android

Global variables are variables that can be accessed from more than one activity.
If you only need primitive values than you can pass them in the Intent, but if you have more complex data types this is not an option.
The first thing you might try is to declare the members as static.
This is a VERY BAD idea.
Android can set static instances to null without you knowing and you will spend hours debugging the application.

The correct way to do this is by extending the Application class.
As stated in the Android documentation:
Base class for those who need to maintain global application state.

Bellow is an example of how to use the application class:
package gulyan.gametoolkit.app;

import android.app.Application;

public class GameApplication extends Application {

 private static GameApplication instance = null;

 @Override
 public void onCreate() {
  super.onCreate();
  instance = this;
 }

 public static GameApplication getInstance() {
  return instance;
 }

}
Next, you must edit the AndroidManifest file in order to use the custom application class, by setting the name attribute.
<application
     android:name="gulyan.gametoolkit.app.GameApplication"
     android:icon="@drawable/ic_launcher"
     android:label="@string/app_name"
     android:allowBackup="true"
     android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
You can now declare all the variables as members of this class (non-static members).
If you need to access them you can use the getInstance method in order to obtain the application instance. This instance is the same in all the activities.
GameApplication inst = GameApplication.getInstance();
MyVar x = inst.getMyVar();
inst.setMyVar(x);
Hope you find this useful.

git bisect

The bisect command does a binary search through the commit history in order to help you identify which commit introduced an issue.
To begin a git bisect, the first command is:
git bisect start
After this, you must tell git a bad and a good commit:
git bisect bad # or git bisect bad HEAD
git bisect good HEAD~10
In this example, the HEAD is broken and that 10 commits before the HEAD is a good point.
Git will then begin the binary search and ask you about the state (good/bad) of the current revision.
You tell this by running:
git bisect good
or
git bisect bad
At the end, git will tell you the commit that introduced the issue.
In order to stop the git bisect you must run:
git bisect reset
You should run this command after finishing the search.

Finally, if you have a script that returns 0 if a commit is good and a non-zero result otherwise, you can find out the bad commit in 2 lines:
git bisect HEAD last_good_revision
git bisect run ./script.sh

References:
http://git-scm.com/book/en/Git-Tools-Debugging-with-Git