aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorDaniel Borkmann <dborkman@redhat.com>2014-02-22 12:37:53 -0500
committerDavid S. Miller <davem@davemloft.net>2014-02-24 19:02:10 -0500
commit7debf7806e0e984ea7e43cc271dc9400b0dffc14 (patch)
tree28db6f2edc89f3fc8fa134a38c0a3e1187b5624d /tools
parentb17c706987fa6f28bdc1771c8266e7a69e22adcb (diff)
tools: bpf_dbg: various misc code cleanups
Lets clean up bpf_dbg a bit and improve its code slightly in various areas: i) Get rid of some macros as there's no good reason for keeping them, ii) remove one unused variable and reduce scope of various variables found by cppcheck, iii) Close non-default file descriptors when exiting the shell. Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'tools')
-rw-r--r--tools/net/bpf_dbg.c119
1 files changed, 55 insertions, 64 deletions
diff --git a/tools/net/bpf_dbg.c b/tools/net/bpf_dbg.c
index 65dc757f7f7b..bb31813e43dd 100644
--- a/tools/net/bpf_dbg.c
+++ b/tools/net/bpf_dbg.c
@@ -87,9 +87,6 @@
87 __attribute__ ((format (printf, (pos_fmtstr), (pos_fmtargs)))) 87 __attribute__ ((format (printf, (pos_fmtstr), (pos_fmtargs))))
88#endif 88#endif
89 89
90#define CMD(_name, _func) { .name = _name, .func = _func, }
91#define OP(_op, _name) [_op] = _name
92
93enum { 90enum {
94 CMD_OK, 91 CMD_OK,
95 CMD_ERR, 92 CMD_ERR,
@@ -145,32 +142,32 @@ static size_t pcap_map_size = 0;
145static char *pcap_ptr_va_start, *pcap_ptr_va_curr; 142static char *pcap_ptr_va_start, *pcap_ptr_va_curr;
146 143
147static const char * const op_table[] = { 144static const char * const op_table[] = {
148 OP(BPF_ST, "st"), 145 [BPF_ST] = "st",
149 OP(BPF_STX, "stx"), 146 [BPF_STX] = "stx",
150 OP(BPF_LD_B, "ldb"), 147 [BPF_LD_B] = "ldb",
151 OP(BPF_LD_H, "ldh"), 148 [BPF_LD_H] = "ldh",
152 OP(BPF_LD_W, "ld"), 149 [BPF_LD_W] = "ld",
153 OP(BPF_LDX, "ldx"), 150 [BPF_LDX] = "ldx",
154 OP(BPF_LDX_B, "ldxb"), 151 [BPF_LDX_B] = "ldxb",
155 OP(BPF_JMP_JA, "ja"), 152 [BPF_JMP_JA] = "ja",
156 OP(BPF_JMP_JEQ, "jeq"), 153 [BPF_JMP_JEQ] = "jeq",
157 OP(BPF_JMP_JGT, "jgt"), 154 [BPF_JMP_JGT] = "jgt",
158 OP(BPF_JMP_JGE, "jge"), 155 [BPF_JMP_JGE] = "jge",
159 OP(BPF_JMP_JSET, "jset"), 156 [BPF_JMP_JSET] = "jset",
160 OP(BPF_ALU_ADD, "add"), 157 [BPF_ALU_ADD] = "add",
161 OP(BPF_ALU_SUB, "sub"), 158 [BPF_ALU_SUB] = "sub",
162 OP(BPF_ALU_MUL, "mul"), 159 [BPF_ALU_MUL] = "mul",
163 OP(BPF_ALU_DIV, "div"), 160 [BPF_ALU_DIV] = "div",
164 OP(BPF_ALU_MOD, "mod"), 161 [BPF_ALU_MOD] = "mod",
165 OP(BPF_ALU_NEG, "neg"), 162 [BPF_ALU_NEG] = "neg",
166 OP(BPF_ALU_AND, "and"), 163 [BPF_ALU_AND] = "and",
167 OP(BPF_ALU_OR, "or"), 164 [BPF_ALU_OR] = "or",
168 OP(BPF_ALU_XOR, "xor"), 165 [BPF_ALU_XOR] = "xor",
169 OP(BPF_ALU_LSH, "lsh"), 166 [BPF_ALU_LSH] = "lsh",
170 OP(BPF_ALU_RSH, "rsh"), 167 [BPF_ALU_RSH] = "rsh",
171 OP(BPF_MISC_TAX, "tax"), 168 [BPF_MISC_TAX] = "tax",
172 OP(BPF_MISC_TXA, "txa"), 169 [BPF_MISC_TXA] = "txa",
173 OP(BPF_RET, "ret"), 170 [BPF_RET] = "ret",
174}; 171};
175 172
176static __check_format_printf(1, 2) int rl_printf(const char *fmt, ...) 173static __check_format_printf(1, 2) int rl_printf(const char *fmt, ...)
@@ -1127,7 +1124,6 @@ static int cmd_step(char *num)
1127static int cmd_select(char *num) 1124static int cmd_select(char *num)
1128{ 1125{
1129 unsigned int which, i; 1126 unsigned int which, i;
1130 struct pcap_pkthdr *hdr;
1131 bool have_next = true; 1127 bool have_next = true;
1132 1128
1133 if (!pcap_loaded() || strlen(num) == 0) 1129 if (!pcap_loaded() || strlen(num) == 0)
@@ -1144,7 +1140,7 @@ static int cmd_select(char *num)
1144 1140
1145 for (i = 0; i < which && (have_next = pcap_next_pkt()); i++) 1141 for (i = 0; i < which && (have_next = pcap_next_pkt()); i++)
1146 /* noop */; 1142 /* noop */;
1147 if (!have_next || (hdr = pcap_curr_pkt()) == NULL) { 1143 if (!have_next || pcap_curr_pkt() == NULL) {
1148 rl_printf("no packet #%u available!\n", which); 1144 rl_printf("no packet #%u available!\n", which);
1149 pcap_reset_pkt(); 1145 pcap_reset_pkt();
1150 return CMD_ERR; 1146 return CMD_ERR;
@@ -1177,9 +1173,8 @@ static int cmd_breakpoint(char *subcmd)
1177static int cmd_run(char *num) 1173static int cmd_run(char *num)
1178{ 1174{
1179 static uint32_t pass = 0, fail = 0; 1175 static uint32_t pass = 0, fail = 0;
1180 struct pcap_pkthdr *hdr;
1181 bool has_limit = true; 1176 bool has_limit = true;
1182 int ret, pkts = 0, i = 0; 1177 int pkts = 0, i = 0;
1183 1178
1184 if (!bpf_prog_loaded() || !pcap_loaded()) 1179 if (!bpf_prog_loaded() || !pcap_loaded())
1185 return CMD_ERR; 1180 return CMD_ERR;
@@ -1189,10 +1184,10 @@ static int cmd_run(char *num)
1189 has_limit = false; 1184 has_limit = false;
1190 1185
1191 do { 1186 do {
1192 hdr = pcap_curr_pkt(); 1187 struct pcap_pkthdr *hdr = pcap_curr_pkt();
1193 ret = bpf_run_all(bpf_image, bpf_prog_len, 1188 int ret = bpf_run_all(bpf_image, bpf_prog_len,
1194 (uint8_t *) hdr + sizeof(*hdr), 1189 (uint8_t *) hdr + sizeof(*hdr),
1195 hdr->caplen, hdr->len); 1190 hdr->caplen, hdr->len);
1196 if (ret > 0) 1191 if (ret > 0)
1197 pass++; 1192 pass++;
1198 else if (ret == 0) 1193 else if (ret == 0)
@@ -1245,14 +1240,14 @@ static int cmd_quit(char *dontcare)
1245} 1240}
1246 1241
1247static const struct shell_cmd cmds[] = { 1242static const struct shell_cmd cmds[] = {
1248 CMD("load", cmd_load), 1243 { .name = "load", .func = cmd_load },
1249 CMD("select", cmd_select), 1244 { .name = "select", .func = cmd_select },
1250 CMD("step", cmd_step), 1245 { .name = "step", .func = cmd_step },
1251 CMD("run", cmd_run), 1246 { .name = "run", .func = cmd_run },
1252 CMD("breakpoint", cmd_breakpoint), 1247 { .name = "breakpoint", .func = cmd_breakpoint },
1253 CMD("disassemble", cmd_disassemble), 1248 { .name = "disassemble", .func = cmd_disassemble },
1254 CMD("dump", cmd_dump), 1249 { .name = "dump", .func = cmd_dump },
1255 CMD("quit", cmd_quit), 1250 { .name = "quit", .func = cmd_quit },
1256}; 1251};
1257 1252
1258static int execf(char *arg) 1253static int execf(char *arg)
@@ -1280,7 +1275,6 @@ out:
1280static char *shell_comp_gen(const char *buf, int state) 1275static char *shell_comp_gen(const char *buf, int state)
1281{ 1276{
1282 static int list_index, len; 1277 static int list_index, len;
1283 const char *name;
1284 1278
1285 if (!state) { 1279 if (!state) {
1286 list_index = 0; 1280 list_index = 0;
@@ -1288,9 +1282,9 @@ static char *shell_comp_gen(const char *buf, int state)
1288 } 1282 }
1289 1283
1290 for (; list_index < array_size(cmds); ) { 1284 for (; list_index < array_size(cmds); ) {
1291 name = cmds[list_index].name; 1285 const char *name = cmds[list_index].name;
1292 list_index++;
1293 1286
1287 list_index++;
1294 if (strncmp(name, buf, len) == 0) 1288 if (strncmp(name, buf, len) == 0)
1295 return strdup(name); 1289 return strdup(name);
1296 } 1290 }
@@ -1322,16 +1316,9 @@ static void init_shell(FILE *fin, FILE *fout)
1322{ 1316{
1323 char file[128]; 1317 char file[128];
1324 1318
1325 memset(file, 0, sizeof(file)); 1319 snprintf(file, sizeof(file), "%s/.bpf_dbg_history", getenv("HOME"));
1326 snprintf(file, sizeof(file) - 1,
1327 "%s/.bpf_dbg_history", getenv("HOME"));
1328
1329 read_history(file); 1320 read_history(file);
1330 1321
1331 memset(file, 0, sizeof(file));
1332 snprintf(file, sizeof(file) - 1,
1333 "%s/.bpf_dbg_init", getenv("HOME"));
1334
1335 rl_instream = fin; 1322 rl_instream = fin;
1336 rl_outstream = fout; 1323 rl_outstream = fout;
1337 1324
@@ -1348,37 +1335,41 @@ static void init_shell(FILE *fin, FILE *fout)
1348 rl_bind_key_in_map('\t', rl_complete, emacs_meta_keymap); 1335 rl_bind_key_in_map('\t', rl_complete, emacs_meta_keymap);
1349 rl_bind_key_in_map('\033', rl_complete, emacs_meta_keymap); 1336 rl_bind_key_in_map('\033', rl_complete, emacs_meta_keymap);
1350 1337
1338 snprintf(file, sizeof(file), "%s/.bpf_dbg_init", getenv("HOME"));
1351 rl_read_init_file(file); 1339 rl_read_init_file(file);
1340
1352 rl_prep_terminal(0); 1341 rl_prep_terminal(0);
1353 rl_set_signals(); 1342 rl_set_signals();
1354 1343
1355 signal(SIGINT, intr_shell); 1344 signal(SIGINT, intr_shell);
1356} 1345}
1357 1346
1358static void exit_shell(void) 1347static void exit_shell(FILE *fin, FILE *fout)
1359{ 1348{
1360 char file[128]; 1349 char file[128];
1361 1350
1362 memset(file, 0, sizeof(file)); 1351 snprintf(file, sizeof(file), "%s/.bpf_dbg_history", getenv("HOME"));
1363 snprintf(file, sizeof(file) - 1,
1364 "%s/.bpf_dbg_history", getenv("HOME"));
1365
1366 write_history(file); 1352 write_history(file);
1353
1367 clear_history(); 1354 clear_history();
1368 rl_deprep_terminal(); 1355 rl_deprep_terminal();
1369 1356
1370 try_close_pcap(); 1357 try_close_pcap();
1358
1359 if (fin != stdin)
1360 fclose(fin);
1361 if (fout != stdout)
1362 fclose(fout);
1371} 1363}
1372 1364
1373static int run_shell_loop(FILE *fin, FILE *fout) 1365static int run_shell_loop(FILE *fin, FILE *fout)
1374{ 1366{
1375 char *buf; 1367 char *buf;
1376 int ret;
1377 1368
1378 init_shell(fin, fout); 1369 init_shell(fin, fout);
1379 1370
1380 while ((buf = readline("> ")) != NULL) { 1371 while ((buf = readline("> ")) != NULL) {
1381 ret = execf(buf); 1372 int ret = execf(buf);
1382 if (ret == CMD_EX) 1373 if (ret == CMD_EX)
1383 break; 1374 break;
1384 if (ret == CMD_OK && strlen(buf) > 0) 1375 if (ret == CMD_OK && strlen(buf) > 0)
@@ -1387,7 +1378,7 @@ static int run_shell_loop(FILE *fin, FILE *fout)
1387 free(buf); 1378 free(buf);
1388 } 1379 }
1389 1380
1390 exit_shell(); 1381 exit_shell(fin, fout);
1391 return 0; 1382 return 0;
1392} 1383}
1393 1384