Wednesday, November 18, 2009

About GDB

GDB is a wonderful tool if you are using linux as your platform for software development.
I will list done some of the frequently used approaches here.

watch point:
awatch *0x8092581000
rwatch *0x8092581000
it will start breaking whenever there is read/write at: 0xa87c0e255f7f0080

find /g 0x8080000000,0x8092581000, 0x8080459578
-- this is to search for value: 0x8080459578 between address 0x8080000000 && 0x8092581000
find  0x8080000000,0x8092581000, "tbl_sm_main"
-- this is to search a string. 
 
Printf usage
printf "Delete object: %d\n", i

Commands:
1. info frame
 -- will list down the registry level info about the particular frame, it will be useful when you are debugging cores.
2. info args
  -- will list the arguments passed to the function in particular frame.
3. disassemble
  -- will give the assembly code for the function.
4. disassemble 0xf1704f0 0xf17050f
  -- will give the assembly code between two addresses
5.  info registers
  -- will give the details of registers and stored values
6. info reg OR x $
  -- will output the contents for regiser, ex1: info reg r32 ex2: x $r32
7. info source
8. info sources
9. set pagination on/off
10. x/32w 0x100f3000
  -- To print 32 words from address 0x100f3000
11. x/s
  -- To print contents of the address as string
12. x/nc
  -- To print content of the address as character, it will print "n" characters starting from the address.
13. x/t
  -- To print in binary
14. x/nx
  -- To print in hexa
15. core
  -- To attached to the core file
16. generate-core-file
  -- To generate a core at the particular instance on a running process.
17. set follow-fork-mode parent/child/ask
  -- To set whether to follow parent or child or ask whom to follow.
18.  show follow-fork-mode
  -- To display the status of fork mode
19. set history expansion on
  -- To enable capturing history, you can "!" to rerun the olde commands as similar to Bash command.
20. set height
21. set width 
   -- To set width of the screen. if the lines in gdb are not wrapping after giving above command then run "shell reset" to make it work properly
22. set confirm off/on
  -- Disables/Enables confirmation requests
23. show confirm
   -- Displays the status of confirmation requests
24. show convenience
   -- Print a list of convenience variables used so far, and their values. Abbreviated show conv.
      Convenience variables are prefixed with `$'. Any name preceded by `$' can be used  for a 
      convenience variable, unless it is one of the predefined machine-specific register names.
25. output/fmt expression
   -- Print the value of expression and nothing but that value: no newlines, no `$nn = '. 
      The value is not entered in the value history either. You can use the same formats as for print.
26. set print pretty on/off
   -- Cause GDB to print structures in an indented format with one member per line.
27. show print pretty
   -- Show which format GDB is using to print structures.  
28. info proc map
   -- to list process memory mapping
29.  maintenance info sections
   -- to list core memory mapping
30. info files
   -- list the memory mapping of all the files 
31. info line <function name>
   -- display 
32. add-symbol-file <library.so> .txt 
  -- we can get.txt address of library from info files.
  -- this will help adding loading symbol files manually


To allow the system to dump the core:
ulimit -c unlimited
 
History related commands:
These commands display the state of the GDB history parameters.
show history
show history filename
show history save
show history size
show history expansion
show commands
Display the last ten commands in the command history.
show commands n
Print ten commands centered on command number n.
show commands +
Print ten commands just after the commands last printed.
 
Logging related commands:
show logging                    # displays weather logging is on / off
set logging on                  # enable logging
set logging off                 # disable logging
set logging file log-file.txt   # set name for log file, default is gdb.txt
set logging overwrite   # To append logs of debug sessions or
                                 create new on every debug session using 
 
 
 Good post on checkpoints:
http://sourceware.org/gdb/onlinedocs/gdb/Checkpoint_002fRestart.html#Checkpoint_002fRestart
 
Tracing function calls of process through gdb:
http://stackoverflow.com/questions/311840/tool-to-trace-local-function-calls-in-linux#312058
Breaking the process whenever a function from a specific file is getting executed:
http://stackoverflow.com/questions/475283/using-gdb-stop-the-program-when-it-is-using-any-function-from-file-x

default configuration file for gdb is: .gdbinit

To print the size of the structure in GDB:
p sizeof(struct ) ---- make sure symbol for the structure is present.

Breakpoints:

break FUNCTION
Set a breakpoint at entry to function FUNCTION.
break LINENUM
Set a breakpoint at line LINENUM in the current source file.
break FILENAME:LINENUM
Set a breakpoint at line LINENUM in source file FILENAME.
break FILENAME:FUNCTION
Set a breakpoint at entry to function FUNCTION found in file FILENAME.
break ... if COND
Set a breakpoint with condition COND; evaluate the expression COND each time the breakpoint is reached, and stop only if the value is nonzero--that is, if COND evaluates as true. ... stands for one of the possible arguments described above (or no argument) specifying where to break.
To generate core dumps during program running:
#include
void assert(int expression);

If expression evaluates to 0 (false), then the expression, sourcecode filename, and line number are sent to the standard error, and then calls the abort function.
To enable core dumps for your current shell, use ulimit to set a maximal core dump size in megabytes.
For this example, we'll "limit" core dumps to 1 gigabyte:
$ulimit -c 1024

To use macros in the gdb use following option during compiling:

gcc -gdwarf-2 -g3 sample.c -o sample

#define NDEBUG === > then the macro assert does nothing.

References:
http://sourceware.org/gdb/onlinedocs/gdb/Forks.html
http://arioch.unomaha.edu/~jclark/gdb_plus.html
http://discussknowhow.blogspot.com/2008/07/analyzing-core-dumps-and-assert.html
http://developer.apple.com/mac/library/documentation/DeveloperTools/gdb/gdb/gdb_10.html
http://www.haifux.org/lectures/222/GDB_haifux_David_Khosid.pdf -- Advanced debbuging with gdb
GNU Debugger - Advanced GDB Actions - GoldhiveWiki
http://sunsite.ualberta.ca/Documentation/Gnu/gdb-4.18/html_chapter/gdb_15.html
http://web.mit.edu/gnu/doc/html/gdb_10.html 
https://eli.thegreenplace.net/2011/09/06/stack-frame-layout-on-x86-64

Thursday, November 12, 2009

Compilers

I like to put some words on compilers sooner or later. So starting this article. In the meanwhile I will be adding articles I came across.

References:
1. http://www.linuxjournal.com/content/examining-compilation-process-part-1
2. http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html#Preprocessor-Output

Monday, November 9, 2009

File recovery

References:
1. http://www.makeuseof.com/tag/3-remarkable-file-recovery-tools/
2. http://www.makeuseof.com/tag/restore-your-deleted-files-easily-with-recuva/
3. http://www.makeuseof.com/tag/recover-lost-computer-files-with-undelete-plus/
4. http://www.makeuseof.com/tag/recover-deleted-files-from-your-linux-system/
5. http://www.geekgirls.com/windows_recycle_bin.htm

IP Fragmentation

Encapsulation:
When a host or router handles a datagram, the IP software determines the next hop to which the datagram should be sent. Thus a datagram could traverse many physical networks, each with their own frame formats. An IP datagram is encapsulated in the data area of the frame.

MTU:
The basic fact in networking is that not all networking technologies were created equal. One of the differences between various layer-2 technologies is the maximum payload (commonly called Maximum Transmission Unit – MTU) a layer-2 frame can transport.

Fragmentation:
IP uses a technique called fragmentation to solve the problem of heterogeneous MTUs. When a datagram is larger than the MTU of the network over which it must be sent, it is divided into smaller fragments which are each sent separately.

Path MTU Discovery: The generic solution to the IP fragmentation issues should be the Path MTU Discovery that was issued as an RFC.
It works by setting the DF (Don't Fragment) option in the IP headers of outgoing packets. Any device along the path whose MTU is smaller than the packet will drop such packets and send back an ICMP "Destination Unreachable (Datagram Too Big)" message containing its MTU, allowing the source host to reduce its assumed path MTU appropriately. The process repeats until the MTU is small enough to traverse the entire path without fragmentation.

References:
http://penguin.dcs.bbk.ac.uk/academic/networks/network-layer/fragmentation/index.php
http://www-inteng.fnal.gov/Integrated_Eng/software/locsys/syscode/ipsoftware/IPFragmentation.html
http://www.nil.si/ipcorner/IP_Fragmentation/

Friday, November 6, 2009

Format USB drive

Long long ago I used to format external memory devices. But recently when I needed an USB to be formatted because of virus, interestingly I forgot the method to do it. So I thought of adding this info here for future reference.

steps:
1. First, connect your USB device to your computer. Then right-click on My Computer from the desktop and choose Manage.
2. Next click on Device Manager and then expand out Disk Drives. You should see your USB drive listed there as “Generic USB 2.0 USB Drive” or something similar.
3. Now right-click on the USB drive under Disk Drives and choose Properties. Then go to the Policies tab.
4. Now you will see two options, the “Optimize for quick removal” selected by default. Go ahead and change that by selecting the “Optimize for performance” option. This enables writing caching on the drive and therefore allows you to format it as NTFS! Sweet.
5. That’s it. Now click OK and then go to My Computer. Right click on the drive in My Computer and choose Format. In the File System drop down you will now see the option for NTFS!

References:
http://www.online-tech-tips.com/computer-tips/format-usb-ntfs/