Subversion Repositories HelenOS

Rev

Rev 3424 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3424 Rev 4377
Line 30... Line 30...
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file
34
 * @file
35
 * @brief   Kernel symbol resolver.
35
 * @brief Kernel symbol resolver.
36
 */
36
 */
37
 
37
 
38
#include <symtab.h>
38
#include <symtab.h>
39
#include <byteorder.h>
39
#include <byteorder.h>
40
#include <func.h>
40
#include <string.h>
41
#include <print.h>
41
#include <print.h>
42
#include <arch/types.h>
42
#include <arch/types.h>
43
#include <typedefs.h>
43
#include <typedefs.h>
-
 
44
#include <errno.h>
44
 
45
 
45
/** Return entry that seems most likely to correspond to argument.
46
/** Get name of a symbol that seems most likely to correspond to address.
46
 *
-
 
47
 * Return entry that seems most likely to correspond
-
 
48
 * to address passed in the argument.
-
 
49
 *
47
 *
50
 * @param addr Address.
48
 * @param addr Address.
-
 
49
 * @param name Place to store pointer to the symbol name.
-
 
50
 *
-
 
51
 * @return Zero on success or negative error code, ENOENT if not found,
-
 
52
 *         ENOTSUP if symbol table not available.
51
 *
53
 *
52
 * @return Pointer to respective symbol string on success, NULL otherwise.
-
 
53
 */
54
 */
54
char * get_symtab_entry(unative_t addr)
55
int symtab_name_lookup(unative_t addr, char **name)
55
{
56
{
-
 
57
#ifdef CONFIG_SYMTAB
56
    count_t i;
58
    count_t i;
57
 
59
   
58
    for (i = 1; symbol_table[i].address_le; ++i) {
60
    for (i = 1; symbol_table[i].address_le; i++) {
59
        if (addr < uint64_t_le2host(symbol_table[i].address_le))
61
        if (addr < uint64_t_le2host(symbol_table[i].address_le))
60
            break;
62
            break;
61
    }
63
    }
-
 
64
   
62
    if (addr >= uint64_t_le2host(symbol_table[i - 1].address_le))
65
    if (addr >= uint64_t_le2host(symbol_table[i - 1].address_le)) {
63
        return symbol_table[i - 1].symbol_name;
66
        *name = symbol_table[i - 1].symbol_name;
-
 
67
        return EOK;
-
 
68
    }
-
 
69
   
-
 
70
    *name = NULL;
64
    return NULL;
71
    return ENOENT;
-
 
72
   
-
 
73
#else
-
 
74
    *name = NULL;
-
 
75
    return ENOTSUP;
-
 
76
#endif
65
}
77
}
66
 
78
 
67
/** Find symbols that match the parameter forward and print them.
79
/** Lookup symbol by address and format for display.
68
 *
80
 *
-
 
81
 * Returns name of closest corresponding symbol, "Not found" if none exists
-
 
82
 * or "N/A" if no symbol information is available.
-
 
83
 *
69
 * @param name - search string
84
 * @param addr Address.
70
 * @param startpos - starting position, changes to found position
85
 * @param name Place to store pointer to the symbol name.
-
 
86
 *
71
 * @return Pointer to the part of string that should be completed or NULL
87
 * @return Pointer to a human-readable string.
-
 
88
 *
72
 */
89
 */
73
static char * symtab_search_one(const char *name, int *startpos)
90
char *symtab_fmt_name_lookup(unative_t addr)
74
{
91
{
75
    unsigned int namelen = strlen(name);
-
 
76
    char *curname;
92
    char *name;
77
    int i, j;
-
 
78
    int colonoffset = -1;
93
    int rc = symtab_name_lookup(addr, &name);
79
 
94
   
-
 
95
    switch (rc) {
-
 
96
    case EOK:
80
    for (i = 0; name[i]; i++)
97
        return name;
81
        if (name[i] == ':') {
98
    case ENOENT:
82
            colonoffset = i;
99
        return "Not found";
83
            break;
100
    default:
-
 
101
        return "N/A";
84
        }
102
    }
-
 
103
}
-
 
104
 
-
 
105
#ifdef CONFIG_SYMTAB
85
 
106
 
-
 
107
/** Find symbols that match the parameter forward and print them.
-
 
108
 *
-
 
109
 * @param name     Search string
86
    for (i = *startpos; symbol_table[i].address_le; ++i) {
110
 * @param startpos Starting position, changes to found position
-
 
111
 *
-
 
112
 * @return Pointer to the part of string that should be completed or NULL.
-
 
113
 *
-
 
114
 */
-
 
115
static const char *symtab_search_one(const char *name, count_t *startpos)
-
 
116
{
87
        /* Find a ':' in name */
117
    count_t namelen = str_length(name);
-
 
118
   
-
 
119
    count_t pos;
88
        curname = symbol_table[i].symbol_name;
120
    for (pos = *startpos; symbol_table[pos].address_le; pos++) {
89
        for (j = 0; curname[j] && curname[j] != ':'; j++)
121
        const char *curname = symbol_table[pos].symbol_name;
90
            ;
122
       
-
 
123
        /* Find a ':' in curname */
-
 
124
        const char *colon = str_chr(curname, ':');
91
        if (!curname[j])
125
        if (colon == NULL)
92
            continue;
126
            continue;
93
        j -= colonoffset;
-
 
94
        curname += j;
127
       
95
        if (strlen(curname) < namelen)
128
        if (str_length(curname) < namelen)
96
            continue;
129
            continue;
-
 
130
       
97
        if (strncmp(curname, name, namelen) == 0) {
131
        if (str_lcmp(name, curname, namelen) == 0) {
98
            *startpos = i;
132
            *startpos = pos;
99
            return curname + namelen;
133
            return (curname + str_lsize(curname, namelen));
100
        }
134
        }
101
    }
135
    }
-
 
136
   
102
    return NULL;
137
    return NULL;
103
}
138
}
104
 
139
 
-
 
140
#endif
-
 
141
 
105
/** Return address that corresponds to the entry
142
/** Return address that corresponds to the entry.
106
 *
143
 *
107
 * Search symbol table, and if there is one match, return it
144
 * Search symbol table, and if there is one match, return it
108
 *
145
 *
109
 * @param name Name of the symbol
146
 * @param name Name of the symbol
-
 
147
 * @param addr Place to store symbol address
-
 
148
 *
110
 * @return 0 - Not found, -1 - Duplicate symbol, other - address of symbol
149
 * @return Zero on success, ENOENT - not found, EOVERFLOW - duplicate
-
 
150
 *         symbol, ENOTSUP - no symbol information available.
-
 
151
 *
111
 */
152
 */
112
uintptr_t get_symbol_addr(const char *name)
153
int symtab_addr_lookup(const char *name, uintptr_t *addr)
113
{
154
{
-
 
155
#ifdef CONFIG_SYMTAB
114
    count_t found = 0;
156
    count_t found = 0;
115
    uintptr_t addr = NULL;
157
    count_t pos = 0;
116
    char *hint;
158
    const char *hint;
117
    int i;
-
 
118
 
159
   
119
    i = 0;
-
 
120
    while ((hint = symtab_search_one(name, &i))) {
160
    while ((hint = symtab_search_one(name, &pos))) {
121
        if (!strlen(hint)) {
161
        if (str_length(hint) == 0) {
122
            addr =  uint64_t_le2host(symbol_table[i].address_le);
162
            *addr = uint64_t_le2host(symbol_table[pos].address_le);
123
            found++;
163
            found++;
124
        }
164
        }
125
        i++;
165
        pos++;
126
    }
166
    }
-
 
167
   
127
    if (found > 1)
168
    if (found > 1)
128
        return ((uintptr_t) -1);
169
        return EOVERFLOW;
-
 
170
   
-
 
171
    if (found < 1)
-
 
172
        return ENOENT;
-
 
173
   
129
    return addr;
174
    return EOK;
-
 
175
   
-
 
176
#else
-
 
177
    return ENOTSUP;
-
 
178
#endif
130
}
179
}
131
 
180
 
132
/** Find symbols that match parameter and prints them */
181
/** Find symbols that match parameter and print them */
133
void symtab_print_search(const char *name)
182
void symtab_print_search(const char *name)
134
{
183
{
135
    int i;
-
 
136
    uintptr_t addr;
-
 
137
    char *realname;
184
#ifdef CONFIG_SYMTAB
138
 
-
 
139
 
-
 
140
    i = 0;
185
    count_t pos = 0;
141
    while (symtab_search_one(name, &i)) {
186
    while (symtab_search_one(name, &pos)) {
142
        addr =  uint64_t_le2host(symbol_table[i].address_le);
187
        uintptr_t addr = uint64_t_le2host(symbol_table[pos].address_le);
143
        realname = symbol_table[i].symbol_name;
188
        char *realname = symbol_table[pos].symbol_name;
144
        printf("%p: %s\n", addr, realname);
189
        printf("%p: %s\n", addr, realname);
145
        i++;
190
        pos++;
146
    }
191
    }
-
 
192
   
-
 
193
#else
-
 
194
    printf("No symbol information available.\n");
-
 
195
#endif
147
}
196
}
148
 
197
 
149
/** Symtab completion
198
/** Symtab completion
150
 *
199
 *
151
 * @param input - Search string, completes to symbol name
200
 * @param input Search string, completes to symbol name
-
 
201
 * @param size  Input buffer size
-
 
202
 *
152
 * @returns - 0 - nothing found, 1 - success, >1 print duplicates
203
 * @return 0 - nothing found, 1 - success, >1 print duplicates
-
 
204
 *
153
 */
205
 */
154
int symtab_compl(char *input)
206
int symtab_compl(char *input, count_t size)
155
{
207
{
156
    char output[MAX_SYMBOL_NAME + 1];
-
 
157
    int startpos = 0;
208
#ifdef CONFIG_SYMTAB
158
    char *foundtxt;
-
 
159
    int found = 0;
-
 
160
    int i;
-
 
161
    char *name = input;
209
    const char *name = input;
162
 
210
   
163
    /* Allow completion of pointers  */
211
    /* Allow completion of pointers */
164
    if (name[0] == '*' || name[0] == '&')
212
    if ((name[0] == '*') || (name[0] == '&'))
165
        name++;
213
        name++;
166
 
214
   
167
    /* Do not print everything */
215
    /* Do not print all symbols */
168
    if (!strlen(name))
216
    if (str_length(name) == 0)
169
        return 0;
217
        return 0;
170
   
218
   
-
 
219
    count_t found = 0;
-
 
220
    count_t pos = 0;
-
 
221
    const char *hint;
-
 
222
    char output[MAX_SYMBOL_NAME];
171
 
223
   
172
    output[0] = '\0';
224
    output[0] = 0;
173
 
225
   
174
    while ((foundtxt = symtab_search_one(name, &startpos))) {
226
    while ((hint = symtab_search_one(name, &pos))) {
175
        startpos++;
-
 
176
        if (!found)
-
 
177
            strncpy(output, foundtxt, strlen(foundtxt) + 1);
-
 
178
        else {
-
 
179
            for (i = 0; output[i] && foundtxt[i] &&
227
        if ((found == 0) || (str_length(output) > str_length(hint)))
180
                 output[i] == foundtxt[i]; i++)
228
            str_cpy(output, MAX_SYMBOL_NAME, hint);
181
                ;
229
       
182
            output[i] = '\0';
-
 
183
        }
230
        pos++;
184
        found++;
231
        found++;
185
    }
232
    }
186
    if (!found)
-
 
187
        return 0;
-
 
188
 
233
   
189
    if (found > 1 && !strlen(output)) {
234
    if ((found > 1) && (str_length(output) != 0)) {
190
        printf("\n");
235
        printf("\n");
191
        startpos = 0;
236
        pos = 0;
192
        while ((foundtxt = symtab_search_one(name, &startpos))) {
237
        while ((hint = symtab_search_one(name, &pos))) {
193
            printf("%s\n", symbol_table[startpos].symbol_name);
238
            printf("%s\n", symbol_table[pos].symbol_name);
194
            startpos++;
239
            pos++;
195
        }
240
        }
196
    }
241
    }
-
 
242
   
-
 
243
    if (found > 0)
197
    strncpy(input, output, MAX_SYMBOL_NAME);
244
        str_cpy(input, size, output);
-
 
245
   
198
    return found;
246
    return found;
199
   
247
   
-
 
248
#else
-
 
249
    return 0;
-
 
250
#endif
200
}
251
}
201
 
252
 
202
/** @}
253
/** @}
203
 */
254
 */