Debug: freertos gdb plugin.

This commit is contained in:
Aleksandr Kutuzov
2020-10-17 16:34:16 +03:00
parent 72e78dcc1b
commit c318d54995
12 changed files with 1112 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
# File: EventGroup.py
# Author: Carl Allendorph
# Date: 05NOV2014
#
# Description:
# This file contains the implementation of a Event Group Inspector
import gdb
from .List import ListInspector
from .Task import TaskInspector
class EventGroupInspector:
EvtGrpType = gdb.lookup_type("EventGroup_t")
def __init__(self, handle):
""""""
self._evtgrp = gdb.Value(handle).cast(EventGroupInspector.EvtGrpType)
def GetTasksWaiting(self):
""""""
taskListObj = self._evtgrp["xTasksWaitingForBits"]
taskList = ListInspector(taskListObj)
return taskList.GetElements(TaskInspector.TCBType)
def GetEventBits(self):
"""Get the Event Flag Bits
@return L{gdb.Value} of EventBits_t
"""
return self._evtgrp["uxEventBits"]

View File

@@ -0,0 +1,139 @@
# File: GDBCommands.py
# Author: Carl Allendorph
# Date: 05NOV2014
#
# Description:
# This file contains the implementation of some custom
# GDB commands for Inspecting the FreeRTOS state
import gdb
from .List import ListInspector
from .Task import TaskInspector
from .HandleRegistry import HandleRegistry
from .QueueTools import *
class ShowQueueInfo(gdb.Command):
"""Generate a print out of info about a particular
set of queues.
"""
def __init__(self):
super(ShowQueueInfo, self).__init__("show Queue-Info", gdb.COMMAND_SUPPORT)
def invoke(self, arg, from_tty):
argv = gdb.string_to_argv(arg)
qTypes = []
if len(argv) > 0:
for a in argv:
try:
qType = QueueMode.Map[a]
qTypes.append(qType)
except KeyError:
print("Arg %s does not map to a Queue Type!" % a)
reg = HandleRegistry()
qToShow = []
if len(qTypes) > 0:
# We will only print info about queues
for qType in qTypes:
qObjs = reg.FilterBy(qType)
qToShow.extend(qObjs)
else:
qToShow = reg.FilterBy(None)
print("Num Queues: %d" % len(qToShow))
print("%20s %4s %16s %16s" % ("NAME", "CNT", "SEND", "RECEIVE"))
for q in qToShow:
self.PrintQueueInfo(q)
def PrintQueueInfo(self, q):
"""Print Info about the Queue"""
sendList = q.GetTasksWaitingToSend()
rxList = q.GetTasksWaitingToReceive()
# print("TxLen: %d, RxLen: %d" % (len(sendList), len(rxList)))
maxCount = max(len(sendList), len(rxList))
outputFmt = "%20s %4s %16s %16s"
if maxCount == 0:
print(outputFmt % (q.GetName(), q.GetQueueMessagesWaiting(), "", ""))
else:
for i in range(0, maxCount):
txName = ""
if i < len(sendList):
tcbRef, val = sendList[i]
tcb = TaskInspector(tcbRef)
txName = tcb.GetName()
rxName = ""
if i < len(rxList):
tcbRef, val = rxList[i]
tcb = TaskInspector(tcbRef)
rxName = tcb.GetName()
if i == 0:
print(
outputFmt
% (q.GetName(), q.GetQueueMessagesWaiting(), txName, rxName)
)
else:
print(outputFmt % ("", "", txName, rxName))
class ShowHandleName(gdb.Command):
"""Generate a print out of the handle by name"""
def __init__(self):
super(ShowHandleName, self).__init__("show Handle-Name", gdb.COMMAND_SUPPORT)
def invoke(self, arg, from_tty):
argv = gdb.string_to_argv(arg)
if len(argv) != 1:
print("Invalid Argument: Requires one handle arg")
handle = int(argv[0], 0)
reg = HandleRegistry()
name = reg.GetName(handle)
print("Handle 0x%08x: %s" % (handle, name))
class ShowRegistry(gdb.Command):
"""Generate a print out of the queue handle registry"""
def __init__(self):
super(ShowRegistry, self).__init__("show Handle-Registry", gdb.COMMAND_SUPPORT)
def invoke(self, arg, from_tty):
reg = HandleRegistry()
reg.PrintRegistry()
class ShowList(gdb.Command):
"""Generate a print out of the elements in a list
passed to this command. User must pass a symbol that
will be looked up.
"""
def __init__(self):
super(ShowList, self).__init__(
"show List-Handle", gdb.COMMAND_SUPPORT, gdb.COMPLETE_SYMBOL
)
def invoke(self, arg, from_tty):
argv = gdb.string_to_argv(arg)
CastTypeStr = None
if len(argv) > 0:
symbolArg = argv[0]
if len(argv) > 1:
CastTypeStr = argv[1]
listVal = ListInspector(symbolArg)
elems = listVal.GetElements(CastTypeStr)
for elem in elems:
print("Elem: %s" % str(elem))

View File

@@ -0,0 +1,84 @@
# File: HandleRegistry.py
# Author: Carl Allendorph
# Date: 06NOV2014
#
# Description:
# THis file contains the implementation of a class for accessing the
# handle registry. This contains a mapping of queue handles to
# strings for labeling purposes.
import gdb
from .Types import StdTypes
from .QueueTools import *
class HandleRegistry:
"""The FreeRTOS system can be configured with a table that
associates a name with a QueueHandle_t.
This class can be used to access this table and
label queue/mutex/semaphore/event groups
"""
def __init__(self, regSymbol="xQueueRegistry"):
symbol, methodObj = gdb.lookup_symbol(regSymbol)
self._registry = symbol.value()
self._minIndex = 0
self._maxIndex = 0
self._minIndex, self._maxIndex = self._registry.type.range()
def GetName(self, handle):
"""Find the string name associated with a queue
handle if it exists in the registry
"""
for i in range(self._minIndex, self._maxIndex):
elem = self._registry[i]
h = elem["xHandle"]
val = h.cast(StdTypes.uint32_t)
if handle == val:
print("Found Entry for: %x" % handle)
name = elem["pcQueueName"].string()
return name
def PrintRegistry(self):
for i in range(self._minIndex, self._maxIndex):
elem = self._registry[i]
h = elem["xHandle"]
if h != 0:
name = elem["pcQueueName"].string()
print("%d: %3s %16s" % (i, h, name))
def FilterBy(self, qMode):
"""Retrieve a List of Mutex Queue Handles"""
resp = []
for i in range(self._minIndex, self._maxIndex):
elem = self._registry[i]
h = elem["xHandle"]
if h != 0:
name = elem["pcQueueName"].string()
q = QueueInspector(h)
q.SetName(name)
if qMode != None:
qType = q.GetQueueType()
if qType != None:
if qType == qMode:
resp.append(q)
else:
print("qType == None")
else:
resp.append(q)
return resp
def GetMutexes(self):
"""Retrieve all the Mutex Objects in the Handle Registry"""
return self.FilterBy(QueueMode.MUTEX)
def GetSemaphores(self):
"""Retrieve all the Semaphore Objects in the Handle Registry"""
return self.FilterBy(QueueMode.BINARY)
def GetQueues(self):
"""Retrieve all the Queue Objects in the Handle Registry"""
return self.FilterBy(QueueMode.QUEUE)

View File

@@ -0,0 +1,103 @@
# File: List.py
# Author: Carl Allendorph
# Date: 05NOV2014
#
# Description:
# This file contains the class for inspecting
# a FreeRTOS List Object
#
import gdb
from .Types import StdTypes
class ListInspector:
"""FreeRTOS List Inspector Object"""
ListType = gdb.lookup_type("List_t")
def __init__(self, handle):
""""""
self._list = None
# print("List: Handle: %s" % handle)
self.Assign(handle)
def Assign(self, listObj):
try:
if listObj.type == ListInspector.ListType:
self._list = listObj
return
else:
raise TypeError("Invalid List Object Type!")
except Exception as exc:
# print(" Failed to assign from List object: %s" % str(exc))
pass
symbol, methodObj = gdb.lookup_symbol(listObj)
if symbol != None:
self._list = symbol.value()
else:
addrInt = int(listObj, 0)
listObjPtr = gdb.Value(addrInt).cast(ListInspector.ListType.pointer())
self._list = listObjPtr.dereference()
def GetElements(self, CastTypeStr=None, startElem=1):
"""Get the Elements of the list as an array of
gdb.Value type objects.
@param CastTypeStr string name of the type of object that
we will cast the void *pvOwner elements of the list to.
User can also pass a L{gdb.Type} object as the type
If None, we will simply cast to uint32_t and print these
as hex values.
@param startElem This is a flag to indicate whether
we will start getting elements starting at 0 or 1. Note
that this is to deal with some non-consistent behavior
of some of the TCB Task lists.
"""
if self._list != None:
CastType = None
if CastTypeStr != None:
if type(CastTypeStr) == str:
try:
CastType = gdb.lookup_type(CastTypeStr).pointer()
except:
print("Failed to find type: %s" % CastTypeStr)
elif type(CastTypeStr) == gdb.Type:
CastType = CastTypeStr.pointer()
resp = []
numElems = self._list["uxNumberOfItems"]
# print("List Elements: %d" % numElems)
index = self._list["pxIndex"]
if numElems > 0 and numElems < 200:
if startElem == 0:
curr = index
else:
curr = index["pxPrevious"]
for i in range(0, numElems):
owner = curr["pvOwner"]
ownerObj = None
if CastType != None:
castObjPtr = owner.cast(CastType)
castObj = castObjPtr.dereference()
ownerObj = castObj
else:
ownerUInt = owner.cast(StdTypes.uint32_t)
ownerObj = ownerUInt
itemVal = curr["xItemValue"]
resp.append((ownerObj, itemVal.cast(StdTypes.uint32_t)))
curr = curr["pxPrevious"]
return resp
else:
raise ValueError("Invalid List Object - Possibly Failed to Initialize!")

View File

@@ -0,0 +1,110 @@
# File: Queue.py
# Author: Carl Allendorph
# Date: 05NOV2014
#
# Description:
# This file contains the implementation of a Queue Inspector
# class.
#
import gdb
from .List import ListInspector
from .Task import TaskInspector
class QueueMode:
QUEUE = 0
MUTEX = 1
COUNTING = 2
BINARY = 3
RECURSIVE = 4
Map = None
@staticmethod
def IsValid(qType):
if (
qType == QueueMode.QUEUE
or qType == QueueMode.MUTEX
or qType == QueueMode.COUNTING
or qType == QueueMode.BINARY
or qType == QueueMode.RECURSIVE
):
return True
else:
return False
QueueMap = {
"mutex": QueueMode.MUTEX,
"queue": QueueMode.QUEUE,
"semaphore": QueueMode.BINARY,
"counting": QueueMode.COUNTING,
"recursive": QueueMode.RECURSIVE,
}
QueueMode.Map = QueueMap
class QueueInspector:
QueueType = gdb.lookup_type("Queue_t")
def __init__(self, handle):
""""""
# print("Queue: Handle: %s" % handle)
self.name = None
queueObjPtr = None
if type(handle) == gdb.Value:
queueObjPtr = handle.cast(QueueInspector.QueueType.pointer())
self._queue = queueObjPtr.dereference()
else:
queueObjPtr = gdb.Value(handle).cast(QueueInspector.QueueType.pointer())
self._queue = queueObjPtr.dereference()
def GetName(self):
return self.name
def SetName(self, name):
self.name = name
def GetTasksWaitingToSend(self):
"""Retrieve a list of gdb.Value objects of type
TCB that are the tasks that are currently waiting to
send data on this queue object.
"""
sendList = ListInspector(self._queue["xTasksWaitingToSend"])
return sendList.GetElements(TaskInspector.TCBType)
def GetTasksWaitingToReceive(self):
"""Retrieve a list of gdb.Value objects of Type
TCB that are the tasks that are currently waiting to
receive data on this queue object.
"""
rxList = ListInspector(self._queue["xTasksWaitingToReceive"])
return rxList.GetElements(TaskInspector.TCBType)
def GetQueueMessagesWaiting(self):
"""Return the number of messages waiting as a
L{gdb.Value} object
"""
return self._queue["uxMessagesWaiting"]
def GetQueueType(self):
"""Return the Type of the Queue as a enumerated number"""
try:
qType = self._queue["ucQueueType"]
if QueueMode.IsValid(int(qType)):
return qType
else:
raise ValueError(
"Invalid Queue Type In Queue Object! Are you sure this is a Queue Handle?"
)
except Exception as exc:
# If the TRACE functionality of the RTOS is not enabled,
# then the queue type will not be availabe in the queue
# handle - so we return None
print("Failed to get Type: %s" % str(exc))
return None

View File

@@ -0,0 +1,63 @@
# File: Task.py
# Author: Carl Allendorph
# Date: 05NOV2014
#
# Description:
# This file contains the implementation of a class to use for
# inspecting the state of a FreeRTOS Task in GDB
#
import gdb
class TaskInspector:
TCBType = gdb.lookup_type("TCB_t")
def __init__(self, handle):
self._tcb = None
# print("Task: Pass Handle: %s" % str(handle))
try:
if handle.type == TaskInspector.TCBType:
self._tcb = handle
return
else:
print("Handle Type: %s" % str(handle.type))
except AttributeError as aexc:
print("Attribute Error: %s" % str(aexc))
pass
except Exception as exc:
print("Error Initializing Task Inspector: %s" % str(exc))
raise
try:
tcbPtr = gdb.Value(handle).cast(TaskInspector.TCBType.pointer())
self._tcb = tcbPtr.dereference()
return
except Exception as exc:
print("Failed to convert Handle Pointer: %s" % str(handle))
self._tcb = handle
def GetName(self):
if self._tcb != None:
return self._tcb["pcTaskName"].string()
else:
raise ValueError("Invalid TCB")
def GetPriority(self):
if self._tcb != None:
return self._tcb["uxPriority"]
else:
raise ValueError("Invalid TCB")
def GetStackMargin(self):
if self._tcb != None:
topStack = self._tcb["pxTopOfStack"]
stackBase = self._tcb["pxStack"]
highWater = topStack - stackBase
return highWater
else:
raise ValueError("Invalid TCB")

View File

@@ -0,0 +1,14 @@
# File: Types.py
# Author: Carl Allendorph
# Date: 05NOV2014
#
# Description:
# This file contains the implementation of some
# standard types
import gdb
class StdTypes:
uint32_t = gdb.lookup_type("uint32_t")
uint16_t = gdb.lookup_type("uint16_t")

View File