Monday, February 11, 2013

Malloc, calloc, and Realloc

                              Calloc, malloc, free, realloc
                               -------------------------------------------
void *calloc(size_t nmemb, size_t size);
Calloc allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero. if nmemb or size is 0, then calloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().
** the memory is initialized to zeros.

void *malloc(size_t size);
allocates size bytes and returns a pointer to the allocated memory (BLOCK). The memory is neot cleared. If size is 0, then malloc() returns either NULL or a unique pointer value that can later be successfully passed to free().
** The memory is uninitialized.

Ex1:
int *ptr;
ptr=(int*) malloc(sizeof(int));

Ex2.
struct employee *ptr;
ptr=(struct employee *ptr)malloc (sizeof(struct employee));

void free( void *ptr);
free() frees the memory space pointed to by ptr, which must have been returned by a prevoious call to malloc(), caloc() or realloc(). otherwise, or if free(ptr) has already been called before, undefined behaviour occurs. If ptr is NULL, no operation is performed.

void *realloc ( void *ptr, size_t size)
realloc() changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged to the minimum of the old and new sizes; newly alllocated memory will be uninitialized. If ptr is NULL, then the call is equivalent to malloc(size), for all values of size; if size is equal to sero, and pt is not NULL, then the call is equivalent ot free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc or realloc(). If the area pointed to was moved, a freeptr) is done.
------------------------------------------------------------------------------------------------------------------------

Thursday, February 7, 2013

How to use GDB debugger

                                 -------------------GDB-----------------------------

1. gcc -g program.c
2. gdb ./a.out
3. break main
4. run
5. list ( show the all line of the program.c)
6. list 5,10 ( show the line form 5 to 10 )
7. bt ( back trace )
8. whatis variable_name (it will give the datatype of variable)
9. bt n ( print only the innermost n frames)
10. bt -n ( print only the outermost n frames)
11. fn ( select frame number n)
12. f addr ( select the frame at address addr)
13. up n ( move n frames up the stack )
14. down n ( move n frames down the stack )
15. info f ( print info about the frame )
    info b ( to list the number of breakpoints )   
16. break function ( Set a breakpoin at entry to function)
17. break -offset, break +offset ( set a break point some number of lines forward or backward to the position)
18. break linenum ( set the breakpoint at particular linenumber)
19. break filename:linenum ( set a breakpoin at line linenum in sourece file filename)
20. delete <breakpoint_number>
    clear <function>
21. Enable/Disable breakpoints
    a) enable <breakpoint_number>
    b) disable <breakpoint_number>
22. print $bpnum ( It will give the number of break points
23. watch expr (set a watchpint for an exeression. gdb will break when    expr is written into by the program and its value changes.
23. rewatch expr ( set a watchpoint that will brea when watch expr is read by the program.
24. awatch   ---()
25. info  watchpoint

26. continue[ignore-count] ( REsume program execution, at the address where your program last stopped. The optional argument ignore-count allows you to specify a further numbe ro ftimes to ignore a breakpoint at this location.

27. step [count]
    Continue running your program until control reaches a different source line, then stop it and return control to GDB.With the optional argument, it wil continue running as in step, but do so count times.
   
28. next[count]
    Continue to the next sourece line in the current( innermost) stack frame. This is similar to STEP, but function alls that appear within the line of code are executed without stopping . An optional argument count is a repeat count.
   
29 . finish ( continue running until just after function in the selected stack frame returns. Print the returned value( if any).

30. until ( continue running until a source line past the current line, in the current stack frame, is reached. This command is used to avoid single stepping throught a loop mor than once.

31. jump linespec ( Resume execution at line linespec. Execution stops again immediately if ther is a break ping there.

32. jump *address ( Resume execution at the instruction at address .
33. info local ( Show the
34  info reg ( Show all the machine register )

************************************************************************
**) SEGMENTATION FAULT:: It is often caused by trying to access a memory location that is not in the address space of the process.
**) BUS ERROR :: It is typically caused by trying to access an object with an improperly aligned address. (Ex: tring to fetch an int from address 1)
**) ILLEGAL INSTRUCTION:: It is typically occurs when execution branches into data. This sometimes happens when the stack is overwritten.
**) Arithmetic exception :: It is typically caused by integer  division by zero.

*************************************************************************
            LAUNCHING GDBSERVER IN TARGET PLATFORM:
    
     TCP/Ip
     1. gdbserver host:port <application name >
        example gdbserver 10.102.68.51:1233 hello
     2. COM port
        gdbserver commdevice <application name >
        example gdbserver /dev/com1 telephony

    LAUNCHING GDB IN HOST PLATFORM
  
   1. gdb telephony
   2. target remote /dev/com1
        OR
   1. (gdb)target remote <targetip>2345
   (gdb) continue (don't run once again. it is already running in target machine ).

  

Monday, February 4, 2013

SED and AWK command in Linux



-------------------AWK--------------------
1) Pattern scanning and text processing language


awk one-liner Tips

Print column1, column5 and column7 of a data file or output of any columns list

$awk ‘{print $1, $5, $7}’ data_file

$cat file_name |awk ‘{print $1 $5 $7}’

$ls –al |awk ‘{print $1, $5, $7}’ -- Prints file_permissions,size and date

Syntax of running an awk program

Awk ‘program’ input file(s)


List all files names whose file size greater than zero.

$ls –al |awk ‘$5 > 0 {print $9}’


List all files whose file size equal to 512bytes.

$ls –al |awk ‘$5 == 0 {print $9}’

print all lines

$awk ‘{print }’ file_name

$awk ‘{print 0}’ file_name

Number of lines in a file

$awk ‘ END {print NR}’ file_name

Number of columns in each row of a file

$awk ‘ {print NF’} file_name

Sort the output of file and eliminate duplicate rows

$awk ‘{print $1, $5, $7}’ |sort –u

List all file names whose file size is greater than 512bytes and owner is "oracle"

$ls –al |awk ‘$3 == "oracle" && $5 > 512 {print $9}’

List all file names whose owner could be either "oracle" or "root"

$ls –al |awk ‘$3 == "oracle" || $3 == "root" {print $9}’

list all the files whose owner is not "oracle

$ls –al |awk ‘$3 != "oracle" {print $9}’

List all lines which has atlease one or more characters

$awk ‘NF > 0 {print }’ file_name

List all lines longer that 50 characters

$awk ‘length($0) > 50 ‘{print }’ file_name

List first two columns

$awk ‘{print $1, $2}’ file_name

Swap first two columns of a file and print

$awk ‘{temp = $1; $1 = $2; $2 = temp; print }’ file_name

Replace first column as "ORACLE" in a data file

$awk ‘{$1 = "ORACLE"; print }’ data_file

Remove first column values in a data file

$awk ‘{$1 =""; print }’ data_file

Calculate total size of a directory in Mb

$ls –al |awk ‘{total +=$5};END {print "Total size: " total/1024/1024 " Mb"}’

Calculate total size of a directory including sub directories in Mb

$ls –lR |awk ‘{total +=$5};END {print "Total size: " total/1024/1024 " Mb"}’

Find largest file in a directory including sub directories

$ls –lR |awk ‘{print $5 "\t" $9}’ |sort –n |tail -1

$ awk -F":" '{ print $1 $3 }' /etc/passwd

$ awk -f myscript.awk myfile.in


------------------SED---------------------
1)  Stream editor for filtering and transforming text
2) Sed works as filter, which makes it particularly useful for scripts, sed works line oriented. The most simple commands are a pattern and an action . if no pattern is given, then action is applied to all lines, otherwise it is applied only to lines matching the pattern.

EXAMPLE::
1) A sample application of sed would be to delete the first 10 lines of stdin and echo the rest to stdout:

$cat /etc/passwd| sed -e '1,10d'

2) If we wanted to print only the first ten lines, we would have deleted all the lines starting with 11:

$cat /etc/passwd| sed -e '11,$d'

3)Another way to get only the first 10 lines is to use the -n option:

$cat /etc/passwd | sed -n -e '1,10p'

4)Commands in sed programs are separated by new lines. So if we wanted to delete the lines 1 to 4 and 6 to 9, we could use:

$cat /etc/passwd | sed -e '1,4d 
6,9d'

5)If we want to put command in the script files instead of using the command line.

$sed -f program.sed

















Qualcomm Short Term

  113 is the SL. 1st Target by mid July.

Total Pageviews