summaryrefslogtreecommitdiffstats
path: root/src/usr/errl/plugins/symbols.C
blob: c79059ca492c7299459557af0d9f5266353b6d2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//  IBM_PROLOG_BEGIN_TAG
//  This is an automatically generated prolog.
//
//  $Source: src/usr/errl/plugins/symbols.C $
//
//  IBM CONFIDENTIAL
//
//  COPYRIGHT International Business Machines Corp. 2012
//
//  p1
//
//  Object Code Only (OCO) source materials
//  Licensed Internal Code Source Materials
//  IBM HostBoot Licensed Internal Code
//
//  The source code for this program is not published or other-
//  wise divested of its trade secrets, irrespective of what has
//  been deposited with the U.S. Copyright Office.
//
//  Origin: 30
//
//  IBM_PROLOG_END


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <vector>

#include "symbols.H"


/**
 *  @file symbols.C
 *
 *  @brief Read HB symbols file and provide a lookup mechanism.
 *
 */




//------------------------------------------------------------------------
// trim white space from end of s

static char * trim( char *s )
{
  char *p = s;

  /* bump to last char */
  while( *p ) p++;
  p--;

  /* trim */
  while( p >= s && ( *p == ' ' || *p == '\n' || *p == '\r' || *p == '\t' )) *p-- = 0;

  /* return what was passed in */
  return s;
}



//------------------------------------------------------------------------
// hbSymbolTable methods

// Read the syms file, return zero for success.
int hbSymbolTable::readSymbols( const char * i_filename )
{
    FILE * f = NULL;

    iv_vecSymbols.clear();
    iv_fPopulated = false;

    delete[] iv_pFileName;
    iv_pFileName = NULL;

    f = fopen( i_filename,  "r" );
    if( !f )
    {
      return 2;
    }
    fclose(f);

    int cb = 1 + strlen( i_filename );
    iv_pFileName = new char[cb];
    strcpy( iv_pFileName, i_filename );

    populateSymbolVector();

    return 0;
}




// private method
// read the syms file, return 0 if OK
int hbSymbolTable::populateSymbolVector()
{
    FILE * f = NULL;
    char szWork[ 1024 ];

    if( NULL == iv_pFileName )
    {
        return 2;
    }

    f = fopen( iv_pFileName,  "r" );
    if( !f )
    {
        return 2;
    }

    memset(szWork, 0, sizeof(szWork));

    while( fgets( szWork, sizeof( szWork )-1, f ))
    {
        // function symbols only
        if( 'F' == szWork[0] )
        {
            hbSymbol* l_pSymbol = new hbSymbol();

            int k = 0;
            char * pch;
            pch = strtok( szWork, "," );
            while( pch )
            {
                switch( k )
                {
                    case 0:
                        l_pSymbol->setType( *pch );
                        break;
                    case 1:
                        l_pSymbol->setAddress( pch );
                        break;
                    case 3:
                        l_pSymbol->setLength( pch );
                        break;
                    case 4:
                        l_pSymbol->setSymbolName( pch );
                        break;
                    default:
                        // skipping field 2 for now
                        break;
                }
                k++;
                pch = strtok( NULL, "," );
            }

            if( l_pSymbol->isValid() )
            {
                iv_vecSymbols.push_back( l_pSymbol );
            }
            else
            {
                delete l_pSymbol;
            }
        }
    }

    fclose(f);

    // The Hostboot symbols file is pretty much sorted already, but
    // ensure vector is sorted for the sake of binary searching.

    int c = iv_vecSymbols.size() - 1;
    bool fSorted = (iv_vecSymbols.size() <= 1);
    while( !fSorted )
    {
        fSorted = true;
        for ( int i = 0; i < c; i++ )
        {
            if( iv_vecSymbols[i]->iv_Address > iv_vecSymbols[i+1]->iv_Address )
            {
                fSorted = false;
                // swap them
                hbSymbol * l_tempSymbol;
                l_tempSymbol       = iv_vecSymbols[i];
                iv_vecSymbols[i]   = iv_vecSymbols[i+1];
                iv_vecSymbols[i+1] = l_tempSymbol;
            }
        }
    }

    iv_fPopulated = true;
    return 0;
}



// private method
// Given the address, find the vector index of the symbol.
// Return -1 if not found.
// Return 0 for exact match.
// Return 1 for nearest (previous) symbol
int hbSymbolTable::locateSymbol( uint64_t i_address, int &o_index )
{
    int rc = -1;
    int top, bot, mid, i;
    int count = iv_vecSymbols.size();

    if( 0 == count )
    {
      return -1;
    }

    if( 1 == count )
    {
      return 1;
    }


    top = count - 1;
    bot = 0;

    while( top >= bot )
    {
        mid = (top + bot) / 2;

        uint64_t l_midAddress = iv_vecSymbols[mid]->iv_Address;

        if( i_address >  l_midAddress )
        {
            /* input address >  symtable address */
            bot = mid + 1;
        }
        else if( i_address <  l_midAddress )
        {
            /* input address <  symtable address */
            top = mid - 1;
        }
        else
        {
            /* exact match */
            o_index = mid;
            return 0;
        }
    }


    /*  The binary search above rarely returns with mid pointing to
     *  the right symbol, so back up a couple of indices and bump along
     *  until we find the right symbol.
     */

    bot = mid - 2;
    if( bot < 0 )
    {
        bot = 0;
    }

    top = mid + 1;
    if( top > count )
    {
        top = count;
    }


    for( i = bot; i < top; i++ )
    {
        if(  ( i_address >= iv_vecSymbols[i]->iv_Address ) &&
             ( i_address <  ( iv_vecSymbols[i]->iv_Address + iv_vecSymbols[i]->iv_Length )))
        {
            // this is the one
            o_index = i;

            // nearest symbol found
            rc = 1;
            break;
        }
    }
    return rc;
}





// construtor
hbSymbolTable::hbSymbolTable()
{
    iv_pFileName = NULL;
    iv_fPopulated = 0;
}


hbSymbolTable::~hbSymbolTable()
{
    int c = iv_vecSymbols.size();
    for( int i = 0; i < c; i++ )
    {
        delete iv_vecSymbols[i];
    }
    delete[] iv_pFileName;
}


// public method
char * hbSymbolTable::nearestSymbol( uint64_t  i_address )
{
    // search
    int l_index = 0;
    int rc = locateSymbol( i_address, l_index );
    if( rc < 0 )
    {
        // not found
        return NULL;
    }
    return iv_vecSymbols[l_index]->iv_pszName;
}





//------------------------------------------------------------------------
// hbSymbol methods


hbSymbol::hbSymbol()
{
    iv_validationBits = 0;
    iv_Type           = 0;
    iv_Address        = 0;
    iv_Length         = 0;
    iv_pszName        = NULL;
}

void hbSymbol::setAddress( const char * i_pszAddress )
{
    sscanf( i_pszAddress, "%llX", &iv_Address );
    iv_validationBits |= ADDRESS;
}

void hbSymbol::setLength( const char * i_pszLength )
{
    sscanf( i_pszLength,  "%llX", &iv_Length  );
    iv_validationBits |= LENGTH;
}

void hbSymbol::setType( int i_type )
{
    iv_Type = i_type;
    iv_validationBits |= TYPE;
}

void hbSymbol::setSymbolName( char * i_pszName )
{
    trim( i_pszName );

    delete[] iv_pszName;
    int cb = strlen( i_pszName ) + 1;
    iv_pszName =  new char[cb];
    strcpy( iv_pszName, i_pszName );
    if( cb > 1 )
    {
        iv_validationBits |= NAME;
    }
}

bool hbSymbol::isValid()
{
    // ensure somebody called to set address, length,
    // name of symbol, and type.  This would indicate the
    // parsing of the symbols input line went OK.
    return ((TYPE|ADDRESS|LENGTH|NAME)==(iv_validationBits));
}


hbSymbol::~hbSymbol()
{
    delete[] iv_pszName;
}




OpenPOWER on IntegriCloud