Difference between revisions of "VPI DEBUG"

From Vlsiwiki
Jump to: navigation, search
(Created page with '==Retrieved on 11/25/09 from http://bbs.dicder.com/archiver/index.php?tid-499.html== <strong>5life</strong> 发表于 2006-3-13 04:41 PM</p> <h3>How to Debug PLI, DPI, and Dire…')
 
(Retrieved and Adapted on 11/25/09 from http://bbs.dicder.com/archiver/index.php?tid-499.html)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
==Retrieved on 11/25/09 from http://bbs.dicder.com/archiver/index.php?tid-499.html==
+
==Retrieved and Adapted on 11/25/09 from http://bbs.dicder.com/archiver/index.php?tid-499.html==
  
<strong>5life</strong> 发表于 2006-3-13 04:41 PM</p>
 
  
<h3>How to Debug PLI, DPI, and DirectC applications using gdb</h3>&nbsp;&nbsp;NTRODUCTION:<font style="font-size: 0px;">�Z*u�C�p
 
A L!K�^*k</font><br>
 
<font style="font-size: 0px;">3N:l�|#^�t'w#m�r4j</font><br>
 
You can add new functionality to a Verilog simulator such as VCS by<br><span style="display: none;">*c�N:z5|�K�h�A�^</span>
 
linking in your C code.&nbsp;&nbsp;You can also call C code from OpenVera<br><span style="display: none;">�|�q%R�e7u�Q'Q!o6N
 
b�a</span>
 
testbenches.&nbsp;&nbsp;Perhaps you have a C model, made of routines in the file<br><span style="display: none;">6o�J
 
z�N(\;b�a�f!{ w,k</span>
 
  
model.c, and invoked with the Verilog system task $model. The C code<br><span style="display: none;">�U�s�x�y�h9o</span>
+
<h3>How to Debug PLI, VPI, and DirectC applications using gdb</h3>
is known as the application, and will consist of one or more routines.<font style="font-size: 0px;">:T�['R!{�d)m</font><br>
+
==INTRODUCTION==
The connection between the application and the simulator is the PLI,<br><span style="display: none;">9^�U�b�j*a�j#y�k</span>
+
<br>
the Programming Language Interface.&nbsp;&nbsp;The application will be called<br><span style="display: none;">0E�a�x�f�u</span>
+
<br>
during simulation either directly, when VCS executes the system task<font style="font-size: 0px;">�E�G:w�K�}-J�`(j8y X.z</font><br>
+
You can add new functionality to a Verilog simulator such as VCS by<br>
for the application ($model), or asynchronously when VCS is doing an<font style="font-size: 0px;">%g�D&amp;Q�e�c*h5{3O</font><br>
+
linking in your C code. You can also call C code from OpenVera<br>
 +
testbenches. Perhaps you have a C model, made of routines in the file<br>
  
activity such as initialization or shutdown.<br><span style="display: none;">(p�A�]/d){�n7~!E</span>
+
model.c, and invoked with the Verilog system task $model. The C code<br>
<br><span style="display: none;">�P3`2L3n�a4S�_�X�]</span>
+
is known as the application, and will consist of one or more routines.<br>
Alternately, you want to call a C routine such as log2() directly<font style="font-size: 0px;"> @*q8o1J�A�P�D%H</font><br>
+
The connection between the application and the simulator is the PLI,<br>
using VCS&amp;#39;s DirectC interface, or the new DPI (Direct Procedural<br><span style="display: none;">�]�F7a�`�o�z"O</span>
+
the Programming Language Interface.  The application will be called<br>
Interface) that is part of SystemVerilog.&nbsp;&nbsp;Only the linking differs.<br><span style="display: none;">�a�q&amp;G�A2B&amp;w</span>
+
during simulation either directly, when VCS executes the system task<br>
 +
for the application ($model), or asynchronously when VCS is doing an<br>
  
<br><span style="display: none;">+O�|4@�N0f�A
+
activity such as initialization or shutdown.<br>
s$E.\�h</span>
+
<br>
<font style="font-size: 0px;">�q�h�j
+
Alternately, you want to call a C routine such as log2() directly<br>
d%B!}*d</font><br>
+
using VCS  DirectC interface, or the new DPI (Direct Procedural<br>
COMPILING:<br><span style="display: none;">�B�F;Q�D.Z.|�P&amp;p</span>
+
Interface) that is part of SystemVerilog.  Only the linking differs.<br>
<br><span style="display: none;">�@:}"M�S-M
+
<br><br>
?,w</span>
+
==COMPILING==
You must do several steps to debug the C application.&nbsp;&nbsp;The C code must<br><span style="display: none;">�D�w:h�x�Z</span>
+
<br>
be compiled for debug, linked with the Verilog code, the simulator<font style="font-size: 0px;">
+
<br>
?�~�g�p�A�Q)Z9q3o9s</font><br>
+
You must do several steps to debug the C application. The C code must<br>
 +
be compiled for debug, linked with the Verilog code, the simulator<br>
  
must be started with the debugger, and a breakpoint needs to be added<font style="font-size: 0px;">�M,W3E/C�K%e5L</font><br>
+
must be started with the debugger, and a breakpoint needs to be added<br>
in the application.&nbsp;&nbsp;This guide only covers using the GNU gcc compiler<font style="font-size: 0px;">�P�s�z�]�s:i:[4F/q9P3R�|</font><br>
+
in the application. This guide only covers using the GNU gcc compiler<br>
and the matching gdb debugger.&nbsp;&nbsp;Other tools will have a similar flow.<font style="font-size: 0px;">�N�|$q�R#b�y</font><br>
+
and the matching gdb debugger. Other tools will have a similar flow.<br>
<br><span style="display: none;">�J"M�z�t c�]6m</span>
+
<br>
First, the C code must be compiled with -g so that its debug symbols, such<font style="font-size: 0px;">%N�o;m7S#x�P�I�|�e2W</font><br>
+
First, the C code must be compiled with -g so that its debug symbols, such<br>
as variables and routine names, will be visible at run-time.<br><span style="display: none;">�l�y(K,?0N&amp;Q�`</span>
+
as variables and routine names, will be visible at run-time.<br>
  
<font style="font-size: 0px;">�E/g.|2?/A�o</font><br>
+
<br>
&amp;nbsp; &amp;nbsp;&amp;gt; gcc -g -o model.c -I${VCS_HOME}/include<font style="font-size: 0px;">�B6p!V-}�~�y%}</font><br>
+
  gcc -g -o model.c -I${VCS_HOME}/include<br>
<br><span style="display: none;">�z�b�n�e8[!t</span>
+
<br>
The -o switch tells gcc to create object code (model.o) and not an<font style="font-size: 0px;">;_!`�h�A.V O�i</font><br>
+
The -o switch tells gcc to create object code (model.o) and not an<br>
executable.&nbsp;&nbsp;The -I switch points to the VCS include files that define<br><span style="display: none;">�K�c+~�c.?9H�K6{%r-e</span>
+
executable. The -I switch points to the VCS include files that define<br>
  
constants and the PLI routine names.<font style="font-size: 0px;">(s�U6\�v�^�|3q�W v�I&amp;F8I B-l</font><br>
+
constants and the PLI routine names.<br>
<font style="font-size: 0px;">�?9U�Z-w C'I�I!k"u�P</font><br>
+
<br>
Next link the C code into your simulation:<br><span style="display: none;">0l�K�L,j�e�I/n x�H</span>
+
Next link the C code into your simulation:<br>
<br><span style="display: none;">�M!l�U/r�O%b3j</span>
+
<br>
&amp;nbsp; &amp;nbsp;&amp;gt; vcs design.v -P model.tab model.o<font style="font-size: 0px;">9e�P5j�h�W'C�x</font><br>
+
  vcs design.v -P model.tab model.o<br>
  
<br><span style="display: none;">�p+u#S-@�~�|</span>
+
<br>
The Verilog code is in design.v, your C code is in model.o, and<br><span style="display: none;">*N7L�E.B0y'b�{</span>
+
The Verilog code is in design.v, your C code is in model.o, and<br>
model.tab tells VCS which C routines to call when $model is used in<font style="font-size: 0px;">)X�F6{ e:J)e'm&amp;d9`�~</font><br>
+
model.tab tells VCS which C routines to call when $model is used in<br>
the Verilog.&nbsp;&nbsp;This produces the executable simv.<br><span style="display: none;">*S�N�K�Z�G�@</span>
+
the Verilog. This produces the executable simv.<br>
<font style="font-size: 0px;">�h�l�?3P�j#V�Q$T</font><br>
+
<br>
Alternately, you can compile the Verilog and C code together.&nbsp;&nbsp;Use the<br><span style="display: none;">�}�p�Z�?�|�W</span>
+
Alternately, you can compile the Verilog and C code together.;Use the<br>
  
VCS -CFLAGS option to pass flags to the C compiler: &amp;gt; vcs design.v -P<br><span style="display: none;">�~4Q0n:^*p�i�l</span>
+
VCS -CFLAGS option to pass flags to the C compiler:  
model.tab model.c \ -CFLAGS &amp;quot;-g -I${VCS_HOME}/include&amp;quot;<br><span style="display: none;">#P�A0U�}+r
+
  vcs design.v -P model.tab model.c \ -CFLAGS "-g -I${VCS_HOME}/include" <br>
@
+
<br>
]
+
If you are using DirectC, you don't need a table file:<br>
~-I7f�j</span>
+
<br>
<font style="font-size: 0px;">'N�{-`,^/u�g9b�]</font><br>
+
If you are using DirectC, you don&amp;#39;t need a table file:<font style="font-size: 0px;">1C�f&amp;W�w�z,C
+
O�r</font><br>
+
<br><span style="display: none;">$Q�h:X8_2U�O9[</span>
+
  
&amp;nbsp; &amp;nbsp;&amp;gt; vcs +dc design.v model.o<font style="font-size: 0px;"> A:S3z"r�Y+c</font><br>
+
  vcs +dc design.v model.o<br>
<br><span style="display: none;">(g�n
+
<br>
E
+
<br>
S
+
==COMPILING ATC Testbenches using Rake==
[2T/Z,_6d</span>
+
Currently, rake does not support extra CFLAGS to be passed to VCS or Modelsim.
<font style="font-size: 0px;">�p!^�t7h R:B�X R</font><br>
+
One has to run rake with the v=1 option, which will display the commands, not invoke them.
<br><span style="display: none;">Z/O�P�_�M6K</span>
+
Then, by virtue of copy & paste,  you can edit the command and add the -g flag into the -CC field.
RUNNING THE DEBUGGER<font style="font-size: 0px;">8u�w)f*b5p+n*N8\</font><br>
+
<font style="font-size: 0px;">:Z�z!m7h�_)Z</font><br>
+
  
Now you are ready to roll!&nbsp;&nbsp;Start the debugger and tell it which<br><span style="display: none;">�r�M�j�_�M"A I/a%a</span>
+
  rake test:test16 v=1
executable to use:<font style="font-size: 0px;">�W"`�I�R�?.y�E#Q�Y�\</font><br>
+
<font style="font-size: 0px;">7f�f�s
+
O�h�S</font><br>
+
&amp;nbsp; &amp;nbsp;&amp;gt; gdb simv<br><span style="display: none;">r�~�d3r,n�~9R</span>
+
&amp;nbsp; &amp;nbsp;<font style="font-size: 0px;">�k%N�R�B�N</font><br>
+
  
&amp;nbsp; &amp;nbsp;(gdb)<br><span style="display: none;">,m"E2`�F�i�u%D*g s</span>
+
  ... ...
<br><span style="display: none;">�R+H-_�q�c t</span>
+
  vcs -full64 +warn=noSV-LCM-PPWI +v2k -sverilog +cli -q -debug -CC "-Wno-write-strings -I${VCS_HOME}/include ... "
Here you can set a breakpoint on a C routine:<font style="font-size: 0px;">
+
  add the -g option into the quoted string following the -CC vcs option. Like so:
z3{!d�r�H�V�L�w�B�}#q</font><br>
+
  vcs -full64 +warn=noSV-LCM-PPWI +v2k -sverilog +cli -q -debug -CC "'''-g''' -Wno-write-strings -I${VCS_HOME}/include ... "
<font style="font-size: 0px;">�y�v�b�t,E$Q�]6d,|</font><br>
+
&amp;nbsp; &amp;nbsp;(gdb) br model_call<br><span style="display: none;">$F�b$o�|�D</span>
+
  
&amp;nbsp; &amp;nbsp;Breakpoint 1 at 0x8050ba6: file model.c, line 12.<br><span style="display: none;">�Q�}�Q+P4J</span>
+
Run it, and proceed to the next section.
<font style="font-size: 0px;">'D�q$y'A�\�H�u1e�X</font><br>
+
<br>
Now run the simulation, specifying the command line switches to use with simv:<br><span style="display: none;">*i"H�G-I1h�t</span>
+
==RUNNING THE DEBUGGER==
<br><span style="display: none;">(~9g3X�w�}�Q�r$^</span>
+
<br>
&amp;nbsp; &amp;nbsp;(gdb) run -l simv.log<font style="font-size: 0px;">�o*c6r @6V&amp;w�M�o�E�]:K</font><br>
+
<br>
  
  &amp;nbsp; &amp;nbsp;Starting program: /disk/user/simv<font style="font-size: 0px;">2q3F$I�c+c�c-^�K�`</font><br>
+
Now you are ready to roll! Start the debugger and tell it which<br>
&amp;nbsp; &amp;nbsp;Chronologic VCS Simulator copyright 1991-2004<font style="font-size: 0px;">9@�^8^$R�G�P
+
executable to use:<br>
N�X</font><br>
+
<br>
&amp;nbsp; &amp;nbsp;...<font style="font-size: 0px;">6M�V�f5K$u�B�X�i4c</font><br>
+
  gdb simv<br>
&amp;nbsp; &amp;nbsp;Breakpoint 1, model_call() at model.c:12<font style="font-size: 0px;">�{3?�V$b�Z
+
<br>
B�t</font><br>
+
  
  &amp;nbsp; &amp;nbsp;(gdb)<font style="font-size: 0px;">�n-L�@�^�q�o4l</font><br>
+
  (gdb)<br>
<font style="font-size: 0px;">'q�j8L�{�b)m�O)V</font><br>
+
Here you can set a breakpoint on a C routine:<br>
Now you can step through the code (step and next), print variable<font style="font-size: 0px;">*}�m9x:|�E)z�_(\�_</font><br>
+
<br>
values (print and x), look at the call stack (bt) and other debugger<br><span style="display: none;">�z�X�R)~ B�E�S%v4A(R�g�@</span>
+
  (gdb) br model_call<br>
tricks.&nbsp;&nbsp;If you run gdb inside Emacs (M-X gdb) you can see the source<br><span style="display: none;">'k%Y1[�}�`�i�{&amp;w&amp;H</span>
+
  
code in one window while the debugger runs in the other.<font style="font-size: 0px;">:r�k�h�Y�^�p�D</font><br>
+
  Breakpoint 1 at 0x8050ba6: file model.c, line 12.<br>
<br><span style="display: none;">�{�x�`�G8_1_�L8\&amp;p</span>
+
<br>
This guide only shows how to debug code linked directly with VCS, not<font style="font-size: 0px;">�P�c�F�h�O/@�Y�O</font><br>
+
Now run the simulation, specifying the command line switches to use with simv:<br>
shareable code.&nbsp;&nbsp;Shareable code is only loaded into memory when<font style="font-size: 0px;">&amp;q4^4W
+
<br>
N�b
+
(gdb) run -l simv.log<br>
g�o</font><br>
+
needed, not at the start of execution.&nbsp;&nbsp;So you can not set a<font style="font-size: 0px;">�D1L$P1C
+
]�h</font><br>
+
  
breakpoint on your code when gdb loads the executable as your code is<font style="font-size: 0px;">4V-c�_!x�L�{6s�x)Q5}</font><br>
+
Starting program: /disk/user/simv<br>
 +
Chronologic VCS Simulator copyright 1991-2004
 +
<br>
 +
<br>
 +
Breakpoint 1, model_call() at model.c:12<br>
 +
 
 +
(gdb)<br>
 +
<br>
 +
Now you can step through the code (step and next), print variable<br>
 +
values (print and x), look at the call stack (bt) and other debugger<br>
 +
tricks.  If you run gdb inside Emacs (M-X gdb) you can see the source<br>
 +
 
 +
code in one window while the debugger runs in the other.<br>
 +
<br>
 +
This guide only shows how to debug code linked directly with VCS, not<br>
 +
shareable code.  Shareable code is only loaded into memory when<br>
 +
needed, not at the start of execution.  So you can not set a<br>
 +
 
 +
breakpoint on your code when gdb loads the executable as your code is<br>
 
not in memory yet.
 
not in memory yet.

Latest revision as of 23:48, 25 November 2009

Retrieved and Adapted on 11/25/09 from http://bbs.dicder.com/archiver/index.php?tid-499.html

How to Debug PLI, VPI, and DirectC applications using gdb

INTRODUCTION



You can add new functionality to a Verilog simulator such as VCS by
linking in your C code. You can also call C code from OpenVera
testbenches. Perhaps you have a C model, made of routines in the file

model.c, and invoked with the Verilog system task $model. The C code
is known as the application, and will consist of one or more routines.
The connection between the application and the simulator is the PLI,
the Programming Language Interface. The application will be called
during simulation either directly, when VCS executes the system task
for the application ($model), or asynchronously when VCS is doing an

activity such as initialization or shutdown.

Alternately, you want to call a C routine such as log2() directly
using VCS DirectC interface, or the new DPI (Direct Procedural
Interface) that is part of SystemVerilog. Only the linking differs.


COMPILING



You must do several steps to debug the C application. The C code must
be compiled for debug, linked with the Verilog code, the simulator

must be started with the debugger, and a breakpoint needs to be added
in the application. This guide only covers using the GNU gcc compiler
and the matching gdb debugger. Other tools will have a similar flow.

First, the C code must be compiled with -g so that its debug symbols, such
as variables and routine names, will be visible at run-time.


 gcc -g -o model.c -I${VCS_HOME}/include


The -o switch tells gcc to create object code (model.o) and not an
executable. The -I switch points to the VCS include files that define

constants and the PLI routine names.

Next link the C code into your simulation:

 vcs design.v -P model.tab model.o


The Verilog code is in design.v, your C code is in model.o, and
model.tab tells VCS which C routines to call when $model is used in
the Verilog. This produces the executable simv.

Alternately, you can compile the Verilog and C code together.;Use the

VCS -CFLAGS option to pass flags to the C compiler:

 vcs design.v -P  model.tab model.c \ -CFLAGS "-g -I${VCS_HOME}/include" 


If you are using DirectC, you don't need a table file:

 vcs +dc design.v model.o



COMPILING ATC Testbenches using Rake

Currently, rake does not support extra CFLAGS to be passed to VCS or Modelsim. One has to run rake with the v=1 option, which will display the commands, not invoke them. Then, by virtue of copy & paste, you can edit the command and add the -g flag into the -CC field.

 rake test:test16 v=1
  ... ...
  vcs -full64 +warn=noSV-LCM-PPWI +v2k -sverilog +cli -q -debug -CC "-Wno-write-strings -I${VCS_HOME}/include ... "
  add the -g option into the quoted string following the -CC vcs option. Like so:
  vcs -full64 +warn=noSV-LCM-PPWI +v2k -sverilog +cli -q -debug -CC "-g -Wno-write-strings -I${VCS_HOME}/include ... "

Run it, and proceed to the next section.

RUNNING THE DEBUGGER



Now you are ready to roll! Start the debugger and tell it which
executable to use:

 gdb simv


(gdb)

Here you can set a breakpoint on a C routine:

 (gdb) br model_call
 Breakpoint 1 at 0x8050ba6: file model.c, line 12.


Now run the simulation, specifying the command line switches to use with simv:

(gdb) run -l simv.log
Starting program: /disk/user/simv
Chronologic VCS Simulator copyright 1991-2004



Breakpoint 1, model_call() at model.c:12
(gdb)

Now you can step through the code (step and next), print variable
values (print and x), look at the call stack (bt) and other debugger
tricks. If you run gdb inside Emacs (M-X gdb) you can see the source

code in one window while the debugger runs in the other.

This guide only shows how to debug code linked directly with VCS, not
shareable code. Shareable code is only loaded into memory when
needed, not at the start of execution. So you can not set a

breakpoint on your code when gdb loads the executable as your code is
not in memory yet.