Commit Graph

1871 Commits

Author SHA1 Message Date
Arnaldo Carvalho de Melo 407693e2ca btf_loader: Pass cu to the load_sections/types routines
This will allow us to stop using btfe->priv and eventually btf_elf
altogether for the BTF loader.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-06-04 17:28:56 -03:00
Arnaldo Carvalho de Melo 89b9c4f885 libbtf: Remove the 'base_btf' global var, now unused
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-06-04 17:28:56 -03:00
Arnaldo Carvalho de Melo 6f70d0acca pahole: Use conf_load->base_btf to call cu__encode_btf()
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-06-04 17:28:56 -03:00
Arnaldo Carvalho de Melo 0e77be4099 btf_loader: Use conf_load->base_btf instead of the global btf_base var
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-06-04 17:28:56 -03:00
Arnaldo Carvalho de Melo 6ee61b161d pahole: Set conf_load.btf_base when applicable
Next step will be to make the btf_loader use it.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-06-04 17:28:56 -03:00
Arnaldo Carvalho de Melo b8255beb12 core: Allow passing the base_btf object via 'struct conf_load'
So that we can get rid of that global base_btf and use the right way
to pass load configuration to the format loaders.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-06-04 17:28:56 -03:00
Arnaldo Carvalho de Melo f93e05d8bd btf_encoder: Pass the base BTF object to the BTF encoder
We'll get rid of the 'base_btf' global variable in libbtf.c, so stop
using it in the BTF encoder.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-06-04 17:28:56 -03:00
Arnaldo Carvalho de Melo 81797a00fb btf_elf: Remove unused 'wordsize' member
As we are now using btf__pointer_size() from libbpf.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-06-04 17:28:56 -03:00
Arnaldo Carvalho de Melo ecc888b855 btf_loader: Use btf__pointer_size() to set cu->addr_size
To use libbpf APIs and go on removing the btf_elf class.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-06-04 17:28:56 -03:00
Arnaldo Carvalho de Melo e6b69ffc74 btf_elf: Remove unused is_big_endian member
Now that we use btf__endianness() to figure out that info, ditch this
unused member.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-06-04 17:28:56 -03:00
Arnaldo Carvalho de Melo 81fe7d688d btf_loader: Use btf__endianness() instead of to be removed btfe member
When loading a file, via btf__parse_split() libbtf will read the header
and have the endianness made available via the btf__endianness() API,
use it.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-06-04 17:28:56 -03:00
Arnaldo Carvalho de Melo 9f8ce7e113 libbtf: Remove unused btf_elf->raw_btf member
Now that we are using btf__parse_split() from libbpf, we don't need
that.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-06-04 17:28:56 -03:00
Arnaldo Carvalho de Melo 89be5646a0 pahole: Allow encoding BTF into a detached file
Previously the newly encoded BTF info was stored into a ELF section in
the file where the DWARF info was obtained, but it is useful to just
dump it into a separate file, do it.

  $ ls -la vmlinux.btf
  ls: cannot access 'vmlinux.btf': No such file or directory
  $ pahole -j vmlinux.btf vmlinux
  $ ls -la vmlinux.btf
  -rw-r-----. 1 acme acme 4630082 Jun  1 16:15 vmlinux.btf
  $ pahole -C list_head ./vmlinux.btf
  struct list_head {
  	struct list_head *         next;                 /*     0     8 */
  	struct list_head *         prev;                 /*     8     8 */

  	/* size: 16, cachelines: 1, members: 2 */
  	/* last cacheline: 16 bytes */
  };
  acme@toolbox pahole]$ pahole -C raw_spinlock_t ./vmlinux.btf
  typedef struct raw_spinlock raw_spinlock_t;
  acme@toolbox pahole]$ pahole -EC raw_spinlock ./vmlinux.btf
  struct raw_spinlock {
  	/* typedef arch_spinlock_t */ struct qspinlock {
  		union {
  			/* typedef atomic_t */ struct {
  				int counter;                                                  /*     0     4 */
  			} val;                                                                /*     0     4 */
  			struct {
  				/* typedef u8 -> __u8 */ unsigned char locked;                /*     0     1 */
  				/* typedef u8 -> __u8 */ unsigned char pending;               /*     1     1 */
  			};                                                                    /*     0     2 */
  			struct {
  				/* typedef u16 -> __u16 */ short unsigned int locked_pending; /*     0     2 */
  				/* typedef u16 -> __u16 */ short unsigned int tail;           /*     2     2 */
  			};                                                                    /*     0     4 */
  		};                                                                            /*     0     4 */
  	} raw_lock;                                                                           /*     0     4 */

  	/* size: 4, cachelines: 1, members: 1 */
  	/* last cacheline: 4 bytes */
  };
  ⬢[acme@toolbox pahole]$

Requested-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-06-04 17:28:54 -03:00
Arnaldo Carvalho de Melo 22a76fbc8b btf_loader: Use btf__parse_split()
This way we use libbtf to transparently load both ELF files with a BTF
section as well as raw BTF files, such as those in /sys/kernel/btf/ or
the ones generated using 'pahole --btf_encode_detached'

Suggested-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-06-01 16:12:13 -03:00
Arnaldo Carvalho de Melo c9d4c106ab dwarf_loader: Add define for DW_OP_addrx
To fix the build in systems where this isn't defined.

Reported-by: Marcos Paulo de Souza <mpdesouza@suse.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-28 13:23:29 -03:00
Arnaldo Carvalho de Melo 76f7844c81 libbtf: Fix typo
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 21:25:20 -03:00
Shuyi Cheng 3d510406ff pahole: Add --kabi_prefix flag
To solve problems similar to _RH_KABI_REPLACE. The _RH_KABI_REPLACE(_orig, _new) macros perserve size alignment and kabi agreement between _orig and _new.Below is the definition of this macro:

    union {                        \
        _new;                    \
        struct {                \
            _orig;                \
        } __UNIQUE_ID(rh_kabi_hide);        \
        __RH_KABI_CHECK_SIZE_ALIGN(_orig, _new);    \
    }

__UNIQUE_ID uses the __COUNTER__ macro, and the __COUNTER__ macro is automatically incremented by 1 every time it is precompiled. Therefore, in different compilation units, the same structure has different names.Here is a concrete example:

struct acpi_dev_node {
    union {
        struct acpi_device *companion;
        struct {
            void *handle;
        } __UNIQUE_ID_rh_kabi_hide29;
        union {        };
    };
};
struct acpi_dev_node {
    union {
        struct acpi_device *companion;
        struct {
            void *handle;
        } __UNIQUE_ID_rh_kabi_hide31;
        union {        };
    };
};

Finally, it will cause the btf algorithm to de-duplication efficiency is not high, and time-consuming. For example, running ./pahole -J vmlinux-3.10.0-1160.el7.x86_64 without --kabi_prefix flag, the running time is:
                real 8m28.912s
                user 8m27.271s
                sys 0m1.471s
And the size of the generated btf segment is 30678240 bytes.

After adding the patch, running ./pahole --kabi_prefix=__UNIQUE_ID_rh_kabi_hide -J vmlinux-3.10.0-1160.el7.x86_64. The running time of the command is:
                real 0m19.634s
                user 0m18.457s
                sys 0m1.169s
And the size of the generated btf segment is 3117719 bytes.

Signed-off-by: Shuyi Cheng <chengshuyi@linux.alibaba.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Cc: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Wenan Mao <wenan.mao@linux.alibaba.com>
Cc: dwarves@vger.kernel.org
Link: https://lore.kernel.org/dwarves/482e5543-d7da-7bed-098d-cc879d8db253@linux.alibaba.com/
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 13:44:35 -03:00
Andrii Nakryiko 0d17503db0 btf_encoder: fix and complete filtering out zero-sized per-CPU variables
btf_encoder is ignoring zero-sized per-CPU ELF symbols, but the same has to be
done for DWARF variables when matching them with ELF symbols. This is due to
zero-sized DWARF variables matching unrelated (non-zero-sized) variable that
happens to be allocated at the exact same address, leading to a lot of
confusion in BTF.

See [0] for when this causes big problems.

  [0] https://lore.kernel.org/bpf/CAEf4BzZ0-sihSL-UAm21JcaCCY92CqfNxycHRZYXcoj8OYb=wA@mail.gmail.com/

Committer notes:

Kept the {} around the if block with more than one line, which
simplifies the original patch by just removing that assignment
to the 'dwarf_name' variable.

Reported-by: Michal Suchánek <msuchanek@suse.de>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: bpf@vger.kernel.org
Cc: dwarves@vger.kernel.org
Cc: kernel-team@fb.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:56:45 -03:00
Arnaldo Carvalho de Melo fb418f9d83 dwarves: Make handling of NULL by destructos consistent
All should accept a NULL and bail out, just like free().

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo f049fe9ebf dutil: Make handling of NULL by destructos consistent
All should accept a NULL and bail out, just like free().

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo 1512ab8ab6 pahole: Make handling of NULL by destructos consistent
All should accept a NULL and bail out, just like free().

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo 1105b7dad2 elf_symtab: Use zfree() where applicable
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo 131275fa20 codiff: Use zfree() where applicable
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo 6784b03fd4 dutil: Adopt the zalloc()/zfree() from perf
So that we handle const pointers, also zalloc() is much simpler just
calling calloc(1, size).

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo d7f5824a9e libctf: Use zfree() where applicable
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo 45d9eb8602 pahole: Use zfree() where applicable
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo f48cc78e8e dwarf_loader: Use zfree() instead of ad-hoc implementation
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo d0f6a4a1da gobuffer: Use zfree() and make delete accept NULL, like free()
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo db37185d16 dwarves: Use zfree()
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo e7e86c75e1 libbtf: Use zfree()
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo 972001e58e dutil: Adopt zfree(), to use it more pervasively
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo 5847901abf dwarves: Plug leaks in cu__new() found by covscan
Error: GCC_ANALYZER_WARNING (CWE-401):
  dwarves-1.21/dwarves.c: scope_hint: In function 'cu__new'
  dwarves-1.21/dwarves.c:604:16: warning[-Wanalyzer-malloc-leak]: leak of '<unknown>'
  #  602|         }
  #  603|   out:
  #  604|->       return cu;
  #  605|   out_free_name:
  #  606|         free(cu->name);

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo 32114e611a dwarf_loader: Call dwarf_cu__delete() when aborting the load
Addressing these covscan report entry:

  Error: GCC_ANALYZER_WARNING (CWE-401):
  dwarves-1.21/dwarf_loader.c:2645:40: warning[-Wanalyzer-malloc-leak]: leak of 'dcu'
  # 2643|                                      filename);
  # 2644|                         if (cu == NULL || cu__set_common(cu, conf, mod, elf) != 0)
  # 2645|->                         	return DWARF_CB_ABORT;
  # 2646|
  # 2647|                         dcu = malloc(sizeof(struct dwarf_cu));

  Error: RESOURCE_LEAK (CWE-772):
  dwarves-1.21/dwarf_loader.c:2647: alloc_fn: Storage is returned from allocation function "malloc".
  dwarves-1.21/dwarf_loader.c:2647: var_assign: Assigning: "dcu" = storage returned from "malloc(120UL)".
  dwarves-1.21/dwarf_loader.c:2663: leaked_storage: Variable "dcu" going out of scope leaks the storage it points to.
  # 2661|                         }
  # 2662|                         if (hashtags__bits < default_hbits)
  # 2663|->                               return DWARF_CB_ABORT;
  # 2664|
  # 2665|                         dcu->cu = cu;

  Error: CLANG_WARNING:
  dwarves-1.21/dwarf_loader.c:2663:12: warning[unix.Malloc]: Potential leak of memory pointed to by 'dcu'
  # 2661|                         }
  # 2662|                         if (hashtags__bits < default_hbits)
  # 2663|->                               return DWARF_CB_ABORT;
  # 2664|
  # 2665|                         dcu->cu = cu;

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo 26bd4c4164 dwarf_loader: Delete the allocated CU when aborting
As in this case we're not adding it to the cus->cus in
finalize_cu_immediately().

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo d8940df90b dwarf_loader: Make all ABORT returns go thru a single exit label
No change in logic, just prepping the way for proper deletion of
objects.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo 3ba54ee43a dwarf_loader: Use zalloc() to allocate dwarf_cu
We were allocating it with malloc and then trying to initialize it with
dwarf_cu__init(), which may fail and leave the dwarf_cu instance not
completely initialized which would lead to problems when calling
dwarf_cu__delete(), use zalloc to make sure all is zeroed.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo f3957627bb dwarf_loader: Make dwarf_cu__delete() accept NULL, just like free()
To facilitate error handling, where we would be checking for NULL in
multiple places.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo 8772c9d827 dwarves: Accept NULL in cu__delete(), just like free() accepts
To facilitate error handling, where we would be checking for NULL in
multiple places.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo 25cc0c7754 dwarf_loader: Check if we have a CU after the loop in cus__merge_and_process_cu()
Theoretically possible, silences covscan/gcc analyser:

  Error: GCC_ANALYZER_WARNING (CWE-476):
  dwarves-1.21/dwarf_loader.c:2218:27: warning[-Wanalyzer-null-dereference]: dereference of NULL 'cu'
  # 2216|         uint32_t i;
  # 2217|
  # 2218|->       for (i = 0; i < pt->nr_entries; ++i) {
  # 2219|                 struct tag *tag = pt->entries[i];
  # 2220|

  Error: CLANG_WARNING:
  dwarves-1.21/dwarf_loader.c:2245:13: warning[core.NullDereference]: Access to field 'nr_entries' results in a dereference of a null pointer (loaded from variable 'pt')
  # 2243|                                         uint32_t i)
  # 2244|   {
  # 2245|->       for (; i < pt->nr_entries; ++i) {
  # 2246|                 struct tag *tag = pt->entries[i];

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo 2f30062b54 dwarf_loader: Check tag__recode_dwarf_bitfield() return, may be NULL
Found by covscan:

  Error: GCC_ANALYZER_WARNING (CWE-476):
  dwarves-1.21/dwarf_loader.c:718:29: warning[-Wanalyzer-null-dereference]: dereference of NULL 'dtype'
  #  716|                 struct dwarf_tag *dtype = dwarf_cu__find_type_by_ref(cu->priv,
  #  717|                                                                      &dtag->type);
  #  718|->               struct tag *type = dtype->tag;
  #  719|
  #  720|                 id = tag__recode_dwarf_bitfield(type, cu, bit_size);

  Error: GCC_ANALYZER_WARNING (CWE-476):
  dwarves-1.21/dwarf_loader.c:740:29: warning[-Wanalyzer-null-dereference]: dereference of NULL 'dtype'
  #  738|                 const struct dwarf_tag *dtag = tag->priv;
  #  739|                 struct dwarf_tag *dtype = dwarf_cu__find_type_by_ref(cu->priv, &dtag->type);
  #  740|->               struct tag *type = dtype->tag;
  #  741|
  #  742|                 id = tag__recode_dwarf_bitfield(type, cu, bit_size);

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo d68fd2bbc5 ctracer: Initialize the 'parm_list' variable, detected by covscan
It may be used uninitialized, fix it.

  Error: UNINIT (CWE-457):
  dwarves-1.21/ctracer.c:401: var_decl: Declaring variable "parm_list" without initializer.
  dwarves-1.21/ctracer.c:470: uninit_use_in_call: Using uninitialized value "*parm_list" as argument to "%s" when calling "fprintf". [Note: The source code implementation of the function has been overridden by a builtin model.]
  #  468|                                                1, "entry,exit");
  #  469|         }
  #  470|->       fprintf(fp_converter,
  #  471|                 "\\n\",\n\t\t\t %s);\n"
  #  472|                 "\t}\n"

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo 5b0fb9745e codiff: Fix usage of negative errno values with strerror(), reported by covscan
Error: NEGATIVE_RETURNS (CWE-394):
  dwarves-1.21/codiff.c:816: negative_return_fn: Function "cus__load_file(old_cus, &conf_load, old_filename)" returns a negative number.
  dwarves-1.21/codiff.c:816: assign: Assigning: "err" = "cus__load_file(old_cus, &conf_load, old_filename)".
  dwarves-1.21/codiff.c:818: negative_returns: "err" is passed to a parameter that cannot be negative.
  #  816|                 err = cus__load_file(old_cus, &conf_load, old_filename);
  #  817|                 if (err != 0) {
  #  818|->                       cus__print_error_msg("codiff", old_cus, old_filename, err);
  #  819|                         goto out_cus_delete_priv;
  #  820|                 }

  Error: NEGATIVE_RETURNS (CWE-394):
  dwarves-1.21/codiff.c:830: negative_return_fn: Function "cus__load_file(new_cus, &conf_load, new_filename)" returns a negative number.
  dwarves-1.21/codiff.c:830: assign: Assigning: "err" = "cus__load_file(new_cus, &conf_load, new_filename)".
  dwarves-1.21/codiff.c:832: negative_returns: "err" is passed to a parameter that cannot be negative.
  #  830|                 err = cus__load_file(new_cus, &conf_load, new_filename);
  #  831|                 if (err != 0) {
  #  832|->                       cus__print_error_msg("codiff", new_cus, new_filename, err);
  #  833|                         goto out_cus_delete_priv;
  #  834|                 }

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:18 -03:00
Arnaldo Carvalho de Melo cba940fb86 btf_loader: Fix some memory leaks found by covscan
Error: RESOURCE_LEAK (CWE-772):
  dwarves-1.21/btf_loader.c:554: alloc_fn: Storage is returned from allocation function "btf_elf__new".
  dwarves-1.21/btf_loader.c:554: var_assign: Assigning: "btfe" = storage returned from "btf_elf__new(filename, NULL, base_btf)".
  dwarves-1.21/btf_loader.c:561: leaked_storage: Variable "btfe" going out of scope leaks the storage it points to.
  #  559|         struct cu *cu = cu__new(filename, btfe->wordsize, NULL, 0, filename);
  #  560|         if (cu == NULL)
  #  561|->               return -1;
  #  562|
  #  563|         cu->language = LANG_C;

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-27 11:00:14 -03:00
Arnaldo Carvalho de Melo 674063b1ea loaders: Plug enumeration__delete() leak detected by covscan
Error: RESOURCE_LEAK (CWE-772):
  dwarves-1.21/btf_loader.c:293: alloc_fn: Storage is returned from allocation function "type__new".
  dwarves-1.21/btf_loader.c:293: var_assign: Assigning: "enumeration" = storage returned from "type__new(DW_TAG_enumeration_type, tp->name_off, ((*tp).size ? (*tp).size * 8U : 32UL))".
  dwarves-1.21/btf_loader.c:315: noescape: Resource "enumeration" is not freed or pointed-to in "enumeration__delete".
  dwarves-1.21/btf_loader.c:316: leaked_storage: Variable "enumeration" going out of scope leaks the storage it points to.
  #  314|   out_free:
  #  315|         enumeration__delete(enumeration, btfe->priv);
  #  316|->       return -ENOMEM;
  #  317|   }
  #  318|

  Error: RESOURCE_LEAK (CWE-772):
  dwarves-1.21/ctf_loader.c:398: alloc_fn: Storage is returned from allocation function "type__new".
  dwarves-1.21/ctf_loader.c:398: var_assign: Assigning: "enumeration" = storage returned from "type__new(DW_TAG_enumeration_type, ctf__get32(ctf, &tp->base.ctf_name), (size ?: 32UL))".
  dwarves-1.21/ctf_loader.c:421: noescape: Resource "enumeration" is not freed or pointed-to in "enumeration__delete".
  dwarves-1.21/ctf_loader.c:422: leaked_storage: Variable "enumeration" going out of scope leaks the storage it points to.
  #  420|   out_free:
  #  421|         enumeration__delete(enumeration, ctf->priv);
  #  422|->       return -ENOMEM;
  #  423|   }
  #  424|

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-17 16:47:34 -03:00
Arnaldo Carvalho de Melo 505a1f5615 dwarves: Stop using obstacks
When the CTF and later the BTF loaders were implemented they didn't use
obstacks, and then over time some functions, like type__delete(),
class__delete(), enumeration__delete() were shared, which can lead to
crashes by corrupting the obstack by not following its requirements or
to leaks, to avoid such corruption, stop using it.

There is a penalty, but I think its not worth the complexity to keep
using it.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-17 16:20:52 -03:00
Sevan Janiyan 872658b880 CMakeLists.txt: Specify the file extension of srcs
cmake complains about policy CMP0115 otherwise.

Signed-off-by: Sevan Janiyan <venture37@geeklan.co.uk>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-10 21:32:16 -03:00
Sevan Janiyan aa8519378a README: Mention how to specify another prefix
Signed-off-by: Sevan Janiyan <venture37@geeklan.co.uk>
Link: https://github.com/acmel/dwarves/pull/20
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-10 21:32:13 -03:00
Martin KaFai Lau 58a98f76ac btf: Remove ftrace filter
BTF is currently generated for functions that are in ftrace list
or extern.

A recent use case also needs BTF generated for functions included in
allowlist.

In particular, the kernel commit:

  e78aea8b2170 ("bpf: tcp: Put some tcp cong functions in allowlist for bpf-tcp-cc")

allows bpf program to directly call a few tcp cc kernel functions. Those
kernel functions are currently allowed only if CONFIG_DYNAMIC_FTRACE
is set to ensure they are in the ftrace list but this kconfig dependency
is unnecessary.

Those kernel functions are specified under an ELF section .BTF_ids.
There was an earlier attempt [0] to add another filter for the functions in
the .BTF_ids section.  That discussion concluded that the ftrace filter
should be removed instead.

This patch is to remove the ftrace filter and its related functions.

Number of BTF FUNC with and without is_ftrace_func():
My kconfig in x86: 40643 vs 46225
Jiri reported on arm: 25022 vs 55812

[0]: https://lore.kernel.org/dwarves/20210423213728.3538141-1-kafai@fb.com/

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Tested-by: Nathan Chancellor <nathan@kernel.org> # build
Acked-by: Jiri Olsa <jolsa@kernel.org>
Acked-by: Jiri Slaby <jirislaby@kernel.org>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: kernel-team@fb.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-05-08 11:26:29 -03:00
Arnaldo Carvalho de Melo 7c60b0443c pahole: Fix error message when --header couldn't be read
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-04-14 12:32:58 -03:00
Arnaldo Carvalho de Melo 7eea706c14 pahole: Introduce --with_flexible_array option to show just types ending in a flexible array
The kernel has lotsa:

  $ pahole --sizes --with_flexible_array | wc -l
  1134
  $

  $ pahole --with_flexible_array | tail -18
  struct pci_setup_rom {
  	struct setup_data          data;                 /*     0    16 */
  	uint16_t                   vendor;               /*    16     2 */
  	uint16_t                   devid;                /*    18     2 */

  	/* XXX 4 bytes hole, try to pack */

  	uint64_t                   pcilen;               /*    24     8 */
  	long unsigned int          segment;              /*    32     8 */
  	long unsigned int          bus;                  /*    40     8 */
  	long unsigned int          device;               /*    48     8 */
  	long unsigned int          function;             /*    56     8 */
  	/* --- cacheline 1 boundary (64 bytes) --- */
  	uint8_t                    romdata[];            /*    64     0 */

  	/* size: 64, cachelines: 1, members: 9 */
  	/* sum members: 60, holes: 1, sum holes: 4 */
  };
  $

Works together with other filters:

  $ pahole --contains setup_data --with_flexible_array
  pci_setup_rom
  $ pahole pci_setup_rom
  struct pci_setup_rom {
  	struct setup_data          data;                 /*     0    16 */
  	uint16_t                   vendor;               /*    16     2 */
  	uint16_t                   devid;                /*    18     2 */

  	/* XXX 4 bytes hole, try to pack */

  	uint64_t                   pcilen;               /*    24     8 */
  	long unsigned int          segment;              /*    32     8 */
  	long unsigned int          bus;                  /*    40     8 */
  	long unsigned int          device;               /*    48     8 */
  	long unsigned int          function;             /*    56     8 */
  	/* --- cacheline 1 boundary (64 bytes) --- */
  	uint8_t                    romdata[];            /*    64     0 */

  	/* size: 64, cachelines: 1, members: 9 */
  	/* sum members: 60, holes: 1, sum holes: 4 */
  };
  $

  $ pahole --find_pointers_to net_device --with_flexible_array
  neighbour: dev
  pneigh_entry: dev
  xsk_buff_pool: netdev
  cfg80211_sched_scan_request: dev
  switchdev_deferred_item: dev
  $ pahole xsk_buff_pool
  struct xsk_buff_pool {
  	struct device *            dev;                  /*     0     8 */
  	struct net_device *        netdev;               /*     8     8 */
  	struct list_head           xsk_tx_list;          /*    16    16 */
  	spinlock_t                 xsk_tx_list_lock;     /*    32     4 */
  	refcount_t                 users;                /*    36     4 */
  	struct xdp_umem *          umem;                 /*    40     8 */
  	struct work_struct         work;                 /*    48    32 */
  	/* --- cacheline 1 boundary (64 bytes) was 16 bytes ago --- */
  	struct list_head           free_list;            /*    80    16 */
  	u32                        heads_cnt;            /*    96     4 */
  	u16                        queue_id;             /*   100     2 */

  	/* XXX 26 bytes hole, try to pack */

  	/* --- cacheline 2 boundary (128 bytes) --- */
  	struct xsk_queue *         fq;                   /*   128     8 */
  	struct xsk_queue *         cq;                   /*   136     8 */
  	dma_addr_t *               dma_pages;            /*   144     8 */
  	struct xdp_buff_xsk *      heads;                /*   152     8 */
  	u64                        chunk_mask;           /*   160     8 */
  	u64                        addrs_cnt;            /*   168     8 */
  	u32                        free_list_cnt;        /*   176     4 */
  	u32                        dma_pages_cnt;        /*   180     4 */
  	u32                        free_heads_cnt;       /*   184     4 */
  	u32                        headroom;             /*   188     4 */
  	/* --- cacheline 3 boundary (192 bytes) --- */
  	u32                        chunk_size;           /*   192     4 */
  	u32                        frame_len;            /*   196     4 */
  	u8                         cached_need_wakeup;   /*   200     1 */
  	bool                       uses_need_wakeup;     /*   201     1 */
  	bool                       dma_need_sync;        /*   202     1 */
  	bool                       unaligned;            /*   203     1 */

  	/* XXX 4 bytes hole, try to pack */

  	void *                     addrs;                /*   208     8 */
  	spinlock_t                 cq_lock;              /*   216     4 */

  	/* XXX 4 bytes hole, try to pack */

  	struct xdp_buff_xsk *      free_heads[];         /*   224     0 */

  	/* size: 256, cachelines: 4, members: 29 */
  	/* sum members: 190, holes: 3, sum holes: 34 */
  	/* padding: 32 */
  };
  $

Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2021-04-12 13:52:34 -03:00