perf session: Return error code for perf_session__new() function on failure
authorMamatha Inamdar <mamatha4@linux.vnet.ibm.com>
Thu, 22 Aug 2019 07:20:49 +0000 (12:50 +0530)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Fri, 20 Sep 2019 18:58:11 +0000 (15:58 -0300)
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 <rnsastry@linux.vnet.ibm.com>
Signed-off-by: Mamatha Inamdar <mamatha4@linux.vnet.ibm.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Tested-by: Nageswara R Sastry <rnsastry@linux.vnet.ibm.com>
Acked-by: Ravi Bangoria <ravi.bangoria@linux.ibm.com>
Reviewed-by: Jiri Olsa <jolsa@redhat.com>
Reviewed-by: Mukesh Ojha <mojha@codeaurora.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexey Budankov <alexey.budankov@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Jeremie Galarneau <jeremie.galarneau@efficios.com>
Cc: Kate Stewart <kstewart@linuxfoundation.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Shawn Landden <shawn@git.icu>
Cc: Song Liu <songliubraving@fb.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Tzvetomir Stoyanov <tstoyanov@vmware.com>
Link: http://lore.kernel.org/lkml/20190822071223.17892.45782.stgit@localhost.localdomain
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
22 files changed:
tools/perf/builtin-annotate.c
tools/perf/builtin-buildid-cache.c
tools/perf/builtin-buildid-list.c
tools/perf/builtin-c2c.c
tools/perf/builtin-diff.c
tools/perf/builtin-evlist.c
tools/perf/builtin-inject.c
tools/perf/builtin-kmem.c
tools/perf/builtin-kvm.c
tools/perf/builtin-lock.c
tools/perf/builtin-mem.c
tools/perf/builtin-record.c
tools/perf/builtin-report.c
tools/perf/builtin-sched.c
tools/perf/builtin-script.c
tools/perf/builtin-stat.c
tools/perf/builtin-timechart.c
tools/perf/builtin-top.c
tools/perf/builtin-trace.c
tools/perf/tests/topology.c
tools/perf/util/data-convert-bt.c
tools/perf/util/session.c

index 553c651fa0a2ff5e440bd440fc35c4498e5ed824..8db8fc9bddef3b9cd820f0daf233675c4b065b38 100644 (file)
@@ -40,6 +40,7 @@
 #include <dlfcn.h>
 #include <errno.h>
 #include <linux/bitmap.h>
+#include <linux/err.h>
 
 struct perf_annotate {
        struct perf_tool tool;
@@ -584,8 +585,8 @@ int cmd_annotate(int argc, const char **argv)
        data.path = input_name;
 
        annotate.session = perf_session__new(&data, false, &annotate.tool);
-       if (annotate.session == NULL)
-               return -1;
+       if (IS_ERR(annotate.session))
+               return PTR_ERR(annotate.session);
 
        annotate.has_br_stack = perf_header__has_feat(&annotate.session->header,
                                                      HEADER_BRANCH_STACK);
index 1a69eb565dc0f944896ee5752b8f52193e44491d..39efa51d7fb3d0884b97c1a0f59b969332c69cd7 100644 (file)
@@ -28,6 +28,7 @@
 #include "util/util.h"
 #include "util/probe-file.h"
 #include <linux/string.h>
+#include <linux/err.h>
 
 static int build_id_cache__kcore_buildid(const char *proc_dir, char *sbuildid)
 {
@@ -422,8 +423,8 @@ int cmd_buildid_cache(int argc, const char **argv)
                data.force = force;
 
                session = perf_session__new(&data, false, NULL);
-               if (session == NULL)
-                       return -1;
+               if (IS_ERR(session))
+                       return PTR_ERR(session);
        }
 
        if (symbol__init(session ? &session->header.env : NULL) < 0)
index 5a0d8b378cb5e00a21b73a697a11119fe3c8073e..e3ef75583514b61ffe571f4258332a5ca937e7d8 100644 (file)
@@ -18,6 +18,7 @@
 #include "util/symbol.h"
 #include "util/data.h"
 #include <errno.h>
+#include <linux/err.h>
 
 static int sysfs__fprintf_build_id(FILE *fp)
 {
@@ -65,8 +66,8 @@ static int perf_session__list_build_ids(bool force, bool with_hits)
                goto out;
 
        session = perf_session__new(&data, false, &build_id__mark_dso_hit_ops);
-       if (session == NULL)
-               return -1;
+       if (IS_ERR(session))
+               return PTR_ERR(session);
 
        /*
         * We take all buildids when the file contains AUX area tracing data
index 61aaacc2aedd8f5c9054f5b15228c642ee379b1b..3542b6ab9813b5bf7d79a95aafbb1400cef13c2a 100644 (file)
@@ -13,6 +13,7 @@
 #include <errno.h>
 #include <inttypes.h>
 #include <linux/compiler.h>
+#include <linux/err.h>
 #include <linux/kernel.h>
 #include <linux/stringify.h>
 #include <linux/zalloc.h>
@@ -2781,8 +2782,9 @@ static int perf_c2c__report(int argc, const char **argv)
        }
 
        session = perf_session__new(&data, 0, &c2c.tool);
-       if (session == NULL) {
-               pr_debug("No memory for session\n");
+       if (IS_ERR(session)) {
+               err = PTR_ERR(session);
+               pr_debug("Error creating perf session\n");
                goto out;
        }
 
index 827e4800d8624a5f496a9c2aa2df1e1a52f4be2c..c37a78677955526b7fcc1c741981225877da5389 100644 (file)
@@ -23,6 +23,7 @@
 #include "util/time-utils.h"
 #include "util/annotate.h"
 #include "util/map.h"
+#include <linux/err.h>
 #include <linux/zalloc.h>
 #include <subcmd/pager.h>
 #include <subcmd/parse-options.h>
@@ -1153,9 +1154,9 @@ static int check_file_brstack(void)
 
        data__for_each_file(i, d) {
                d->session = perf_session__new(&d->data, false, &pdiff.tool);
-               if (!d->session) {
+               if (IS_ERR(d->session)) {
                        pr_err("Failed to open %s\n", d->data.path);
-                       return -1;
+                       return PTR_ERR(d->session);
                }
 
                has_br_stack = perf_header__has_feat(&d->session->header,
@@ -1185,9 +1186,9 @@ static int __cmd_diff(void)
 
        data__for_each_file(i, d) {
                d->session = perf_session__new(&d->data, false, &pdiff.tool);
-               if (!d->session) {
+               if (IS_ERR(d->session)) {
+                       ret = PTR_ERR(d->session);
                        pr_err("Failed to open %s\n", d->data.path);
-                       ret = -1;
                        goto out_delete;
                }
 
index 294494e60e481a4fc35a98aab6317d2ffc131ca1..60509ce4dd28835f7cb686aafb7ba4c6afb382bd 100644 (file)
@@ -15,6 +15,7 @@
 #include "util/session.h"
 #include "util/data.h"
 #include "util/debug.h"
+#include <linux/err.h>
 
 static int __cmd_evlist(const char *file_name, struct perf_attr_details *details)
 {
@@ -28,8 +29,8 @@ static int __cmd_evlist(const char *file_name, struct perf_attr_details *details
        bool has_tracepoint = false;
 
        session = perf_session__new(&data, 0, NULL);
-       if (session == NULL)
-               return -1;
+       if (IS_ERR(session))
+               return PTR_ERR(session);
 
        evlist__for_each_entry(session->evlist, pos) {
                perf_evsel__fprintf(pos, details, stdout);
index 23a76cf3846f4605834af2d405296a898e0869f5..372ecb3e2c06fb93b551c1e158ba598759c34309 100644 (file)
@@ -23,6 +23,7 @@
 #include "util/symbol.h"
 #include "util/synthetic-events.h"
 #include "util/thread.h"
+#include <linux/err.h>
 
 #include <subcmd/parse-options.h>
 
@@ -835,8 +836,8 @@ int cmd_inject(int argc, const char **argv)
 
        data.path = inject.input_name;
        inject.session = perf_session__new(&data, true, &inject.tool);
-       if (inject.session == NULL)
-               return -1;
+       if (IS_ERR(inject.session))
+               return PTR_ERR(inject.session);
 
        if (zstd_init(&(inject.session->zstd_data), 0) < 0)
                pr_warning("Decompression initialization failed.\n");
index b5682beaad72517a02116f72b688ca076df9294c..1e61e353f579bc6e6910eb9906485f2115eac69d 100644 (file)
@@ -14,6 +14,7 @@
 #include "util/tool.h"
 #include "util/callchain.h"
 #include "util/time-utils.h"
+#include <linux/err.h>
 
 #include <subcmd/pager.h>
 #include <subcmd/parse-options.h>
@@ -1956,8 +1957,8 @@ int cmd_kmem(int argc, const char **argv)
        data.path = input_name;
 
        kmem_session = session = perf_session__new(&data, false, &perf_kmem);
-       if (session == NULL)
-               return -1;
+       if (IS_ERR(session))
+               return PTR_ERR(session);
 
        ret = -1;
 
index 6e3e366589009236ee28587ea38280bae0b86f31..c4b22e1b0a40f88aff0aa3cf7f0b9300e5bdc600 100644 (file)
@@ -33,6 +33,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 
+#include <linux/err.h>
 #include <linux/kernel.h>
 #include <linux/string.h>
 #include <linux/time64.h>
@@ -1091,9 +1092,9 @@ static int read_events(struct perf_kvm_stat *kvm)
 
        kvm->tool = eops;
        kvm->session = perf_session__new(&file, false, &kvm->tool);
-       if (!kvm->session) {
+       if (IS_ERR(kvm->session)) {
                pr_err("Initializing perf session failed\n");
-               return -1;
+               return PTR_ERR(kvm->session);
        }
 
        symbol__init(&kvm->session->header.env);
@@ -1446,8 +1447,8 @@ static int kvm_events_live(struct perf_kvm_stat *kvm,
         * perf session
         */
        kvm->session = perf_session__new(&data, false, &kvm->tool);
-       if (kvm->session == NULL) {
-               err = -1;
+       if (IS_ERR(kvm->session)) {
+               err = PTR_ERR(kvm->session);
                goto out;
        }
        kvm->session->evlist = kvm->evlist;
index 4c2b7f437cdf6f213e29af31bed05eb67a54ec9b..474dfd59d7eb23585b08075d817f51213fbf2f7b 100644 (file)
@@ -30,6 +30,7 @@
 #include <linux/hash.h>
 #include <linux/kernel.h>
 #include <linux/zalloc.h>
+#include <linux/err.h>
 
 static struct perf_session *session;
 
@@ -872,9 +873,9 @@ static int __cmd_report(bool display_info)
        };
 
        session = perf_session__new(&data, false, &eops);
-       if (!session) {
+       if (IS_ERR(session)) {
                pr_err("Initializing perf session failed\n");
-               return -1;
+               return PTR_ERR(session);
        }
 
        symbol__init(&session->header.env);
index 27d2bde943a8feae760f15a3a29e0d958928a18c..a13f5817d6fca4abac7cdfa80f5b90b1a8c3a097 100644 (file)
@@ -17,6 +17,7 @@
 #include "util/dso.h"
 #include "util/map.h"
 #include "util/symbol.h"
+#include <linux/err.h>
 
 #define MEM_OPERATION_LOAD     0x1
 #define MEM_OPERATION_STORE    0x2
@@ -249,8 +250,8 @@ static int report_raw_events(struct perf_mem *mem)
        struct perf_session *session = perf_session__new(&data, false,
                                                         &mem->tool);
 
-       if (session == NULL)
-               return -1;
+       if (IS_ERR(session))
+               return PTR_ERR(session);
 
        if (mem->cpu_list) {
                ret = perf_session__cpu_bitmap(session, mem->cpu_list,
index 4bd11c918e7364f0a35199596d4dd673fe4be363..3f66a49a997fdd2404f3c02abe4d20d42dc54673 100644 (file)
@@ -54,6 +54,7 @@
 #include <signal.h>
 #include <sys/mman.h>
 #include <sys/wait.h>
+#include <linux/err.h>
 #include <linux/string.h>
 #include <linux/time64.h>
 #include <linux/zalloc.h>
@@ -1354,9 +1355,9 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
        }
 
        session = perf_session__new(data, false, tool);
-       if (session == NULL) {
+       if (IS_ERR(session)) {
                pr_err("Perf session creation failed.\n");
-               return -1;
+               return PTR_ERR(session);
        }
 
        fd = perf_data__fd(data);
index 3047e5169d9d34c7779d220fe7de9b5112739644..aae0e57c60fbb582b7c48c71bc6c939d798de2f7 100644 (file)
@@ -1269,8 +1269,8 @@ int cmd_report(int argc, const char **argv)
 
 repeat:
        session = perf_session__new(&data, false, &report.tool);
-       if (session == NULL)
-               return -1;
+       if (IS_ERR(session))
+               return PTR_ERR(session);
 
        ret = evswitch__init(&report.evswitch, session->evlist, stderr);
        if (ret)
index f0b828c201ccdefc07904cb9977516a1090e96aa..079e67a3690478dd3755e6e1ecbc2c8a09f78aab 100644 (file)
@@ -40,6 +40,7 @@
 #include <api/fs/fs.h>
 #include <perf/cpumap.h>
 #include <linux/time64.h>
+#include <linux/err.h>
 
 #include <linux/ctype.h>
 
@@ -1797,9 +1798,9 @@ static int perf_sched__read_events(struct perf_sched *sched)
        int rc = -1;
 
        session = perf_session__new(&data, false, &sched->tool);
-       if (session == NULL) {
-               pr_debug("No Memory for session\n");
-               return -1;
+       if (IS_ERR(session)) {
+               pr_debug("Error creating perf session");
+               return PTR_ERR(session);
        }
 
        symbol__init(&session->header.env);
@@ -2989,8 +2990,8 @@ static int perf_sched__timehist(struct perf_sched *sched)
        symbol_conf.use_callchain = sched->show_callchain;
 
        session = perf_session__new(&data, false, &sched->tool);
-       if (session == NULL)
-               return -ENOMEM;
+       if (IS_ERR(session))
+               return PTR_ERR(session);
 
        evlist = session->evlist;
 
index e079b34201f2f70dbaca07d0231764e348d718bd..a17a9306bdf60c62902eda435385c26896f03ad8 100644 (file)
@@ -52,6 +52,7 @@
 #include <unistd.h>
 #include <subcmd/pager.h>
 #include <perf/evlist.h>
+#include <linux/err.h>
 #include "util/record.h"
 #include "util/util.h"
 #include "perf.h"
@@ -3083,8 +3084,8 @@ int find_scripts(char **scripts_array, char **scripts_path_array, int num,
        int i = 0;
 
        session = perf_session__new(&data, false, NULL);
-       if (!session)
-               return -1;
+       if (IS_ERR(session))
+               return PTR_ERR(session);
 
        snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
 
@@ -3754,8 +3755,8 @@ int cmd_script(int argc, const char **argv)
        }
 
        session = perf_session__new(&data, false, &script.tool);
-       if (session == NULL)
-               return -1;
+       if (IS_ERR(session))
+               return PTR_ERR(session);
 
        if (header || header_only) {
                script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
index 60cdd383af81b3039d31f0f19f1752f4cfcc8a73..f7d13326b830715daefcf02a7b4a2c7085acd7a7 100644 (file)
@@ -83,6 +83,7 @@
 #include <unistd.h>
 #include <sys/time.h>
 #include <sys/resource.h>
+#include <linux/err.h>
 
 #include <linux/ctype.h>
 #include <perf/evlist.h>
@@ -1436,9 +1437,9 @@ static int __cmd_record(int argc, const char **argv)
        }
 
        session = perf_session__new(data, false, NULL);
-       if (session == NULL) {
-               pr_err("Perf session creation failed.\n");
-               return -1;
+       if (IS_ERR(session)) {
+               pr_err("Perf session creation failed\n");
+               return PTR_ERR(session);
        }
 
        init_features(session);
@@ -1635,8 +1636,8 @@ static int __cmd_report(int argc, const char **argv)
        perf_stat.data.mode = PERF_DATA_MODE_READ;
 
        session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
-       if (session == NULL)
-               return -1;
+       if (IS_ERR(session))
+               return PTR_ERR(session);
 
        perf_stat.session  = session;
        stat_config.output = stderr;
index e0e822695a299ae819c4aac3bc308bb209d46397..9e84fae9b096c9863585a9a35c175eea03946705 100644 (file)
@@ -35,6 +35,7 @@
 #include "util/tool.h"
 #include "util/data.h"
 #include "util/debug.h"
+#include <linux/err.h>
 
 #ifdef LACKS_OPEN_MEMSTREAM_PROTOTYPE
 FILE *open_memstream(char **ptr, size_t *sizeloc);
@@ -1601,8 +1602,8 @@ static int __cmd_timechart(struct timechart *tchart, const char *output_name)
                                                         &tchart->tool);
        int ret = -EINVAL;
 
-       if (session == NULL)
-               return -1;
+       if (IS_ERR(session))
+               return PTR_ERR(session);
 
        symbol__init(&session->header.env);
 
index b052470f89b419bc4b9cedbc190308ce076c75f5..8da3c939e6b0cbb6677d4a8913c9a2d04124fd30 100644 (file)
@@ -77,6 +77,7 @@
 #include <linux/stringify.h>
 #include <linux/time64.h>
 #include <linux/types.h>
+#include <linux/err.h>
 
 #include <linux/ctype.h>
 
@@ -1672,8 +1673,8 @@ int cmd_top(int argc, const char **argv)
        }
 
        top.session = perf_session__new(NULL, false, NULL);
-       if (top.session == NULL) {
-               status = -1;
+       if (IS_ERR(top.session)) {
+               status = PTR_ERR(top.session);
                goto out_delete_evlist;
        }
 
index f0f735093e2179ad1c6518e03bc8dae78eb34af1..a292658b4232223a13a43514618047e32c1314ad 100644 (file)
@@ -3585,8 +3585,8 @@ static int trace__replay(struct trace *trace)
        trace->multiple_threads = true;
 
        session = perf_session__new(&data, false, &trace->tool);
-       if (session == NULL)
-               return -1;
+       if (IS_ERR(session))
+               return PTR_ERR(session);
 
        if (trace->opts.target.pid)
                symbol_conf.pid_list_str = strdup(trace->opts.target.pid);
index 7d845d913d7d2363bc3cca6caca234fe66cbe715..4a800499d7c35fbf7337292f5d615a86c26ad382 100644 (file)
@@ -8,6 +8,7 @@
 #include "session.h"
 #include "evlist.h"
 #include "debug.h"
+#include <linux/err.h>
 
 #define TEMPL "/tmp/perf-test-XXXXXX"
 #define DATA_SIZE      10
@@ -39,7 +40,7 @@ static int session_write_header(char *path)
        };
 
        session = perf_session__new(&data, false, NULL);
-       TEST_ASSERT_VAL("can't get session", session);
+       TEST_ASSERT_VAL("can't get session", !IS_ERR(session));
 
        session->evlist = perf_evlist__new_default();
        TEST_ASSERT_VAL("can't get evlist", session->evlist);
@@ -70,7 +71,7 @@ static int check_cpu_topology(char *path, struct perf_cpu_map *map)
        int i;
 
        session = perf_session__new(&data, false, NULL);
-       TEST_ASSERT_VAL("can't get session", session);
+       TEST_ASSERT_VAL("can't get session", !IS_ERR(session));
 
        /* On platforms with large numbers of CPUs process_cpu_topology()
         * might issue an error while reading the perf.data file section
index 0c268449959cb893d3d1f7239c86c1fb7cbfaa3a..dbc772bfb04ecbd6183ef4ccb1519c311ac900bf 100644 (file)
@@ -30,6 +30,7 @@
 #include "machine.h"
 #include "config.h"
 #include <linux/ctype.h>
+#include <linux/err.h>
 
 #define pr_N(n, fmt, ...) \
        eprintf(n, debug_data_convert, fmt, ##__VA_ARGS__)
@@ -1619,8 +1620,10 @@ int bt_convert__perf2ctf(const char *input, const char *path,
        err = -1;
        /* perf.data session */
        session = perf_session__new(&data, 0, &c.tool);
-       if (!session)
+       if (IS_ERR(session)) {
+               err = PTR_ERR(session);
                goto free_writer;
+       }
 
        if (c.queue_size) {
                ordered_events__set_alloc_size(&session->ordered_events,
index 58b5bc34ba12c7f2014cae357d32fab18df1f0af..a621c73bad4212fb604912004955efe22a055f8a 100644 (file)
@@ -34,6 +34,7 @@
 #include "../perf.h"
 #include "arch/common.h"
 #include <internal/lib.h>
+#include <linux/err.h>
 
 #ifdef HAVE_ZSTD_SUPPORT
 static int perf_session__process_compressed_event(struct perf_session *session,
@@ -187,6 +188,7 @@ static int ordered_events__deliver_event(struct ordered_events *oe,
 struct perf_session *perf_session__new(struct perf_data *data,
                                       bool repipe, struct perf_tool *tool)
 {
+       int ret = -ENOMEM;
        struct perf_session *session = zalloc(sizeof(*session));
 
        if (!session)
@@ -201,13 +203,15 @@ struct perf_session *perf_session__new(struct perf_data *data,
 
        perf_env__init(&session->header.env);
        if (data) {
-               if (perf_data__open(data))
+               ret = perf_data__open(data);
+               if (ret < 0)
                        goto out_delete;
 
                session->data = data;
 
                if (perf_data__is_read(data)) {
-                       if (perf_session__open(session) < 0)
+                       ret = perf_session__open(session);
+                       if (ret < 0)
                                goto out_delete;
 
                        /*
@@ -222,8 +226,11 @@ struct perf_session *perf_session__new(struct perf_data *data,
                        perf_evlist__init_trace_event_sample_raw(session->evlist);
 
                        /* Open the directory data. */
-                       if (data->is_dir && perf_data__open_dir(data))
+                       if (data->is_dir) {
+                               ret = perf_data__open_dir(data);
+                       if (ret)
                                goto out_delete;
+                       }
                }
        } else  {
                session->machines.host.env = &perf_env;
@@ -256,7 +263,7 @@ struct perf_session *perf_session__new(struct perf_data *data,
  out_delete:
        perf_session__delete(session);
  out:
-       return NULL;
+       return ERR_PTR(ret);
 }
 
 static void perf_session__delete_threads(struct perf_session *session)