Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TRACE produces nothing

18 views
Skip to first unread message

RM

unread,
Jul 31, 2020, 2:17:32 PM7/31/20
to
I have problem with my TRACE macro: there's nothing on output.
I have the following definition:

#ifdef DEBUG
# ifdef _MSC_VER
# include <windows.h>
# include <sstream>
# define TRACE(x) \
do { \
std::stringstream s; \
s << __FILE__ << " (" << __LINE__ << "), " <<
__FUNCTION__ <<": " << x << std::endl; \
OutputDebugString(s.str().c_str()); \
} while (false)
# define TRACE_IF(b, x) if (b) TRACE(x)
# else
# include <iostream>
# define TRACE(x) std::clog << __FILE__ << " (" << __LINE__ <<
"), " << __FUNCTION__ <<": " << x << std::endl
# define TRACE_IF(b, x) if (b) TRACE(x)
# endif
#else
# define TRACE(x)
# define TRACE_IF(b, x)
#endif

and I use it this way:

int main(int argc, const char *argv[]) {
using namespace std;
TRACE("main called" << endl);
...
}

I use Makefile and during compilation I see:

c++ -Wall -Wextra -Werror -no-pie -DDEBUG -g -o ./build/apps/dirtyphp
build/objects/src/dirtyphp.o build/objects/src/common.o
build/objects/src/source_file.o build/objects/src/obfuscator.o
-L/usr/lib -lstdc++ -lstdc++fs -lm

so DEBUG symbol is defined.
Please help me find the reason why
TRACE("main called" << endl);
and other TRACEs produce nothing. I don't know what's wrong.

Alf P. Steinbach

unread,
Jul 31, 2020, 3:05:24 PM7/31/20
to
On 31.07.2020 20:17, RM wrote:
> I have problem with my TRACE macro: there's nothing on output.
> I have the following definition:
>
> #ifdef DEBUG
> #   ifdef _MSC_VER
> #       include <windows.h>
> #       include <sstream>
> #       define TRACE(x) \
>             do { \
>                 std::stringstream s; \
>                 s << __FILE__ << " (" << __LINE__ << "), " << __FUNCTION__ <<": " << x << std::endl; \
>                 OutputDebugString(s.str().c_str()); \
>             } while (false)
> #       define TRACE_IF(b, x)  if (b) TRACE(x)
> #   else
> #       include <iostream>
> #       define TRACE(x)  std::clog << __FILE__ << " (" << __LINE__ <<
> "), " << __FUNCTION__ <<": " << x << std::endl
> #       define TRACE_IF(b, x)  if (b) TRACE(x)
> #   endif
> #else
> #   define TRACE(x)
> #   define TRACE_IF(b, x)
> #endif

`__FUNCTION__` is not a standard macro.

Use standard `__func__`, and/or a proper compiler specific macro.

For C++20 and later, consider using `std::source_location`.


> and I use it this way:
>
> int main(int argc, const char *argv[]) {
>     using namespace std;
>     TRACE("main called" << endl);
> ...
> }
>
> I use Makefile and during compilation I see:
>
> c++  -Wall -Wextra -Werror -no-pie  -DDEBUG -g -o ./build/apps/dirtyphp
> build/objects/src/dirtyphp.o build/objects/src/common.o
> build/objects/src/source_file.o build/objects/src/obfuscator.o
> -L/usr/lib -lstdc++ -lstdc++fs -lm
>
> so DEBUG symbol is defined.
> Please help me find the reason why
>     TRACE("main called" << endl);
> and other TRACEs produce nothing. I don't know what's wrong.

Uhm, this is all very ugly.

By including `<windows.h>` you introduce a lot of macros that can wreak
havoc with the source code, including `min` and `max` macros, and
including f***ing up other code's use of e.g. winsocks header.

Apart from the correctness issues that's like 300 000 lines of
irrelevant text that you force-feed the compiler.

Instead of tying that definition to Visual C++, and including <windows.h>,

tie it to Windows platform (i.e. `_WIN32` macro symbol) + an opt-in
option to use debugger communication, and for that case just declare the
`OutputDebugString` function.

- Alf

Mike Terry

unread,
Jul 31, 2020, 4:21:28 PM7/31/20
to
The first question you should resolve is what exactly is being emitted
from all those conditional pre-processing steps. Compilers generally
have a command line option to only perform the pre-processing step and
write the output to a file. For the Microsoft VC compiler this is the
/P switch.

Your situation may be different, but on my Windows 10 system with VC
compiler, the /P option produces a .i output file containing:

....
int main()
{
using namespace std;
do { std::stringstream s; s <<
"c:\\products\\source\\scratch6\\scratch6\\scratch6.cpp" << " (" << 427
<< "), " << __FUNCTION__ <<": " << "main called" << endl << std::endl;
OutputDebugString(s.str().c_str()); } while (false);
....

Ok, and when I remove /P and do a proper compile/link, the code
generated works as intended. But note that OutputDebugString writes
messages TO AN ATTACHED DEBUGGER! Perhaps you aren't running the
program under a debugger? (OutputDebugString does not write to
stdout/stderr, so if you're not running under a suitable debugger the
messages would just be thrown away...)


Mike.



RM

unread,
Aug 1, 2020, 8:55:56 AM8/1/20
to
W dniu 31.07.2020 o 22:21, Mike Terry pisze:
I compiled dirtyphp.cpp under Linux Mint with -E option and I noticed
that in dirtyphp.i file TRACE("main called" << endl); is expanded to
single semicolon. This means that DEBUG symbol is undefined although I
use -DDEBUG option of the compiler:

debug: CXXFLAGS += -DDEBUG -g

and I see this option during compilation:

c++ -Wall -Wextra -Werror -no-pie -DDEBUG -g -Iinclude/ -c
src/dirtyphp.cpp -o build/objects/src/dirtyphp.o -L/usr/lib -lstdc++
-lstdc++fs -lm
c++ -Wall -Wextra -Werror -no-pie -DDEBUG -g -Iinclude/ -c
src/common.cpp -o build/objects/src/common.o -L/usr/lib -lstdc++
-lstdc++fs -lm
c++ -Wall -Wextra -Werror -no-pie -DDEBUG -g -Iinclude/ -c
src/source_file.cpp -o build/objects/src/source_file.o -L/usr/lib
-lstdc++ -lstdc++fs -lm
c++ -Wall -Wextra -Werror -no-pie -DDEBUG -g -Iinclude/ -c
src/obfuscator.cpp -o build/objects/src/obfuscator.o -L/usr/lib -lstdc++
-lstdc++fs -lm
c++ -Wall -Wextra -Werror -no-pie -DDEBUG -g -o ./build/apps/dirtyphp
build/objects/src/dirtyphp.o build/objects/src/common.o
build/objects/src/source_file.o build/objects/src/obfuscator.o
-L/usr/lib -lstdc++ -lstdc++fs -lm

Here's my Makefile:

#ifeq ($(ARCH),win32)
## Windows with the VC++ compiler
#ObjSuf = obj
#SrcSuf = cxx
#ExeSuf = .exe
#DllSuf = dll
#OutPutOpt = -out:
#CXX = cl
#CXXOPT = -O2
#CXXOPT = -Z7
#CXXFLAGS = $(CXXOPT) -G5 -GR -GX -MD -DWIN32 -D_WINDOWS -nologo \
# -DVISUAL_CPLUSPLUS -D_X86_=1 -D_DLL
#LD = link
#LDOPT = -opt:ref
#LDOPT = -debug
#LDFLAGS = $(LDOPT) -pdb:none -nologo
#SOFLAGS = -DLL

CXX := -c++ # gcc czy g++?
CXXFLAGS := -Wall -Wextra -Werror -no-pie # -pedantic-errors; add
"-no-pie" to the CMAKE_CXX_FLAGS
LDFLAGS := -L/usr/lib -lstdc++ -lstdc++fs -lm # -Lsrc/boost_1_65_0/lib/
-lboost_regex-gcc34-mt-d-1_3
BUILD := ./build
OBJ_DIR := $(BUILD)/objects
APP_DIR := $(BUILD)/apps
TARGET := dirtyphp
INCLUDE := -Iinclude/ # -I src/boost_1_65_0
SRC := $(wildcard src/*.cpp)

OBJECTS := $(SRC:%.cpp=$(OBJ_DIR)/%.o)

all: build $(APP_DIR)/$(TARGET)

$(OBJ_DIR)/%.o: %.cpp
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) $(INCLUDE) -c $< -o $@ $(LDFLAGS)

$(APP_DIR)/$(TARGET): $(OBJECTS)
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) -o $(APP_DIR)/$(TARGET) $^ $(LDFLAGS)

.PHONY: all build clean debug release

build:
@mkdir -p $(APP_DIR)
@mkdir -p $(OBJ_DIR)

debug: CXXFLAGS += -DDEBUG -g
debug: all

test: CXXFLAGS += -pg # dodałem dynamic execution profiler
test: all

release: CXXFLAGS += -O2
release: all # production

clean:
-@rm -rvf $(OBJ_DIR)/*
-@rm -rvf $(APP_DIR)/*

Alf P. Steinbach

unread,
Aug 1, 2020, 11:30:52 AM8/1/20
to
Uhm, the navigation in "solving problem" space is a bit haphazard.

I've already provided you with a solution.

Yet you persist in wasting time on it.

- Alf

0 new messages