Skip to content

Commit 6ef81c5

Browse files
Mamatha Inamdaracmel
authored andcommitted
perf session: Return error code for perf_session__new() function on failure
This patch is to return error code of perf_new_session function on failure instead of NULL. Test Results: Before Fix: $ perf c2c report -input failed to open nput: No such file or directory $ echo $? 0 $ After Fix: $ perf c2c report -input failed to open nput: No such file or directory $ echo $? 254 $ Committer notes: Fix 'perf tests topology' case, where we use that TEST_ASSERT_VAL(..., session), i.e. we need to pass zero in case of failure, which was the case before when NULL was returned by perf_session__new() for failure, but now we need to negate the result of IS_ERR(session) to respect that TEST_ASSERT_VAL) expectation of zero meaning failure. Reported-by: Nageswara R Sastry <[email protected]> Signed-off-by: Mamatha Inamdar <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Tested-by: Nageswara R Sastry <[email protected]> Acked-by: Ravi Bangoria <[email protected]> Reviewed-by: Jiri Olsa <[email protected]> Reviewed-by: Mukesh Ojha <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Alexey Budankov <[email protected]> Cc: Greg Kroah-Hartman <[email protected]> Cc: Jeremie Galarneau <[email protected]> Cc: Kate Stewart <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Shawn Landden <[email protected]> Cc: Song Liu <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Tzvetomir Stoyanov <[email protected]> Link: http://lore.kernel.org/lkml/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 9e6124d commit 6ef81c5

22 files changed

+86
-57
lines changed

tools/perf/builtin-annotate.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <dlfcn.h>
4141
#include <errno.h>
4242
#include <linux/bitmap.h>
43+
#include <linux/err.h>
4344

4445
struct perf_annotate {
4546
struct perf_tool tool;
@@ -584,8 +585,8 @@ int cmd_annotate(int argc, const char **argv)
584585
data.path = input_name;
585586

586587
annotate.session = perf_session__new(&data, false, &annotate.tool);
587-
if (annotate.session == NULL)
588-
return -1;
588+
if (IS_ERR(annotate.session))
589+
return PTR_ERR(annotate.session);
589590

590591
annotate.has_br_stack = perf_header__has_feat(&annotate.session->header,
591592
HEADER_BRANCH_STACK);

tools/perf/builtin-buildid-cache.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "util/util.h"
2929
#include "util/probe-file.h"
3030
#include <linux/string.h>
31+
#include <linux/err.h>
3132

3233
static int build_id_cache__kcore_buildid(const char *proc_dir, char *sbuildid)
3334
{
@@ -422,8 +423,8 @@ int cmd_buildid_cache(int argc, const char **argv)
422423
data.force = force;
423424

424425
session = perf_session__new(&data, false, NULL);
425-
if (session == NULL)
426-
return -1;
426+
if (IS_ERR(session))
427+
return PTR_ERR(session);
427428
}
428429

429430
if (symbol__init(session ? &session->header.env : NULL) < 0)

tools/perf/builtin-buildid-list.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "util/symbol.h"
1919
#include "util/data.h"
2020
#include <errno.h>
21+
#include <linux/err.h>
2122

2223
static int sysfs__fprintf_build_id(FILE *fp)
2324
{
@@ -65,8 +66,8 @@ static int perf_session__list_build_ids(bool force, bool with_hits)
6566
goto out;
6667

6768
session = perf_session__new(&data, false, &build_id__mark_dso_hit_ops);
68-
if (session == NULL)
69-
return -1;
69+
if (IS_ERR(session))
70+
return PTR_ERR(session);
7071

7172
/*
7273
* We take all buildids when the file contains AUX area tracing data

tools/perf/builtin-c2c.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <errno.h>
1414
#include <inttypes.h>
1515
#include <linux/compiler.h>
16+
#include <linux/err.h>
1617
#include <linux/kernel.h>
1718
#include <linux/stringify.h>
1819
#include <linux/zalloc.h>
@@ -2781,8 +2782,9 @@ static int perf_c2c__report(int argc, const char **argv)
27812782
}
27822783

27832784
session = perf_session__new(&data, 0, &c2c.tool);
2784-
if (session == NULL) {
2785-
pr_debug("No memory for session\n");
2785+
if (IS_ERR(session)) {
2786+
err = PTR_ERR(session);
2787+
pr_debug("Error creating perf session\n");
27862788
goto out;
27872789
}
27882790

tools/perf/builtin-diff.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "util/time-utils.h"
2424
#include "util/annotate.h"
2525
#include "util/map.h"
26+
#include <linux/err.h>
2627
#include <linux/zalloc.h>
2728
#include <subcmd/pager.h>
2829
#include <subcmd/parse-options.h>
@@ -1153,9 +1154,9 @@ static int check_file_brstack(void)
11531154

11541155
data__for_each_file(i, d) {
11551156
d->session = perf_session__new(&d->data, false, &pdiff.tool);
1156-
if (!d->session) {
1157+
if (IS_ERR(d->session)) {
11571158
pr_err("Failed to open %s\n", d->data.path);
1158-
return -1;
1159+
return PTR_ERR(d->session);
11591160
}
11601161

11611162
has_br_stack = perf_header__has_feat(&d->session->header,
@@ -1185,9 +1186,9 @@ static int __cmd_diff(void)
11851186

11861187
data__for_each_file(i, d) {
11871188
d->session = perf_session__new(&d->data, false, &pdiff.tool);
1188-
if (!d->session) {
1189+
if (IS_ERR(d->session)) {
1190+
ret = PTR_ERR(d->session);
11891191
pr_err("Failed to open %s\n", d->data.path);
1190-
ret = -1;
11911192
goto out_delete;
11921193
}
11931194

tools/perf/builtin-evlist.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "util/session.h"
1616
#include "util/data.h"
1717
#include "util/debug.h"
18+
#include <linux/err.h>
1819

1920
static int __cmd_evlist(const char *file_name, struct perf_attr_details *details)
2021
{
@@ -28,8 +29,8 @@ static int __cmd_evlist(const char *file_name, struct perf_attr_details *details
2829
bool has_tracepoint = false;
2930

3031
session = perf_session__new(&data, 0, NULL);
31-
if (session == NULL)
32-
return -1;
32+
if (IS_ERR(session))
33+
return PTR_ERR(session);
3334

3435
evlist__for_each_entry(session->evlist, pos) {
3536
perf_evsel__fprintf(pos, details, stdout);

tools/perf/builtin-inject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "util/symbol.h"
2424
#include "util/synthetic-events.h"
2525
#include "util/thread.h"
26+
#include <linux/err.h>
2627

2728
#include <subcmd/parse-options.h>
2829

@@ -835,8 +836,8 @@ int cmd_inject(int argc, const char **argv)
835836

836837
data.path = inject.input_name;
837838
inject.session = perf_session__new(&data, true, &inject.tool);
838-
if (inject.session == NULL)
839-
return -1;
839+
if (IS_ERR(inject.session))
840+
return PTR_ERR(inject.session);
840841

841842
if (zstd_init(&(inject.session->zstd_data), 0) < 0)
842843
pr_warning("Decompression initialization failed.\n");

tools/perf/builtin-kmem.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "util/tool.h"
1515
#include "util/callchain.h"
1616
#include "util/time-utils.h"
17+
#include <linux/err.h>
1718

1819
#include <subcmd/pager.h>
1920
#include <subcmd/parse-options.h>
@@ -1956,8 +1957,8 @@ int cmd_kmem(int argc, const char **argv)
19561957
data.path = input_name;
19571958

19581959
kmem_session = session = perf_session__new(&data, false, &perf_kmem);
1959-
if (session == NULL)
1960-
return -1;
1960+
if (IS_ERR(session))
1961+
return PTR_ERR(session);
19611962

19621963
ret = -1;
19631964

tools/perf/builtin-kvm.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <sys/stat.h>
3434
#include <fcntl.h>
3535

36+
#include <linux/err.h>
3637
#include <linux/kernel.h>
3738
#include <linux/string.h>
3839
#include <linux/time64.h>
@@ -1091,9 +1092,9 @@ static int read_events(struct perf_kvm_stat *kvm)
10911092

10921093
kvm->tool = eops;
10931094
kvm->session = perf_session__new(&file, false, &kvm->tool);
1094-
if (!kvm->session) {
1095+
if (IS_ERR(kvm->session)) {
10951096
pr_err("Initializing perf session failed\n");
1096-
return -1;
1097+
return PTR_ERR(kvm->session);
10971098
}
10981099

10991100
symbol__init(&kvm->session->header.env);
@@ -1446,8 +1447,8 @@ static int kvm_events_live(struct perf_kvm_stat *kvm,
14461447
* perf session
14471448
*/
14481449
kvm->session = perf_session__new(&data, false, &kvm->tool);
1449-
if (kvm->session == NULL) {
1450-
err = -1;
1450+
if (IS_ERR(kvm->session)) {
1451+
err = PTR_ERR(kvm->session);
14511452
goto out;
14521453
}
14531454
kvm->session->evlist = kvm->evlist;

tools/perf/builtin-lock.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <linux/hash.h>
3131
#include <linux/kernel.h>
3232
#include <linux/zalloc.h>
33+
#include <linux/err.h>
3334

3435
static struct perf_session *session;
3536

@@ -872,9 +873,9 @@ static int __cmd_report(bool display_info)
872873
};
873874

874875
session = perf_session__new(&data, false, &eops);
875-
if (!session) {
876+
if (IS_ERR(session)) {
876877
pr_err("Initializing perf session failed\n");
877-
return -1;
878+
return PTR_ERR(session);
878879
}
879880

880881
symbol__init(&session->header.env);

0 commit comments

Comments
 (0)