]>
git.proxmox.com Git - mirror_spl.git/blob - cmd/spl.c
ad2c81b4a58959a1de932caf7d00e5d33c88d41f
9 #include "../include/spl-ctl.h"
11 static int spl_debug_mask
= ~0;
12 static int spl_debug_subsystem
= ~0;
14 /* all strings nul-terminated; only the struct and hdr need to be freed */
16 struct spl_debug_header
*hdr
;
23 cmp_rec(const void *p1
, const void *p2
)
25 struct dbg_line
*d1
= *(struct dbg_line
**)p1
;
26 struct dbg_line
*d2
= *(struct dbg_line
**)p2
;
28 if (d1
->hdr
->ph_sec
< d2
->hdr
->ph_sec
)
31 if (d1
->hdr
->ph_sec
== d2
->hdr
->ph_sec
&&
32 d1
->hdr
->ph_usec
< d2
->hdr
->ph_usec
)
35 if (d1
->hdr
->ph_sec
== d2
->hdr
->ph_sec
&&
36 d1
->hdr
->ph_usec
== d2
->hdr
->ph_usec
)
43 print_rec(struct dbg_line
**linev
, int used
, FILE *out
)
47 for (i
= 0; i
< used
; i
++) {
48 struct dbg_line
*line
= linev
[i
];
49 struct spl_debug_header
*hdr
= line
->hdr
;
51 fprintf(out
, "%08x:%08x:%u:%u.%06llu:%u:%u:%u:(%s:%u:%s()) %s",
52 hdr
->ph_subsys
, hdr
->ph_mask
, hdr
->ph_cpu_id
,
53 hdr
->ph_sec
, (unsigned long long)hdr
->ph_usec
,
54 hdr
->ph_stack
, hdr
->ph_pid
, hdr
->ph_stack
, line
->file
,
55 hdr
->ph_line_num
, line
->fn
, line
->text
);
64 add_rec(struct dbg_line
*line
, struct dbg_line
***linevp
, int *lenp
, int used
)
66 struct dbg_line
**linev
= *linevp
;
69 int nlen
= *lenp
+ 512;
70 int nsize
= nlen
* sizeof(struct dbg_line
*);
72 linev
= *linevp
? realloc(*linevp
, nsize
) : malloc(nsize
);
83 parse_buffer(FILE *in
, FILE *out
)
85 struct dbg_line
*line
;
86 struct spl_debug_header
*hdr
;
88 unsigned long dropped
= 0, kept
= 0;
89 struct dbg_line
**linev
= NULL
;
90 const int phl
= sizeof(hdr
->ph_len
);
91 const int phf
= sizeof(hdr
->ph_flags
);
92 int rc
, linev_len
= 0;
95 rc
= fread(buf
, phl
+ phf
, 1, in
);
100 if (hdr
->ph_len
== 0)
102 if (hdr
->ph_len
> 4094) {
103 fprintf(stderr
, "unexpected large record: %d bytes. "
104 "aborting.\n", hdr
->ph_len
);
108 rc
= fread(buf
+ phl
+ phf
, 1, hdr
->ph_len
- phl
- phf
, in
);
113 (!(spl_debug_subsystem
& hdr
->ph_subsys
) ||
114 (!(spl_debug_mask
& hdr
->ph_mask
)))) {
119 line
= malloc(sizeof(*line
));
121 fprintf(stderr
, "malloc failed; printing accumulated "
122 "records and exiting.\n");
126 line
->hdr
= malloc(hdr
->ph_len
+ 1);
127 if (line
->hdr
== NULL
) {
129 fprintf(stderr
, "malloc failed; printing accumulated "
130 "records and exiting.\n");
134 p
= (void *)line
->hdr
;
135 memcpy(line
->hdr
, buf
, hdr
->ph_len
);
136 p
[hdr
->ph_len
] = '\0';
140 p
+= strlen(line
->file
) + 1;
142 p
+= strlen(line
->fn
) + 1;
145 if (!add_rec(line
, &linev
, &linev_len
, kept
)) {
146 fprintf(stderr
, "malloc failed; printing accumulated "
147 "records and exiting.\n");
154 qsort(linev
, kept
, sizeof(struct dbg_line
*), cmp_rec
);
155 print_rec(linev
, kept
, out
);
158 printf("Debug log: %lu lines, %lu kept, %lu dropped.\n",
159 dropped
+ kept
, kept
, dropped
);
164 main(int argc
, char *argv
[])
167 FILE *in
, *out
= stdout
;
170 if (argc
> 3 || argc
< 2) {
171 fprintf(stderr
, "usage: %s <input> [output]\n", argv
[0]);
175 #ifdef __USE_LARGEFILE64
179 fdin
= open(argv
[1], O_RDONLY
| o_lf
);
181 fprintf(stderr
, "open(%s) failed: %s\n", argv
[1],
185 in
= fdopen(fdin
, "r");
187 fprintf(stderr
, "fopen(%s) failed: %s\n", argv
[1],
193 fdout
= open(argv
[2], O_CREAT
| O_TRUNC
| O_WRONLY
| o_lf
, 0600);
195 fprintf(stderr
, "open(%s) failed: %s\n", argv
[2],
200 out
= fdopen(fdout
, "w");
202 fprintf(stderr
, "fopen(%s) failed: %s\n", argv
[2],
210 rc
= parse_buffer(in
, out
);