Subversion Repositories HelenOS

Rev

Rev 4377 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4377 Rev 4692
1
/*
1
/*
2
 * Copyright (c) 2005 Ondrej Palkovsky
2
 * Copyright (c) 2005 Ondrej Palkovsky
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup genericdebug
29
/** @addtogroup genericdebug
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 a 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
 *
50
 *
51
 * @return Zero on success or negative error code, ENOENT if not found,
51
 * @return Zero on success or negative error code, ENOENT if not found,
52
 *         ENOTSUP if symbol table not available.
52
 *         ENOTSUP if symbol table not available.
53
 *
53
 *
54
 */
54
 */
55
int symtab_name_lookup(unative_t addr, char **name)
55
int symtab_name_lookup(unative_t addr, char **name)
56
{
56
{
57
#ifdef CONFIG_SYMTAB
57
#ifdef CONFIG_SYMTAB
58
    count_t i;
58
    size_t i;
59
   
59
   
60
    for (i = 1; symbol_table[i].address_le; i++) {
60
    for (i = 1; symbol_table[i].address_le; i++) {
61
        if (addr < uint64_t_le2host(symbol_table[i].address_le))
61
        if (addr < uint64_t_le2host(symbol_table[i].address_le))
62
            break;
62
            break;
63
    }
63
    }
64
   
64
   
65
    if (addr >= uint64_t_le2host(symbol_table[i - 1].address_le)) {
65
    if (addr >= uint64_t_le2host(symbol_table[i - 1].address_le)) {
66
        *name = symbol_table[i - 1].symbol_name;
66
        *name = symbol_table[i - 1].symbol_name;
67
        return EOK;
67
        return EOK;
68
    }
68
    }
69
   
69
   
70
    *name = NULL;
70
    *name = NULL;
71
    return ENOENT;
71
    return ENOENT;
72
   
72
   
73
#else
73
#else
74
    *name = NULL;
74
    *name = NULL;
75
    return ENOTSUP;
75
    return ENOTSUP;
76
#endif
76
#endif
77
}
77
}
78
 
78
 
79
/** Lookup symbol by address and format for display.
79
/** Lookup symbol by address and format for display.
80
 *
80
 *
81
 * Returns name of closest corresponding symbol, "Not found" if none exists
81
 * Returns name of closest corresponding symbol, "Not found" if none exists
82
 * or "N/A" if no symbol information is available.
82
 * or "N/A" if no symbol information is available.
83
 *
83
 *
84
 * @param addr Address.
84
 * @param addr Address.
85
 * @param name Place to store pointer to the symbol name.
85
 * @param name Place to store pointer to the symbol name.
86
 *
86
 *
87
 * @return Pointer to a human-readable string.
87
 * @return Pointer to a human-readable string.
88
 *
88
 *
89
 */
89
 */
90
char *symtab_fmt_name_lookup(unative_t addr)
90
char *symtab_fmt_name_lookup(unative_t addr)
91
{
91
{
92
    char *name;
92
    char *name;
93
    int rc = symtab_name_lookup(addr, &name);
93
    int rc = symtab_name_lookup(addr, &name);
94
   
94
   
95
    switch (rc) {
95
    switch (rc) {
96
    case EOK:
96
    case EOK:
97
        return name;
97
        return name;
98
    case ENOENT:
98
    case ENOENT:
99
        return "Not found";
99
        return "Not found";
100
    default:
100
    default:
101
        return "N/A";
101
        return "N/A";
102
    }
102
    }
103
}
103
}
104
 
104
 
105
#ifdef CONFIG_SYMTAB
105
#ifdef CONFIG_SYMTAB
106
 
106
 
107
/** Find symbols that match the parameter forward and print them.
107
/** Find symbols that match the parameter forward and print them.
108
 *
108
 *
109
 * @param name     Search string
109
 * @param name     Search string
110
 * @param startpos Starting position, changes to found position
110
 * @param startpos Starting position, changes to found position
111
 *
111
 *
112
 * @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
 *
113
 *
114
 */
114
 */
115
static const char *symtab_search_one(const char *name, count_t *startpos)
115
static const char *symtab_search_one(const char *name, size_t *startpos)
116
{
116
{
117
    count_t namelen = str_length(name);
117
    size_t namelen = str_length(name);
118
   
118
   
119
    count_t pos;
119
    size_t pos;
120
    for (pos = *startpos; symbol_table[pos].address_le; pos++) {
120
    for (pos = *startpos; symbol_table[pos].address_le; pos++) {
121
        const char *curname = symbol_table[pos].symbol_name;
121
        const char *curname = symbol_table[pos].symbol_name;
122
       
122
       
123
        /* Find a ':' in curname */
123
        /* Find a ':' in curname */
124
        const char *colon = str_chr(curname, ':');
124
        const char *colon = str_chr(curname, ':');
125
        if (colon == NULL)
125
        if (colon == NULL)
126
            continue;
126
            continue;
127
       
127
       
128
        if (str_length(curname) < namelen)
128
        if (str_length(curname) < namelen)
129
            continue;
129
            continue;
130
       
130
       
131
        if (str_lcmp(name, curname, namelen) == 0) {
131
        if (str_lcmp(name, curname, namelen) == 0) {
132
            *startpos = pos;
132
            *startpos = pos;
133
            return (curname + str_lsize(curname, namelen));
133
            return (curname + str_lsize(curname, namelen));
134
        }
134
        }
135
    }
135
    }
136
   
136
   
137
    return NULL;
137
    return NULL;
138
}
138
}
139
 
139
 
140
#endif
140
#endif
141
 
141
 
142
/** Return address that corresponds to the entry.
142
/** Return address that corresponds to the entry.
143
 *
143
 *
144
 * Search symbol table, and if there is one match, return it
144
 * Search symbol table, and if there is one match, return it
145
 *
145
 *
146
 * @param name Name of the symbol
146
 * @param name Name of the symbol
147
 * @param addr Place to store symbol address
147
 * @param addr Place to store symbol address
148
 *
148
 *
149
 * @return Zero on success, ENOENT - not found, EOVERFLOW - duplicate
149
 * @return Zero on success, ENOENT - not found, EOVERFLOW - duplicate
150
 *         symbol, ENOTSUP - no symbol information available.
150
 *         symbol, ENOTSUP - no symbol information available.
151
 *
151
 *
152
 */
152
 */
153
int symtab_addr_lookup(const char *name, uintptr_t *addr)
153
int symtab_addr_lookup(const char *name, uintptr_t *addr)
154
{
154
{
155
#ifdef CONFIG_SYMTAB
155
#ifdef CONFIG_SYMTAB
156
    count_t found = 0;
156
    size_t found = 0;
157
    count_t pos = 0;
157
    size_t pos = 0;
158
    const char *hint;
158
    const char *hint;
159
   
159
   
160
    while ((hint = symtab_search_one(name, &pos))) {
160
    while ((hint = symtab_search_one(name, &pos))) {
161
        if (str_length(hint) == 0) {
161
        if (str_length(hint) == 0) {
162
            *addr = uint64_t_le2host(symbol_table[pos].address_le);
162
            *addr = uint64_t_le2host(symbol_table[pos].address_le);
163
            found++;
163
            found++;
164
        }
164
        }
165
        pos++;
165
        pos++;
166
    }
166
    }
167
   
167
   
168
    if (found > 1)
168
    if (found > 1)
169
        return EOVERFLOW;
169
        return EOVERFLOW;
170
   
170
   
171
    if (found < 1)
171
    if (found < 1)
172
        return ENOENT;
172
        return ENOENT;
173
   
173
   
174
    return EOK;
174
    return EOK;
175
   
175
   
176
#else
176
#else
177
    return ENOTSUP;
177
    return ENOTSUP;
178
#endif
178
#endif
179
}
179
}
180
 
180
 
181
/** Find symbols that match parameter and print them */
181
/** Find symbols that match parameter and print them */
182
void symtab_print_search(const char *name)
182
void symtab_print_search(const char *name)
183
{
183
{
184
#ifdef CONFIG_SYMTAB
184
#ifdef CONFIG_SYMTAB
185
    count_t pos = 0;
185
    size_t pos = 0;
186
    while (symtab_search_one(name, &pos)) {
186
    while (symtab_search_one(name, &pos)) {
187
        uintptr_t addr = uint64_t_le2host(symbol_table[pos].address_le);
187
        uintptr_t addr = uint64_t_le2host(symbol_table[pos].address_le);
188
        char *realname = symbol_table[pos].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
        pos++;
190
        pos++;
191
    }
191
    }
192
   
192
   
193
#else
193
#else
194
    printf("No symbol information available.\n");
194
    printf("No symbol information available.\n");
195
#endif
195
#endif
196
}
196
}
197
 
197
 
198
/** Symtab completion
198
/** Symtab completion
199
 *
199
 *
200
 * @param input Search string, completes to symbol name
200
 * @param input Search string, completes to symbol name
201
 * @param size  Input buffer size
201
 * @param size  Input buffer size
202
 *
202
 *
203
 * @return 0 - nothing found, 1 - success, >1 print duplicates
203
 * @return 0 - nothing found, 1 - success, >1 print duplicates
204
 *
204
 *
205
 */
205
 */
206
int symtab_compl(char *input, count_t size)
206
int symtab_compl(char *input, size_t size)
207
{
207
{
208
#ifdef CONFIG_SYMTAB
208
#ifdef CONFIG_SYMTAB
209
    const char *name = input;
209
    const char *name = input;
210
   
210
   
211
    /* Allow completion of pointers */
211
    /* Allow completion of pointers */
212
    if ((name[0] == '*') || (name[0] == '&'))
212
    if ((name[0] == '*') || (name[0] == '&'))
213
        name++;
213
        name++;
214
   
214
   
215
    /* Do not print all symbols */
215
    /* Do not print all symbols */
216
    if (str_length(name) == 0)
216
    if (str_length(name) == 0)
217
        return 0;
217
        return 0;
218
   
218
   
219
    count_t found = 0;
219
    size_t found = 0;
220
    count_t pos = 0;
220
    size_t pos = 0;
221
    const char *hint;
221
    const char *hint;
222
    char output[MAX_SYMBOL_NAME];
222
    char output[MAX_SYMBOL_NAME];
223
   
223
   
224
    output[0] = 0;
224
    output[0] = 0;
225
   
225
   
226
    while ((hint = symtab_search_one(name, &pos))) {
226
    while ((hint = symtab_search_one(name, &pos))) {
227
        if ((found == 0) || (str_length(output) > str_length(hint)))
227
        if ((found == 0) || (str_length(output) > str_length(hint)))
228
            str_cpy(output, MAX_SYMBOL_NAME, hint);
228
            str_cpy(output, MAX_SYMBOL_NAME, hint);
229
       
229
       
230
        pos++;
230
        pos++;
231
        found++;
231
        found++;
232
    }
232
    }
233
   
233
   
234
    if ((found > 1) && (str_length(output) != 0)) {
234
    if ((found > 1) && (str_length(output) != 0)) {
235
        printf("\n");
235
        printf("\n");
236
        pos = 0;
236
        pos = 0;
237
        while ((hint = symtab_search_one(name, &pos))) {
237
        while ((hint = symtab_search_one(name, &pos))) {
238
            printf("%s\n", symbol_table[pos].symbol_name);
238
            printf("%s\n", symbol_table[pos].symbol_name);
239
            pos++;
239
            pos++;
240
        }
240
        }
241
    }
241
    }
242
   
242
   
243
    if (found > 0)
243
    if (found > 0)
244
        str_cpy(input, size, output);
244
        str_cpy(input, size, output);
245
   
245
   
246
    return found;
246
    return found;
247
   
247
   
248
#else
248
#else
249
    return 0;
249
    return 0;
250
#endif
250
#endif
251
}
251
}
252
 
252
 
253
/** @}
253
/** @}
254
 */
254
 */
255
 
255