Print nsTArrays in gdb

This code was shamelessly stolen and modified from the pvector command from Dan Marinescu’s et al. gdb-stl-views.

##
## nsTArray
##
define ptarray
	if $argc == 0
		help ptarray
	else
		set $size = $arg0.mHdr->mLength
		set $capacity = $arg0.mHdr->mCapacity
		set $size_max = $size - 1
                set $elts = $arg0.Elements()
	end
	if $argc == 1
		set $i = 0
		while $i < $size
			printf "elem[%u]: ", $i
			p *($elts + $i)
			set $i++
		end
	end
	if $argc == 2
		set $idx = $arg1
		if $idx < 0 || $idx > $size_max
			printf "idx1, idx2 are not in acceptable range: [0..%u].\n", $size_max
		else
			printf "elem[%u]: ", $idx
			p *($elts + $idx)
		end
	end
	if $argc == 3
	  set $start_idx = $arg1
	  set $stop_idx = $arg2
	  if $start_idx > $stop_idx
	    set $tmp_idx = $start_idx
	    set $start_idx = $stop_idx
	    set $stop_idx = $tmp_idx
	  end
	  if $start_idx < 0 || $stop_idx < 0 || $start_idx > $size_max || $stop_idx > $size_max
	    printf "idx1, idx2 are not in acceptable range: [0..%u].\n", $size_max
	  else
	    set $i = $start_idx
		while $i <= $stop_idx
			printf "elem[%u]: ", $i
			p *($elts + $i)
			set $i++
		end
	  end
	end
	if $argc > 0
		printf "nsTArray length = %u\n", $size
		printf "nsTArray capacity = %u\n", $capacity
		printf "Element "
		whatis *$elts
	end
end

document ptarray
	Prints nsTArray information.
	Syntax: ptarray   
	Note: idx, idx1 and idx2 must be in acceptable range [0...size()-1].
	Examples:
	ptarray a - Prints tarray content, size, capacity and T typedef
	ptarray a 0 - Prints element[idx] from tarray
	ptarray a 1 2 - Prints elements in range [idx1..idx2] from tarray
end 

Example session

(gdb) ptarray arr
elem[0]: $1 = 1
elem[1]: $2 = 2
elem[2]: $3 = 3
nsTArray length = 3
nsTArray capacity = 4
Element type = int
(gdb) ptarray arr 1
elem[1]: $4 = 2
nsTArray length = 3
nsTArray capacity = 4
Element type = int
(gdb) ptarray arr 1 2
elem[1]: $5 = 2
elem[2]: $6 = 3
nsTArray length = 3
nsTArray capacity = 4
Element type = int

Comment (1)

  1. Justin Dolske wrote::

    To bug 184013 with you!

    Friday, April 30, 2010 at 19:19 #