Skip to content

Commit 6ef81c5

Browse files
Mamatha Inamdaracmel
Mamatha Inamdar
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);

tools/perf/builtin-mem.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "util/dso.h"
1818
#include "util/map.h"
1919
#include "util/symbol.h"
20+
#include <linux/err.h>
2021

2122
#define MEM_OPERATION_LOAD 0x1
2223
#define MEM_OPERATION_STORE 0x2
@@ -249,8 +250,8 @@ static int report_raw_events(struct perf_mem *mem)
249250
struct perf_session *session = perf_session__new(&data, false,
250251
&mem->tool);
251252

252-
if (session == NULL)
253-
return -1;
253+
if (IS_ERR(session))
254+
return PTR_ERR(session);
254255

255256
if (mem->cpu_list) {
256257
ret = perf_session__cpu_bitmap(session, mem->cpu_list,

tools/perf/builtin-record.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include <signal.h>
5555
#include <sys/mman.h>
5656
#include <sys/wait.h>
57+
#include <linux/err.h>
5758
#include <linux/string.h>
5859
#include <linux/time64.h>
5960
#include <linux/zalloc.h>
@@ -1354,9 +1355,9 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
13541355
}
13551356

13561357
session = perf_session__new(data, false, tool);
1357-
if (session == NULL) {
1358+
if (IS_ERR(session)) {
13581359
pr_err("Perf session creation failed.\n");
1359-
return -1;
1360+
return PTR_ERR(session);
13601361
}
13611362

13621363
fd = perf_data__fd(data);

tools/perf/builtin-report.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,8 +1269,8 @@ int cmd_report(int argc, const char **argv)
12691269

12701270
repeat:
12711271
session = perf_session__new(&data, false, &report.tool);
1272-
if (session == NULL)
1273-
return -1;
1272+
if (IS_ERR(session))
1273+
return PTR_ERR(session);
12741274

12751275
ret = evswitch__init(&report.evswitch, session->evlist, stderr);
12761276
if (ret)

tools/perf/builtin-sched.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <api/fs/fs.h>
4141
#include <perf/cpumap.h>
4242
#include <linux/time64.h>
43+
#include <linux/err.h>
4344

4445
#include <linux/ctype.h>
4546

@@ -1797,9 +1798,9 @@ static int perf_sched__read_events(struct perf_sched *sched)
17971798
int rc = -1;
17981799

17991800
session = perf_session__new(&data, false, &sched->tool);
1800-
if (session == NULL) {
1801-
pr_debug("No Memory for session\n");
1802-
return -1;
1801+
if (IS_ERR(session)) {
1802+
pr_debug("Error creating perf session");
1803+
return PTR_ERR(session);
18031804
}
18041805

18051806
symbol__init(&session->header.env);
@@ -2989,8 +2990,8 @@ static int perf_sched__timehist(struct perf_sched *sched)
29892990
symbol_conf.use_callchain = sched->show_callchain;
29902991

29912992
session = perf_session__new(&data, false, &sched->tool);
2992-
if (session == NULL)
2993-
return -ENOMEM;
2993+
if (IS_ERR(session))
2994+
return PTR_ERR(session);
29942995

29952996
evlist = session->evlist;
29962997

tools/perf/builtin-script.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include <unistd.h>
5353
#include <subcmd/pager.h>
5454
#include <perf/evlist.h>
55+
#include <linux/err.h>
5556
#include "util/record.h"
5657
#include "util/util.h"
5758
#include "perf.h"
@@ -3083,8 +3084,8 @@ int find_scripts(char **scripts_array, char **scripts_path_array, int num,
30833084
int i = 0;
30843085

30853086
session = perf_session__new(&data, false, NULL);
3086-
if (!session)
3087-
return -1;
3087+
if (IS_ERR(session))
3088+
return PTR_ERR(session);
30883089

30893090
snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
30903091

@@ -3754,8 +3755,8 @@ int cmd_script(int argc, const char **argv)
37543755
}
37553756

37563757
session = perf_session__new(&data, false, &script.tool);
3757-
if (session == NULL)
3758-
return -1;
3758+
if (IS_ERR(session))
3759+
return PTR_ERR(session);
37593760

37603761
if (header || header_only) {
37613762
script.tool.show_feat_hdr = SHOW_FEAT_HEADER;

tools/perf/builtin-stat.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
#include <unistd.h>
8484
#include <sys/time.h>
8585
#include <sys/resource.h>
86+
#include <linux/err.h>
8687

8788
#include <linux/ctype.h>
8889
#include <perf/evlist.h>
@@ -1436,9 +1437,9 @@ static int __cmd_record(int argc, const char **argv)
14361437
}
14371438

14381439
session = perf_session__new(data, false, NULL);
1439-
if (session == NULL) {
1440-
pr_err("Perf session creation failed.\n");
1441-
return -1;
1440+
if (IS_ERR(session)) {
1441+
pr_err("Perf session creation failed\n");
1442+
return PTR_ERR(session);
14421443
}
14431444

14441445
init_features(session);
@@ -1635,8 +1636,8 @@ static int __cmd_report(int argc, const char **argv)
16351636
perf_stat.data.mode = PERF_DATA_MODE_READ;
16361637

16371638
session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
1638-
if (session == NULL)
1639-
return -1;
1639+
if (IS_ERR(session))
1640+
return PTR_ERR(session);
16401641

16411642
perf_stat.session = session;
16421643
stat_config.output = stderr;

tools/perf/builtin-timechart.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "util/tool.h"
3636
#include "util/data.h"
3737
#include "util/debug.h"
38+
#include <linux/err.h>
3839

3940
#ifdef LACKS_OPEN_MEMSTREAM_PROTOTYPE
4041
FILE *open_memstream(char **ptr, size_t *sizeloc);
@@ -1601,8 +1602,8 @@ static int __cmd_timechart(struct timechart *tchart, const char *output_name)
16011602
&tchart->tool);
16021603
int ret = -EINVAL;
16031604

1604-
if (session == NULL)
1605-
return -1;
1605+
if (IS_ERR(session))
1606+
return PTR_ERR(session);
16061607

16071608
symbol__init(&session->header.env);
16081609

tools/perf/builtin-top.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#include <linux/stringify.h>
7878
#include <linux/time64.h>
7979
#include <linux/types.h>
80+
#include <linux/err.h>
8081

8182
#include <linux/ctype.h>
8283

@@ -1672,8 +1673,8 @@ int cmd_top(int argc, const char **argv)
16721673
}
16731674

16741675
top.session = perf_session__new(NULL, false, NULL);
1675-
if (top.session == NULL) {
1676-
status = -1;
1676+
if (IS_ERR(top.session)) {
1677+
status = PTR_ERR(top.session);
16771678
goto out_delete_evlist;
16781679
}
16791680

0 commit comments

Comments
 (0)