Showing posts with label Embedded. Show all posts
Showing posts with label Embedded. Show all posts

Monday, November 3, 2008

On-Target source Level Debugging Using NFS

Many a times we would like to have to have a luxury of source level debugging but do not have fancy tools like a JTAG/ICE. But thanks to NFS, we do have a solution. Using NFS one can mount the entire source directory ( or any other directory for that matter) on to the target and run a debuggger on the target.

Besides aiding source level debugging NFS can help save crucial development time by circumventing the need to flash the board for every little change. If only a small portion of the code changes, selectively compile that particular program and replace the existing program on the board with the newly built program. Copy-And-Paste instead of Flash-And-Burn!!!


1. Build the Kernel Image for the target with NFS support.

2. Install NFS server package on the host. On debian machines it is as simple as

sudo apt-get install nfs-kernel-server

3. A file called /etc/exports is created. Open this file for editing.

add the following line :-

/home/kernelexploit 192.168.1.1(rw,all_squash,anonuid=kernelexploit,anongid=1000,insecure,no_subtree_check)

where /home/kernelexploit is the path on the host. This will be mounted on the target. [192.168.1.1] is the IP address of the target in my system. Replace it with your board specific parameters.

4. On the target type the following command

mkdir -p /home/kernelexploit-target [ This is where the remote directory would be mapped]

mount -t nfs -o async,noatime,exec,rw,nfsvers=2,nolock 192.168.1.11:/home/kernelexploit /home/kernelexploit-target


Where, 192.168.1.11 is the IP address of the host machine and /home/kernelexploit is the path on the host. The trailing /home/kernelexploit-target is a directory we created on the target where the remote directory is mounted.





Saturday, September 20, 2008

Interview FAQs: C programming

1.  What is the significance of the Keyword "static" in C ?  [Believe me 99% of programmers either give a completely wrong answer or an incomplete one ]

[Answer] Well the answer I would look forward to is :-
a .  static limits the scope of a variable or a function to a file : NO EXTERNAL LINKAGE 
b.  function :  Implication of point (a) is that a function declared static cannot be invoked from anyother file than in which the function is defined.
c.  variable : 
global static: it is same as a global  variable with just the restriction of scope steming rom point (a) i.e. scope is limited to the file

Static variables are initialized only once.
 
local static :  the value persists across function calls and scope is limited to a block
          
                      

2. Inline Vs Macros

3.  What does the qualifier Volatile signify ? Where would you use them. Give examples.

4.  How about "const" ? 

5.  Can a variable be const and volatile at the same time ? Substatiate your argument. [ONLY If the person being interviewed answers question 3 and 4 correctly ]

6.  What is endian-ness ? Write a small piece of code to output the endian-ness of the architecture

7. What are register variables ? Why cannot we take the address of the register variable ?

8. Are pointers and arrays the same? [one of my favourites]
[Answer] : N0: Pointers and arrays are NOT the same although many programmers believe that they are. K&R are explicitly says that " pointers and arrays are equivalent as formal arguments to a function". That is it.

To clinch the argument (pun intended) consider this :-

int *pointer;
int array[10];

pointer++  /*  Allowed */
array++   /*  Not Allowed  */

Modifying the base address of an array is not permitted. No such restrictions apply to a pointer.

But as formal parameters f0r a function definition, following are equivalent:-

char *s;
and char s[ ];

9.  Why is the lifetime of a local variable only till the duration of the local block whereas static variable retain their values across function calls ?

[Answer] local variables (automatic) are placed on stack, the contents of which depend on the function execution. When a function is being executed , its arguments as well as all the local variables are stored on the stack. As soon as the function exits , this place is freed automatically to make way for the arguments and local variables of the next function.Therefore, an automatical variable loses its value as soon as the function exits.

whereas, a static variable is stored onto the data segment. This segment persists throughout the execution of the program and hence variables stored on the data segment retain their values across function calls.

10. function pointers. Invocation and use.
[Answer] : 
a. Function pointers are variables which point to the address of a function.
b. Function pointers are less error prone than normal pointers as memory is never allocated or deallocated with them
c. protype :  

int (*pFunc)(); /* pFunc is a pointer to a func that returns an int and doesnt take a param */

No actual data is being pointed to yet.

Now,let us assume that there is a function with the following prototype:-
int func( ) ;

In this case the following two operations are equivalent :- 

pFunc = func;
pFunc = & func;

Usage :
 a . To replace the switch/if statements. Most commonly used in implementing state machines
 b.  Implementing call backs