How to list source code when there are both declaration and definition in LLDB? - lldb

I have some problems about source code list when there are both class/function declaration and definition. I want to go to definition, however, it always goes to declaration. What can I do for this?
Thanks!

If the problem is that:
(lldb) source list -n functionName
lists the declaration not the definition, that's a known bug.
One way to work around it is to set a breakpoint on the name, and then use the breakpoint listing to get the file & line number. That's pretty gross but does work...

Related

Is there a syntax error in this function declaration?

This is from a textbook:
/* This function locates the address of where a new structure
should be inserted within an existing list.
It receives the address of a name and returns the address of a
structure of type NameRec
*/
struct NameRec *linear Locate(char *name)
{
...
}
I understand it returns a pointer to a struct NameRec. Why is "linear" there and why is there a space between "linear" and "Locate"?
#define linear
will make it syntactically correct even if it wasn't before (though, technically, you'd probably want a #undef linear beforehand to avoid possible conflicting macro definitions).
It depends entirely on the context of the code, which you haven't shown. As it stands now, with no header inclusions or definitions like -Dlinear= on the compiler command line, it would not compile in a standards-conformant environment without extensions.
The best way to tell, of course, is to just try to actually compile the thing and see what happens :-)
Given that the solutions link for chapter 13 (the one you're asking about) has no mention of the linear word in the solution, I'd say it's a safe bet to assume your book is incorrect. I'd consider contacting the author (apparently currently working at FDU in New Jersey) to clear it up.
It's a typo in the book. See the locate function here:
https://users.ipfw.edu/chansavj/ACY2017/ANSI_C/ANSI_C_4thEd/Solutions%20to%20Exercises%20(Windows)/Solutions/83556-0s/Ch13/pgm13-5ex3.c
(Posted by ta.speot.is in the comments)

Bool reported as undefined. When typedef added; error that Bool is already defined

I'm writing embedded software for the ti C5515 in ccstudio, using the CSL libraries. I'm having trouble with type definitions. In specific, I get this error:
#20 identifier "Bool" is undefined in csl_intc.h
Taking a look at the header, I find the bool definition in tistdtypes.h; within scope, as far as I can tell. In desperation, I add a typedef to the top of the file. However, then it complains that bool is already defined.
I've made sure that the case is the same, and that there isn't another tistdtypes.h somewhere overriding the definition. How is it possible that it complains that there is no typedef, but when I add one, it says that there is one already?
Here are the two errors, one after the other:
#20 identifier "Bool" is undefined TMS320C5515 line 992, external location: ... csl_intc.h
#102 "Bool" has already been declared in the current scope TMS320C5515 line 914, external location: ... csl_intc.h
The file path is exactly the same. I can also paste the lines between 992 and 914 if necessary. There isn't much.
There is a #ifndef STD_ around the definition. Do you know what defines are passed by your compiler/other includes ?
One way to find out it to turn up the verbosity of the preprocessor to see all the code which is parsed. Maybe that will give you some insight why that is being skipped.
With gcc that would be gcc -E to stop after preprocessing and dump the parsed content. You might want to find the option for your compiler if you use a different one.
When I use the file in question:
#include "tistdtypes.h"
int main(){
Bool something = 0;
and run this program via gcc -Werror -Wall, I'm not seeing any such errors. So unless your version is different from the one I linked to, I'd say there's something wrong with how it's being compiled into your program.
Could you add some more details regarding how it's included, if your version of tistdtypes.h is different than when I linked to, and how the target is being compiled?
As a "worst case" backup at least you know that a Bool is really a unsigned short, so you could always modify your code to just not use the typedef.
EDIT:
I just took a look at csl_intc.h. It uses Bool in that file without including the definition of it. What happens if you include tistdtypes.h in this file?

Argument 'sample2' conceals a global declaration of the same symbol

I have a weird problem in C. I have a structure and I pointed sample to that structure :
test sample;
now in the code, I call that structure through a function :
function is called something, so something(&sample) is used to point the structure in the function.
Now I need to copy values of sample to sample2 .. So I want sample2 to point to the same structure as well. So I also declared test sample2 before main, and used it as a global variable. Now when its used to point to contents in the structure in the function, sample must be called without a (*sample2).content or sample2->content. I only need to write sample2.content. I understand that this happens because sample2 declared globally... But I also get this when compiling :
comment 528 - Argument 'sample2' conceals a global declaration of the same symbol
The program runs fine, but I want to get rid of this compiler message... Why does it say that ? what does it mean ?
The issue is that inside the function if you refer to the symbol sample the compiler has two things to choose from. The first is the global variable and the second is the argument you provided to the function. What the compiler is doing is alerting you to the fact that it assumes you mean the local variable and not the global one.
In general this is a recipe for grief and bugs, and you say that your code runs as intended. I cannot say how or why without looking at it in detail. The simplest answer is to just change the name of the argument to your function to something different or the global variable to something different.
Without seeing the code I can't be sure...
But it sounds like you have a function which is taking a 'test' which you have called 'sample2', by doing that it means you can't access the sample2 you defined globally.
Placing the code in your question would be useful.
Actually, you have to use the dot (.) member selector rather than
the arrow (->) member selector because sample is a struct, rather
than a pointer to a struct. Which has nothing to do with the error message you receive;
My guess (since I can't see your code) is that you pass sample2 as an argument to a function. This sample2 is a different sample2 than the structure you've declared globally. Since they have the same name, you will only be able to use the argument in that function, and not the global sample2.
Please consider editing your question and posting your entire code for review. There are a lot of strange assumptions in your question, and it's possible that you're relying on more than one misunderstanding.

How to track down cause of missing struct from include files in C?

I have a rather large project I'm porting, and in one of the MANY headers I've included a file that contains a struct definition for pmc_mdep. (prior in the file its just declared, but later its actually defined).
Trying to compile it gives me errors about that struct being an incomplete type (which I believe means that it's lacking a definition).
When I run the preprocessor over this project, it does include that file, but the preprocessor output does not have the struct definition (but does include enum's from that file).
Is there a method to figure out why some of a header file gets to the preprocessor output, and some does not?
TIA
(Also, this is not the only compile error, the port is half done but it should be at least getting past this part)
I usually just track back from the structure to find all the enclosing "#ifdef" and "#if" lines that the preprocessor will encounter and see which one is controlling the removal of the structure from the input stream into the compiler.
That generally works pretty quickly for all but the hairiest of header files (i.e., those with a great many nested conditional compile statements). For those, I generally have a look at the preprocessor output to identify the last line in the header file that made it to the compiler input stream.
Almost certainly the next line will be a conditional compile statement where you haven't met the condition for inclusion.
For example, if this is the header file, you would need to track back to see that _KERNEL should be defined in order to get the declaration and definition.
I'm afraid not; you will have to look for #ifdefs that surround your area of interest and track down why those symbols are not defined. If it's porting to Linux/UNIX and you are missing things from the standard headers, you might have not defined the right _XOPEN_SOURCE or _BSD_SOURCE in your Makefile or config.h .
The most likely reason is there's a #define somewhere around the definition. Since the corresponding symbol is not defined or defined to some other value the definition is not included even when the header itself is included. You'll have to inspect this manually.
Raymond Chen has a blog post about this.
You may find yourself in a twisty maze of #ifdefs. Or you may be wondering why your macros aren't working.
I have these lines in my header file:
#define MM_BUSY 0x0001
#define MM_IDLE 0x0002
but when I try to use them, I get errors.
sample.cpp(23): error C2065: 'MM_BUSY': undeclared identifier
sample.cpp(40): error C2065: 'MM_IDLE': undeclared identifier
Any idea why this is happening?
Solution: Use #error to track down the problem the same way you'd scatter printf around to track down a regular bug.
Source: Use the #error directive to check whether the compiler even sees you
I do not think that there is a better way beside checking the preprocessor output to know why one file is included or not. Here is the gcc's preprocessor's output format that may help you understand the preprocessor's ouput.
Also, another way you may have a try to compare the outputs between that you are porting and the existing one.
You said:
I have a rather large project I'm porting, and in one of the MANY headers I've included a file that contains a struct definition for pmc_mdep. (Prior in the file its just declared, but later its actually defined).
Trying to compile it gives me errors about that struct being an incomplete type (which I believe means that it's lacking a definition).
This error can occur if you try to embed a pmc_mdep into some other structure before you have defined a pmc_mdep fully. Note that you can embed pointers to incomplete types into structures, but not actual instances of the incomplete type.
You also discuss running the preprocessor over the file that should define the structure, and you see enums form the header, but not the structure definition. That suggests that maybe you have a stray comment that is removing the structure unintentionally, or perhaps you have the structure definition embedded between #ifdef XXX and #endif but XXX is not defined when you do the compilation. It could even be #if 0.
I'd run the C preprocessor on just the header that contains the structure definition to see what that produces; it will be shorter than trying to look at the output for the entire program (source file). If I couldn't spot the issue swiftly, I'd mark parts with something like stray enums to see which ones get through and which ones don't.

C Macro to Override Variable Assignment with Function Call

Calling all C macro gurus...
Is there any way to write a C macro that will replace something like this:
my_var = 5;
with this:
setVar(&my_var, 5);
In other words, can I write a C macro that will override assignments for a specific variable (in the above example, my_var) and instead pass it to a function whose job it is to set that variable? If possible, I'd like to be able to hook into assignments of a specific variable.
EDIT: After thinking about this some more, I'm not sure it could be done. Even if you can come up with a macro to do it, setVar wouldn't necessarily know the type of the variable its setting, so what would be the type of its second argument?
EDIT: The reason I'd like to hook assignments of specific variables is for use in a primitive debugger for some specialized embedded C code. It would be nice to be able to have a "watch list", essentially like you have in an IDE. My first instinct was to try to hook variable assignments with a C macro so you could just drop the macro into your code and have that variable "watched", but then again I've never really written a debugger before so maybe I'm going about that all wrong.
Not with the standard preprocessor. It cannot change the parsing of the file, only replace proper names with a piece of code (and "=" isn't valid in a name).
If you're feeling adventurous, you can try to replace the executable "cpp" with a small script which pre-processes the source code. But that might wreck havoc with the debugging information (file name and, if you're replacing one line of code with several, with line number information, too). The script would call "sed"`:
sed -e 's/my_var\s*=\s*([^;]+);/MY_VAR(my_var, $1);/' file.c > file_tmp.c
But your best bet is probably to put this into a script and simply run it on all your sources. This will change the code and you'll see what is happening in your debugger.
#define setVar(_left_, _right_) *(_left_) = _right_

Resources