如何打印所有vxWorks wvEvents

时间:2015-09-09 15:42:06

标签: vxworks

是否有一个vxWorks命令列出了代码中编译的所有wv事件。我试图在我们的一个使用vxWorks 6.5的产品上分离所有事件。

1 个答案:

答案 0 :(得分:0)

没有命令可以为您提供代码的WindView事件列表。

为什么不搜索包含所有源文件wvEvent的所有行并删除重复项?这应该产生一个所有事件的列表......

---更新---

我编写了以下小型库来挂钩VxWorks符号表中的符号。这可以用于挂钩符号被挂钩后加载的应用程序中的系统调用。

以下机制有效,因为ld命令使用系统符号表解析应用程序的所有未解析引用(例如,对printf的调用将替换为符号printf的值。是printf实现的地址。

以下是图书馆的代码:

#include <vxWorks.h>
#include <symLib.h>

#define MAX_HOOKS   256

typedef struct vxhook_struct
{
   SYMTAB_ID symTblId;    /** id of symbol table containing the symbol */
   SYMBOL *symbol;        /** pointer to symbol entry within symbol table */

   void *originalValue;   /** original value of symbol */
   void *hookedValue;     /** hooked value of symbol */
} VXHOOK;

/** counter of installed hooks */
static int hooks = 0;

/** list of installed hooks */
static VXHOOK hook[MAX_HOOKS];


/*
**  symbolIterator
**  VxWorks callback for symbol table iteration. See symEach(..) for more information.
*/
static BOOL symbolIterator(char *name,
                           int val,
                           SYM_TYPE type,
                           int arg,
                           UINT16 group)
{
   BOOL result = TRUE;   /* continue iteration */
   char *pName;


   pName = (char *)arg;
   if ((pName != NULL) && (name != NULL))
   {
      if (!strcmp(name, pName))
      {
         result = FALSE;   /* symbol found => stop iteration! */
      }
   }

   return (result);
}


/*
**  vxHookGet
**  Searches the hook list for a specific entry and returns a pointer to it if found.
*/
static VXHOOK *vxHookGet(SYMTAB_ID symTblId,   /* symbol table to seachr for name */
                         char *name,           /* name of symbol */
                         int *index)           /* optional return value of index within hook list */
{
   VXHOOK *pHook = NULL;
   int i;


   if (index != NULL)
   {
      *index = -1;
   }

   for (i = 0; i < hooks; i++)
   {
      if ((hook[i].symTblId == symTblId) && (!strcmp(hook[i].symbol->name, name)))
      {
         pHook = &(hook[i]);

         if (index != NULL)
         {
            *index = i;
         }

         break;
      }
   }

   return (pHook);
}


/*
**  vxHook
**  Hooks a symbol within a symbol table (if not already hooked).
*/
STATUS vxHook(SYMTAB_ID symTblId,   /* symbol table to seachr for name */
              char *name,           /* name of symbol */
              void *value,          /* new value to replace symbol value with */
              void **pValue)        /* optional pointer receiving original value from symbol table */
{
   STATUS result = ERROR;
   SYMBOL *symbol;
   VXHOOK *pHook;


   if ((name != NULL) && (value != NULL))
   {
      pHook = vxHookGet(symTblId, name, NULL);
      if (pHook == NULL)   /* symbol not hooked, yet? */
      {
         if (hooks < MAX_HOOKS)   /* free entries available? */
         {
            pHook = &(hook[hooks]);

            symbol = symEach(symTblId, symbolIterator, (int)name);
            if (symbol != NULL)   /* symbol found? */
            {
               pHook->symTblId = symTblId;
               pHook->symbol = symbol;
               pHook->originalValue = symbol->value;
               pHook->hookedValue = value;

               if (pValue != NULL)
               {
                  *pValue = symbol->value;
               }

               /* install hook */
               symbol->value = value;
               printf("Value of symbol '%s' modified from 0x%x to 0x%x.\n", name, pHook->originalValue, pHook->hookedValue);

               hooks++;
               result = OK;    
            }
            else
            {
               printf("Symbol '%s' not found in symTblId=0x%08x.\n", name, symTblId);
            }
         }
         else
         {
            printf("Maximum number of hooks (%d) reached.\n", MAX_HOOKS);
         }
      }
      else
      {
         printf("Symbol '%s' in symTblId=0x%08x already hooked.\n", name, symTblId);
      }
   }

   return (result);
}


/*
**  vxHookList
**  Prints a list of all installed hooks to stdout.
*/
STATUS vxHookList(void)
{
   int i;

   printf(" name                symTblId   value      original\n");
   printf("------------------- ---------- ---------- ----------\n");
   for (i = 0; i < hooks; i++)
   {

      printf("%3d: 0x%08x  %s\n", i, hook[i].symTblId, hook[i].symbol->name);
   }

   return (OK);
}


/*
**  vxUnhook
**  Unhooks a hooked symbol (restoring the original value).
*/
STATUS vxUnhook(SYMTAB_ID symTblId,   /* symbol table to search for name */
                char *name)           /* name of symbol */
{
   STATUS result = ERROR;  
   VXHOOK *pHook;
   int i;  


   pHook = vxHookGet(symTblId, name, &i);
   if (pHook != NULL)
   {
      pHook->symbol->value = pHook->originalValue;  
      printf("Original value 0x%x of symbol '%s' restored.\n", pHook->originalValue, name);

      /* remove hook */
      hooks--;

      /* shift remaining hooks... */
      for (; i < hooks; i++)
      {
         memcpy(&(hook[i]), &(hook[i + 1]), sizeof(VXHOOK));
      }

      result = OK;
   }
   else
   {
      printf("Hook for '%s' (symTblId=0x%08x) not found.\n", name, symTblId);
   }

   return (result);
}

要挂钩wvEvent(..)来电,您必须执行以下操作:

#include <wvLib.h>
#include <symLib.h>

STATUS (*WVEVENT_FPTR)(event_t, char *, size_t);

WVEVENT_FPTR orig_wvEvent = wvEvent;

STATUS my_wvEvent(event_t usrEventId,       /* event */
                  char *  buffer,           /* buffer */
                  size_t  bufSize)          /* buffer size */
{
    /* create a list of usrEventId... */

    return (orig_wvEvent(usrEventId, buffer, bufSize));
}

STATUS hookWvEvents(void)
{
    STATUS result = ERROR;

    result = vxHook(sysSymTbl, "wvEvent", my_wvEvent, &orig_wvEvent);

    return (result);
}

致电hookWvEvents后,您可以加载您的应用程序,并且您应用程序中对wvEvent的所有来电都将被重定向到my_wvEvent(然后传递给原始wvEvent功能)。请记住,即使您使用vxUnhook取消符号,也会为您的应用程序保持钩状符号。

注意:此机制对于应用程序测试和调试也非常有用,因为您可以通过强制系统调用失败来强调应用程序...