Educating yourself does not mean that you were stupid in the first place; it means that you are intelligent enough to know that there is plenty left to 'learn'. -Melanie Joy

Saturday, 26 January 2013

dbx: Error checking initialization failed ??

January 26, 2013 Posted by Dinesh , , , ,

How to Run:
                           dbx <binaryname>

Enabling access checking :   in dbx prompt enter :  check -access
Enabling memory leak checking: in dbx prompt enter: check -memuse
Enable all checks :  in dbx prompt enter : check -all

once this is done use run command with appropriate  command line arguments to run the program.
if there are any leaks are access errors present in the binary, an error file (binary.err) will be generated and all errors will be redirected to that file.

Some times if access/memory checks are enabled, system may give errors because of some libraries used by the binary.

Ex:

(dbx) check -access
access checking - ON
(dbx) run 1
Running: a.out 1
(process id 17010)
Reading rtcapihook.so
Reading rtcaudit.so
Reading libmapmalloc.so.1
Reading libc_psr.so.1
Reading rtcboot.so
Reading librtc.so
RTC: Enabling Error Checking...
dbx: internal warning: rtc: ld/st instruction uses %r6 at 0xd55c2388 in `/bghux018/bgh26882/AMS80/FSS_MDS/contrib/lib/libnnz10.so`SHATransform_SOL
dbx: system error: cannot recover; Access checking disabled
dbx: Error checking initialization failed.  All error checking disabled.
(dbx)

here because of libnnz10.so, access check is disabled by dbx. 
to tell dbx not use that library use 'rtc skip' command. and enable the access check and run the process.


(dbx) rtc skip libnnz10.so
RTC: please turn on access checking first
(dbx) check -access
access checking - ON
(dbx) rtc skip libnnz10.so
(dbx) run 1
Running: a.out 1
(process id 17046)
RTC: Enabling Error Checking...
RTC: Using UltraSparc trap mechanism
RTC: See `help rtc showmap' and `help rtc limitations' for details.
RTC: Running program...


When access checking is turned on, RTC detects and reports the following kinds of errors:
        baf     # Bad free
        duf     # Duplicate free
        maf    # Misaligned free
        mar    # Misaligned read
        maw   # Misaligned write
        oom    # Out of memory
        rua     # Read from unallocated memory
        rui      # Read from uninitialized memory
        wro    # Write to read-only memory
        wua    # Write to unallocated memory


With leaks checking, RTC will report the following kinds of errors:


        aib     # Possible memory leak - only pointer points in the middle of the block
        air     # Possible memory leak - pointer to the block exists only in register
        mel   # Memory leak - no pointers to the block


Memory errors can be suppressed. The following command suppress read from uninitialized (rui) in all functions in a.out
(dbx) suppress rui in a.out


RTC instruments memory access assembly instructions for access checking. You can exclude load objects, object files and functions from being instrumented. The following command
(dbx) rtc skippatch a.out -f main
excludes the function main from being instrumented.




Thursday, 10 January 2013

Extract particular lines from a file using 'sed'

January 10, 2013 Posted by Dinesh , , No comments


1. Extract Using Line Numbers :

1
2
3
sed -n '10'p <filename>                 #Prints  10th line of file
sed -n '10,20'p <filename>            #Print lines 10 to 20
sed -n '10,$'p <filename>              #Print lines 10 to end

$ sed -n '10'p file is equivalent to  
$ sed '10!d' file and results
Line 10

The default output behavior is to print every line of the input file stream.The explicit 10p command just tells it to print the tenth line
So in the first example since sed is called with -n flag, it suppress the default output and prints 10th line
in the  second example, (note -n is not used) sed will delete (d) the line if it is not (!) line number 10

Similarly printing lines from 2 to 4 :

1
2
sed -n '2,4'p file
sed '2,4!d'  file

Line 2
Line 3
Line 4


2. Extract Using Pattern :

Some time we may need to print the lines which matches the pattern. that also can be done using sed

1
sed -n '/main/p' check.cpp

will print the lines which matches the pattern main in check.cpp file.
grep can be used as an alternative

similarly instead of 'p' (print), 'd' (delete) can be used to delete the line matching the pattern from the file.

1
sed '/main/d' check.cpp > newfile.cpp


3. Extract non empty lines :

Following will remove the empty lines from the file. and redirect remaining to new file.

1
2
sed '/./!d' check.cpp > newfile.cpp
sed '/^$/d' check.cpp > newfile.cpp





Saturday, 5 January 2013

Useful aliases in Shell

January 05, 2013 Posted by Dinesh , , No comments

Like 'ddd' , we can see the code while debugging with gdb too ... 
Set the '-tui' flag for gdb to enable this feature. and '-quiet' will suppress copyright messages. 
      
  alias gdb='gdb -tui -quiet'





Read as many as possible without scrolling

alias tail='tail -n $((${LINES}-2))'

Reading hex data
what most people want from od
alias od='od -Ad -tx1 -v'
Create a directory and go into it
md () { mkdir -p "$1" && cd "$1"; }


Write these functions in bashrc file,common in other languages, available in the shell
ord() { printf "0x%x\n" "'$1"; }
chr() { printf $(printf '\\%03o\\n' "$1"); }

Ex Usage:
dinesh@ubuntu:~$ chr 65
A
dinesh@ubuntu:~$ ord a
0x61
dinesh@ubuntu:~$ ord 1
0x31
dinesh@ubuntu:~$ 


Sunday, 23 December 2012

Searching for a sub string in a String

December 23, 2012 Posted by Dinesh No comments


It is usual to check for an item in a list, tuple, or dict using 'in' operator.

List=[ 'l' , 'i' , 's' , 't']
if 's' in List:
    print "success"

for string we usually go with 'find' function. Its quite easier to go with 'in' operator for string too.


Saturday, 22 December 2012

Fancy Formatting using Python

December 22, 2012 Posted by Dinesh 1 comment

Its quite easy in python to pretty print the output...
Here are two ways to write a table of squares and cubes:
for x in range(1, 6):
...     print repr(x).rjust(2), repr(x*x).rjust(3),repr(x*x*x).rjust(4)
...
 1   1    1
 2   4    8
 3   9   27
 4  16   64
 5  25  125

>>> for x in range(1,6):
...     print '{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x)
...
 1   1    1
 2   4    8
 3   9   27
 4  16   64
 5  25  125
_____________________________________________________________________________________________

similar to rjust function, there are ljsut and center, which justifies the string with spaces (if filler is not specified)

>>> print 'PYTHON'.center(15)
     PYTHON  
>>>
>>> print 'PYTHON'.center(15, '*' )
*****PYTHON****
_____________________________________________________________________________________________

We can use the brackets ( {} ) to format the data.
The brackets and characters within them (called format fields) are replaced  with the objects passed
into the str.format() method.
A number in the brackets refers to the position of the object passed into the str.format() method.

>>> print 'The story of {0}, {1}, and {other}' .format('Bill', 'Manfred', other='Georg')
The story of Bill, Manfred, and Georg
_____________________________________________________________________________________________

An optional ':' and format specifier can follow the field name. This allows greater control over how the value is formatted. The following example rounds Pi to three places after the decimal.

>>> import math
>>> print 'The value of PI is approximately {0:.3f}.'.format(math.pi)
The value of PI is approximately 3.142.


Passing an integer after the ':' will cause that field to be a minimum number of characters wide. This is useful for making tables pretty.

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
>>> for name, phone in table.items():
...             print '{0:10} ==> {1:10d}'.format(name, phone)
...
Jack        ==>       4098
Dcab       ==>       7678
Sjoerd     ==>       4127

___________________________________________________________________________

The % operator can also be used for string formatting in place of str.format() . but it is old fashion. this old style of formatting will eventually be removed from the language. 

>>> import math
>>> print 'The value of PI is approximately %.3f' % math.pi
The value of PI is approximately 3.142



Ref: Python v2.7 Documentation

Some Optimization Tips/Tricks in Python

December 22, 2012 Posted by Dinesh

Looping:
Use xrange for looping across long ranges; it uses much less memory than range, and may save time as well. Both versions are likely to be faster than a while loop:


xrange is a generator. The performance improvement from the use of generators is the result of the lazy generation of values, means values are generated on demand. Furthermore, we do not need to wait until all the elements have been generated before we start to use them.

You can often eliminate a loop by calling map instead.

Strings:

Building up strings with the concatenation operator + can be slow, because it often involves copying strings several times. Formatting using the % operator is generally faster, and uses less memory.

For example:


If you are building up a string with an unknown number of components, consider using string.join to combine them all, instead of concatenating them as you go:


Sample code :



Here are the results for above codes:


range xrange list_range list_xrange
real 0m4.136s 0m2.863s0m1.867s 0m1.569s
user 0m3.960s 0m2.804s0m1.732s 0m1.516s
system 0m0.160s 0m0.048s 0m0.124s0m0.048s




File Operation: (Ext to my previous post)

# Each call to a file’s readline method is quite slow:


# It is much faster to read the entire file into memory by calling readlines; however, this uses up a lot of RAM.


# Another approach is to read blocks of lines.


#Best of all is to use the xreadlines method of a file:


Sample Codes:



Here are the results for above codes:

readlinereadlinesreadblockxreadlines
real0m9.948s0m13.037s0m9.880s0m9.574s
user0m7.144s0m4.316s0m2.716s0m2.372s
system0m0.332s0m1.092s0m0.404s0m0.328s

Friday, 21 December 2012

Best way to open files in Python

December 21, 2012 Posted by Dinesh No comments
C like syntax is as follows,

f = open("file", 'r')
line = f.readline( )
print line
close(f)

some one may forget to close f. this leads to leak, which is considerable in large scale programs. in order to avoid that we  can make use of 'with' or 'for' keywords.


with open("file", 'r') as f:
        line = f.readline()
        print line
or

for line in open("file", 'r'):
       print line


in these scenarios, file will get closed when f is out of scope.