[Cocci] use Coccinelle to perform "queries" on C code
julia.lawall at lip6.fr
Thu Mar 1 08:03:18 CET 2012
On Thu, 1 Mar 2012, David Wagner wrote:
> On 29/02/2012 07:29, Julia Lawall wrote:
>> On Wed, 29 Feb 2012, David Wagner wrote:
>> With Coccinelle, you can make arbitrarily complicated matches. Look at
>> the examples on the webiste. They are a bit oriented towards bug
>> finding, but you should get the idea.
>>> I also imagined it could be possible to parse the code and build an
>>> SQL database from it but I'm not sure that it is fit and I can only
>>> begin to imagine all the problems I would run into.
>> You can also use python or ocaml to insert information into a database.
> Do you know of someone who has done that or is it just a suggestion ? Maybe
> it could be worth a try once I manage to get a grasp at spatch. It's great
> that there is a Python support; that will save me some effort (or you could
> say it's a shame I won't have to learn OCaml) :)
We have done that. We just used the programming language to run the
appropriate shell command. Actually we tried that option and the option
of printing the commands to a file and then running that file. The latter
has the advantage that when something goes wrong withyour database, you
can just rerun the file. But the file can take up a lot of disk space.
>> Here is your semantic patch, using ocaml scripting:
>> let tbl = Hashtbl.create 101
>> identifier virtual.func;
>> expression e1,e2;
>> e2 << match.e2;
>> if not (Hashtbl.mem tbl e2) then Hashtbl.add tbl e2 ()
>> let elements =
>> Hashtbl.fold (fun key _ rest -> key :: rest) tbl  in
>> Printf.printf "(%s)" (String.concat ", " elements)
>> func is a virtual identifier so you can give it on the command line:
>> spatch -sp_file david.cocci -D func=usb_submit_urb -dir /path/to/linux
>> (flags, GFP_KERNEL, gfp_flags, GFP_ATOMIC, mf, gfp, mem_flags, GFP_NOIO,
>> gfp_mask, f)
> Awesome !
> I guess it could be possible with something like
> e2 = x
> to be sometimes able to get the real value of gfp_flags, for instance ?
> Is "nesting"/"including" spatches possible ?
Not sure what you mean. I think you can say include "foo.cocci" if you
have some code you want to use in many rules, but I'm not sure that that
is what you mean.
> Anyway, I will explore the possibilities of Coccinelle and find out by
>> If you want to do this on a large code base, you may want to run glimpse
>> or idutils on it first to improve efficiency. For glimpse, there is
>> coccinelle/scripts/glimpseindex_cocci.sh to use the right options. Then
>> run coccinelle with --use-glimpse. Glimpse is not free software though.
>> I guess you just run it in the normal way and then use the option
>> --use-idutils with spatch.
> Thanks a lot for your help !
More information about the Cocci