File: adb_db.cpp

package info (click to toggle)
mstflint 4.31.0%2B1-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 37,200 kB
  • sloc: ansic: 205,509; cpp: 103,810; ada: 15,143; sh: 11,406; python: 8,039; makefile: 1,413
file content (511 lines) | stat: -rw-r--r-- 13,600 bytes parent folder | download
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
 *
 * Copyright (c) 2010-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 *  Version: $Id$
 *
 *  Author: Wasim Abu Moch (wasim@mellanox.co.il)
 */

#include <vector>
#include <stdio.h>
#include <string>
#include "common/tools_algorithm.h"
#include "adb_db.h"

#define CHECK_FIELD(field, node_w)                                                  \
    if (((AdbInstance*)field)->isReserved())                                        \
    {                                                                               \
        continue;                                                                   \
    }                                                                               \
    offset = db_field_offset(field);                                                \
    if (offset - node_w->node->offset < from || offset - node_w->node->offset > to) \
    {                                                                               \
        continue;                                                                   \
    }

struct node_wrapper
{
    AdbInstance* node;
    vector<AdbInstance*>* fields;
};

static char err[1024] = {0};

/**
 * db_create
 */
adb_db_t* db_create()
{
    return new Adb();
}

/**
 * db_load
 */
int db_load(adb_db_t* db, const char* adb_file_path, int add_reserved)
{
    if (!((Adb*)db)->load(adb_file_path, add_reserved, true, false))
    {
        sprintf(err, "Failed to load adabe project: %s", ((Adb*)db)->getLastError().c_str());
        return 1;
    }

    return 0;
}

/**
 * db_load_from_str
 */
int db_load_from_str(adb_db_t* db, const char* adb_data, int add_reserved)
{
    if (!((Adb*)db)->loadFromString(adb_data, add_reserved, true, false))
    {
        sprintf(err, "Failed to load adabe project: %s", ((Adb*)db)->getLastError().c_str());
        return 1;
    }

    return 0;
}

/**
 * db_destroy
 */
void db_destroy(adb_db_t* db)
{
    if (db)
    {
        delete (Adb*)db;
    }
}

/**
 * db_destroy
 */
const char* db_get_last_err()
{
    return err;
}

adb_limits_map_t* db_create_limits_map(adb_db_t* db)
{
    map<string, string>* limits = new map<string, string>();

    // Load defines
    if (db)
    {
        for (size_t i = 0; i < ((Adb*)db)->configs.size(); i++)
        {
            AttrsMap::iterator attrs_map = ((Adb*)db)->configs[i]->attrs.find("define");
            if (attrs_map != ((Adb*)db)->configs[i]->attrs.end())
            {
                vector<string> defVal;
                mstflint::common::algorithm::split(defVal, attrs_map->second, mstflint::common::algorithm::is_any_of(string("=")));

                if (defVal.size() == 1)
                {
                    ((map<string, string>*)limits)->insert(pair<string, string>(defVal[0], "0"));
                }
                else
                {
                    ((map<string, string>*)limits)->insert(pair<string, string>(defVal[0], defVal[1]));
                }
            }
        }
    }
    return (adb_limits_map_t*)limits;
}

void db_destroy_limits_map(adb_limits_map_t* limits)
{
    if (limits)
    {
        ((map<string, string>*)limits)->clear();
        delete (map<string, string>*)limits;
    }
}

/**
 * db_get_node
 */
adb_node_t* db_get_node(adb_db_t* db, const char* node_name)
{
    Adb* adb = (Adb*)db;
    AdbInstance* node = adb->createLayout(node_name, false);
    if (!node)
    {
        sprintf(err, "Failed to create node %s: %s", node_name, adb->getLastError().c_str());
        return NULL;
    }
    struct node_wrapper* node_w = (struct node_wrapper*)malloc(sizeof(struct node_wrapper));
    if (!node_w)
    {
        delete node;
        sprintf(err, "Failed to allocate memory for node");
        return NULL;
    }
    memset(node_w, 0, sizeof(*node_w));
    node_w->node = node;
    node_w->fields = new vector<AdbInstance*>;
    *node_w->fields = node->getLeafFields(false);
    return node_w;
}

/**
 * db_node_destroy
 */
void db_node_destroy(adb_node_t* node)
{
    struct node_wrapper* node_w = (struct node_wrapper*)node;
    if (node_w)
    {
        if (node_w->node)
        {
            delete node_w->node;
        }
        if (node_w->fields)
        {
            delete node_w->fields;
        }
        free(node_w);
    }
}

/**
 * db_node_name
 */
void db_node_name(adb_node_t* node, char name[])
{
    struct node_wrapper* node_w = (struct node_wrapper*)node;
    strcpy(name, node_w->node->get_field_name().c_str());
}

/**
 * db_node_size
 */
int db_node_size(adb_node_t* node)
{
    struct node_wrapper* node_w = (struct node_wrapper*)node;
    return node_w->node->size;
}

/**
 * db_node_num_of_fields
 */
int db_node_num_of_fields(adb_node_t* node)
{
    struct node_wrapper* node_w = (struct node_wrapper*)node;
    return (int)node_w->fields->size();
}

/**
 * db_node_get_field
 */
adb_field_t* db_node_get_field(adb_node_t* node, int field_idx)
{
    if (field_idx >= db_node_num_of_fields(node))
    {
        sprintf(err, "index out of range");
        return NULL;
    }

    struct node_wrapper* node_w = (struct node_wrapper*)node;
    return node_w->fields->at(field_idx);
}

/**
 * db_node_get_field_by_path
 */
adb_field_t* db_node_get_field_by_path(adb_node_t* node, const char* path, int is_case_sensitive)
{
    struct node_wrapper* node_w = (struct node_wrapper*)node;
    string fullPath(path);

    if (node_w->node->isConditionalNode())
    {
        fullPath += ".val";
    }
    for (size_t i = 0; i < node_w->fields->size(); i++)
    {
        if (is_case_sensitive)
        {
            if (node_w->fields->at(i)->fullName(1) == fullPath)
            {
                return node_w->fields->at(i);
            }
        }
        else
        {
            string field_name_lower = mstflint::common::algorithm::to_lower_copy(node_w->fields->at(i)->fullName(1));
            string path_lower = mstflint::common::algorithm::to_lower_copy(fullPath);

            if (path_lower == field_name_lower)
            {
                return node_w->fields->at(i);
            }
        }
    }

    sprintf(err, "Can't find field (%s)", path);
    return NULL;
}

/**
 * db_node_dump
 * Returns: false on error
 */
bool db_node_dump(adb_node_t* node, u_int8_t buf[], dump_format_t format, FILE* stream)
{
    struct node_wrapper* node_w = (struct node_wrapper*)node;
    return db_node_range_dump(node, 0, node_w->node->size - 1, buf, format, stream);
}

/**
 * db_node_dump_with_limits
 * Returns: false on error
 */
bool db_node_conditional_dump(adb_node_t* node,
                              u_int8_t buf[],
                              dump_format_t format,
                              FILE* stream,
                              adb_limits_map_t* values_map)
{
    struct node_wrapper* node_w = (struct node_wrapper*)node;
    return db_node_conditional_range_dump(node, 0, node_w->node->size - 1, buf, format, stream, values_map);
}

/*
 * db_node_range_dump
 * Returns: false on error
 */
bool db_node_range_dump(adb_node_t* node,
                        u_int32_t from,
                        u_int32_t to,
                        u_int8_t buf[],
                        dump_format_t format,
                        FILE* stream)
{
    return db_node_conditional_range_dump(node, from, to, buf, format, stream, NULL);
}

/*
 * db_node_range_dump_with_limits
 * Returns: false on error
 */
bool db_node_conditional_range_dump(adb_node_t* node,
                                    u_int32_t from,
                                    u_int32_t to,
                                    u_int8_t buf[],
                                    dump_format_t format,
                                    FILE* stream,
                                    adb_limits_map_t* values_map)
{
    int i;
    adb_field_t* field;
    char enum_str[256];
    char name[256];
    int is_enum;
    u_int32_t value;
    u_int32_t offset;
    u_int32_t size;
    struct node_wrapper* node_w = (struct node_wrapper*)node;
    if (values_map)
    {
        // Fill values map for conditions
        for (i = 0; i < db_node_num_of_fields(node); i++)
        {
            field = db_node_get_field(node, i);
            if (!field)
            {
                return false;
            }
            CHECK_FIELD(field, node_w);
            db_field_full_name(field, 0, name);
            value = db_field_value(field, (u_int8_t*)buf);
            if (node_w->node->isConditionalNode())
            {
                char* p = strstr(name, ".val");
                if (p != NULL)
                {
                    *p = '\0';
                }
            }
            ((map<string, string>*)values_map)->insert(pair<string, string>(name, to_string(value)));
        }
    }

    for (i = 0; i < db_node_num_of_fields(node); i++)
    {
        field = db_node_get_field(node, i);
        if (!field)
        {
            return false;
        }
        CHECK_FIELD(field, node_w);

        db_field_full_name(field, 0, name);
        value = db_field_value(field, (u_int8_t*)buf);
        if (node_w->node->isConditionalNode())
        {
            char* p = strstr(name, ".val");
            if (p != NULL)
            {
                *p = '\0';
            }
        }
        /*
         * Just if the node is Conditional
         */
        if (node_w->node->isConditionalNode() && values_map != NULL)
        {
            try
            {
                AdbInstance* parentField = node_w->node->subItems[i];
                if (!parentField->isConditionValid(((map<string, string>*)values_map)))
                {
                    ((map<string, string>*)values_map)->erase(name);
                    continue;
                }
            }
            catch (...)
            {
                continue;
            }
        }

        is_enum = db_field_enum(field, value, enum_str);
        size = db_field_size(field);
        if (stream != NULL)
        {
            switch (format)
            {
                case DB_FORMAT_STANDARD:
                    fprintf(stream, "%-40s : 0x%x %s\n", name, value, is_enum ? enum_str : "");
                    break;

                case DB_FORMAT_STANDARD_NO_ENUM:
                    fprintf(stream, "%-40s : 0x%x\n", name, value);
                    break;

                case DB_FORMAT_FULL_DETAILS:
                    fprintf(stream, "%-40s : 0x%x %s - address: 0x%x.%d:%d\n", name, value, is_enum ? enum_str : "",
                            offset / 32 * 4, offset % 32, size);
                    break;
            }
        }
    }
    return true;
}

/**
 * db_field_name
 */
void db_field_name(adb_field_t* field, char name[])
{
    strcpy(name, ((AdbInstance*)field)->get_field_name().c_str());
}

/**
 * db_field_full_name
 */
void db_field_full_name(adb_field_t* field, int skip_level, char name[])
{
    strcpy(name, ((AdbInstance*)field)->fullName(skip_level + 1).c_str());
}

/**
 * db_field_offset
 */
int db_field_offset(adb_field_t* field)
{
    return ((AdbInstance*)field)->offset;
}

/**
 * db_field_size
 */
int db_field_size(adb_field_t* field)
{
    return ((AdbInstance*)field)->size;
}

/**
 * db_field_value
 */
u_int64_t db_field_value(adb_field_t* field, u_int8_t buf[])
{
    //    cout << ((AdbInstance*)field)->offset / 8 << endl;
    //    for (unsigned i = 0; i < ((AdbInstance*)field)->offset / 8; i++) {
    //        printf ("%02x ", buf[i]);
    //    }
    return ((AdbInstance*)field)->popBuf(buf);
}

/**
 * db_field_set_value
 */
void db_field_set_value(adb_field_t* field, u_int8_t buf[], u_int64_t value)
{
    ((AdbInstance*)field)->pushBuf(buf, value);
}

/**
 * db_field_enum
 */
int db_field_enum(adb_field_t* field, u_int64_t value, char enum_str[])
{
    string s;
    if (((AdbInstance*)field)->intToEnum(value, s))
    {
        strcpy(enum_str, s.c_str());
        return 1;
    }
    else
    {
        strcpy(enum_str, "");
        return 0;
    }
}

/**
 * db_print_nodes
 */
void db_print_nodes(adb_db_t* db)
{
    int i = 0;

    printf("DB nodes list:\n");
    for (NodesMap::iterator iter = ((Adb*)db)->nodesMap.begin(); iter != ((Adb*)db)->nodesMap.end(); iter++)
    {
        i++;
        printf("%-5d) %s\n", i, iter->first.c_str());
    }
}