lldb equivalent to (gdb) set target-charset? - lldb

In gdb I can switch between different character set encodings (ASCII and EBCDIC say) using commands like:
(gdb) set target-charset EBCDIC
(gdb) set target-charset ASCII
Does lldb have this sort of functionality (perhaps undocumented)?
If not, looking at the advanced python integration available in lldb, I'd guess the best way to implement this would be with a python method. Would anybody be able to point me to a sample python script where the display of a given type is specialized that I could adapt in this respect.

There isn't such a feature, lldb assumes ASCII.
You can add a custom summary from Python to present values of a given type any way you want. This is discussed here:
http://lldb.llvm.org/varformats.html
There are a handful of summary examples here:
https://github.com/llvm/llvm-project/tree/main/lldb/examples/summaries
and you'll also want to consult the SB API documentation, which is here:
http://lldb.llvm.org/python_reference/index.html

Related

getting live variables with clang's python binding

I'm writing an analysis tool for C language in Python and I am using clang python binding for this purpose. So far it has been great and I was able to get AST of the C files I have and process them. Now I need to find live variable at a certain point of program. By live variables I mean the variables that are used after that line of code before begin re-assigned. I noticed that clang already has some related code in liveVariables.h.
Does anyone know how I can get it to work in the python binding?
How can I call liveVariables functions by having the AST and TranslationUnit?

Examining a C source code easily

In Java, just by clicking the classes in Eclipse, I can go to that specific class being referenced. In C, how can I do that? As far as I can remember I wasn't able to do that in Eclipse. I had hard times manually finding where externs are declared/defined etc.
use
ctags
This is command line tool. You need to first create tags for your whole source code and then you can jump to defination of any function or variable.
In eclipce IDE
You can go its defination by using F3 and come back using Alt + <-
If you do not want to use F3 then press Ctrl and move curcer to that place and click on that...you will go to its defination.
How to go to a certain place in the source code is IDE-specific and not related to the programming language, so your question doesn't make any sense.
Many C compilers support Eclipse.
In Eclipse, you do what you are asking for by placing the cursor on the item you are interested in, then press F3.
In addition of the other replies (notably mentionning ctags), and if using a recent GCC compiler (i.e. gcc, g++, preferably version 4.8 or 4.9, etc....) you could use the MELT plugin and DSL.
MELT enables you to work on the internal representations (such as Gimple) of the GCC compiler.
In particular, MELT has powerful pattern matching facilities, and a command line interface to find particular Gimple patterns. So for example you could, with a few command line arguments to gcc, find all the calls to malloc with a constant argument bigger than 30 bytes. This requires working on the compiler internal representations (e.g. because of sizeof operators) and is not possible in a purely textual tool.
For finding occurrences of identifiers, you could use grep or also the ack perl tool.
The tools mentioned here (ctags, grep, MELT, ack) are command line tools. It is up to you to configure or adapt your IDE (or editor like emacs) and/or your builder (like make) to invoke them.
Remember that compilers are command-line tools, at least on Linux
PS. I am the main author of MELT.

Using parse_datetime from gnu c

I am developing a program for analyzing time series under gnu/linux. To analyze a time window, I want to be able to specify start/end times on the command line. Parsing dates using strptime is simple enough, however I would like to use the flexible 'natural language' format as it is used by the unix ''date'' command. There, this is done using the parse_datetime function.
I have the source of the coreutils, but would like to avoid copying over the code and all attached header files.
My question is: is there a standard library under Unix/Linux which gives access to the full power of parse_datetime().
The function you refer to is not part of any standard, nor any stock utility library. However, it is available as a semi-standalone component as part of gnulib, namely the parse-datetime module. You will need to take it and incorporate it into your program; the gnulib distribution has tools for that. Be aware that if you do this you have to GPL your entire program (this is not a big deal if the program is only for your personal use -- the GPL's requirements only kick in when you start giving the compiled program to other people).
A possible alternative is g_date_set_parse from GLib, but I can't speak to how clever it is.

Getting type information of C symbols

Let me try to give some background first. I'm working on some project with some micro controller (AVR) which I'm accessing through some interface (UART). I'm doing direct writes to its global variables and I'm also able to directly execute functions (write args, trigger execution, read back return values).
AVR code is in C compiled with GCC toolchain. PC, that is communicating with it, is running python code. As of now I have imported adress & size information into python easily by parsing 'objdump -x' output. Now what would greatly boost my development would be information about types of the symbols (types & sizes of structs elements, enums values, functions arguments & return values, ...).
Somehow this seemed like a common thing that people do daily, and I was naively expecting ready-made python tools at start. Well, not so easy. By now I've spend many hours looking into various ways how to accomplish that.
One approach would be to just parse the C code (using e.g. pycparser). But seems like I would have to at least 'pre-parse' the code to exclude various unsupported constructs and various ordering problems and so on. Also, in theory, the problem would be if compiler would do some optimizations, like struct or enum reordering and so on.
I've been also looking into various gcc, gdb and objdump options to get such information. Have spent some time looking for tools for extracting information from various debugging formats (dwarf, stabs).
The closest I get so far is to dump stabs debugging information with objdump -g option. This outputs C-like information, which I would then parse using pycparser or on my own.
But before I spent my time doing that, I decided to raise a question here, strongly hoping that someone will hit me with possibly totally different approach I just haven't think of.
There's a quite nice tool called c2ph that dumps a parsable descripton of the types and sizes (using debug info as the source)
To answer myself... this is what I found:
http://code.google.com/p/pydevtools/
Actually I knew about it before, but it didn't really work for me at first.
So basically I made it Python 3 compatible and did few other fixes/changes also - here you can get it all:
http://code.google.com/p/pydevtools/source/checkout
Actually there is some more code which actually uses this module, but it is not finished yet. I will probably add it when finished.

what is the easiest way to lookup function names of a c binary in a cross-platform manner?

I want to write a small utility to call arbitrary functions from a C shared library. User should be able to list all the exported functions similar to what objdump or nm does. I checked these utilities' source but they are intimidating. Couldn't find enough information on google, if dl library has this functionality either.
(Clarification edit: I don't want to just call a function which is known beforehand. I will appreciate an example fragment along your answer.)
This might be near to what you're looking for:
http://python.net/crew/theller/ctypes/
Well, I'll speak a little bit about Windows. The C functions exported from DLLs do not contain information about the types, names, or number of arguments -- nor do I believe you can determine what the calling convention is for a given function.
For comparison, take a look at National Instrument's LabVIEW programming environment. You can import functions from DLLs, but you have to manually type in the type and names of the arguments before you use a given function. If this limitation is OK, please edit your question to reflect that.
I don't know what is possible with *nix environments.
EDIT: Regarding your clarification. If you don't know what the function is ahead of time, you're pretty screwed on Windows because in general you won't be able to determine what the number and types of arguments the functions take.
You could try ParaDyn's SymtabAPI. It lets you grab all the symbols in a shared library (or executable) and look at their types, offset, etc. It's all wrapped up in a reasonably nice C++ interface and runs on a lot of platforms. It also provides support for binary rewriting, which you could potentially use to do what you're talking about at runtime.
Webpage is here:
http://www.paradyn.org/html/symtab2.1-features.html
Documentation is here:
http://ftp.cs.wisc.edu/paradyn/releases/release5.2/doc/symtabProgGuide.21.pdf
A standard-ish API is the dlopen/dlsym API; AFAIK it's implemented by GNU libc on Linux and Mac OS X's standard C library (libSystem), and it might be implemented on Windows by MinGW or other compatibility packages.
Only sensible solution (without reinventing the wheel) seems to use libbfd. Downsides are its documentation is scarce and it is a bit bloated for my purposes.
The source code for nm and objdump are available. If you want to start from specification then ELF is what you want to look into.
/Allan
I've written something like this in Perl. On Win32 it runs dumpbin /exports, on POSIX it runs nm -gP. Then, since it's Perl, the results are interpreted using regular expressions: / _(\S+)#\d+/ for Win32 (stdcall functions) and /^(\S+) T/ for POSIX.
Eek! You've touched on one of the very platform-dependent topics of programming. On windows, you have DLLs, on linux, you have ld.so, ld-linux.so, and mac os x's dyld.

Resources