###############################################################################
#
#    Networking debugging macro definitions
#
###############################################################################

define pktlistdump

   set $pktList = $arg0
   set $link = $pktList.csList.slist.head
   set $numPkts = $pktList.csList.numElements

   set $i = 1
   while ($link != 0)
      set $pkt = (PktHandle *) $link

      # In case the whole frame is not mapped we'll only dump what we can
      if ($pkt->public.frameMappedLen < $pkt->public.bufDesc->frameLen)
         set $dumpBytes = $pkt->public.frameMappedLen
         printf "Dumping packet %d/%d (%d bytes only %d mapped)\n", \
                $i,$numPkts,$pkt->public.bufDesc->frameLen,$dumpBytes
      else
         set $dumpBytes = $pkt->public.bufDesc->frameLen
         printf "Dumping packet %d/%d (%d bytes)\n",$i,$numPkts,$dumpBytes
      end

      # Dump the frame contents to a temporary file, and use od to hexdump and
      # append it with the rest of the list
      dump binary memory /tmp/pkt.bin $pkt->public.frameVA $pkt->public.frameVA+$dumpBytes
      shell od -v -Ax -tx1 /tmp/pkt.bin >> /tmp/list.od

      set $link = $link.next
      set $i = $i + 1
   end 

   shell text2pcap /tmp/list.od $arg1
   shell rm -f /tmp/pkt.bin /tmp/list.od
end

document pktlistdump
   pktlistdump <pktlist> <output file>

   Dumps the given PktList into a tcpdump file. Depends on text2pcap (from
   ethereal/wireshark distribution) and od shell commands. Also assumes
   /tmp is available and writable.
end


define pktattrdump

   set $pkt = $arg0
   set $found = 0

   set $chunk = &$pkt.attributes.staticChunk

   while $chunk != NULL
      set $slot = 0

      while $slot < $chunk.numSlots
	 set $curkey = $chunk->index[$slot]

         if $curkey != 0
            set $found = 1
            printf "\nChunk: <%p>\nSlot: %d\n",$chunk,$slot
            printf "Key: %d\nName: %s\n",$curkey, pktAttrRegTable[$curkey-1].name
            set $value = ((uint64 *)(((uint8 *) &$chunk.index) + $chunk.attrOffset))[$slot]
            printf "Value: %ld (%lx)\n",$value,$value
         end
         set $slot = $slot + 1
      end
      set $chunk = $chunk.nextChunk
   end

   if $found == 0
      printf "No attributes are set for pkt <%p>\n",$pkt
   end
end

document pktattrdump
   pktattrdump <pkt>

   Dump all attributes set within the given PktHandle.
end