handle SIGPIPE pass noprint nostop
handle SIGTTIN pass noprint nostop

# FreeSWITCH Custom GDB commands
define switch_list_sessions
	dont-repeat
	printf "Listing sessions: \n"
	set $i = 0
	set $x=session_manager.session_table->table->first
	while($x != 0x0)
		printf "uuid %s is at %p\n", $x->pKey, $x->data
		set $x = $x->next
		set $i = $i + 1
	end
	printf "Found %d sessions.\n", $i
end
document switch_list_sessions
Print a list of session uuid and pointers
end

define switch_hash_it_str
	dont-repeat
	set $i = 0
	set $x=$arg0->table->first
	while($x != 0x0)
		printf "key: %s valueptr: %p\n", $x->pKey, $x->data
		set $x = $x->next
		set $i = $i + 1
	end
end
document switch_hash_it_str 
Usage: switch_hash_it_str [hashtable]
Prints the content of a hashtable displaying the key as a string and the value as pointer
end


define switch_hash_it_str_x
	dont-repeat
	set $i = 0
	set $x=$arg0->table->first
	while($x != 0x0)
		printf "key: %s\n", $x->pKey
		print (($arg1*)$x->data)->$arg2
		printf "\n\n"
		set $x = $x->next
		set $i = $i + 1
		end
end
document switch_hash_it_str_x
Usage: switch_hash_it_str_x [hashtable] [value_type] [member]
Prints the content of a hashtable displaying the key as a string and a specific member of the value struct. Args: hashtable value_type member
end

define switch_event_dump
	dont-repeat
	set $x = $arg0->headers
	while($x != 0x0)
		printf "%s = %s\n", $x->name, $x->value
		set $x = $x->next
	end
end
document switch_event_dump
Usage: switch_event_dump [switch_event_t*]
Print an event's headers and values
end

define switch_print_list
	dont-repeat
	set $x = $arg0
	while ($x != 0x0)
		print *$x
		set $x = $x->next
	end
end
document switch_print_list
Usage switch_print_list [symbol]
Prints all the remaining elements of a linked list
end

define switch_print_tags
	dont-repeat
	set $x = $arg0
	while (*((int*)$x) != 0x0)
		info sym $x->t_tag
		printf "%p \"%s\"\n", $x->t_value, $x->t_value
		set $x = $x + 1
	end
end
document switch_print_tags
Usage switch_print_tags [tags]
List sofia tags and their values
end

define switch_setup_session
	set $session=(switch_core_session_t*)$arg0
	set $channel = $session->channel
	printf "UUID: %s\nName: %s\nState: %d\n", $session->uuid_str, $channel->name, $channel->state
end
document switch_setup_session
Usage switch_setup_session [session address]
Sets session and channel from the given address
end

define switch_setup_sofia
	set $tech_pvt = (private_object_t*)$session->private_info
	set $nh = $tech_pvt->nh
end
document switch_setup_sofia
No arguments. Sets nh and tech_pvt from the current session
end

define __indentby
	set $indent_count = 0
	while $indent_count < $arg0
		printf "%4c", ' '
		set $indent_count = $indent_count + 1
	end
end

define __walk_pool_tree
	dont-repeat

	set $depth_$arg0 = $arg0
	set $cdepth_$arg0 = $arg0 + 2
	set $child_$arg0 = $arg1->child
	set $tag_$arg0 = $arg1->tag
	set $total_pool_count = $total_pool_count + 1
	__indentby $depth_$arg0
	if ($tag_$arg0 != 0x0)
		printf "%p -> \"%s\" (child=%p, sibling=%p, depth=%d)\n", $arg1, $tag_$arg0, $child_$arg0, $arg1->sibling, $depth_$arg0
	end
	if ($tag_$arg0 == 0x0)
		printf "%p -> \"(none)\" (child=%p, sibling=%p, depth=%d)\n", $arg1, $child_$arg0, $arg1->sibling, $depth_$arg0
	end
	while ($child_$arg0 != 0x0)
		if ($child_$arg0 > 0x1000)
			__walk_pool_tree $cdepth_$arg0 $child_$arg0
			set $child_$arg0 = $child_$arg0->sibling
		end
		if ($child_$arg0 > 0x0 && $child_$arg0 <= 0x1000)
			__indentby $cdepth_$arg0
			printf "----> Suspicious pool ---> %p (child=%p, depth=%d)\n", $arg1, $child_$arg0, $depth_$arg0
			set $child_$arg0 = 0x0
			set $total_pool_count = $total_pool_count + 1
			set $suspicious_pools = $suspicious_pools + 1
		end
	end
end
document __walk_pool_tree
Private implementation of walk_pool_tree
end

define switch_walk_pool_tree
	printf "Walking pool tree starting at %p\n\n", $arg0
	set $total_pool_count = 0
	set $suspicious_pools = 0
	__walk_pool_tree 0 $arg0
	printf "\n\nTotal number of pools: %d\n", $total_pool_count
	printf "Total number of suspicious pools: %d\n", $suspicious_pools
end
document switch_walk_pool_tree
Usage switch_walk_pool_tree [pool address]
Walk the APR pool tree starting from the provided address
end

define tcmalloc_list
  set $list = ($arg0)
  while (((tcmalloc::ThreadCache*)$list)->next_ != 0)
    p *((tcmalloc::ThreadCache*)$list)
    set $list = ((tcmalloc::ThreadCache *)$list)->next_
  end
end

define switch_profile_nua_list
	dont-repeat
	set $i = 0
	set $x=$arg0->nua->nua_handles
	while($x != 0x0)
		printf "nua: %p magic: %p destroyed: %d\n", $x, $x->nh_magic, $x->nh_destroyed
		set $x = $x->nh_next
		set $i = $i + 1
	end
	printf "\n\n Total nua handles by profile %p (%s): %d", $arg0, $arg0->name, $i
end
document switch_profile_nua_list
Usage: switch_profile_nua_list [profile]
List all nua handles by a given profile
end
