diff options
author | Quentin Monnet <quentin.monnet@netronome.com> | 2017-10-23 12:24:13 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2017-10-23 20:25:09 -0400 |
commit | 9a5ab8bf1d6d16ef47fdf55dba1683ec00d751ad (patch) | |
tree | bd5c00e184e771ecc21fc593454ff63203797a29 /tools/bpf/bpftool/prog.c | |
parent | 3aaca6bf7a09150e4c87f2932dc8ebe82a586252 (diff) |
tools: bpftool: turn err() and info() macros into functions
Turn err() and info() macros into functions.
In order to avoid naming conflicts with variables in the code, rename
them as p_err() and p_info() respectively.
The behavior of these functions is similar to the one of the macros for
plain output. However, when JSON output is requested, these macros
return a JSON-formatted "error" object instead of printing a message to
stderr.
To handle error messages correctly with JSON, a modification was brought
to their behavior nonetheless: the functions now append a end-of-line
character at the end of the message. This way, we can remove end-of-line
characters at the end of the argument strings, and not have them in the
JSON output.
All error messages are formatted to hold in a single call to p_err(), in
order to produce a single JSON field.
Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'tools/bpf/bpftool/prog.c')
-rw-r--r-- | tools/bpf/bpftool/prog.c | 51 |
1 files changed, 25 insertions, 26 deletions
diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index 43e49799a624..41bd5390b4fc 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c | |||
@@ -104,21 +104,21 @@ static int prog_fd_by_tag(unsigned char *tag) | |||
104 | while (true) { | 104 | while (true) { |
105 | err = bpf_prog_get_next_id(id, &id); | 105 | err = bpf_prog_get_next_id(id, &id); |
106 | if (err) { | 106 | if (err) { |
107 | err("%s\n", strerror(errno)); | 107 | p_err("%s", strerror(errno)); |
108 | return -1; | 108 | return -1; |
109 | } | 109 | } |
110 | 110 | ||
111 | fd = bpf_prog_get_fd_by_id(id); | 111 | fd = bpf_prog_get_fd_by_id(id); |
112 | if (fd < 0) { | 112 | if (fd < 0) { |
113 | err("can't get prog by id (%u): %s\n", | 113 | p_err("can't get prog by id (%u): %s", |
114 | id, strerror(errno)); | 114 | id, strerror(errno)); |
115 | return -1; | 115 | return -1; |
116 | } | 116 | } |
117 | 117 | ||
118 | err = bpf_obj_get_info_by_fd(fd, &info, &len); | 118 | err = bpf_obj_get_info_by_fd(fd, &info, &len); |
119 | if (err) { | 119 | if (err) { |
120 | err("can't get prog info (%u): %s\n", | 120 | p_err("can't get prog info (%u): %s", |
121 | id, strerror(errno)); | 121 | id, strerror(errno)); |
122 | close(fd); | 122 | close(fd); |
123 | return -1; | 123 | return -1; |
124 | } | 124 | } |
@@ -142,14 +142,14 @@ int prog_parse_fd(int *argc, char ***argv) | |||
142 | 142 | ||
143 | id = strtoul(**argv, &endptr, 0); | 143 | id = strtoul(**argv, &endptr, 0); |
144 | if (*endptr) { | 144 | if (*endptr) { |
145 | err("can't parse %s as ID\n", **argv); | 145 | p_err("can't parse %s as ID", **argv); |
146 | return -1; | 146 | return -1; |
147 | } | 147 | } |
148 | NEXT_ARGP(); | 148 | NEXT_ARGP(); |
149 | 149 | ||
150 | fd = bpf_prog_get_fd_by_id(id); | 150 | fd = bpf_prog_get_fd_by_id(id); |
151 | if (fd < 0) | 151 | if (fd < 0) |
152 | err("get by id (%u): %s\n", id, strerror(errno)); | 152 | p_err("get by id (%u): %s", id, strerror(errno)); |
153 | return fd; | 153 | return fd; |
154 | } else if (is_prefix(**argv, "tag")) { | 154 | } else if (is_prefix(**argv, "tag")) { |
155 | unsigned char tag[BPF_TAG_SIZE]; | 155 | unsigned char tag[BPF_TAG_SIZE]; |
@@ -159,7 +159,7 @@ int prog_parse_fd(int *argc, char ***argv) | |||
159 | if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2, | 159 | if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2, |
160 | tag + 3, tag + 4, tag + 5, tag + 6, tag + 7) | 160 | tag + 3, tag + 4, tag + 5, tag + 6, tag + 7) |
161 | != BPF_TAG_SIZE) { | 161 | != BPF_TAG_SIZE) { |
162 | err("can't parse tag\n"); | 162 | p_err("can't parse tag"); |
163 | return -1; | 163 | return -1; |
164 | } | 164 | } |
165 | NEXT_ARGP(); | 165 | NEXT_ARGP(); |
@@ -176,7 +176,7 @@ int prog_parse_fd(int *argc, char ***argv) | |||
176 | return open_obj_pinned_any(path, BPF_OBJ_PROG); | 176 | return open_obj_pinned_any(path, BPF_OBJ_PROG); |
177 | } | 177 | } |
178 | 178 | ||
179 | err("expected 'id', 'tag' or 'pinned', got: '%s'?\n", **argv); | 179 | p_err("expected 'id', 'tag' or 'pinned', got: '%s'?", **argv); |
180 | return -1; | 180 | return -1; |
181 | } | 181 | } |
182 | 182 | ||
@@ -311,7 +311,7 @@ static int show_prog(int fd) | |||
311 | 311 | ||
312 | err = bpf_obj_get_info_by_fd(fd, &info, &len); | 312 | err = bpf_obj_get_info_by_fd(fd, &info, &len); |
313 | if (err) { | 313 | if (err) { |
314 | err("can't get prog info: %s\n", strerror(errno)); | 314 | p_err("can't get prog info: %s", strerror(errno)); |
315 | return -1; | 315 | return -1; |
316 | } | 316 | } |
317 | 317 | ||
@@ -349,17 +349,16 @@ static int do_show(int argc, char **argv) | |||
349 | err = 0; | 349 | err = 0; |
350 | break; | 350 | break; |
351 | } | 351 | } |
352 | err("can't get next program: %s\n", strerror(errno)); | 352 | p_err("can't get next program: %s%s", strerror(errno), |
353 | if (errno == EINVAL) | 353 | errno == EINVAL ? " -- kernel too old?" : ""); |
354 | err("kernel too old?\n"); | ||
355 | err = -1; | 354 | err = -1; |
356 | break; | 355 | break; |
357 | } | 356 | } |
358 | 357 | ||
359 | fd = bpf_prog_get_fd_by_id(id); | 358 | fd = bpf_prog_get_fd_by_id(id); |
360 | if (fd < 0) { | 359 | if (fd < 0) { |
361 | err("can't get prog by id (%u): %s\n", | 360 | p_err("can't get prog by id (%u): %s", |
362 | id, strerror(errno)); | 361 | id, strerror(errno)); |
363 | err = -1; | 362 | err = -1; |
364 | break; | 363 | break; |
365 | } | 364 | } |
@@ -498,7 +497,7 @@ static int do_dump(int argc, char **argv) | |||
498 | member_len = &info.xlated_prog_len; | 497 | member_len = &info.xlated_prog_len; |
499 | member_ptr = &info.xlated_prog_insns; | 498 | member_ptr = &info.xlated_prog_insns; |
500 | } else { | 499 | } else { |
501 | err("expected 'xlated' or 'jited', got: %s\n", *argv); | 500 | p_err("expected 'xlated' or 'jited', got: %s", *argv); |
502 | return -1; | 501 | return -1; |
503 | } | 502 | } |
504 | NEXT_ARG(); | 503 | NEXT_ARG(); |
@@ -513,7 +512,7 @@ static int do_dump(int argc, char **argv) | |||
513 | if (is_prefix(*argv, "file")) { | 512 | if (is_prefix(*argv, "file")) { |
514 | NEXT_ARG(); | 513 | NEXT_ARG(); |
515 | if (!argc) { | 514 | if (!argc) { |
516 | err("expected file path\n"); | 515 | p_err("expected file path"); |
517 | return -1; | 516 | return -1; |
518 | } | 517 | } |
519 | 518 | ||
@@ -531,12 +530,12 @@ static int do_dump(int argc, char **argv) | |||
531 | 530 | ||
532 | err = bpf_obj_get_info_by_fd(fd, &info, &len); | 531 | err = bpf_obj_get_info_by_fd(fd, &info, &len); |
533 | if (err) { | 532 | if (err) { |
534 | err("can't get prog info: %s\n", strerror(errno)); | 533 | p_err("can't get prog info: %s", strerror(errno)); |
535 | return -1; | 534 | return -1; |
536 | } | 535 | } |
537 | 536 | ||
538 | if (!*member_len) { | 537 | if (!*member_len) { |
539 | info("no instructions returned\n"); | 538 | p_info("no instructions returned"); |
540 | close(fd); | 539 | close(fd); |
541 | return 0; | 540 | return 0; |
542 | } | 541 | } |
@@ -545,7 +544,7 @@ static int do_dump(int argc, char **argv) | |||
545 | 544 | ||
546 | buf = malloc(buf_size); | 545 | buf = malloc(buf_size); |
547 | if (!buf) { | 546 | if (!buf) { |
548 | err("mem alloc failed\n"); | 547 | p_err("mem alloc failed"); |
549 | close(fd); | 548 | close(fd); |
550 | return -1; | 549 | return -1; |
551 | } | 550 | } |
@@ -558,28 +557,28 @@ static int do_dump(int argc, char **argv) | |||
558 | err = bpf_obj_get_info_by_fd(fd, &info, &len); | 557 | err = bpf_obj_get_info_by_fd(fd, &info, &len); |
559 | close(fd); | 558 | close(fd); |
560 | if (err) { | 559 | if (err) { |
561 | err("can't get prog info: %s\n", strerror(errno)); | 560 | p_err("can't get prog info: %s", strerror(errno)); |
562 | goto err_free; | 561 | goto err_free; |
563 | } | 562 | } |
564 | 563 | ||
565 | if (*member_len > buf_size) { | 564 | if (*member_len > buf_size) { |
566 | err("too many instructions returned\n"); | 565 | p_err("too many instructions returned"); |
567 | goto err_free; | 566 | goto err_free; |
568 | } | 567 | } |
569 | 568 | ||
570 | if (filepath) { | 569 | if (filepath) { |
571 | fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600); | 570 | fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
572 | if (fd < 0) { | 571 | if (fd < 0) { |
573 | err("can't open file %s: %s\n", filepath, | 572 | p_err("can't open file %s: %s", filepath, |
574 | strerror(errno)); | 573 | strerror(errno)); |
575 | goto err_free; | 574 | goto err_free; |
576 | } | 575 | } |
577 | 576 | ||
578 | n = write(fd, buf, *member_len); | 577 | n = write(fd, buf, *member_len); |
579 | close(fd); | 578 | close(fd); |
580 | if (n != *member_len) { | 579 | if (n != *member_len) { |
581 | err("error writing output file: %s\n", | 580 | p_err("error writing output file: %s", |
582 | n < 0 ? strerror(errno) : "short write"); | 581 | n < 0 ? strerror(errno) : "short write"); |
583 | goto err_free; | 582 | goto err_free; |
584 | } | 583 | } |
585 | } else { | 584 | } else { |