Subversion Repositories HelenOS

Rev

Rev 1963 | 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. /**
  30.  * @file    symtab.c
  31.  * @brief   Kernel symbol resolver.
  32.  */
  33.  
  34. #include <symtab.h>
  35. #include <typedefs.h>
  36. #include <arch/byteorder.h>
  37. #include <func.h>
  38. #include <print.h>
  39.  
  40. /** Return entry that seems most likely to correspond to argument.
  41.  *
  42.  * Return entry that seems most likely to correspond
  43.  * to address passed in the argument.
  44.  *
  45.  * @param addr Address.
  46.  *
  47.  * @return Pointer to respective symbol string on success, NULL otherwise.
  48.  */
  49. char * get_symtab_entry(__native addr)
  50. {
  51.     count_t i;
  52.  
  53.     for (i=1;symbol_table[i].address_le;++i) {
  54.         if (addr < __u64_le2host(symbol_table[i].address_le))
  55.             break;
  56.     }
  57.     if (addr >= __u64_le2host(symbol_table[i-1].address_le))
  58.         return symbol_table[i-1].symbol_name;
  59.     return NULL;
  60. }
  61.  
  62. /** Find symbols that match the parameter forward and print them.
  63.  *
  64.  * @param name - search string
  65.  * @param startpos - starting position, changes to found position
  66.  * @return Pointer to the part of string that should be completed or NULL
  67.  */
  68. static char * symtab_search_one(const char *name, int *startpos)
  69. {
  70.     int namelen = strlen(name);
  71.     char *curname;
  72.     int i,j;
  73.     int colonoffset = -1;
  74.  
  75.     for (i=0;name[i];i++)
  76.         if (name[i] == ':') {
  77.             colonoffset = i;
  78.             break;
  79.         }
  80.  
  81.     for (i=*startpos;symbol_table[i].address_le;++i) {
  82.         /* Find a ':' in name */
  83.         curname = symbol_table[i].symbol_name;
  84.         for (j=0; curname[j] && curname[j] != ':'; j++)
  85.             ;
  86.         if (!curname[j])
  87.             continue;
  88.         j -= colonoffset;
  89.         curname += j;
  90.         if (strlen(curname) < namelen)
  91.             continue;
  92.         if (strncmp(curname, name, namelen) == 0) {
  93.             *startpos = i;
  94.             return curname+namelen;
  95.         }
  96.     }
  97.     return NULL;
  98. }
  99.  
  100. /** Return address that corresponds to the entry
  101.  *
  102.  * Search symbol table, and if there is one match, return it
  103.  *
  104.  * @param name Name of the symbol
  105.  * @return 0 - Not found, -1 - Duplicate symbol, other - address of symbol
  106.  */
  107. __address get_symbol_addr(const char *name)
  108. {
  109.     count_t found = 0;
  110.     __address addr = NULL;
  111.     char *hint;
  112.     int i;
  113.  
  114.     i = 0;
  115.     while ((hint=symtab_search_one(name, &i))) {
  116.         if (!strlen(hint)) {
  117.             addr =  __u64_le2host(symbol_table[i].address_le);
  118.             found++;
  119.         }
  120.         i++;
  121.     }
  122.     if (found > 1)
  123.         return ((__address) -1);
  124.     return addr;
  125. }
  126.  
  127. /** Find symbols that match parameter and prints them */
  128. void symtab_print_search(const char *name)
  129. {
  130.     int i;
  131.     __address addr;
  132.     char *realname;
  133.  
  134.  
  135.     i = 0;
  136.     while (symtab_search_one(name, &i)) {
  137.         addr =  __u64_le2host(symbol_table[i].address_le);
  138.         realname = symbol_table[i].symbol_name;
  139.         printf("%.*p: %s\n", sizeof(__address) * 2, addr, realname);
  140.         i++;
  141.     }
  142. }
  143.  
  144. /** Symtab completion
  145.  *
  146.  * @param input - Search string, completes to symbol name
  147.  * @returns - 0 - nothing found, 1 - success, >1 print duplicates
  148.  */
  149. int symtab_compl(char *input)
  150. {
  151.     char output[MAX_SYMBOL_NAME+1];
  152.     int startpos = 0;
  153.     char *foundtxt;
  154.     int found = 0;
  155.     int i;
  156.     char *name = input;
  157.  
  158.     /* Allow completion of pointers  */
  159.     if (name[0] == '*' || name[0] == '&')
  160.         name++;
  161.  
  162.     /* Do not print everything */
  163.     if (!strlen(name))
  164.         return 0;
  165.    
  166.  
  167.     output[0] = '\0';
  168.  
  169.     while ((foundtxt = symtab_search_one(name, &startpos))) {
  170.         startpos++;
  171.         if (!found)
  172.             strncpy(output, foundtxt, strlen(foundtxt)+1);
  173.         else {
  174.             for (i=0; output[i] && foundtxt[i] && output[i]==foundtxt[i]; i++)
  175.                 ;
  176.             output[i] = '\0';
  177.         }
  178.         found++;
  179.     }
  180.     if (!found)
  181.         return 0;
  182.  
  183.     if (found > 1 && !strlen(output)) {
  184.         printf("\n");
  185.         startpos = 0;
  186.         while ((foundtxt = symtab_search_one(name, &startpos))) {
  187.             printf("%s\n", symbol_table[startpos].symbol_name);
  188.             startpos++;
  189.         }
  190.     }
  191.     strncpy(input, output, MAX_SYMBOL_NAME);
  192.     return found;
  193.    
  194. }
  195.