• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nasa / trick / 25459968639

06 May 2026 08:42PM UTC coverage: 55.916% (-0.8%) from 56.7%
25459968639

Pull #2011

github

web-flow
Merge f11412d5f into 7054e405e
Pull Request #2011: Single-file CI and code style adoption

14607 of 26123 relevant lines covered (55.92%)

466416.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.11
/trick_source/sim_services/CheckPointAgent/PythonPrint.cpp
1
#include <string>
2
#include <cstring>
3
#include <iostream>
4
#include <iomanip>
5
#include <sstream>
6
#include <stdlib.h>
7
#include <math.h>
8

9
#include "trick/PythonPrint.hh"
10
#include "trick/memorymanager_c_intf.h"
11
#include "trick/parameter_types.h"
12
#include "trick/bitfield_proto.h"
13
#include "trick/message_proto.h"
14
#include "trick/message_type.h"
15

16
// MEMBER FUNCTION
17
bool Trick::PythonPrint::input_perm_check(ATTRIBUTES * attr __attribute__((unused))) {
×
18
    return true ;
×
19
}
20

21
// MEMBER FUNCTION
22
bool Trick::PythonPrint::output_perm_check(ATTRIBUTES * attr __attribute__((unused))) {
20✔
23
    return true ;
20✔
24
}
25

26

27
// MEMBER FUNCTION
28
void Trick::PythonPrint::write_decl(std::ostream& chkpnt_os, ALLOC_INFO *info) {
×
29
    (void)chkpnt_os ;
30
    (void)info ;
31
    return ;
×
32
}
33

34
// MEMBER FUNCTION
35
void Trick::PythonPrint::assign_rvalue(std::ostream& chkpnt_os, void* address, ATTRIBUTES* attr, int curr_dim, int offset) {
20✔
36

37
    chkpnt_os << left_side_name() << " = ";
20✔
38
    write_rvalue(chkpnt_os, (void*)address, attr, curr_dim, offset, true, false, nullptr);
20✔
39
    chkpnt_os << ";" << std::endl;
20✔
40

41
    return ;
20✔
42
}
43

44
// MEMBER FUNCTION
45
int Trick::PythonPrint::restore( std::istream* checkpoint_stream) {
×
46
    (void)checkpoint_stream ;
47
    return 0 ;
×
48
}
49

50

51
// STATIC FUNCTION
52
static void write_quoted_str( std::ostream& os, const char* s, bool in_list) {
17✔
53
    int ii;
54
    int len = strlen(s);
17✔
55
    if ( in_list ) { os << "\"" ; }
17✔
56
    for (ii=0 ; ii<len ; ii++) {
125✔
57
        switch ((s)[ii]) {
108✔
58
        case '\n': os << "\\n"; break;
×
59
        case '\t': os << "\\t"; break;
×
60
        case '\b': os << "\\b"; break;
×
61
        case '\"': os << "\\\""; break;
×
62
        default  : os << s[ii] ; break;
108✔
63
        }
64
    }
65
    if ( in_list ) { os << "\"" ; }
17✔
66
}
17✔
67

68
void Trick::PythonPrint::write_singleton( std::ostream& chkpnt_os, void* address,
4,786✔
69
 ATTRIBUTES* attr, int offset, bool write_units , bool in_list ) {
70

71
    void* src_addr;
72

73
    switch(attr->type) {
4,786✔
74
        case TRICK_UNSIGNED_CHARACTER:
66✔
75
            src_addr = (char*)address + offset * sizeof(unsigned char);
66✔
76
            chkpnt_os << std::dec << (int)*(unsigned char*)src_addr ;
66✔
77
        break;
66✔
78
        case TRICK_BOOLEAN:
213✔
79
            src_addr = (char*)address + offset * sizeof(bool);
213✔
80
            if (*(bool*)src_addr) {
213✔
81
                chkpnt_os << "True" ;
100✔
82
            } else {
83
                chkpnt_os << "False" ;
113✔
84
            }
85
        break;
213✔
86
        case TRICK_CHARACTER:
30✔
87
            src_addr = (char*)address + offset * sizeof(char);
30✔
88
            if (isprint( *(char*)src_addr) ) {
30✔
89
                chkpnt_os << "'" << *(char*)src_addr << "'" ;
4✔
90
            } else {
91
                //unsigned int ch = *(unsigned char*)src_addr;
92
                //chkpnt_os << "'\\x" << hex << ch << "'" ;
93
                int ch = *(char*)src_addr;
26✔
94
                chkpnt_os << ch ;
26✔
95
            }
96
        break;
30✔
97
        case TRICK_WCHAR: {
×
98
            src_addr = (char*)address + offset * sizeof(wchar_t);
×
99
            char buff[16] = {0};
×
100
            wctomb(buff,*(wchar_t*)src_addr);
×
101
            chkpnt_os << std::dec << buff;
×
102
            }
103
            break;
×
104
        case TRICK_SHORT:
218✔
105
            src_addr = (char*)address + offset * sizeof(short);
218✔
106
            chkpnt_os << std::dec << *(short*)src_addr;
218✔
107
            break;
218✔
108
        case TRICK_UNSIGNED_SHORT:
218✔
109
            src_addr = (char*)address + offset * sizeof(unsigned short);
218✔
110
            chkpnt_os << std::dec << *(unsigned short*)src_addr;
218✔
111
            break;
218✔
112
        case TRICK_ENUMERATED: {
×
113
                int ii = 0;
×
114
                int found = 0;
×
115
                int value;
116
                ENUM_ATTR* enum_attr;
117

118
                if ((size_t)attr->size == sizeof(int)) {
×
119
                    src_addr = (char*)address + offset * sizeof(int);
×
120
                    value =  *(int*)src_addr;
×
121
                } else if ((size_t)attr->size == sizeof(short)) {
×
122
                    src_addr = (char*)address + offset * sizeof(short);
×
123
                    value =  *(short*)src_addr;
×
124
                } else if ((size_t)attr->size == sizeof(char)) {
×
125
                    src_addr = (char*)address + offset * sizeof(char);
×
126
                    ENUM_ATTR* eattr = (ENUM_ATTR*)attr->attr;
×
127
                    if (eattr != nullptr && (eattr[0].mods & 0x40000000)) {
×
128
                        value = (int)(unsigned char)*(char*)src_addr;
×
129
                    } else {
130
                        value = (int)*(char*)src_addr;
×
131
                    }
132
                } else {
133
                    std::cerr << __FUNCTION__ << " enumeration size error." << std::endl;
×
134
                    std::cerr.flush();
×
135
                    value = -1;
×
136
                }
137

138
                enum_attr = (ENUM_ATTR*)attr->attr;
×
139

140
                while ( !found && (enum_attr[ii].label[0] != '\0')) {
×
141
                   if (value == enum_attr[ii].value) {
×
142
                       chkpnt_os << enum_attr[ii].label;
×
143
                       found = 1;
×
144
                   }
145
                   ii++;
×
146
                }
147
                if (!found) {
×
148
                    chkpnt_os << std::dec << value;
×
149
                }
150

151
            } break;
×
152
        case TRICK_INTEGER:
376✔
153
            src_addr = (char*)address + offset * sizeof(int);
376✔
154
            chkpnt_os << std::dec << *(int*)src_addr;
376✔
155
            break;
376✔
156
        case TRICK_UNSIGNED_INTEGER:
218✔
157
            src_addr = (char*)address + offset * sizeof(unsigned int);
218✔
158
            chkpnt_os << std::dec << *(unsigned int*)src_addr;
218✔
159
            break;
218✔
160
        case TRICK_LONG:
218✔
161
            src_addr = (char*)address + offset * sizeof(long);
218✔
162
            chkpnt_os << std::dec << *(long*)src_addr;
218✔
163
            break;
218✔
164
        case TRICK_UNSIGNED_LONG:
218✔
165
            src_addr = (char*)address + offset * sizeof(unsigned long);
218✔
166
            chkpnt_os << std::dec << *(unsigned long*)src_addr;
218✔
167
            break;
218✔
168
        case TRICK_FLOAT:
225✔
169
            src_addr = (char*)address + offset * sizeof(float);
225✔
170
            if (fpclassify( *(float*)src_addr) != FP_NAN) {
225✔
171
                chkpnt_os << std::setprecision(8) << *(float*)src_addr;
225✔
172
            } else {
173
                chkpnt_os << "nan";
×
174
            }
175
            break;
225✔
176
        case TRICK_DOUBLE:
2,349✔
177
            src_addr = (char*)address + offset * sizeof(double);
2,349✔
178
            if (fpclassify(*(double*)src_addr) != FP_NAN) {
2,349✔
179
                chkpnt_os << std::setprecision(16) << *(double*)src_addr;
2,349✔
180
            } else {
181
                chkpnt_os << "nan";
×
182
            }
183
            break;
2,349✔
184
        case TRICK_BITFIELD: {
×
185
                int sbf = 0;
×
186
                src_addr = (char*)address + offset * (size_t)attr->size;
×
187
                if (attr->size == sizeof(int)) {
×
188
                     sbf = extract_bitfield_any( *(int*)src_addr, attr->size, attr->index[0].start, attr->index[0].size);
×
189
                } else if (attr->size == sizeof(short)) {
×
190
                     sbf = extract_bitfield_any( *(short*)src_addr, attr->size, attr->index[0].start, attr->index[0].size);
×
191
                } else if (attr->size == sizeof(char)) {
×
192
                     sbf = extract_bitfield_any( *(char*)src_addr, attr->size, attr->index[0].start, attr->index[0].size);
×
193
                } else {
194
                     message_publish(MSG_ERROR, "Checkpoint Agent INTERNAL ERROR:\n"
×
195
                                                "Unsupported bitfield size (%d) bytes.\n", attr->size) ;
196
                }
197
                chkpnt_os << std::dec << sbf;
×
198
            } break;
×
199
        case TRICK_UNSIGNED_BITFIELD: {
×
200
                int bf = 0;
×
201
                src_addr = (char*)address + offset * (size_t)attr->size;
×
202
                if (attr->size == sizeof(int)) {
×
203
                     bf = extract_unsigned_bitfield_any( *(unsigned int*)src_addr, attr->size, attr->index[0].start, attr->index[0].size);
×
204
                } else if (attr->size == sizeof(short)) {
×
205
                     bf = extract_unsigned_bitfield_any( *(unsigned short*)src_addr, attr->size, attr->index[0].start, attr->index[0].size);
×
206
                } else if (attr->size == sizeof(char)) {
×
207
                     bf = extract_unsigned_bitfield_any( *(unsigned char*)src_addr, attr->size, attr->index[0].start, attr->index[0].size);
×
208
                } else {
209
                     message_publish(MSG_ERROR, "Checkpoint Agent INTERNAL ERROR:\n"
×
210
                                                "Unsupported bitfield size (%d) bytes.\n", attr->size) ;
211
                }
212
                chkpnt_os << std::dec << bf;
×
213
            } break;
×
214
        case TRICK_LONG_LONG:
218✔
215
            src_addr = (char*)address + offset * sizeof(long long);
218✔
216
            chkpnt_os << std::dec << *(long long*)src_addr;
218✔
217
            break;
218✔
218
        case TRICK_UNSIGNED_LONG_LONG:
218✔
219
            src_addr = (char*)address + offset * sizeof(unsigned long long);
218✔
220
            chkpnt_os << std::dec << *(unsigned long long*)src_addr;
218✔
221
            break;
218✔
222
        case  TRICK_FILE_PTR:
×
223
            src_addr = (char*)address + offset * sizeof(void*);
×
224
            chkpnt_os << *(void**)src_addr;
×
225
            break;
×
226
        case TRICK_STRING:
1✔
227
            src_addr = (char*)address + offset * sizeof(std::string);
1✔
228
            write_quoted_str(chkpnt_os, (*(std::string*)src_addr).c_str(), in_list);
1✔
229
            break;
1✔
230
        case TRICK_OPAQUE_TYPE:
×
231
            chkpnt_os << "ERROR";
×
232
            break;
×
233
        default:
×
234
            chkpnt_os << "0";
×
235
            message_publish(MSG_ERROR, "Checkpoint Agent file %s: Unhandled Type (%d).\n", __FILE__, attr->type) ;
×
236
            break;
×
237
    }
238

239
    if ( write_units && attr->units != NULL && strcmp(attr->units, "1") ) {
4,786✔
240
            chkpnt_os << " " << attr->units ;
2,336✔
241
    }
242
}
4,786✔
243

244
// MEMBER FUNCTION
245
std::string Trick::PythonPrint::
126✔
246
    ref_string_from_ptr( void* pointer, ATTRIBUTES* attr, int curr_dim) {
247

248
    /** Notice that attr and curr_dim together specify the type. */
249

250
    std::string reference_string;
126✔
251

252
    if ((curr_dim >= attr->num_index) || (attr->index[curr_dim].size != 0)) {
126✔
253
        message_publish(MSG_ERROR, "Checkpoint Agent ERROR: ref_string_from_ptr called with a non-pointer type.\n") ;
×
254
    }
255

256
    if (pointer == NULL) {
126✔
257
        reference_string = "NULL";
124✔
258
    } else {
259
        ALLOC_INFO *alloc_info;
260

261
        /** Find the allocation that contains the pointer-address. */
262
        alloc_info = get_alloc_info_of( pointer);
2✔
263

264
        if (alloc_info != NULL) {
2✔
265
            std::stringstream workss;
2✔
266

267
            if ( *(void **)pointer == NULL ) {
2✔
268
                workss << "NULL" ;
×
269
            } else if (((curr_dim + 1) == attr->num_index) && (attr->type == TRICK_CHARACTER)) {
2✔
270
                workss << "\"" << *(char**)pointer << "\"" ;
1✔
271
            } else {
272
                workss << *(void **)pointer ;
1✔
273
            }
274

275
            reference_string = workss.str() ;
2✔
276

277
        } else if (((curr_dim + 1) == attr->num_index) && (attr->type == TRICK_CHARACTER)) {
2✔
278
            reference_string += (char*)pointer;
×
279
        } else if (((curr_dim + 1) == attr->num_index) && (attr->type == TRICK_WCHAR)) {
×
280
            message_publish(MSG_ERROR, "Checkpoint Agent INTERNAL ERROR: TRICK_WCHAR not fully supported yet.\n") ;
×
281
            reference_string = "NULL /* INTERNAL ERROR: TRICK_WCHAR not fully supported yet.*/";
×
282
        } else {
283
            message_publish(MSG_ERROR, "Checkpoint Agent ERROR: Pointer <%p> is not in Trick managed memory\n"
×
284
                                       "nor is it a character pointer.\n", pointer) ;
285
            reference_string = "NULL /*ERROR*/";
×
286
        }
287
    }
288
    return( reference_string);
126✔
289
}
×
290

291
// MEMBER FUNCTION
292
void Trick::PythonPrint::write_rvalue(std::ostream& chkpnt_os, void* address,
6,302✔
293
    ATTRIBUTES* attr, int curr_dim, int offset, bool write_units, bool in_list, int* startingIndex)
294
{
295

296
    // If the variable that we are pointing to is Un-arrayed
297
    if (curr_dim == attr->num_index) {
6,302✔
298

299
        write_singleton( chkpnt_os, address, attr, offset, write_units, in_list);
4,786✔
300

301
    // If the variable that we are pointing to is Arrayed
302
    } else if (curr_dim < attr->num_index) {
1,516✔
303

304
        // If the variable is a pointer
305
        if (attr->index[curr_dim].size == 0) {
1,516✔
306
            std::string ref_string;
126✔
307

308
            //void* pointer = *(void**)((char*)address + offset * sizeof(void*));
309

310
            void* pointer = (char*)address + offset * sizeof(void*);
126✔
311
            ref_string = ref_string_from_ptr( pointer, attr, curr_dim);
126✔
312

313
            chkpnt_os << ref_string.c_str() ;
126✔
314

315
        } else { // Fixed dimension
126✔
316

317
            char* src_addr;
318

319
            // If this is the final, fixed dimension
320
            if (((curr_dim + 1) == attr->num_index) ||
1,390✔
321
                (attr->index[curr_dim + 1].size == 0)) {
329✔
322

323
                int use_quoted_string;
324

325
                // If ALL but the last of the elements of a character array
326
                // "isprintable" AND the last is '\0' then print out the array
327
                // as a quoted string. Otherwise print out each of the characters.
328

329
                use_quoted_string = 1;
1,061✔
330
                if (attr->type == TRICK_CHARACTER) {
1,061✔
331
                    int ii = attr->index[curr_dim].size - 1;
20✔
332
                    src_addr = (char*)address + offset * attr->index[curr_dim].size;
20✔
333

334
                    if (src_addr[ii] != '\0' || src_addr[0] == '\0' ) {
20✔
335
                        use_quoted_string = 0;
4✔
336
                    }
337
                    ii--;
20✔
338
                    while ( use_quoted_string && (ii >= 0) ) {
192✔
339
                        use_quoted_string = isprint( src_addr[ii]) || src_addr[ii] == '\0' ;
172✔
340
                        ii--;
172✔
341
                    }
342
                }
343

344
                if ((attr->type == TRICK_CHARACTER) && use_quoted_string)  {
1,061✔
345
                    write_quoted_str(chkpnt_os, src_addr, in_list);
16✔
346
                } else {
347

348
                    int ii;
349
                    int array_len;
350

351
                    // Determine the number of array elements we need to print out
352
                    // to get all of the non-zero values.
353

354
                    array_len = attr->index[curr_dim].size ;
1,045✔
355
                    chkpnt_os << "[";
1,045✔
356

357
                    bool isFirstElemWritten = false;
1,045✔
358
                    for (ii = 0; ii < array_len ; ii++ ) {
5,905✔
359
                        if (startingIndex != nullptr && *startingIndex > 0)
4,860✔
360
                        {
361
                            --(*startingIndex);
92✔
362
                            continue;
92✔
363
                        }
364
                        if (isFirstElemWritten)
4,768✔
365
                        {
366
                            chkpnt_os << ", ";
3,723✔
367
                        }
368
                        else
369
                        {
370
                            isFirstElemWritten = true;
1,045✔
371
                        }
372
                        write_rvalue(chkpnt_os, address, attr, curr_dim + 1,
4,768✔
373
                            offset * attr->index[curr_dim].size + ii, write_units, true, startingIndex);
4,768✔
374
                    }
375
                    chkpnt_os << "]";
1,045✔
376

377
                }
378

379
            } else { // Not the final fixed dimension.
1,061✔
380

381
                int ii;
382

383
                chkpnt_os << "[";
329✔
384

385
                int elem_multiplier = 1;
329✔
386
                for (int jj = curr_dim + 1; jj < attr->num_index; ++jj)
750✔
387
                {
388
                    elem_multiplier *= attr->index[jj].size;
421✔
389
                }
390

391
                bool isFirstElemWritten = false;
329✔
392
                for (ii=0 ; ii< attr->index[curr_dim].size ; ii++) {
1,160✔
393
                    bool skipIndex = false;
831✔
394
                    if (startingIndex != nullptr)
831✔
395
                    {
396
                        int nextIterStartingIndex = *startingIndex - elem_multiplier;
831✔
397
                        if (nextIterStartingIndex >= 0)
831✔
398
                        {
399
                            skipIndex = true;
78✔
400
                            *startingIndex = nextIterStartingIndex;
78✔
401
                        }
402
                    }
403
                    if (!skipIndex)
831✔
404
                    {
405
                        if (isFirstElemWritten)
753✔
406
                        {
407
                            chkpnt_os << ",";
424✔
408
                        }
409
                        else
410
                        {
411
                            isFirstElemWritten = true;
329✔
412
                        }
413
                        write_rvalue(chkpnt_os, address, attr, curr_dim + 1,
753✔
414
                            offset * attr->index[curr_dim].size + ii, write_units, true, startingIndex);
753✔
415
                    }
416
                }
417

418
                chkpnt_os << "]";
329✔
419
            }
420
        }
421

422
    } else {
423
        chkpnt_os << "/*ERROR*/";
×
424

425
        message_publish(MSG_ERROR, "Checkpoint Agent ERROR: The specified current dimension \"%d\" is greater\n"
×
426
                                   "than the number of dimensions specified in the type ATTRIBUTES.\n", curr_dim) ;
427

428
        return;
×
429
    }
430
}
431

432
std::string Trick::PythonPrint::left_side_name() {
20✔
433

434
    std::string name ;
20✔
435
    int ii;
436
    int n_elements = leftside_stack.size();
20✔
437

438
    for (ii = 0; ii < n_elements ; ii++) {
40✔
439

440
        VarNameElement & element = leftside_stack[ii];
20✔
441

442
        switch( element.type) {
20✔
443

444
            case BASE_NAME: {
×
445
                name = element.name;
×
446
            } break;
×
447

448
            case ELEM_NAME: {
20✔
449
                name += '.' + element.name;
20✔
450
            } break;
20✔
451

452
            case ARRAY_INDEX: {
×
453
                std::stringstream index_string;
×
454
                index_string << element.index;
×
455
                name += '[';
×
456
                name += index_string.str();
×
457
                name += ']';
×
458
            } break;
×
459

460
            default: {
×
461
                message_publish(MSG_ERROR, "Checkpoint Agent ERROR: BADNESS!!\n") ;
×
462
            }
463
        }
464
    }
465
    return( name);
20✔
466
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc