> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/sast-and-sca/ocular/common-queries.md).

# Common Queries

![](/files/mlZo7QhbBaWAQlpvEVi9)note

The following examples were written for the source code of the VLC Media Player v3.0.8. You can download the source code [here](https://github.com/videolan/vlc/archive/3.0.0-git.tar.gz).

## Basic Analysis <a href="#basic-analysis" id="basic-analysis"></a>

### Methods <a href="#methods" id="methods"></a>

* List all methods that match `.*parse.*` to the shell

```
ocular> cpg.method.name(".*parse.*").name.l
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Dump all methods that match `.*parse.*` to the shell (syntax-highlighted)

```
ocular> cpg.method.name(".*parse.*").dump
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Create K-V pair of all methods that match `.*parse.*` and their code

```
ocular> cpg.method.name(".*parse.*").map( m=> (m.name, m.start.dump)).l
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Dump all methods that match `.*parse.*` to file (no highlighting)

```
ocular> cpg.method.name(".*parse.*").dumpRaw |> "/tmp/foo.c"
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* View all methods that match `.*parse.*` in a pager (e.g., less)

```
ocular> browse(cpg.method.name(".*parse.*").dump)
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Dump dot representations of ASTs for all methods that match `parse` into file

```
ocular> cpg.method.name("parse_public_key_packet").dot |> "/tmp/foo.dot"
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Find all local variables defined in a method

```
ocular> cpg.method.name("parse_public_key_packet").local.name.l
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Find which file and line number they are in

```
ocular> cpg.method.name("parse_public_key_packet").location.map( x=> (x.lineNumber.get, x.filename)).l
```

![](/files/CRrxFX9wuboXxDrdB2dv)

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Find the type of the first local variable defined in a method

```
ocular> cpg.method.name("parse_public_key_packet").local.typ.name.l.head
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Find all outgoing calls (call-sites) in a method

```
ocular> cpg.method.name("parse_public_key_packet").callOut.name.l
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Find which method calls a method

```
ocular> cpg.method.name("parse_public_key_packet").caller.name.l
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

### Types and Filters <a href="#types-and-filters" id="types-and-filters"></a>

* List all local variables of type `vlc_.*`

```
ocular> cpg.types.name("vlc_.*").localsOfType.name.l
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Find member variables of a struct

```
ocular> cpg.types.name("vlc_log_t").map( x=> (x.name, x.start.member.name.l)).l
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Find local variables and filter them by their type

```
ocular> cpg.local.where(_.typ.name("vlc_log_t")).name.l
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Which method are they used in?

```
ocular> cpg.local.where(_.typ.name("vlc_log_t")).method.dump
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Get the filenames where these methods are

```
ocular> cpg.local.where(_.typ.name("vlc_log_t")).method.file.name.l
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

## Insights <a href="#insights" id="insights"></a>

### Overview <a href="#overview" id="overview"></a>

* Identify functions with more than 4 parameters

```
ocular> cpg.method.filter(_.parameter.size > 4).name.l
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Identify functions with > 4 control structures (cyclomatic complexity)

```
ocular> cpg.method.filter(_.controlStructure.size > 4).name.l
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Identify functions with more than 500 lines of code

```
ocular> cpg.method.filter(_.numberOfLines >= 500).name.l
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Identify functions with multiple return statements

```
ocular> cpg.method.filter(_.ast.isReturn.l.size > 1).name.l
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Identify functions with more than 4 loop statements (including loops nested inside other loops)

```
ocular> cpg.method.filter(_.ast.isControlStructure.parserTypeName("(For|Do|While).*").size > 4).name.l
```

![](/files/CRrxFX9wuboXxDrdB2dv)

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Identify functions with nesting depth larger than 3

```
ocular> cpg.method.filter(_.depth(_.isControlStructure) > 3).name.l
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

### Calls into libraries <a href="#calls-into-libraries" id="calls-into-libraries"></a>

* All names of external methods used by the program

```
ocular> cpg.method.external.name.l.distinct.sorted
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* All calls to strcpy

```
ocular> cpg.call("str.*").code.l
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* All methods that call strcpy

```
ocular> cpg.call("str.*").method.name.l
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Looking into parameters: second argument to sprintf is NOT a literal

```
ocular> cpg.call("sprintf").argument(2).whereNot(_.isLiteral).code.l
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)

* Quickly see this method above

```
ocular> cpg.call("sprintf").argument(2).whereNot(_.isLiteral).dump
```

![](/files/DfHDmRpYn5HJOmhRtTva) ![](/files/P5vj8JOtHNJWVEvAqKym)
