9

I am reading source code of xl2tpd, and face lots of problems when reading this code. For example I cannot find where the structure lac is defined. How do I find the definition of this structure?

I have used ctags and vim to read this code, but failed to find the structure. I googled and could not find the structure. Is there any method that can make the code reading process more comfortable? That is, I can jump to definition of most variables, functions and structures?

AlG
  • 14,697
  • 4
  • 41
  • 54
thinke365
  • 1,305
  • 3
  • 14
  • 22

4 Answers4

11

try cscope with vim. follow steps below -
1) run cscope -R in xl2tpd directory . it will create file cscope.out
2) open file with vim where structure lac is used
3) use :cs f g <lac> . now it will show the files where lac is defined .
4) choose file.h. it contain the definition .
if you are perticulerly interested in definition of struct lac it is below -

struct lac
{
    struct lac *next;
    struct host *lns;           /* LNS's we can connect to */
    struct schedule_entry *rsched;
    int tun_rws;                /* Receive window size (tunnel) */
    int call_rws;               /* Call rws */
    int rxspeed;                /* Tunnel rx speed */
    int txspeed;                /* Tunnel tx speed */
    int active;                 /* Is this connection in active use? */
    int hbit;                   /* Permit hidden AVP's? */
    int lbit;                   /* Use the length field? */
    int challenge;              /* Challenge authenticate the peer? */
    unsigned int localaddr;     /* Local IP address */    
    unsigned int remoteaddr;    /* Force remote address to this */
    char authname[STRLEN];      /* Who we authenticate as */
    char password[STRLEN];      /* Password to authenticate with */
    char peername[STRLEN];      /* Force peer name to this */
    char hostname[STRLEN];      /* Hostname to report */
    char entname[STRLEN];       /* Name of this entry */
    int authpeer;               /* Authenticate our peer? */
    int authself;               /* Authenticate ourselves? */
    int pap_require;            /* Require PAP auth for PPP */
    int chap_require;           /* Require CHAP auth for PPP */
    int pap_refuse;             /* Refuse PAP authentication for us */
    int chap_refuse;            /* Refuse CHAP authentication for us */
    int idle;                   /* Idle timeout in seconds */
    int autodial;               /* Try to dial immediately? */
    int defaultroute;           /* Use as default route? */
    int redial;                 /* Redial if disconnected */
    int rmax;                   /* Maximum # of consecutive redials */
    int rtries;                 /* # of tries so far */
    int rtimeout;               /* Redial every this many # of seconds */
    char pppoptfile[STRLEN];    /* File containing PPP options */
    int debug;
    struct tunnel *t;           /* Our tunnel */
    struct call *c;             /* Our call */
};
raj_gt1
  • 4,653
  • 2
  • 20
  • 28
  • is cscope more powerful than ctags? are they both tools to index source code? what are the differences between them? – thinke365 Sep 09 '12 at 21:17
  • This was pretty helpful! Where can I find documentation about the ":cs f g" vim command? I'm new to vim and now that I know how to find the definition, I'm trying to find who invokes the method. – radj Nov 07 '12 at 01:40
  • Nevermind. Found it in the vim help pages. ":cs" is for cscope functions, "f" is find and "g" is Find this definition. Type in ":help cs" in vim for more details. – radj Nov 07 '12 at 01:44
  • @thinke365 i personally prefer cscope..u can see more difference [hear](http://stackoverflow.com/questions/934233/cscope-or-ctags-why-choose-one-over-the-other) – raj_gt1 Jun 06 '13 at 10:11
  • There is an [comment](http://stackoverflow.com/a/9073762/1528712) on how to find the real definition of a type using a simple program, and I modify it a little so I can use it as command in terminal for any type, I really hope for a program that list the real definition of a struct. – CodyChan Oct 23 '15 at 03:33
3

When going through third-party code, there are a few tools that I have found invaluable:

I believe that the Eclipse CDT also allows you to quickly find the definition of any variable you are looking at, but I have not actually used it - I prefer using console programs for my actual C coding.

None of those are vim-based, although at least ctags can be used via vim or emacs. Nevertheless, they can be very useful when exploring a new codebase that you know nothing about...

thkala
  • 84,049
  • 23
  • 157
  • 201
1

Are you talking about this?

The source code already comes with a tags file.

Loading any file (common.h in my case) in Vim you can use :tag lac to jump to the first definition of lac or :tselect lac to choose between the 3 occurrences in this project and :tag gconfig to jump to the unique definition of gconfig.

See :help tags.

romainl
  • 186,200
  • 21
  • 280
  • 313
0

I'm using vim + cscope and have the same issue with you. I find a way to workaround this issue.

in vim, search the text instead of the definition. for example, in the linux kernel source code, if you're trying to find "struct file", commands this:

cs find t struct file {

you will have a accurate definition timely in most cases, take care, no quotation mark for the text "struct file {".

hope it will help you.

Jinhua Wu
  • 1
  • 1