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
|
/*---------------------------------------------------------------------------*\
FILE........: modem_probe.c
AUTHOR......: Brady O'Brien
DATE CREATED: 9 January 2016
Library to easily extract debug traces from modems during development and
verification
\*---------------------------------------------------------------------------*/
/*
Copyright (C) 2016 David Rowe
All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 2.1, as
published by the Free Software Foundation. This program is
distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "comp.h"
#include "octave.h"
#define TRACE_I 1
#define TRACE_F 2
#define TRACE_C 3
typedef struct probe_trace_info_s probe_trace_info;
typedef struct datlink_s datlink;
struct datlink_s {
void *data;
size_t len;
datlink *next;
};
struct probe_trace_info_s {
int type;
char name[255];
datlink *data;
datlink *last;
probe_trace_info *next;
};
static char *run = NULL;
static char *mod = NULL;
static probe_trace_info *first_trace = NULL;
/* Init the probing library */
void modem_probe_init_int(char *modname, char *runname) {
mod = malloc((strlen(modname) + 1) * sizeof(char));
run = malloc((strlen(runname) + 1) * sizeof(char));
strcpy(run, runname);
strcpy(mod, modname);
}
/*
* Gather the data stored in the linked list into a single blob,
* freeing links and buffers as it goes
*/
void *gather_data(datlink *d, size_t *len) {
size_t size = 0;
datlink *cur = d;
datlink *next;
while (cur != NULL) {
size += d->len;
cur = cur->next;
}
cur = d;
size_t i = 0;
void *newbuf = malloc(size);
while (cur != NULL) {
memcpy(newbuf + i, cur->data, cur->len);
i += cur->len;
free(cur->data);
next = cur->next;
free(cur);
cur = next;
}
*len = size;
return newbuf;
}
/* Dump all of the traces into a nice octave-able dump file */
void modem_probe_close_int() {
if (run == NULL) return;
probe_trace_info *cur, *next;
cur = first_trace;
FILE *dumpfile = fopen(run, "w");
void *dbuf;
size_t len;
while (cur != NULL) {
dbuf = gather_data(cur->data, &len);
switch (cur->type) {
case TRACE_I:
octave_save_int(dumpfile, cur->name, (int32_t *)dbuf, 1,
len / sizeof(int32_t));
break;
case TRACE_F:
octave_save_float(dumpfile, cur->name, (float *)dbuf, 1,
len / sizeof(float), 10);
break;
case TRACE_C:
octave_save_complex(dumpfile, cur->name, (COMP *)dbuf, 1,
len / sizeof(COMP), 10);
break;
}
next = cur->next;
free(cur);
free(dbuf);
cur = next;
}
fclose(dumpfile);
free(run);
free(mod);
}
/* Look up or create a trace by name */
probe_trace_info *modem_probe_get_trace(char *tracename) {
probe_trace_info *cur, *npti;
/* Make sure probe session is open */
if (run == NULL) return NULL;
cur = first_trace;
/* Walk through list, find trace with matching name */
while (cur != NULL) {
/* We got one! */
if (strcmp(cur->name, tracename) == 0) {
return cur;
}
cur = cur->next;
}
/* None found, open a new trace */
npti = (probe_trace_info *)malloc(sizeof(probe_trace_info));
npti->next = first_trace;
npti->data = NULL;
npti->last = NULL;
strcpy(npti->name, tracename);
first_trace = npti;
return npti;
}
void modem_probe_samp_i_int(char *tracename, int32_t samp[], size_t cnt) {
probe_trace_info *pti;
datlink *ndat;
pti = modem_probe_get_trace(tracename);
if (pti == NULL) return;
pti->type = TRACE_I;
ndat = (datlink *)malloc(sizeof(datlink));
ndat->data = malloc(sizeof(int32_t) * cnt);
ndat->len = cnt * sizeof(int32_t);
ndat->next = NULL;
memcpy(ndat->data, (void *)&(samp[0]), sizeof(int32_t) * cnt);
if (pti->last != NULL) {
pti->last->next = ndat;
pti->last = ndat;
} else {
pti->data = ndat;
pti->last = ndat;
}
}
void modem_probe_samp_f_int(char *tracename, float samp[], size_t cnt) {
probe_trace_info *pti;
datlink *ndat;
pti = modem_probe_get_trace(tracename);
if (pti == NULL) return;
pti->type = TRACE_F;
ndat = (datlink *)malloc(sizeof(datlink));
ndat->data = malloc(sizeof(float) * cnt);
ndat->len = cnt * sizeof(float);
ndat->next = NULL;
memcpy(ndat->data, (void *)&(samp[0]), sizeof(float) * cnt);
if (pti->last != NULL) {
pti->last->next = ndat;
pti->last = ndat;
} else {
pti->data = ndat;
pti->last = ndat;
}
}
void modem_probe_samp_c_int(char *tracename, COMP samp[], size_t cnt) {
probe_trace_info *pti;
datlink *ndat;
pti = modem_probe_get_trace(tracename);
if (pti == NULL) return;
pti->type = TRACE_C;
ndat = (datlink *)malloc(sizeof(datlink));
ndat->data = malloc(sizeof(COMP) * cnt);
ndat->len = cnt * sizeof(COMP);
ndat->next = NULL;
memcpy(ndat->data, (void *)&(samp[0]), sizeof(COMP) * cnt);
if (pti->last != NULL) {
pti->last->next = ndat;
pti->last = ndat;
} else {
pti->data = ndat;
pti->last = ndat;
}
}
|