Tool to determine symbol origin in C - c

I'm looking for a tool that, given a bit of C, will tell you what symbols (types, precompiler definitions, functions, etc) are used from a given header file. I'm doing a port of a large driver from Solaris to Windows and figuring out where things are coming from is getting to be difficult, so this would be a huge help. Any ideas?
Edit: Not an absolute requirement, but tools that work on Windows would be a plus.
Edit #2: To clarify what I'm trying to do, I have a codebase I'm trying to port, which brings in a large number of headers. What I'd like is a tool that, given foo.c, will tell me which symbols it uses from bar.h.

I like KScope, which copes with very large projects.
KScope http://img110.imageshack.us/img110/4605/99101zd3.png

I use on both Linux and Windows :
gvim + ctags + cscope.
Same environment will work on solaris as well, but this is of course force you to use vim as editor, i pretty sure that emacs can work with both ctags and cscope as well.
You might want give a try to vim, it's a bit hard at first, but soon you can't work another way. The most efficient editor (IMHO).
Comment replay:
Look into the cscope man:
...
Find functions called by this function:
Find functions calling this function:
...
I think it's exactly what are you looking for ... Please clarify if not.
Comment replay 2:
ok, now i understand you. The tools i suggested can help you understand code flow, and find there certain symbol is defined, but not what are you looking for.
Not what you asking for but since we are talking i have some experience with porting and drivers (feel free to ignore)
It seems like compiler is good enough for your task. You just starting with original file and let compiler find what missing part, it will be a lot of empty stubs and you will get you code compiled.
At least for beginning i suggest you to create a lot of stubs and modifying original code as less as possible, later on once you get it working you can optimize.
It's might be more complex depending on the type of driver your are porting (I'm assuming kernel driver), the Windows and Solaris subsystems are not so alike. We do have a driver working on both solaris and windows, but it was designed to be multi platform from the beginning.

emacs and etags.
And I leverage make to run the tag indexing for me---that way I can index a large project with one command. I've been thinking about building a master index and separate module indecies, but haven't gotten around to implementing this yet...
#Ilya: Would pistols at dawn be acceptable?

Try doxygen, it can produce graphs and/or HTML and highly customizable

Related

How can I inject or dynamically load an c function into another c program

I want to build an interface in a c program which is running on an embedded system. This should accept some bytecode that represents a c function. This code will then be loaded into the memory and executed. This will then be something like remotely inject code into a running app. The only difference here is that i can implement, or change the running code and provide an interface.
The whole thing should be used to inject test code on a target system.
My current problem is that I do not know how to build such a byte code out of an existing c function. Mapping and executing this is no problem if I would knew the start address of the function.
Currently I am working with Ubuntu for testing purposes, this allows me to try some techniques which are not possible in the embedded system (according to missing operating system libs).
I build an shared object and used dlopen() and dlsym() to run this function. This works fine, the problem is just that i do not have such functions in the embedded system. I read something about loading a shared object into memory and run it, but i could not find examples for that. (see http://www.nologin.org/Downloads/Papers/remote-library-injection.pdf)
I also took a simple byte code that just print hello world in stdout. I stored this code in memory using mmap() and execute it. This also worked fine. Here the problem is that I don't know how to create such a byte code, I just used an hello world example form the internet. (see https://www.daniweb.com/programming/software-development/threads/353077/store-binary-code-in-memory-then-execute-it)
I also found something here: https://stackoverflow.com/a/12139145/2479996 which worked very well. But here i need a additional linker script, already for such a simple program.
Further I looked at this post: https://stackoverflow.com/a/9016439/2479996
According to that answer my problem would be solved with the "X11 project".
But I did not really find much about that, maybe some of you can provide me a link.
Is there another solution to do that? Did I miss something? Or can someone provide me another solution to this?
I hope I did not miss something.
Thanks in advance
I see no easy solution. The closest that I am aware of is GCC's JIT backend (libgccjit). Here is a blog post about it.
As an alternative, you could using a scripting language for that code that needs to be injected. For instance, ChaiScript or Lua. In this question, there is a summary of options. As you are on an embedded device, the overhead might be significant, though.
If using an LLVM based backend instead of GCC is possible, you can have a look at Cling. It is a C++ interpreter based on LLVM and Clang. In my personal experience, it was not always stable, but it is used in production in CERN. I would except that the dynamic compilation features are more advanced in LLVM than in GCC.

Code refactoring tools for C, usable on GNU/Linux? FOSS preferable

Variations of this question have been asked, but not specific to GNU/Linux and C. I use Komodo Edit as my usual Editor, but I'd actually prefer something that can be used from CLI.
I don't need C++ support; it's fine if the tool can only handle plain C.
I really appreciate any direction, as I was unable to find anything.
I hope I'm not forced to 'roll' something myself.
NOTE: Please refrain from mention vim; I know it exists and what its capabilities are. I purposefully choose to avoid vim, which is why I use Komodo (or nano on the servers).
I don't think that a pure console refactoring tool would be nice to use.
I use Eclipse CDT on linux to write and refactor C-Code.
There exists also Xrefactory for Emacs http://www.xref.sk/xrefactory/main.html
if a non console refactoring tool is o.k for you as well.
C-xrefactory was an open source version of xrefactory, covering C and Java, made available on SourceForge by Marián Vittek under GPLv2.
For those interested, there's an actively maintained c-xrefactory fork on GitHub:
https://github.com/thoni56/c-xrefactory
The goal of the GitHub fork is to refactor c-xrefactory itself, add a test suite, and try to document the original source code (which is rather obscure). Maybe, in the future, also convert it into an LSP C language server and refactoring tool.
C-xrefactory works on Emacs; setup scripts and instructions can be found at the repository. Windows users can run it via WSL/WSL2.
You could consider coding a GCC plugin or a MELT extension (MELT is a domain specific language to extend GCC) for your needs.
However, such approach would take you some time, because you'll need to understand some of GCC internals.
For Windows only, and not FOSS but you said "any direction..."
Our DMS Software Reengineering Toolkit" with its C Front End can apply transformations to C source code. DMS can be configured to carry out custom, complex reliable transformations, although the configuration isn't as easy as typing just a command like "refactor frazzle by doobaz".
One of the principal stumbling blocks is still the preprocessor. DMS can transform code that has preprocessor directives in typical places (around statements, expressions, if/for/while loop heads, declarations, etc.) but other "unstructured conditionals" give it trouble. You can run DMS by expanding the preprocessor directives out of existence, or more imporantly, expanding out the ones that give it trouble, but mostly people don't like this because they prefer to keep thier preprocessor directives. So it isn't perfect.
[Another answer suggested Concinelle, which looks pretty good from my point of view. As far as I know, it doesn't handle preprocessor directives at all; I could be wrong and it might handle some cases as DMS does, but I'm sure it can't handle all the cases].
You don't want to consider rolling your own. Building a transformation/refactoring tool is much harder than you might guess having never tried it. You need full, accurate parsers for the (C) dialect of interest and just that is pretty hard to get right. You need a preprocessor, symbol tables, flow analysis, transformation, code regeneration machinery, ... this stuff takes years of effort to build and get right. Trust me, been there, done that.

Can I use winnt.h in Linux?

My program is written in C. I want to use library winnt.h, but I don't use Windows anymore.
Seems like a strange question; you should probably clarify which function(s) you actually need from winnt.h so that you can learn the Linux equivalent. winnt.h isn't really a general purpose "library", it's just an interface to built in Windows-specific functions.
With that as a major caveat, you may get some degree of what you want by attempting to run your app with the help of Wine. See http://www.winehq.org/ If you're just trying to run an existing app, that's may be a reasonable solution. If you're trying to make a Linux version of your app, though, that won't help you very much.
No, well you could but it's not going to do any good - the.h file just declares functions that are defined in libs that are only on windows
No. You can't.
winnt.h contains lots of macros that depend on a Windows environment and a lot of function declarations that only exist in Windows-specific libraries. So, it's not really useful (or possible) to use winnt.h on Linux.
That said, you can use Winelib, which includes most of the functionality exposed by those Windows-specific headers, and you can get those features by linking your program with Winelib. In general, this is probably not a good idea, because Winelib is relatively unstable (the functionality of a given API function may be absent, incomplete, buggy, or incompatible compared to the native Windows version). It is a much better idea to look for a Linux-native alternative to what you need.
What parts of winnt.h do you want to use? Of course, if you need some nice macroses or type definitions from it, you can freely copy it to your own header file (of course, with dependencies). But if you include all winnt.h file to your program in linux environment, you will get tons of error messages. One of the reasons for it is pronounced by Martin Beckett in his reply.

Porting Autodesk Animator Pro to be cross platform

a previous relevant question from me is here Reverse Engineering old paint programs
I have set up my base of operations here: http://animatorpro.org
wiki coming soon.
Okay, so now I have a 300,000 line legacy MSDOS codebase. It's sort of a "be careful what you wish for" situation. I am not an experienced C programmer. I'm not entirely inexperienced either, but for all intents and purposes I'm a noob to the language and in particular the intricacies of its libraries. I am especially ignorant of the vagaries of the differences between C programs written specifically for MSDOS and programs that are cross platform. However I have been studying this code base for over a year now, and this is what I know about Animator Pro:
Compilers and tools used:
Watcom C compiler
tcmake (make program from Turbo C)
386asm, a specialised assembler for the Phar Lap dos extender
and of course, the Phar Lap dos extender itself.
a selection of obscure dos utilities
Much of the compilation seems to be driven by batch files. Though I have obtained copies of all these tools, I have not yet succeeded at compiling it. (though I have compiled its older brother, autodesk animator original.
It's got a plugin system that replicates DLL before DLL's were available, based on REX. The plugin system handles:
Video Drivers (with a plethora of included VESA drivers)
Input drivers (including wacom tablets, and keyboards)
Drawing Tools
Inks (Like photoshop's filters, or blending modes)
Scripting Addons (essentially compiled scripts)
File formats
It's got its own script interpreter named POCO, based on the C language- The scripting language has enough power to do virtually all the things the plugin system can do- Just slower.
Given this information, this is my development plan. Please criticise this. The source code is available in the link above, so you can easily, if you are so inclined, assess the situation yourself.
Compile with its original tools.
Switch to using DJGPP, and make the necessary changes to get it to compile with that, plus the original assembler.
Include the Allegro.cc "Game" library, and switch over as much functionality to that library as possible- Perhaps by simply writing new video and input drivers that use the Allegro API. I'm thinking allegro rather than SDL because: there is a DOS version of Allegro, and fascinatingly, one of its core functions is the ability to play Animator Pro's native format FLIC.
Hopefully after 3, I will have eliminated most or all of the Assembler in the project. I say hopefully, because it's in an obscure dialect that doesn't assemble in any modern free assembler without significant modification. I have tried them all. Whatever is left gets converted to assemble in NASM, or to C code if I can define the assembler's actual function.
Switch the dos extender from Phar Lap to HX Dos http://www.japheth.de/HX.html, Which promises to replicate as much of the WIN32 api as possible. Then make all the necessary code changes for that to work.
Switch to the win32 version of Allegro.cc, assuming that the win32 version can run on top of HXDos. Make any further necessary changes
Modify the plugin system to use some kind of standard cross platform plugin library. What this would be, I have no idea. Maybe you can offer some suggestions? I talked to the developer who originally wrote the plugin system, and he said some of the things it does aren't possible on modern OS's because of segmentation restrictions. I'm not sure what this means, but I'm guessing it means all the plugins will need to be rewritten almost from scratch.
Magically, I got all the above done, and we can try and make it run in windows, osx, and linux, whilst dealing with other cross platform niggles like long file names, and things I haven't thought of.
Anyone got a problem with any of this? Is allegro a good choice? if not, why? what would you do about this plugin system? What would you do different? Is this whole thing foolish, and should I just rewrite it from scratch, using the original as inpiration? (it would apparently take the original developer "About a month" to do that)
One thing I haven't covered above is the text/font system. Not sure what to do about that, but Animator Pro has its own custom font format, but also is able to use Postscript Type 1 fonts, and some other formats.
My biggest concern with your plan, in a nutshell: Your approach seems to be to attempt to keep the whole enormous thing working at all times, tweaking the environment ever-further away from DOS. During each tweak to the environment, that means you will have approximately a billion subtle assumptions that might have broken at once, none of which you necessarily understand yet. Untangling them all at once will be incredibly painful.
If I were doing the port, my approach would be to disable as much code as possible to get SOMETHING running in a modern environment, and bring the parts back online, one piece at a time. Write a simple test harness program that loads a display driver and draws some stuff, and compile it for DOS to make sure you understand the interface. Then write some C code that implements the same interface, but with Allegro (or SDL or SFML), and make that program work under Windows or Linux. When the output differs, you have a simple test case to work from.
Your entire job on this port is swapping out implementations of various interfaces and functions with completely new ones. This is a job that unit testing excels at. Don't write any new code without a test of some kind that runs on the old code under DOS! Make your potential problems as small and simple as you possibly can. Port assembly code instead of rewriting it only if you're reasonably confident that it will actually make your job easier (ie, algorithmic stuff that compiles fine with few tweaks under NASM). Don't bite off a bigger piece than you can comfortably fit in your brain at once.
I, for one, look forward to seeing your progress! I think what you're attempting to do is great. Thanks for doing it.
Hmmm - I might approach it by writing an OpenGL video "driver" for it. and todays machines are fast enough with tons of ram that you could do all the pixel specific algorithms on main CPU into a back buffer and it would work. As the "generic" VGA driver just mapped the video buffer to a pointer this would be a place to start. There was a zoom mode in the UI so you can look at the pixels on a high res display.
It is often very difficult to take an existing non-trivial code base that wasn't written with portability in mind - you mention a few - and then try to make it portable. There will be a lot of problems on the way. It is probably a better idea to start from scratch and rewrite the code using the existing code as reference only. If you start from scratch you can leverage existing portable UI solution in your new project like Qt.

What is a good tool to aid in browsing/following C code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I sometimes need to modify OSS code or other peoples' code (usually C-based, but sometimes C++/Java) and find myself "grep"ing headers for types, function declarations etc. as I follow code flow and try to understand the system. Is there a good tool that exists to aid in code browsing. I'd love to be able to click on a type and be taken to the declaration or click on a function name and be taken to it's implementation. I'm on a linux box, so replies like "just use Visual Studio" won't necessarily work for me. Thanks!
You can't get anything better than SourceInsight.
I use Vim with ctags and taglist plugins. You can move the cursor to a variable name and with a key combination it will take you to the declaration of the variable (and back). Taglist will also show an overview of all functions, classes etc. in a side bar.
I've heard amazing things about OpenGrok. I know another team at my place of employ uses it and they find it very handly. From its web site:
OpenGrok is a fast and usable source
code search and cross reference
engine. It helps you search,
cross-reference and navigate your
source tree. It can understand various
program file formats and version
control histories like Mercurial, Git,
SCCS, RCS, CVS, Subversion, Teamware,
ClearCase, Perforce and Bazaar. In
other words it lets you grok
(profoundly understand) the open
source, hence the name OpenGrok. It is
written in Java.
Doxygen can generate an set of web pages that include a source browser. Not quite as fancy as an IDE, but all it needs is a web browser.
The Eclipse IDE is capable of working with C/C++ in addition to Java. There is a write-up on how to configure Eclipse for C/C++ development on IBM's Developer Works site.
edit: Why has this been voted down? It is a valid answer. Eclipse with the C/C++ addon will allow the question author to do what he is wanting to do. I am not the only one to have suggested, yet the others have not been voted down. So why has this one been voted down?
I do a bit in the kernel space, and keep coming back the scope.
For example:
$ cd /usr/src/redhat/BUILD/kernel-version
$ cscope -R -p4
Find this C symbol:
Find this function definition:
Find functions called by this function:
Find functions calling this function:
Find this text string:
Change this text string:
Find this egrep pattern:
Find this file:
Find files #including this file:
I usually "live" in c-scope when working on someone elses project. I use this to open files with "gvim" (my IDE), edit things, then quit "back" to c-scope, It helps me keep task focused.
I believe that cscope can be configured to work with vim and emacs, although I've seen people use other editors also.
Best of luck to you.
Vim and Ctags works for me.
If you're looking for something simple and ubiquitous, try etags. It's not going to be as good as the heavyweight tools, but it's on pretty much everything and it works with emacs. Use ctags for vi.
ctags is very useful. There are two steps involved. First run the program ctags on all your source and include directories. This creates a file named 'tags' in the local directory. ctags *.c *.h would do fine if all your source is in a single directory. When you work with source in multiple directories, it can be worth running ctags in multiple locations. Then, within vi, with your cursor on any function, defined type or variable use ctl-] to go to the definition of that entity. Use etags if you're using emacs.
Doxygen is wonderful. I've had to get across several legacy code bases that I was never involved in before, and it's been fantastic for that (even though the code bases were not documented using Doxygen format).
Go for Doxygen and set EXTRACT_ALL to YES. It is simply powerful and easy. Once you love it, you can stick to it across all platforms and languages.
http://www.doxygen.org
If you are involved in projects which have a mix of HLL code along with Assembly i'd recommend Opengrok, i have recently shifted to Opengrok and find it amazing, Opengrok + Firefox + Extensions is the best combination in my opinion, a few firefox extensions like Scrapbook etc allow you to modify and add notes while you are browsing code, again this is mostly for 'Browsing' through code and not for modifying it on the fly.
I support the use of doxygen.
This tool generated a javadoc like bunch of html pages, allowing to index all the code, to browse in it (where is this function used, and by which function...), like you can do in an IDE. It is very easy to make it work.
I had once to maintain 2000 files of C code of a 15 years old C project. It took me an hour to index the code with doxygen a provide the other developers with the generated doc. (I know, this phrase sounds like an add, but it is true... It's really a nice tool)
A wonderful tool, which works on all C-like languages.
IntelliJ is pretty good as a source browser under Linux. It's got really good support for jumping between source and function declarations. Haven't tried it with C/C++ code, but it works well with Ruby and Java.
I've not used it directly, but I have used sites created with lxr and thought it very handy. It converts your project into line-numbered and cross-referenced HTML files, using links to cross-reference function and file names. There are some examples of projects source indexed with it here.
It doesn't appear that there is a version newer than 2006, but it may still work for what you want.
I use Anjuta IDE. Not bad. Not sure how it compares to Eclipse IDE.
Any IDE will work fine.
Netbeans and Eclipse are java based but have plugins for C/C++
I use kscope, which uses cscope in the background, but provides function lists etc. as well. Seems to handle large projects like the linux kernel well too. The
kscope homepage has a good concise description of what it does and doesn't do.
cscope has always been my favorite. There is also cbrowser, but I have not tried it. ctags is also used a lot.
I use the Understand for C++. It's very handy tool to deal with large amounts of code. It also can calculate code statistics and draw call graph.
Must have!
I've had great success using doxygen. For best results (particularly when creating documentation for c++) install graphviz and enable in your doxygen configuration file.
This will automatically generate dependency maps and class diagrams that are linked to the rest of the html documentation.
Even if you are not a developer go for Source Insight
And if you are, its a MUST HAVE :)
cscope. (wanted to mod up other scope post, but i don't have karma yet).
global search and replace
find all places a function is called
find all places called by a function
find files including this file.
really simple usage: $ cscope -R
if you don't know vi, then change your EDITOR and VIEWER environmental variables to your preferred editor.
I find ID Utils quite handy. It is like an instant recursive grep. There are bunch of vim recipes to go with it.
I use and like the free software tool GNU global.
A language-sensitive source code search engine can be found
at SD Source Code Search Engine. It can handle many languages at the same time.
Searches can be performed for patterns in a specific langauge,
or patterns across languages (such as "find identifiers involving TAX").
By being sensitive to langauge tokens, the number of false positives is reduced,
saving time for the user. It understands C, C++, C#, COBOL, Java, ECMAScript, Java, XML, Verilog, VHDL, and a number of other languages.
I use Source-Navigator(TM) from here. It is quite impressive and helps a lot. It is written in Tcl/Tk, is available as an executable for windows and as source code ready to build on *nix.

Resources