Subversion Repositories HelenOS

Rev

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

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