altomaltes / cross / slibtool

Forked from cross/slibtool 2 years ago
Clone

c58607 heuristics: added (annotated) trace output.

Authored and Committed by midipix 4 years ago
    heuristics: added (annotated) trace output.
    
        
src/internal/slibtool_lconf_impl.c CHANGED
@@ -24,16 +24,345 @@ enum slbt_lconf_opt {
24
24
SLBT_LCONF_OPT_YES,
25
25
};
26
26
27
+ static const char aclr_reset[] = "\x1b[0m";
28
+ static const char aclr_bold[] = "\x1b[1m";
29
+
30
+ static const char aclr_red[] = "\x1b[31m";
31
+ static const char aclr_green[] = "\x1b[32m";
32
+ static const char aclr_yellow[] = "\x1b[33m";
33
+ static const char aclr_blue[] = "\x1b[34m";
34
+ static const char aclr_magenta[] = "\x1b[35m";
35
+
27
36
static void slbt_lconf_close(int fdcwd, int fdlconfdir)
28
37
{
29
38
if (fdlconfdir != fdcwd)
30
39
close(fdlconfdir);
31
40
}
32
41
42
+ static int slbt_lconf_trace_lconf_plain(
43
+ struct slbt_driver_ctx * dctx,
44
+ const char * lconf)
45
+ {
46
+ int fderr = slbt_driver_fderr(dctx);
47
+
48
+ if (slbt_dprintf(
49
+ fderr,
50
+ "%s: %s: {.name=%c%s%c}.\n",
51
+ dctx->program,
52
+ "lconf",
53
+ '"',lconf,'"') < 0)
54
+ return -1;
55
+
56
+ return 0;
57
+ }
58
+
59
+ static int slbt_lconf_trace_lconf_annotated(
60
+ struct slbt_driver_ctx * dctx,
61
+ const char * lconf)
62
+ {
63
+ int fderr = slbt_driver_fderr(dctx);
64
+
65
+ if (slbt_dprintf(
66
+ fderr,
67
+ "%s%s%s%s: %s%s%s: {.name=%s%s%c%s%c%s}.\n",
68
+
69
+ aclr_bold,aclr_magenta,
70
+ dctx->program,
71
+ aclr_reset,
72
+
73
+ aclr_bold,
74
+ "lconf",
75
+ aclr_reset,
76
+
77
+ aclr_bold,aclr_green,
78
+ '"',lconf,'"',
79
+ aclr_reset) < 0)
80
+ return -1;
81
+
82
+ return 0;
83
+ }
84
+
85
+ static int slbt_lconf_trace_openat_silent(
86
+ struct slbt_driver_ctx * dctx,
87
+ int fdat,
88
+ const char * path,
89
+ int oflag,
90
+ int mode)
91
+ {
92
+ (void)dctx;
93
+ return openat(fdat,path,oflag,mode);
94
+ }
95
+
96
+ static int slbt_lconf_trace_openat_plain(
97
+ struct slbt_driver_ctx * dctx,
98
+ int fdat,
99
+ const char * path,
100
+ int oflag,
101
+ int mode)
102
+ {
103
+ char scwd[20];
104
+ char serr[512];
105
+
106
+ int ret = openat(fdat,path,oflag,mode);
107
+ int fderr = slbt_driver_fderr(dctx);
108
+
109
+ if (fdat == AT_FDCWD) {
110
+ strcpy(scwd,"AT_FDCWD");
111
+ } else {
112
+ sprintf(scwd,"%d",fdat);
113
+ }
114
+
115
+ if ((ret < 0) && (errno == ENOENT)) {
116
+ strcpy(serr," [ENOENT]");
117
+ } else if (ret < 0) {
118
+ memset(serr,0,sizeof(serr));
119
+ strerror_r(errno,&serr[2],sizeof(serr)-4);
120
+ serr[0] = ' ';
121
+ serr[1] = '(';
122
+ serr[strlen(serr)] = ')';
123
+ } else {
124
+ serr[0] = 0;
125
+ }
126
+
127
+ slbt_dprintf(
128
+ fderr,
129
+ "%s: %s: openat(%s,%c%s%c,%s,%d) = %d%s.\n",
130
+ dctx->program,
131
+ "lconf",
132
+ scwd,
133
+ '"',path,'"',
134
+ (oflag == O_DIRECTORY) ? "O_DIRECTORY" : "O_RDONLY",
135
+ mode,ret,serr);
136
+
137
+ return ret;
138
+ }
139
+
140
+ static int slbt_lconf_trace_openat_annotated(
141
+ struct slbt_driver_ctx * dctx,
142
+ int fdat,
143
+ const char * path,
144
+ int oflag,
145
+ int mode)
146
+ {
147
+ char scwd[20];
148
+ char serr[512];
149
+
150
+ int ret = openat(fdat,path,oflag,mode);
151
+ int fderr = slbt_driver_fderr(dctx);
152
+
153
+ if (fdat == AT_FDCWD) {
154
+ strcpy(scwd,"AT_FDCWD");
155
+ } else {
156
+ sprintf(scwd,"%d",fdat);
157
+ }
158
+
159
+ if ((ret < 0) && (errno == ENOENT)) {
160
+ strcpy(serr," [ENOENT]");
161
+ } else if (ret < 0) {
162
+ memset(serr,0,sizeof(serr));
163
+ strerror_r(errno,&serr[2],sizeof(serr)-4);
164
+ serr[0] = ' ';
165
+ serr[1] = '(';
166
+ serr[strlen(serr)] = ')';
167
+ } else {
168
+ serr[0] = 0;
169
+ }
170
+
171
+ slbt_dprintf(
172
+ fderr,
173
+ "%s%s%s%s: %s%s%s: openat(%s%s%s%s,%s%s%c%s%c%s,%s%s%s%s,%d) = %s%d%s%s%s%s%s.\n",
174
+
175
+ aclr_bold,aclr_magenta,
176
+ dctx->program,
177
+ aclr_reset,
178
+
179
+ aclr_bold,
180
+ "lconf",
181
+ aclr_reset,
182
+
183
+ aclr_bold,aclr_blue,
184
+ scwd,
185
+ aclr_reset,
186
+
187
+ aclr_bold,aclr_green,
188
+ '"',path,'"',
189
+ aclr_reset,
190
+
191
+ aclr_bold,aclr_blue,
192
+ (oflag == O_DIRECTORY) ? "O_DIRECTORY" : "O_RDONLY",
193
+ aclr_reset,
194
+
195
+ mode,
196
+
197
+ aclr_bold,
198
+ ret,
199
+ aclr_reset,
200
+
201
+ aclr_bold,aclr_red,
202
+ serr,
203
+ aclr_reset);
204
+
205
+ return ret;
206
+ }
207
+
208
+ static int slbt_lconf_trace_fstat_silent(
209
+ struct slbt_driver_ctx * dctx,
210
+ int fd,
211
+ const char * path,
212
+ struct stat * st)
213
+ {
214
+ (void)dctx;
215
+
216
+ return path ? fstatat(fd,path,st,0) : fstat(fd,st);
217
+ }
218
+
219
+ static int slbt_lconf_trace_fstat_plain(
220
+ struct slbt_driver_ctx * dctx,
221
+ int fd,
222
+ const char * path,
223
+ struct stat * st)
224
+ {
225
+ char scwd[20];
226
+ char serr[512];
227
+ char quot[2] = {'"',0};
228
+
229
+ int ret = path ? fstatat(fd,path,st,0) : fstat(fd,st);
230
+ int fderr = slbt_driver_fderr(dctx);
231
+
232
+ if (fd == AT_FDCWD) {
233
+ strcpy(scwd,"AT_FDCWD");
234
+ } else {
235
+ sprintf(scwd,"%d",fd);
236
+ }
237
+
238
+ if ((ret < 0) && (errno == ENOENT)) {
239
+ strcpy(serr," [ENOENT]");
240
+ } else if (ret < 0) {
241
+ memset(serr,0,sizeof(serr));
242
+ strerror_r(errno,&serr[2],sizeof(serr)-4);
243
+ serr[0] = ' ';
244
+ serr[1] = '(';
245
+ serr[strlen(serr)] = ')';
246
+ } else {
247
+ serr[0] = 0;
248
+ }
249
+
250
+ slbt_dprintf(
251
+ fderr,
252
+ "%s: %s: %s(%s%s%s%s%s,...) = %d%s%s",
253
+ dctx->program,
254
+ "lconf",
255
+ path ? "fstatat" : "fstat",
256
+ scwd,
257
+ path ? "," : "",
258
+ path ? quot : "",
259
+ path ? path : "",
260
+ path ? quot : "",
261
+ ret,
262
+ serr,
263
+ ret ? ".\n" : "");
264
+
265
+ if (ret == 0)
266
+ slbt_dprintf(
267
+ fderr,
268
+ " {.st_dev = %ld, .st_ino = %ld}.\n",
269
+ st->st_dev,
270
+ st->st_ino);
271
+
272
+ return ret;
273
+ }
274
+
275
+ static int slbt_lconf_trace_fstat_annotated(
276
+ struct slbt_driver_ctx * dctx,
277
+ int fd,
278
+ const char * path,
279
+ struct stat * st)
280
+ {
281
+ char scwd[20];
282
+ char serr[512];
283
+ char quot[2] = {'"',0};
284
+
285
+ int ret = path ? fstatat(fd,path,st,0) : fstat(fd,st);
286
+ int fderr = slbt_driver_fderr(dctx);
287
+
288
+ if (fd == AT_FDCWD) {
289
+ strcpy(scwd,"AT_FDCWD");
290
+ } else {
291
+ sprintf(scwd,"%d",fd);
292
+ }
293
+
294
+ if ((ret < 0) && (errno == ENOENT)) {
295
+ strcpy(serr," [ENOENT]");
296
+ } else if (ret < 0) {
297
+ memset(serr,0,sizeof(serr));
298
+ strerror_r(errno,&serr[2],sizeof(serr)-4);
299
+ serr[0] = ' ';
300
+ serr[1] = '(';
301
+ serr[strlen(serr)] = ')';
302
+ } else {
303
+ serr[0] = 0;
304
+ }
305
+
306
+ slbt_dprintf(
307
+ fderr,
308
+ "%s%s%s%s: %s%s%s: %s(%s%s%s%s%s%s%s%s%s%s%s,...) = %s%d%s%s%s%s%s%s",
309
+
310
+ aclr_bold,aclr_magenta,
311
+ dctx->program,
312
+ aclr_reset,
313
+
314
+ aclr_bold,
315
+ "lconf",
316
+ aclr_reset,
317
+
318
+ path ? "fstatat" : "fstat",
319
+
320
+ aclr_bold,aclr_blue,
321
+ scwd,
322
+ aclr_reset,
323
+
324
+ aclr_bold,aclr_green,
325
+ path ? "," : "",
326
+ path ? quot : "",
327
+ path ? path : "",
328
+ path ? quot : "",
329
+ aclr_reset,
330
+
331
+ aclr_bold,
332
+ ret,
333
+ aclr_reset,
334
+
335
+ aclr_bold,aclr_red,
336
+ serr,
337
+ aclr_reset,
338
+
339
+ ret ? ".\n" : "");
340
+
341
+ if (ret == 0)
342
+ slbt_dprintf(
343
+ fderr,
344
+ " {%s%s.st_dev%s = %s%ld%s, %s%s.st_ino%s = %s%ld%s}.\n",
345
+
346
+ aclr_bold,aclr_yellow,aclr_reset,
347
+
348
+ aclr_bold,
349
+ st->st_dev,
350
+ aclr_reset,
351
+
352
+ aclr_bold,aclr_yellow,aclr_reset,
353
+
354
+ aclr_bold,
355
+ st->st_ino,
356
+ aclr_reset);
357
+
358
+ return ret;
359
+ }
360
+
33
361
static int slbt_lconf_open(
34
362
struct slbt_driver_ctx * dctx,
35
363
const char * lconf)
36
364
{
365
+ int fderr;
37
366
int fdcwd;
38
367
int fdlconf;
39
368
int fdlconfdir;
@@ -42,29 +371,70 @@ static int slbt_lconf_open(
42
371
struct stat stparent;
43
372
ino_t stinode;
44
373
374
+ int (*trace_lconf)(struct slbt_driver_ctx *,
375
+ const char *);
376
+
377
+ int (*trace_fstat)(struct slbt_driver_ctx *,
378
+ int,const char *, struct stat *);
379
+
380
+ int (*trace_openat)(struct slbt_driver_ctx *,
381
+ int,const char *,int,int);
382
+
383
+ lconf = lconf ? lconf : "libtool";
384
+ fderr = slbt_driver_fderr(dctx);
45
385
fdcwd = slbt_driver_fdcwd(dctx);
46
386
fdlconfdir = fdcwd;
47
387
388
+ if (dctx->cctx->drvflags & SLBT_DRIVER_SILENT) {
389
+ trace_lconf = 0;
390
+ trace_fstat = slbt_lconf_trace_fstat_silent;
391
+ trace_openat = slbt_lconf_trace_openat_silent;
392
+
393
+ } else if (dctx->cctx->drvflags & SLBT_DRIVER_ANNOTATE_NEVER) {
394
+ trace_lconf = slbt_lconf_trace_lconf_plain;
395
+ trace_fstat = slbt_lconf_trace_fstat_plain;
396
+ trace_openat = slbt_lconf_trace_openat_plain;
397
+
398
+ } else if (dctx->cctx->drvflags & SLBT_DRIVER_ANNOTATE_ALWAYS) {
399
+ trace_lconf = slbt_lconf_trace_lconf_annotated;
400
+ trace_fstat = slbt_lconf_trace_fstat_annotated;
401
+ trace_openat = slbt_lconf_trace_openat_annotated;
402
+
403
+ } else if (isatty(fderr)) {
404
+ trace_lconf = slbt_lconf_trace_lconf_annotated;
405
+ trace_fstat = slbt_lconf_trace_fstat_annotated;
406
+ trace_openat = slbt_lconf_trace_openat_annotated;
407
+
408
+ } else {
409
+ trace_lconf = slbt_lconf_trace_lconf_plain;
410
+ trace_fstat = slbt_lconf_trace_fstat_plain;
411
+ trace_openat = slbt_lconf_trace_openat_plain;
412
+ }
413
+
414
+ if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT)) {
415
+ trace_lconf(dctx,lconf);
416
+ slbt_output_fdcwd(dctx);
417
+ }
418
+
48
419
if (lconf && strchr(lconf,'/'))
49
- return ((fdlconf = openat(fdcwd,lconf,O_RDONLY,0)) < 0)
420
+ return ((fdlconf = trace_openat(dctx,fdcwd,lconf,O_RDONLY,0)) < 0)
50
421
? SLBT_CUSTOM_ERROR(dctx,SLBT_ERR_LCONF_OPEN)
51
422
: fdlconf;
52
423
53
- if (fstatat(fdlconfdir,".",&stcwd,0) < 0)
424
+ if (trace_fstat(dctx,fdlconfdir,".",&stcwd) < 0)
54
425
return SLBT_SYSTEM_ERROR(dctx,0);
55
426
56
- lconf = lconf ? lconf : "libtool";
57
- fdlconf = openat(fdlconfdir,lconf,O_RDONLY,0);
58
427
stinode = stcwd.st_ino;
428
+ fdlconf = trace_openat(dctx,fdlconfdir,lconf,O_RDONLY,0);
59
429
60
430
while (fdlconf < 0) {
61
- fdparent = openat(fdlconfdir,"../",O_DIRECTORY,0);
431
+ fdparent = trace_openat(dctx,fdlconfdir,"../",O_DIRECTORY,0);
62
432
slbt_lconf_close(fdcwd,fdlconfdir);
63
433
64
434
if (fdparent < 0)
65
435
return SLBT_SYSTEM_ERROR(dctx,0);
66
436
67
- if (fstat(fdparent,&stparent) < 0) {
437
+ if (trace_fstat(dctx,fdparent,0,&stparent) < 0) {
68
438
close(fdparent);
69
439
return SLBT_SYSTEM_ERROR(dctx,0);
70
440
}
@@ -82,7 +452,7 @@ static int slbt_lconf_open(
82
452
}
83
453
84
454
fdlconfdir = fdparent;
85
- fdlconf = openat(fdlconfdir,lconf,O_RDONLY,0);
455
+ fdlconf = trace_openat(dctx,fdlconfdir,lconf,O_RDONLY,0);
86
456
stinode = stparent.st_ino;
87
457
}
88
458