Skip to main content

Semantic grep for C based on coccinelle

Project description

Introduction

coccigrep is a semantic grep for the C language based on coccinelle (http://coccinelle.lip6.fr). It can be used to find where a given structure is used in code files. coccigrep depends on the spatch program which comes with coccinelle.

Usage

Run coccigrep -h for complete options.

Examples

To find where in a set of files the structure named Packet is used, you can run

$ coccigrep  -t Packet *c
source-af-packet.c:272:         p = ptv->in_p;
source-af-packet.c:300:     p->datalink = ptv->datalink;
source-af-packet.c:758:     switch(p->datalink) {

To find where in a set of files the datalink attribute is used in the structure named Packet, you can simply do

$ coccigrep  -t Packet -a datalink  *c
source-af-packet.c:300:     p->datalink = ptv->datalink;
source-af-packet.c:758:     switch(p->datalink) {
source-erf-dag.c:525:     p->datalink = LINKTYPE_ETHERNET;

If you want to be more precise and find where this attribute is set, you can use the operation flag (-o). One of its value is set which indicate we only want the match where the attribute is set

$ coccigrep  -t Packet -a datalink -o set  source*c
source-af-packet.c:300:     p->datalink = ptv->datalink;
source-erf-dag.c:525:     p->datalink = LINKTYPE_ETHERNET;

Running coccigrep in vim

To use coccigrep in vim, you can use the cocci-grep.vim plugin provided in the editors directory. To do so you can simply copy it to your plugin directory which is usually ~/.vim/plugin/. If your coccigrep script in not in your path, you can use the coccigrep_path variable to give complete path. For example, you can add to your .vimrc

let g:coccigrep_path = '/usr/local/bin/coccigrep'

And then you can run commands like

:Coccigrep
:Coccigrep Packet datalink source-*.c
:Coccigrep Packet datalink set source-*.c

First command will interactively ask you the value. Second one will search all dereference of the datalink attribute for Packet structure. The last one will look where the set operation is done on the datalink attribute of Packet. To get the list of operations on your system, you can run coccigrep -L or look at the list provided when input for operation is asked in interactive mode.

The matches will appear in the quickfix list and the file corresponding to first match will be opened at the corresponding line. Note that you can use completion on structure and attribute names based on tags (generated by make tags).

To run a search in vim on a non-named structure, you must quote the spaces and thus run something like

:Coccigrep "struct nfq_data"  s*c

Please note that, in interactive mode, quoting is not necessary.

Running coccigrep in emacs

To use coccigrep in emacs, you need to load the cocci-grep.el module provided in the editors directory of the source code. For example, if you copy it in ~/.emacs.d/site-lisp/, you can do

(add-to-list 'load-path "~/.emacs.d/site-lisp/")
(require 'cocci-grep)

And then you can run something like

Meta+x cocci-grep

and answer to the questions which are

  • Type: The structure type you are searching

  • Attribut: The attribute in the structure

  • Operation: The operation on the structure. The set of commands include set,used,func,test,deref

  • Files: A blob expression that will match the file you want to search in

The matches will appear in a buffer with mode set to grep-mode and you will thus be able to jump on occurence. History is available on the different parameters.

Installation

To install coccigrep run

sudo python ./setup.py install

Configuration

As from version 0.8, coccigrep can be configured via a configuration file. A complete sample of configuration file is available in the src/coccinelle.cfg.

Hierarchical configuration

The configuration file system is hierarchical and the following files are parsed in that order

  • host config in /etc/coccigrep

  • user config in ~/.coccigrep

  • directory config in .coccigrep

Thus, for example, the directory config settings will overwrite host config settings.

Interesting options

In the global section, the concurrency_level is the most interesting. It codes the number of spatch commands that will be launched in parallel. If multiple files are search, this will increase dramatically performances at the cost of a little increase of memory usage.

If you want to add your own semantic patches, you just have to put them in a directory with name matchting the wanted operation name (zeroed.cocci will lead to the zeroed operation). Then add a local_cocci_dir pointing to this directory in the global section.

For a description of the writing of semantic patches see coccigrep homepage.

Other options are more explicit and are direct mapping of the associated command line option.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

coccigrep-1.11.tar.gz (32.7 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page