LLDB fails to examine global variables - swift

A global var is defined in my framework
var showersInProgress: [ProgressShower] = []
It's global so that I can stop the program
and hopefully examine the state like so:
(lldb) po showersInProgress
error: <user expression 7>:1:1: use of undeclared identifier 'showersInProgress'
showersInProgress
(lldb) p showersInProgress
error: <user expression 8>:1:1: use of undeclared identifier 'showersInProgress'
showersInProgress
(lldb) frame variable showersInProgress
(lldb) frame showersInProgress
invalid command 'frame showersInProgress'.
(lldb) frame -g showersInProgress
invalid command 'frame -g'.
(lldb) frame -g variable showersInProgress
invalid command 'frame -g'.
(lldb) frame variable -g showersInProgress
(lldb) frame variable -g showersInProgress.count
(lldb)
The log enable -f /tmp/lldb-log.txt lldb expr types
in case it's useful to anyone:
== [UserExpression::Evaluate] Parsing expression showersInProgress ==
ClangUserExpression::ScanContext()
[CUE::SC] Null function
[C++ module config] Language doesn't support C++ modules
List of imported modules in expression:
List of include directories gathered for modules:
Parsing the following code:
#line 1 "<lldb wrapper prefix>"
#ifndef offsetof
#define offsetof(t, d) __builtin_offsetof(t, d)
#endif
#ifndef NULL
#define NULL (__null)
#endif
#ifndef Nil
#define Nil (__null)
#endif
#ifndef nil
#define nil (__null)
#endif
#ifndef YES
#define YES ((BOOL)1)
#endif
#ifndef NO
#define NO ((BOOL)0)
#endif
typedef __INT8_TYPE__ int8_t;
typedef __UINT8_TYPE__ uint8_t;
typedef __INT16_TYPE__ int16_t;
typedef __UINT16_TYPE__ uint16_t;
typedef __INT32_TYPE__ int32_t;
typedef __UINT32_TYPE__ uint32_t;
typedef __INT64_TYPE__ int64_t;
typedef __UINT64_TYPE__ uint64_t;
typedef __INTPTR_TYPE__ intptr_t;
typedef __UINTPTR_TYPE__ uintptr_t;
typedef __SIZE_TYPE__ size_t;
typedef __PTRDIFF_TYPE__ ptrdiff_t;
typedef unsigned short unichar;
extern "C"
{
int printf(const char * __restrict, ...);
}
typedef bool BOOL;
void
$__lldb_expr(void *$__lldb_arg)
{
;
#line 1 "<user expression 9>"
showersInProgress
;
#line 1 "<lldb wrapper suffix>"
}
Using x86_64-apple-ios-simulator as the target triple
Using SIMD alignment: 128
Target datalayout string: 'e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128'
Target ABI: ''
Target vector alignment: 128
ClangExpressionDeclMap::FindExternalVisibleDecls[469] for '$__lldb_arg' in a 'TranslationUnit'
CEDM::FEVD[469] Searching the root namespace
ClangASTSource::FindExternalVisibleDecls[469] on (ASTContext*)0x7fb6b48f1800 for '$__lldb_arg' in a 'TranslationUnit'
CAS::FEVD[469] Searching the root namespace
ClangExpressionDeclMap::FindExternalVisibleDecls[470] for '$__lldb_expr' in a 'TranslationUnit'
CEDM::FEVD[470] Searching the root namespace
ClangASTSource::FindExternalVisibleDecls[470] on (ASTContext*)0x7fb6b48f1800 for '$__lldb_expr' in a 'TranslationUnit'
CAS::FEVD[470] Searching the root namespace
ClangExpressionDeclMap::FindExternalVisibleDecls[471] for 'showersInProgress' in a 'TranslationUnit'
CEDM::FEVD[471] Searching the root namespace
Skipped a definition because it has no Clang AST
ClangASTSource::FindExternalVisibleDecls[471] on (ASTContext*)0x7fb6b48f1800 for 'showersInProgress' in a 'TranslationUnit'
CAS::FEVD[471] Searching the root namespace
AppleObjCDeclVendor::FindDecls [434] ('showersInProgress', false, 1, )
AOCTV::FT [434] Couldn't find showersInProgress in the ASTContext
AOCTV::FT [434] Couldn't find the isa
[ClangASTImporter] Forgetting destination (ASTContext*)0x7fb6b48f1800
[ClangASTImporter] Forgetting source->dest (ASTContext*)0x7fb6b48f1800->(ASTContext*)0x7fb69758cc00

frame variable only shows statics in the CompileUnit of the current frame. If you want to see all the globals you need to use target variable. Note, however, target variable only searches the shared library of the current frame. You can add the --shlib flag to direct the search to a specific shared library.

Related

C++ - Why do I have to include .cpp file along with/ instead of .h file to acces the value of a global variable in the following case?

I am trying to properly declare and define global variables in separate files and include them in a third file which deals with class declaration.
The three files are:
1) global.h
#ifndef GLOBAL_H_INCLUDED
#define GLOBAL_H_INCLUDED
extern const int marker_num;
extern const int dim;
using namespace std;
#endif // GLOBAL_H_INCLUDED
2) global.cpp
#include <iostream>
#include <cstdio>
#include <cmath>
#include "global.h"
#include "WorldState.h"
#include "Robot.h"
#include "Sensor.h"
#include "Marker.h"
constexpr const int marker_num = 10;
constexpr const int dim = (2 * marker_num) + 3;
3) WorldState.h
#ifndef WORLDSTATE_H
#define WORLDSTATE_H
#include "global.h"
#include "global.cpp"
class WorldState{
public:
WorldState(float a[], float b[dim][dim]);
get_wstate();
protected:
private:
float w_state[];
float covar_matrix[dim][dim];
};
#endif // WORLDSTATE_H
I am using the global variable dim to declare and define a multidimensional array. I have declared dim inside global.h and defined it inside global.cpp. Now, I have a class called WorldState and inside its header, I am using dim. If I comment out #include "global.cpp", it throws the following error:
C:\Users\syamp\Documents\codeblocks\slam\WorldState.h|10|error: array bound is not an integer constant before ']' token
My understanding is that including the .h file includes the corresponding .cpp as well, and that all declarations should be inside .h and all definitions should be inside .cpp. However, it doesn't seem to work in this case.
1) If I decide to include global.cpp file inside WorldState.h, isn't it bad programming practice? I am trying to write a good code not just a code that works.
2) An alternative is to define values of variable(s) dim (and marker_num) inside global.h. Is that good programming practice?
3) I believe there is something that I am missing. Kindly suggest the best method to resolve this issue. I am using codeblocks and C++11. Thanks in advance.
I am using the global variable dim to declare and define a multidimensional array.
When declaring a fixed-length array at compile-time, the value(s) of its dimension(s) must be known to the compiler, but your separation prevents the value of dim from being known to the compiler at all, so dim cannot be used to specify fixed array dimensions. Any code that uses dim will just compile into a reference to it, and then the linker will resolve the references after compilation is done. Just because dim is declared as const does not make it suitable as a compile-time constant. To do that, you must define its value in its declaration, eg:
#ifndef GLOBAL_H_INCLUDED
#define GLOBAL_H_INCLUDED
static constexpr const int marker_num = 10;
static constexpr const int dim = (2 * marker_num);
using namespace std;
#endif // GLOBAL_H_INCLUDED
Otherwise, if you keep dim's declaration and definition in separate files, you will have to dynamically allocate the array at run-time instead of statically at compile-time.
I have declared dim inside global.h and defined it inside global.cpp.
That is fine for values you don't need to use until run-time. That will not work for values you need to use at compile-time.
My understanding is that including the .h file includes the corresponding .cpp as well
That is not even remotely true. The project/makefile brings in the .cpp file when invoking the compiler. The .h file has nothing to do with that.
that all declarations should be inside .h and all definitions should be inside .cpp.
Typically yes, but not always.
If I decide to include global.cpp file inside WorldState.h, isn't it bad programming practice?
Yes.
An alternative is to define values of variable(s) dim (and marker_num) inside global.h. Is that good programming practice?
Yes, if you want to use them where compile-time constants are expected.

How to force g++ to create C symbol name

I have a function called init in a cpp file, but when I compile it, g++ creates in the object file a symbol named _Z4initv, so when I link after with ld with the option -e init, obviously ld doesn't recognize the symbol init. Is there a way to create symbols name in C style with g++ ?
If you have a definition like e.g.
void init() { ... /* some code */ ... }
Then to inhibit name mangling you need to declare it as extern "C":
extern "C" void init() { ... /* some code */ ... }
If you have a declaration in a header file that you want to include in a C source file you need to check if you're including the header file in a C or C++ source file, using the __cplusplus macro:
#ifdef __cplusplus
extern "C"
#endif
void init(void);
Note that the function in the header file has to be declared with void in the argument list, if it doesn't take any arguments. That's because the declaration void init() means something else in C.

Why does Perl access to cross-platform packed structs not work with SWIG?

Working from:
Is ignoring __attribute__((packed)) always safe in SWIG interfaces?
Visual C++ equivalent of GCC's __attribute__ ((__packed__))
My .i does:
#define __attribute__(x)
then uses %include to include my cross-platform definition of PACK():
#if defined(SWIG)
#define PACK(...) VA_ARGS
#elif defined(_MSC_VER)
#define PACK(__Decl__) __pragma(pack(push, 1)) __Decl__ __pragma(pack(pop))
#else // GCC
#define PACK(__Decl__) __Decl__ __attribute__ ((packed))
#endif
Then I have code like:
PACK(
typedef struct {
uint8_t something;
uint32_t more;
} ) aName;
With earlier versions of the PACK() macro, I got syntax error from SWIG on the typedef line. Now I get past that but when compiling the SWIG-generated .c file, I have get and set functions that complain aName doesn't exist. The messages are like (edited):
libudr_perl_swig.c: In function '_wrap_aName_set':
libudr_perl_swig.c:2367:20: error: expected identifier or '(' before
'=' token libudr_perl_swig.c: In function '_wrap_aName_get':
libudr_perl_swig.c:2377:3: error: expected expression before 'aName'
SWIG sort of seems to know about my struct -- it creates access functions -- but the doesn't expose them enough that the access functions can find it.
Before I started to make this cross-platform -- when it was still Linux-only with __attribute__ ((packed)) -- it worked in SWIG. And it still works in Linux. So there appears to be something about SWIG's interpretation of PACK() that is flawed.
The old way generated a lot of per-field code like:
XS(_wrap_aName_something_set) {
{
aName *arg1 = (aName *) 0 ;
...
the new way generates a little per-struct code like:
SWIGCLASS_STATIC int _wrap_aName_set(pTHX_ SV* sv, MAGIC * SWIGUNUSEDPARM(mg)) {
MAGIC_PPERL
{
Why should my PACK() (which should be a no-op in SWIG) do that?
Googling "cpp standard variadic macros" leads to http://en.wikipedia.org/wiki/Variadic_macro which notes the expansion of ... is __VA_ARGS__, not VA_ARGS (as I had found somewhere). When I change my macro definition to be:
#if defined(SWIG)
#define PACK(...) __VA_ARGS__
#elif defined(_MSC_VER)
#define PACK(__Decl__) __pragma(pack(push, 1)) __Decl__ __pragma(pack(pop))
#else // GCC
#define PACK(__Decl__) __Decl__ __attribute__ ((packed))
#endif
it works.

How to use #define to assign pins in Arduino?

I am trying to use #define to create a constant and define a pin, check this code
#define PIN_MICROPHONE 13;
void loop()
{
analogRead(PIN_MICROPHONE);
}
But when trying to compile, it says this error:
: In function 'void loop()':
error: expected `)' before ';' token
error: expected primary-expression before ')' token
error: expected `;' before ')' token
How can I use #define macros to define pins?
This code compiles ok
#define PIN_MICROPHONE 13;
void loop()
{
analogRead(13);
}
I am using Arduino 1.0.5
The issue is your semicolon.
#define does not require a semicolon on the end of it.
#define PIN_MICROPHONE 13
void loop()
{
analogRead(PIN_MICROPHONE);
}
Typically the #define is a pre-compiled directive. That means before the code is compiled a find and replace is done on the text. So the IDE "saw" the code below.
void loop()
{
analogRead(13;); //not going to work
}
PS: I thought #defines were not encouraged in Arduino style guides.

Eclipse undefined reference

I'm using Eclipse and MinGW. I've got undefined reference to error to all that I write in h files, that I do include in cpp-file where main located. I create an empty project, and the same thing again (
main.cpp
#include <iostream>
#include "Stack.h"
using namespace std;
int main(){
Stack<int> stack(10);
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
stack.h
#ifndef STACK_H_
#define STACK_H_
template <class T>
class Stack{
private:
struct StackEl;
StackEl *top;
public:
Stack();
Stack(T el);
~Stack();
void Push(const T& el);
T Pop();
};
#endif /* STACK_H_ */
and stack.cpp inplements everything from stack.h
If I include not h-file, but cpp - all works. Help please!
I've got following errors
D:/Workspacee/Stack2/Debug/../src/Stack2.cpp:16: undefined reference to `Stack<int>::Stack(int)'
D:/Workspacee/Stack2/Debug/../src/Stack2.cpp:18: undefined reference to `Stack<int>::~Stack()'
D:/Workspacee/Stack2/Debug/../src/Stack2.cpp:18: undefined reference to `Stack<int>::~Stack()'
This is a linker error. I'm no Eclipse expert, but you have to tell it somehow to add Stack.o to the linking command.
If you include Stack.cpp instead of Stack.h, the implementations from the cpp-file get included into main.cpp by the preprocessor before compilation, so the linking stage has no unresolved references to outside functions.
My bad, that is becouse templates! When you use template, all code, including realization of functions, must be in header-file, or you have to write prototypes for every type you are going to use you template-functions with. I've forgot about that working with templates is not the same as with usual function :(