存档在 2011年2月

GDB学习笔记

2011年2月16日

一些参考网页:
8 gdb tricks you should know
学习使用 GNU GDB Debugger
GDB中应该知道的几个调试方法

用GDB调试程序系列:  http://blog.csdn.net/haoel/archive/2003/07/02/2879.aspx

Two more very useful tricks:

1. Setting hardware breakpoints on data does not slow program execution and stops when the specific variable is modified. Sometimes gdb automatically sets HW breakpoint when you say watch . Sometimes you have to force it by using something like:

set x = &
watch *$x

2. You can set an “ignore count” on any breakpoint using “ignore “. GDB will stop after the breakpoint was hit that many times. The standard way to use this is to set the ignore count very high (say, 9999), run the program till it crashes or otherwise stops after an interesting event, and then use “info bp ” to determine how many times the breakpoint was hit. Then set the ignore count to that number -1 and rerun. The debugger will stop just before the interesting event.

GDB Essential commands

The command set of GDB really is quite concise and intuitive,
so learn it now so you don’t have to when things are going pear shaped!

Command Abbrev Description
set args set command args. Also can do: gdb –args command arg1 …
break b set breakpoint (at function, line number, …)
disable dis disable breakpoints (all by default)
enable en enable breakpoints (all by default)
run r (re)start execution
continue c continue execution
step s next line
next n next line without recursing into functions
finish fin next line after this function returns
until <line> continue execution until line number
list l show source (for line, function, offset, …)
backtrace bt show the stack of functions. Add “full” to include local variables
thread apply all bt t a a bt show the stack of functions in all threads
print var p display a variable. Use p/x for hex
x/16xb <addr> hexdump 16 bytes at address
watch wa break when variable changes value
display disp display expression each time program stops
info locals inf loc display local variables
help … h display online help
focus next fs n switch window (allows cursor keys in CMD window for e.g.)
[Ctrl-x a] toggle the text windowing interface
[Ctrl-L] redraw the display (if program outputs for example)
[Return] repeat the last command

Gratuitous ARP

2011年2月14日

以太网中为什么要用到Gratuitous ARP(以下简称GARP)呢?

第一,主机可以通过GARP向网络发送广播,以告诉整个广播域,目前某个IP所对应的MAC地址是什么——这一行为就像是在发宣传单,而宣传单是不需要回应的。

第二,主机是想看看广播域内有没有别的主机使用自己的IP地址,如果使用了,则在界面上弹出“IP冲突”字样。普通ARP请求报文广播发送出去,广播域内所有主机都会接收到,计算机系统判断ARP请求报文中的目的IP地址字段,如果发现和本机的IP地址相同,则将自己的MAC 地址填写到该报文的目的MAC地址字段,并将该报文发回给源主机。所以只要发送ARP请求的主机接收到报文,则证明广播域内有别的主机使用和自己相同的 IP地址(这里不考虑路由器的ARP代理问题)。免费ARP的报文发出去是不希望收到回应的,只希望是起宣告作用;如果收到回应,则证明对方也使用自己目前使用的IP地址,

Gratuitous ARP could mean both gratuitous ARP request or gratuitous ARP reply. Gratuitous in this case means a request/reply that is not normally needed according to the ARP specification (RFC 826) but could be used in some cases. A gratuitous ARP request is an AddressResolutionProtocol request packet where the source and destination IP are both set to the IP of the machine issuing the packet and the destination MAC is the broadcast address ff:ff:ff:ff:ff:ff. Ordinarily, no reply packet will occur. A gratuitous ARP reply is a reply to which no request has been made.