From: Avi Kivity <avi@redhat.com>
To: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Alexander Graf <agraf@suse.de>,
Marcelo Tosatti <mtosatti@redhat.com>,
kvm@vger.kernel.org
Subject: Re: [PATCH kvm-unit-tests 2/4] Introduce a C++ wrapper for the kvm APIs
Date: Mon, 29 Nov 2010 11:22:44 +0200 [thread overview]
Message-ID: <4CF370E4.5050903@redhat.com> (raw)
In-Reply-To: <20101128165758.GE12874@redhat.com>
On 11/28/2010 06:57 PM, Michael S. Tsirkin wrote:
> > >
> > >sparce lets you solve C problems that C++ inherited as is.
> > >E.g. if you have a pointer you can always dereference it.
> >
> > It's the other way round.
> >
> > For example __user cannot be done in C. It has to be done as an add-on.
> >
> > In C++ it's simply:
> >
> > template<class T>
> > class user_ptr<T>
> > {
> > public:
> > explicit user_ptr(unsigned long addr);
> > void copy_from(T& to); // throws EFAULT
> > void copy_to(const T& from); // throws EFAULT
> > private:
> > unsigned long addr;
> > };
>
> This does not allow simple uses such as arithmetic,
Add a raw_addr() method that returns addr.
> void,
Fixable.
> builtin
> types,
should work
> sizeof,
sizeof(T)?
> arrays,
should work
> NULL comparizon,
Do we ever compare __user pointers against NULL? It's a valid address.
> inheritance, cast,
What do these mean in the context of user pointers?
> memory management.
And this?
> Examples to ponder: what is the appropriate value of T for void *?
Probably a specialization user_ptr<> or a separate class user_void_ptr.
After all you can't do anthing with such a pointer.
> What if you want a shared/auto ptr to manage this memory?
What does it mean for user pointers?
> Some of these might be fixable, with a lot of code.
> Boost might haver some solutions, I haven't looked.
>
> Meanwhile sparse is already there.
With sparse you have to implement every rule in a separate compiler.
With C++ you introduce the rules into the code.
> > No need for an additional toolchain.
>
> It's a feature :) This way you are not forced to rewrite all code each
> time you realize you need an extra check, and checks can be added
> gradually without breaking build.
You can see that user_ptr<> is not just for the checks, it adds
functionality (sizeof-less copy_from and copy_to). That's usually the
case. If there's something you must not do because of some rule,
there's also something you want to do, and those become member functions.
In C++ you could also introduce user_ptr<> gradually, it won't break
anything.
> > >>
> > >> Things like __user are easily done in C++.
> > >
> > >Some of what sparce does can be done if you create a separate type for
> > >all address spaces. This can be done in C too, and the result won't
> > >be like __user at all.
> >
> > That's quite a lot of work.
> >
> > Sparse: T __user *foo;
> > C++: user_ptr<T> foo;
>
> Sparse has some advantages: it makes the contract obvious so you clearly
> see it's a pointer and know ->, [], + will work, * and<< will not.
I don't really see how you can tell this from __user. You have to look
up the definition. For user_ptr<>, the definition is actually available.
> > C : struct T_user_ptr { unsigned long addr } foo; + lots of accessors.
>
> Some kind of macro can be closer to user_ptr above.
Those macros are called templates, and the compiler can check that they
are used correctly.
> >
> > >> >C++ support in gdb has some limitations
> > >> >if you use overloading, exceptions, templates. The example posted here
> > >> >uses two of these, so it would be harder to debug.
> > >>
> > >> I haven't seen issues with overloading or exceptions.
> > >
> > >Build your test with -g, fire up gdb from command line,
> > >try to put a breakpoint in the constructor of
> > >the fd object, maybe you will see what I mean :)
> > >
> > (gdb) break 'kvm::fd::fd'
> > Breakpoint 3 at 0x8049650: file api/kvmxx.cc, line 25.
> > Breakpoint 4 at 0x8049628: file api/kvmxx.cc, line 31.
> > Breakpoint 5 at 0x8049080: file api/kvmxx.cc, line 21
>
> But it's hard to figure out that you need the kvm namespace. Your code
> only has one namespace, but with multiple namespaces, you don't even
> know in which namespace to look up the fd. With templates you might not
> even know the fd class.
If you like, you can avoid namespaces and prefix everything with kvm_.
I never found it necessary.
> > >An example of an issue with overloading is that gdb seems unable to resolve
> > >them properly depending on the current scope. So you see a call to
> > >foo() and want to put a breakpoint there, first problem is just to find
> > >one which namespace it is in. Once you did the best
> > >it can do it prompt you to select one of the overloaded options.
> > >How do you know which one do you want? You don't, so you guess. Sometimes
> > >gdb will guess, because of a complex set of name resolution rules, and
> > >sometimes it will this wrongly.
> > >Which is not what I want to spend mental cycles on when I am debugging a problem.
> > >
> > >Functions using exceptions can not be called from the gdb prompt
> > >(gdb is not smart enough to catch them).
> > >
> > >There are more issues.
> >
> > That's not restricted to gdb. C has just three scopes: block (may
> > be nested), file static, and global. C++ has more. Stating
> > everything leads to verbose code and potential conflicts. Having
> > more scopes allows tighter code usually
> >
> > but more head-scratching if
> > something goes wrong.
> >
> > In my experience conflicts are very rare. But it's true that when
> > they happen they can be surprising.
>
> There is some difference: when you write code, conflicts are rare
> because you let the compiler guess the right call from the scope. When
> you read code or debug, conflicts are everywhere. They are rarely
> surprising, they are always time consuming to resolve.
I guess this is subjective. If you use good tools this shouldn't happen
though.
> > >> Templates are
> > >> indeed harder to debug, simply because names can become very long.
> > >
> > >That's not the only problem. A bigger one is when you type tab to
> > >complete function name and get a list of options to select from for each
> > >of the times a template was instantiated. Only one of them is relevant
> > >in a given scope. No hint is given which. Further when you step into
> > >the template, the source does not give you any hint about the types
> > >used.
> > >
> > >Some of this is true for macros as well of course. Except people know
> > >macros are bad and so make them thin wrappers around proper functions.
> >
> > Or they simply avoid it and duplicate the code. You can't always
> > wrap functions with macros.
>
> Always is a strong word. What are the examples of such duplicated code
> in qemu? Let's see if they are easy to fix.
qemu in fact uses macros extensively (glue()), they are hardly readable.
Do a 'git grep hash' for examples of duplication.
--
error compiling committee.c: too many arguments to function
next prev parent reply other threads:[~2010-11-29 9:22 UTC|newest]
Thread overview: 130+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-11-24 10:52 [PATCH kvm-unit-tests 0/4] API test framework Avi Kivity
2010-11-24 10:52 ` [PATCH kvm-unit-tests 1/4] Makefile: add support for C++ Avi Kivity
2010-11-24 10:52 ` [PATCH kvm-unit-tests 2/4] Introduce a C++ wrapper for the kvm APIs Avi Kivity
2010-11-24 12:59 ` Alexander Graf
2010-11-24 13:33 ` Gleb Natapov
2010-11-24 14:18 ` Anthony Liguori
2010-11-24 14:24 ` Anthony Liguori
2010-11-24 14:32 ` Avi Kivity
2010-11-24 14:31 ` Avi Kivity
2010-11-24 14:41 ` Anthony Liguori
2010-11-24 15:40 ` Gleb Natapov
2010-11-24 15:50 ` Anthony Liguori
2010-11-24 16:10 ` Avi Kivity
2010-12-02 13:52 ` Mike Day
2010-11-24 16:12 ` [PATCH kvm-unit-tests 2/4] " Gleb Natapov
2010-11-24 16:14 ` Avi Kivity
2010-11-24 16:21 ` Gleb Natapov
2010-11-24 16:25 ` Avi Kivity
2010-11-24 16:29 ` Gleb Natapov
2010-11-24 16:33 ` Avi Kivity
2010-11-24 16:52 ` Gleb Natapov
2010-11-24 16:56 ` Avi Kivity
2010-11-24 17:02 ` Gleb Natapov
2010-11-24 17:07 ` Avi Kivity
2010-11-24 17:10 ` Jes Sorensen
2010-11-24 17:12 ` Avi Kivity
2010-11-24 17:14 ` Anthony Liguori
2010-11-24 16:43 ` Anthony Liguori
2010-11-24 16:48 ` Gleb Natapov
2010-11-24 16:56 ` Anthony Liguori
2010-11-24 17:04 ` Gleb Natapov
2010-11-24 16:40 ` Anthony Liguori
2010-11-24 17:33 ` Gleb Natapov
2010-11-24 17:39 ` Avi Kivity
2010-11-24 17:41 ` Gleb Natapov
2010-11-24 17:50 ` Avi Kivity
2010-11-24 18:23 ` Gleb Natapov
2010-11-24 18:50 ` Avi Kivity
2010-11-24 18:17 ` Anthony Liguori
2010-11-24 18:34 ` Gleb Natapov
2010-11-24 18:53 ` Anthony Liguori
2010-11-25 8:35 ` Gleb Natapov
2010-11-24 16:40 ` Jes Sorensen
2010-11-24 16:47 ` Avi Kivity
2010-11-24 16:51 ` Jes Sorensen
2010-11-24 16:57 ` Anthony Liguori
2010-11-24 17:29 ` Avi Kivity
2010-11-24 16:59 ` Avi Kivity
2010-11-24 17:06 ` Jes Sorensen
2010-11-24 17:11 ` Avi Kivity
2010-11-24 17:17 ` Jes Sorensen
2010-11-24 17:25 ` Avi Kivity
2010-11-24 17:28 ` Jes Sorensen
2010-11-24 17:31 ` Avi Kivity
2010-11-24 17:36 ` Gleb Natapov
2010-11-24 17:41 ` Avi Kivity
2010-11-24 17:27 ` Anthony Liguori
2010-11-24 17:35 ` Avi Kivity
2010-11-24 17:36 ` Jes Sorensen
2010-11-24 17:41 ` Avi Kivity
2010-11-24 17:43 ` Gleb Natapov
2010-11-24 17:50 ` Avi Kivity
2010-11-24 18:10 ` Gleb Natapov
2010-11-24 18:55 ` Avi Kivity
2010-11-24 19:29 ` Jes Sorensen
2010-11-24 19:33 ` Avi Kivity
2010-11-24 17:43 ` Jes Sorensen
2010-11-24 17:51 ` Avi Kivity
2010-11-24 17:43 ` Anthony Liguori
2010-11-24 17:45 ` Jes Sorensen
2010-11-24 17:51 ` Avi Kivity
2010-11-24 18:01 ` Anthony Liguori
2010-11-24 18:56 ` Avi Kivity
2010-11-24 16:55 ` Gleb Natapov
2010-11-24 17:01 ` Avi Kivity
2010-11-24 17:16 ` Gleb Natapov
2010-11-24 17:26 ` Avi Kivity
2010-11-24 16:53 ` Anthony Liguori
2010-11-24 17:03 ` Jes Sorensen
2010-11-28 12:27 ` Michael S. Tsirkin
2010-11-28 22:04 ` Anthony Liguori
2010-11-28 22:28 ` Michael S. Tsirkin
2010-11-28 23:13 ` Anthony Liguori
2010-11-29 8:04 ` Michael S. Tsirkin
2010-11-29 13:44 ` Anthony Liguori
2010-11-29 13:48 ` Avi Kivity
2010-11-24 16:29 ` Jes Sorensen
2010-11-24 16:34 ` Avi Kivity
2010-11-24 16:44 ` Jes Sorensen
2010-11-24 16:49 ` Avi Kivity
2010-11-28 11:59 ` Michael S. Tsirkin
2010-11-28 13:02 ` Avi Kivity
2010-11-28 13:57 ` Michael S. Tsirkin
2010-11-28 14:34 ` Avi Kivity
2010-11-28 16:57 ` Michael S. Tsirkin
2010-11-29 9:22 ` Avi Kivity [this message]
2010-11-29 10:47 ` Michael S. Tsirkin
2010-11-29 10:52 ` Avi Kivity
2010-11-29 11:26 ` Michael S. Tsirkin
2010-11-29 13:38 ` Anthony Liguori
2010-11-24 14:10 ` Anthony Liguori
2010-11-24 14:29 ` Avi Kivity
2010-11-24 14:45 ` Anthony Liguori
2010-11-24 14:53 ` Avi Kivity
2010-11-24 14:55 ` Anthony Liguori
2010-11-25 16:32 ` Avi Kivity
2010-11-26 10:16 ` Michael S. Tsirkin
[not found] ` <4CF0CB9A.5060403@redhat.com>
2010-11-28 8:58 ` Michael S. Tsirkin
2010-11-28 9:31 ` Avi Kivity
2010-11-28 9:50 ` Michael S. Tsirkin
2010-11-28 9:54 ` Avi Kivity
2010-11-28 11:44 ` Michael S. Tsirkin
2010-11-28 13:14 ` Avi Kivity
2010-11-28 14:40 ` Michael S. Tsirkin
2010-11-28 22:12 ` Anthony Liguori
2010-11-29 9:30 ` Avi Kivity
2010-11-28 11:49 ` Michael S. Tsirkin
2010-11-28 13:15 ` Avi Kivity
2010-11-28 14:49 ` Michael S. Tsirkin
2010-11-29 9:30 ` Avi Kivity
2010-11-24 10:52 ` [PATCH kvm-unit-tests 3/4] Add support for calling a function in guest mode Avi Kivity
2010-11-26 14:17 ` Michael S. Tsirkin
[not found] ` <4CF0CC26.8030407@redhat.com>
2010-11-28 8:59 ` Michael S. Tsirkin
2010-11-28 9:22 ` Avi Kivity
2010-11-24 10:52 ` [PATCH kvm-unit-tests 4/4] Add sample test using the api test harness Avi Kivity
2010-11-26 14:17 ` Michael S. Tsirkin
[not found] ` <4CF0CC4A.8070100@redhat.com>
2010-11-28 9:04 ` Michael S. Tsirkin
2010-11-28 9:21 ` Avi Kivity
2010-11-29 16:09 ` [PATCH kvm-unit-tests 0/4] API test framework Marcelo Tosatti
2010-12-01 10:38 ` Avi Kivity
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4CF370E4.5050903@redhat.com \
--to=avi@redhat.com \
--cc=agraf@suse.de \
--cc=kvm@vger.kernel.org \
--cc=mst@redhat.com \
--cc=mtosatti@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.