Subversion Repositories HelenOS-historic

Rev

Rev 1264 | Rev 1705 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (C) 2005 Ondrej Palkovsky
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29.  /** @defgroup debug Debug
  30.  * @ingroup kernel
  31.  * @{
  32.  * @}
  33.  */
  34.  
  35.  
  36.  /** @addtogroup genericdebug generic
  37.  * @ingroup debug
  38.  * @{
  39.  */
  40.  
  41. /**
  42.  * @file
  43.  * @brief   Kernel symbol resolver.
  44.  */
  45.  
  46. #include <symtab.h>
  47. #include <typedefs.h>
  48. #include <arch/byteorder.h>
  49. #include <func.h>
  50. #include <print.h>
  51.  
  52. /** Return entry that seems most likely to correspond to argument.
  53.  *
  54.  * Return entry that seems most likely to correspond
  55.  * to address passed in the argument.
  56.  *
  57.  * @param addr Address.
  58.  *
  59.  * @return Pointer to respective symbol string on success, NULL otherwise.
  60.  */
  61. char * get_symtab_entry(__native addr)
  62. {
  63.     count_t i;
  64.  
  65.     for (i=1;symbol_table[i].address_le;++i) {
  66.         if (addr < __u64_le2host(symbol_table[i].address_le))
  67.             break;
  68.     }
  69.     if (addr >= __u64_le2host(symbol_table[i-1].address_le))
  70.         return symbol_table[i-1].symbol_name;
  71.     return NULL;
  72. }
  73.  
  74. /** Find symbols that match the parameter forward and print them.
  75.  *
  76.  * @param name - search string
  77.  * @param startpos - starting position, changes to found position
  78.  * @return Pointer to the part of string that should be completed or NULL
  79.  */
  80. static char * symtab_search_one(const char *name, int *startpos)
  81. {
  82.     int namelen = strlen(name);
  83.     char *curname;
  84.     int i,j;
  85.     int colonoffset = -1;
  86.  
  87.     for (i=0;name[i];i++)
  88.         if (name[i] == ':') {
  89.             colonoffset = i;
  90.             break;
  91.         }
  92.  
  93.     for (i=*startpos;symbol_table[i].address_le;++i) {
  94.         /* Find a ':' in name */
  95.         curname = symbol_table[i].symbol_name;
  96.         for (j=0; curname[j] && curname[j] != ':'; j++)
  97.             ;
  98.         if (!curname[j])
  99.             continue;
  100.         j -= colonoffset;
  101.         curname += j;
  102.         if (strlen(curname) < namelen)
  103.             continue;
  104.         if (strncmp(curname, name, namelen) == 0) {
  105.             *startpos = i;
  106.             return curname+namelen;
  107.         }
  108.     }
  109.     return NULL;
  110. }
  111.  
  112. /** Return address that corresponds to the entry
  113.  *
  114.  * Search symbol table, and if there is one match, return it
  115.  *
  116.  * @param name Name of the symbol
  117.  * @return 0 - Not found, -1 - Duplicate symbol, other - address of symbol
  118.  */
  119. __address get_symbol_addr(const char *name)
  120. {
  121.     count_t found = 0;
  122.     __address addr = NULL;
  123.     char *hint;
  124.     int i;
  125.  
  126.     i = 0;
  127.     while ((hint=symtab_search_one(name, &i))) {
  128.         if (!strlen(hint)) {
  129.             addr =  __u64_le2host(symbol_table[i].address_le);
  130.             found++;
  131.         }
  132.         i++;
  133.     }
  134.     if (found > 1)
  135.         return ((__address) -1);
  136.     return addr;
  137. }
  138.  
  139. /** Find symbols that match parameter and prints them */
  140. void symtab_print_search(const char *name)
  141. {
  142.     int i;
  143.     __address addr;
  144.     char *realname;
  145.  
  146.  
  147.     i = 0;
  148.     while (symtab_search_one(name, &i)) {
  149.         addr =  __u64_le2host(symbol_table[i].address_le);
  150.         realname = symbol_table[i].symbol_name;
  151.         printf("%.*p: %s\n", sizeof(__address) * 2, addr, realname);
  152.         i++;
  153.     }
  154. }
  155.  
  156. /** Symtab completion
  157.  *
  158.  * @param input - Search string, completes to symbol name
  159.  * @returns - 0 - nothing found, 1 - success, >1 print duplicates
  160.  */
  161. int symtab_compl(char *input)
  162. {
  163.     char output[MAX_SYMBOL_NAME+1];
  164.     int startpos = 0;
  165.     char *foundtxt;
  166.     int found = 0;
  167.     int i;
  168.     char *name = input;
  169.  
  170.     /* Allow completion of pointers  */
  171.     if (name[0] == '*' || name[0] == '&')
  172.         name++;
  173.  
  174.     /* Do not print everything */
  175.     if (!strlen(name))
  176.         return 0;
  177.    
  178.  
  179.     output[0] = '\0';
  180.  
  181.     while ((foundtxt = symtab_search_one(name, &startpos))) {
  182.         startpos++;
  183.         if (!found)
  184.             strncpy(output, foundtxt, strlen(foundtxt)+1);
  185.         else {
  186.             for (i=0; output[i] && foundtxt[i] && output[i]==foundtxt[i]; i++)
  187.                 ;
  188.             output[i] = '\0';
  189.         }
  190.         found++;
  191.     }
  192.     if (!found)
  193.         return 0;
  194.  
  195.     if (found > 1 && !strlen(output)) {
  196.         printf("\n");
  197.         startpos = 0;
  198.         while ((foundtxt = symtab_search_one(name, &startpos))) {
  199.             printf("%s\n", symbol_table[startpos].symbol_name);
  200.             startpos++;
  201.         }
  202.     }
  203.     strncpy(input, output, MAX_SYMBOL_NAME);
  204.     return found;
  205.    
  206. }
  207.  
  208.  /** @}
  209.  */
  210.  
  211.