I am reading R sources and trying to learn about the Heap Structure. I'm looking for the definition of PROTECT(), but I've founded:
$ grep -rn "#define PROTECT(" *
src/include/Rinternals.h:642:#define PROTECT(s) Rf_protect(s)
and then
$ grep -rn "Rf_protect(" *
src/include/Rinternals.h:803:SEXP Rf_protect(SEXP);
src/include/Rinternals.h:1267:SEXP Rf_protect(SEXP);
But I didn't find Rf_protect()'s definition.
Thanks.
The Rf_ prefix is a common idiom giving this plain C code the resemblance of a namespace. So you want to look for protect(...) instead:
/usr/share/R/include/Rinternals.h:#define protect Rf_protect
And given how 'core' this, you may as well start in src/main where a quick grep -c leads you to src/main/memory.c. Et voila on lines 3075 to 3081
SEXP protect(SEXP s)
{
if (R_PPStackTop >= R_PPStackSize)
R_signal_protect_error();
R_PPStack[R_PPStackTop++] = CHK(s);
return s;
}
Now that said, you probably want to pay attention to most of the file and not just this function.
Related
How can I get all modulo(%) operation in my c source code?
I don't think a regular expression can do it, due to C MACRO inclusion.
And, must take care of string printf formatting.
Which static analyse source code can I use to do this?
For exemple, Codesonar or codecheck "only" try find problems, not that.
I want to check manually all modulo operation with zero. (like division by 0).
It's probably not the best way to do this, but it seems to work. Here's my test file:
#include <stdio.h>
#define MACRO %
int main(void) {
int a = 1%1;
int b = 1 MACRO 1;
printf("%1s", "hello");
}
Then I ran this and got some useful output:
$ clang -fsyntax-only -Xclang -dump-tokens main.c 2>&1 > /dev/null | grep percent
percent '%' Loc=<main.c:6:14>
percent '%' [LeadingSpace] Loc=<main.c:7:15 <Spelling=main.c:3:15>>
Short explanation:
This is the output from the lexical analysis. The command seem to write to stderr instead of stdout. That's the reason for 2>&1 > /dev/null. After that I'm just using grep to find all occurrences of the modulo operator.
I want to do this:
extract_prototypes file1.c file2.cpp file3.c
and have whatever script/program print a nice list of function prototypes for all functions defined in the given C / C++ files. It must handle multi-line declarations nicely.
Is there a program that can do this job? The simpler the better.
EDIT: after trying to compile two C programs, bonus points for something that uses {perl, python, ruby}.
I use ctags
# p = function declaration, f = function definition
ctags -x --c-kinds=fp /usr/include/hal/libhal.h
Also works with C++
ctags -x --c++-kinds=pf --language-force=c++ /usr/include/c++/4.4.1/bits/deque.tcc
Note, you may need to add include paths, do this using the -I /path/to/includes.
The tool cproto does what you want and allows to tune the output to your requirements.
Note: This tool also only works for C files.
I use ctags and jq
ctags --output-format=json --totals=no --extras=-F --fields=nP file1.c |
jq -sr 'sort_by(.line) | .[].pattern | ltrimstr("/^") | rtrimstr("$/") | . + ";"'
If you have universal-ctags (https://ctags.io), --_xformat option may be useful though you need sed and tr commands to get what you want.
$ cat input.c
struct object *new_object (struct
/* COMMENT */
param
/* IGNORE ME */
*p)
{
return NULL;
}
int main (void)
{
return 0;
}
$ ./ctags -o - --kinds-C=f --kinds-C++=f -x --_xformat='%{typeref} %{name} %{signature};' input.c | tr ':' ' ' | sed -e 's/^typename //'
struct object * new_object (struct param * p);
int main (void);
$
This is similar to the answer posted by Steve Ward but this one requires sed, and tr instead of jq.
http://cfunctions.sourceforge.net
(This only does C and a limited subset of C++. Disclaimer: this is my program.)
I used to use doxygen to generate documentation for my C++ code. I am not an expert, but i think you can use doxygen to generate some sort of index file of the function prototypes.
Here is a thread of someone asking a similar question
gccxml is interesting, but it print a xml tree. You need to extract information about class, functions, types, and even the specialized templates of class and functions. gccxml use parser of GCC, so you don't need to do the worst job wich is parsing C++ file, and you are 100% sure that it's what probably the best compilator understand.
If you format your comments suitably, you could try DOxygen. In fact, if you've not tried it before I'd recommend giving it a go anyway - it will produce inheritance graphs as well as full member function lists and descriptions (from your comments).
In more modern versions of GCC, you can also use -aux-info to get this information when writing C code. See here.
Here's a sample of what the output looks like:
/* src/main.c:30:NC */ static void usage (const char *);
/* src/main.c:32:NF */ extern int main (int argc, char **argv); /* (argc, argv) int argc; char **argv; */
/* src/main.c:57:NF */ static void usage (const char *prog_name); /* (prog_name) const char *prog_name; */
gcc-xml might help, although as it is, it only does half the job you want. You'll need some processing of the XML output
You can run the source file through this program:
/* cproto_parser.c */
#include <stdio.h>
int main (void)
{
int c;
int infundef = 0;
int nb = 0,
np = 0;
while((c=getc(stdin))!=EOF){
if(c=='{'){
if((np==0)&&(nb==0)){infundef=1;}
nb++;
}
if (infundef==0) {putc(c,stdout);}
if(c=='}'){
if((np==0)&&(nb==1)){infundef=0;}
nb--;
}
if(c=='('){np++;}
if(c==')'){np--;}
}
return 0;
}
Run through the preprocessor to get rid of comments. If you have unmatched braces due to #ifdefs you have to set defines, include files to make it not so.
e.g., cc cproto_parser.c -o cproto_parser; cc -E your_source_file.c|./cproto_parser
I have a big bunch of source files and I want to grep through it to find the definition of a specific user-defined type dev_if_type_t. All I know about it so far it's that some functions in the code I'm examining use it as a return value.
Right now I'm using the following:
typedef.*dev_if_type_t|(define|typedef|enum|struct)\s*dev_if_type_t
but it returns no results. Is there another method of C type definition I'm neglecting to mention?
The grep line itself, in the code base's top directory:
grep -rn "typedef.*dev_if_type_t\|\(define\|typedef\|enum\|struct\)\s*dev_if_type_t" *
There could be much more variants of the definition like:
typedef struct {
/* some code */
} dev_if_type_t;
Some code could also look like this:
#define \
dev_if_type_t int
struct
dev_if_type_t
{
/* some code */
};
You'll never know.
I would suggest you try it just with grepping dev_if_type_t and using the context option -C <num>of grep to find the definition by yourself.
When using expressions including | don't forget to use egrep (deprecated) or the proper command grep -E ....
Note that \| and \( has a different meaning. Use | and ( for your purpose.
So the correct pattern should be:
grep -Ern "typedef.*dev_if_type_t|(define|typedef|enum|struct)\s*dev_if_type_t" *
I'm trying to debug one core dump (mainly using gdb) and all I found out so far, is that that there is a structure of exactly 124 bytes, that is causing problems. Given all the sources of this program, is there a way to find that structure? (I mean is there a way to find structure, whose size is 124 bytes)
PS. I know exact place in memory of this structure, yet there is no
clue about it's purpose if I look at it. It is also common structure,
so I can make as many core dumps as I wish.
PS2. So far I tried:
to use regular expression grep '^ *[a-zA-Z][^ ;,."()]* [a-zA-Z][^
;,."()]*' * | grep -v 'return' | sed 's/[^:]*: *\([^ ]*\).*/\1/' |
sort | uniq > tmp.txt , add p sizeof(x) to each found line and
input to gdb.
to use info variables in gdb, log output, extract variable types
and add sizeof(x) to each type and output to gdb.
In a header file which is included by all the source file, define a macro,
#define malloc(size) my_malloc(size, __FILE__, __LINE__)
And then in the implementation:
#undef malloc
void * my_malloc(size_t size, const char* file, int line)
{
//if the size equal to 124 bytes, log it, then you will have a chance know where this kind of allocation happens, so you know the struct.
if(124==size) printf(...);
return malloc(size);
}
Try to use this:
objdump -W <elf-name> | grep -B 2 "DW_AT_byte_size : 124"
This command dump all debugging symbols in the ELF file and find those size is 124.
I have code that has a lot of complicated #define error codes that are not easy to decode since they are nested through several levels.
Is there any elegant way I can get a list of #defines with their final numerical values (or whatever else they may be)?
As an example:
<header1.h>
#define CREATE_ERROR_CODE(class, sc, code) ((class << 16) & (sc << 8) & code)
#define EMI_MAX 16
<header2.h>
#define MI_1 EMI_MAX
<header3.h>
#define MODULE_ERROR_CLASS MI_1
#define MODULE_ERROR_SUBCLASS 1
#define ERROR_FOO CREATE_ERROR_CODE(MODULE_ERROR_CLASS, MODULE_ERROR_SUBCLASS, 1)
I would have a large number of similar #defines matching ERROR_[\w_]+ that I'd like to enumerate so that I always have a current list of error codes that the program can output. I need the numerical value because that's all the program will print out (and no, it's not an option to print out a string instead).
Suggestions for gcc or any other compiler would be helpful.
GCC's -dM preprocessor option might get you what you want.
I think the solution is a combo of #nmichaels and #aschepler's answers.
Use gcc's -dM option to get a list of the macros.
Use perl or awk or whatever to create 2 files from this list:
1) Macros.h, containing just the #defines.
2) Codes.c, which contains
#include "Macros.h"
ERROR_FOO = "ERROR_FOO"
ERROR_BAR = "ERROR_BAR"
(i.e: extract each #define ERROR_x into a line with the macro and a string.
now run gcc -E Codes.c. That should create a file with all the macros expanded. The output should look something like
1 = "ERROR_FOO"
2 = "ERROR_BAR"
I don't have gcc handy, so haven't tested this...
The program 'coan' looks like the tool you are after. It has the 'defs' sub-command, which is described as:
defs [OPTION...] [file...] [directory...]
Select #define and #undef directives from the input files in accordance with the options and report them on the standard output in accordance with the options.
See the cited URL for more information about the options. Obtain the code here.
If you have a complete list of the macros you want to see, and all are numeric, you can compile and run a short program just for this purpose:
#include <header3.h>
#include <stdio.h>
#define SHOW(x) printf(#x " = %lld\n", (long long int) x)
int main(void) {
SHOW(ERROR_FOO);
/*...*/
return 0;
}
As #nmichaels mentioned, gcc's -d flags may help get that list of macros to show.
Here's a little creative solution:
Write a program to match all of your identifiers with a regular expression (like \#define :b+(?<NAME>[0-9_A-Za-z]+):b+(?<VALUE>[^(].+)$ in .NET), then have it create another C file with just the names matched:
void main() {
/*my_define_1*/ my_define_1;
/*my_define_2*/ my_define_2;
//...
}
Then pre-process your file using the /C /P option (for VC++), and you should get all of those replaced with the values. Then use another regex to swap things around, and put the comments before the values in #define format -- now you have the list of #define's!
(You can do something similar with GCC.)
Is there any elegant way I can get a list of #defines with their final numerical values
For various levels of elegance, sort of.
#!/bin/bash
file="mount.c";
for macro in $(grep -Po '(?<=#define)\s+(\S+)' "$file"); do
echo -en "$macro: ";
echo -en '#include "'"$file"'"\n'"$macro\n" | \
cpp -E -P -x c ${CPPFLAGS} - | tail -n1;
done;
Not foolproof (#define \ \n macro(x) ... would not be caught - but no style I've seen does that).