HOWTO debug smart pointer

From Nsnam
Revision as of 18:16, 17 March 2011 by Tomh (talk | contribs) (new howto on debugging packets)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Suppose I have an ns-3 smart pointer such as:

 Ptr<Packet> p = ...

To access the underlying raw pointer via gdb (e.g. to inspect the contents), you want to access the p->m_ptr member:

 (gdb) p *p
 $22 = (
   ns3::Packet &) @0x80b0ea0: {<ns3::SimpleRefCount<ns3::Packet, ns3::empty, ns3::DefaultDeleter<ns3::Packet> >> 
 = {<ns3::empty> = {<No data fields>}, 
   m_count = 1}, m_buffer = {m_data = 0x80b0f28, m_maxZeroAreaStart = 7, 
   static g_recommendedStart = 7, m_zeroAreaStart = 7, m_zeroAreaEnd = 27, 
   m_start = 0, m_end = 29}, m_byteTagList = {m_used = 0, m_data = 0x0}, 
 m_packetTagList = {static g_free = <optimized out>, 
   static g_nfree = <optimized out>, m_next = 0x0}, m_metadata = {
   static m_freeList = {<std::vector<ns3::PacketMetadata::Data*, std::allocator<ns3::PacketMetadata::Data*> >> = 
 {<std::_Vector_base<ns3::PacketMetadata::Data*, std::allocator<ns3::PacketMetadata::Data*> >> = {
         _M_impl = {<std::allocator<ns3::PacketMetadata::Data*>> = 
 {<__gnu_cxx::new_allocator<ns3::PacketMetadata::Data*>> = {<No data fields>}, <No data fields>}, _M_start = 0x0, 
 _M_finish = 0x0, 
           _M_end_of_storage = 0x0}}, <No data fields>}, <No data fields>}, 
   static m_enable = true, static m_enableChecking = false, 
   static m_metadataSkipped = false, static m_maxSize = 26, 
   static m_chunkUid = 4, m_data = 0x80b0f48, m_head = 8, m_tail = 17, 
   m_used = 26, m_packetUid = 0}, m_nixVector = {m_ptr = 0x0}, 
 static m_globalUid = 2}

This will not work:

 (gdb) p p->m_buffer
 There is no member or method named m_buffer.

Instead, do this:

 (gdb) p (p->m_ptr)->m_buffer
 $24 = {m_data = 0x80b0f28, m_maxZeroAreaStart = 7, 
 static g_recommendedStart = 7, m_zeroAreaStart = 7, m_zeroAreaEnd = 27, 
 m_start = 0, m_end = 29}

Going further, suppose you want to look at the raw bytes of the packet buffer. This is stored in the m_data field of the buffer, (0x80b0f28 above). To inspect the memory contents of that address, use the "x" command of gdb; e.g. this will print out three bytes starting at the referenced address:

 (gdb) x/3 0x80b0f28
 0x80b0f28:	0x00000001	0x00000009	0x00000000

Keep in mind that data contents in a packet buffer are in network byte order and may be endian swapped with respect to your (x86) host operating system.