Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the --btf option #1669

Merged
merged 3 commits into from
Feb 9, 2021
Merged

Conversation

viktormalik
Copy link
Contributor

@viktormalik viktormalik commented Jan 8, 2021

This is an improved version of #1506, which caused type redefinition conflicts for some cases. Now, all the examples mentioned in the old PR should work.

BTF is parsed automatically if it is available and if the user didn't define any type (either via user definitions or via includes) that would redefine some type from BTF.

I think that this also closes #1588. It would seem nicer to allow the user to redefine a part of the types and take the rest from BTF, but I believe that could cause some unexpected behaviour (see this comment for details).

The only problem of this implementation is that it checks for redefinitions by running the Clang parser and then checking the error messages. This is not a nice solution, but I haven't been able to find a better one, yet. If you have any ideas, please bring them in.

Also, I'm not sure if this solves the problem from #1511, as linux/types.h will be used when BTF is not and that may cause redefinition errors with sys/types.h. It shouldn't be hard to resolve this in a way similar to this PR, but I haven't been able to come with a reproducer.

Checklist
  • Language changes are updated in docs/reference_guide.md
  • User-visible and non-trivial changes updated in CHANGELOG.md
  • The new behaviour is covered by tests

@danobi
Copy link
Member

danobi commented Jan 12, 2021

Skimmed the code and it looks reasonable. I'll spend some time tomorrow to try and break it.

@danobi
Copy link
Member

danobi commented Jan 12, 2021

Can you write up the set of "rules" regarding how type definitions are chosen/acquired? Would help users understand errors and help us maintain behavior over time.

Also, there should be more runtime tests for this PR to test combinations of user-defined and BTF defined types. We can use REQUIRES_FEATURE btf and operate on struct task_struct (task_struct is unlikely to go away any time soon).

@danobi
Copy link
Member

danobi commented Jan 12, 2021

Seeing some errors with dcsnoop.bt:

$ sudo ./build/src/bpftrace tools/dcsnoop.bt
/lib/modules/5.9.14-100.fc32.x86_64/source/include/linux/kasan-checks.h:18:9: error: use of undeclared identifier 'true'
/lib/modules/5.9.14-100.fc32.x86_64/source/include/linux/kasan-checks.h:22:9: error: use of undeclared identifier 'true'
/lib/modules/5.9.14-100.fc32.x86_64/source/include/linux/kasan-checks.h:36:9: error: use of undeclared identifier 'true'
/lib/modules/5.9.14-100.fc32.x86_64/source/include/linux/kasan-checks.h:40:9: error: use of undeclared identifier 'true'
/lib/modules/5.9.14-100.fc32.x86_64/source/include/asm-generic/qspinlock_types.h:14:16: error: redefinition of 'qspinlock'
/bpftrace/include/__btf_generated_header.h:67:8: note: previous definition is here
/lib/modules/5.9.14-100.fc32.x86_64/source/include/asm-generic/qspinlock_types.h:44:3: error: typedef redefinition with different types ('struct (anonymous struct at /lib/modules/5.9.14-100.fc32.x86_64/source/include/asm-generic/qspinlock_types.h:14:16)' vs 'struct qspinlock')
/bpftrace/include/__btf_generated_header.h:81:26: note: previous definition is here
/lib/modules/5.9.14-100.fc32.x86_64/source/include/asm-generic/qrwlock_types.h:13:16: error: redefinition of 'qrwlock'
/bpftrace/include/__btf_generated_header.h:1415:8: note: previous definition is here
/lib/modules/5.9.14-100.fc32.x86_64/source/include/asm-generic/qrwlock_types.h:27:3: error: typedef redefinition with different types ('struct (anonymous struct at /lib/modules/5.9.14-100.fc32.x86_64/source/include/asm-generic/qrwlock_types.h:13:16)' vs 'struct qrwlock')
/bpftrace/include/__btf_generated_header.h:1426:24: note: previous definition is here
/lib/modules/5.9.14-100.fc32.x86_64/source/include/linux/lockdep_types.h:187:8: error: redefinition of 'lock_class_key'
/bpftrace/include/__btf_generated_header.h:24:8: note: previous definition is here
/lib/modules/5.9.14-100.fc32.x86_64/source/include/linux/lockdep_types.h:192:8: error: redefinition of 'lockdep_map'
/bpftrace/include/__btf_generated_header.h:4566:8: note: previous definition is here
/lib/modules/5.9.14-100.fc32.x86_64/source/include/linux/spinlock_types.h:20:16: error: redefinition of 'raw_spinlock'
/bpftrace/include/__btf_generated_header.h:83:8: note: previous definition is here
/lib/modules/5.9.14-100.fc32.x86_64/source/include/linux/spinlock_types.h:29:3: error: typedef redefinition with different types ('struct (anonymous struct at /lib/modules/5.9.14-100.fc32.x86_64/source/include/linux/spinlock_types.h:20:16)' vs 'struct raw_spinlock')
/bpftrace/include/__btf_generated_header.h:441:29: note: previous definition is here
/lib/modules/5.9.14-100.fc32.x86_64/source/include/linux/spinlock_types.h:71:16: error: redefinition of 'spinlock'
/bpftrace/include/__btf_generated_header.h:87:8: note: previous definition is here
/lib/modules/5.9.14-100.fc32.x86_64/source/include/linux/spinlock_types.h:83:3: error: typedef redefinition with different types ('struct (anonymous struct at /lib/modules/5.9.14-100.fc32.x86_64/source/include/linux/spinlock_types.h:71:16)' vs 'struct spinlock')
/bpftrace/include/__btf_generated_header.h:537:25: note: previous definition is here
/lib/modules/5.9.14-100.fc32.x86_64/source/include/linux/rwlock_types.h:20:3: error: typedef redefinition with different types ('struct rwlock_t' vs 'struct rwlock_t')
/bpftrace/include/__btf_generated_header.h:1430:3: note: previous definition is here
/lib/modules/5.9.14-100.fc32.x86_64/source/include/linux/ratelimit_types.h:15:8: error: redefinition of 'ratelimit_state'
/bpftrace/include/__btf_generated_header.h:1622:8: note: previous definition is here
/lib/modules/5.9.14-100.fc32.x86_64/source/include/linux/jump_label.h:122:8: error: redefinition of 'jump_entry'
/bpftrace/include/__btf_generated_header.h:2382:8: note: previous definition is here
/lib/modules/5.9.14-100.fc32.x86_64/source/include/linux/time64.h:13:8: error: redefinition of 'timespec64'
/bpftrace/include/__btf_generated_header.h:2003:8: note: previous definition is here
/lib/modules/5.9.14-100.fc32.x86_64/source/include/linux/restart_block.h:16:6: error: redefinition of 'timespec_type'
/bpftrace/include/__btf_generated_header.h:397:6: note: previous definition is here
fatal error: too many errors emitted, stopping now [-ferror-limit=]

@viktormalik
Copy link
Contributor Author

Can you write up the set of "rules" regarding how type definitions are chosen/acquired? Would help users understand errors and help us maintain behavior over time.

Good idea. Actually, there are rules from the user point of view (i.e., what happens if I redefine a kernel type?) and from the implementation point of view (i.e., how the types are actually resolved?). I'd put the former into the reference guide and the latter as a comment into the code, sounds reasonable?

Also, there should be more runtime tests for this PR to test combinations of user-defined and BTF defined types. We can use REQUIRES_FEATURE btf and operate on struct task_struct (task_struct is unlikely to go away any time soon).

Ok, I'll try to come with some tests.

@danobi
Copy link
Member

danobi commented Jan 13, 2021

I'd put the former into the reference guide and the latter as a comment into the code, sounds reasonable?

Sounds great

There are 3 main refactorings done:
- make input, args, and input_files to be class members (instead of
  local variables of the parse() method) so that they don't have to be
  passed around as function arguments,
- factor out resolution of incomplete and of unknown types into separate
  methods,
- factor out parsing of files and subsequent error handling and
  diagnostics into a new method ClangParserHandler::parse_file.
BTF is used automatically, if it is available and the user did not
define any types (via user definitions or includes) that redefine types
from BTF. If the user defined aby such types, BTF is not parsed (and the
user must specify all the neccessary types).

Checking if the user redefined any types is done by running Clang parser
and searching the produced error messages for the "redefinition" word.

The --btf (-b) CLI option is left as no-op for compatibility.
@viktormalik
Copy link
Contributor Author

Instructions for users concerning BTF types are here.
Details of type resolution implementation are here (link may require to expand clang_parser.cpp).

I also added 2 runtime tests.

Copy link
Member

@danobi danobi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for working on this. Small doc nit

@@ -108,6 +108,7 @@ discussion to other files in /docs, the /tools/\*\_examples.txt files, or blog p
- [1. `printf()`: Per-Event Output](#1-printf-per-event-output)
- [2. `interval`: Interval Output](#2-interval-interval-output)
- [3. `hist()`, `printf()`: Histogram Printing](#3-hist-print-histogram-printing)
- [BTF Types](#btf-types)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is a bit more descriptive

Suggested change
- [BTF Types](#btf-types)
- [BTF Support](#btf-types)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I fixed it.

@danobi danobi merged commit b618af4 into bpftrace:master Feb 9, 2021
@viktormalik viktormalik deleted the remove-btf-option-new branch February 11, 2021 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BTF and user types
2 participants