How can I enable mtrace() (and MALLOC_TRACE env) for a binary program without sources?
mtrace is feature of glibc: http://www.gnu.org/s/hello/manual/libc/Allocation-Debugging.html
Thanks
mtrace.c
#include <mcheck.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void __mtracer_on () __attribute__((constructor));
void __mtracer_off () __attribute__((destructor));
void __mtracer_on ()
{
char *p=getenv("MALLOC_TRACE");
char tracebuf[1023];
if(!p)
p="malloc_trace";
sprintf(tracebuf, "%s.%d", p, getpid());
setenv("MALLOC_TRACE",tracebuf, 1);
atexit(&__mtracer_off);
mtrace();
}
void __mtracer_off ()
{
muntrace();
}
Compile with gcc mtrace.c -fPIC -shared -o libmmtrace.so
Run with
MALLOC_TRACE=echo LD_PRELOAD=./libmmtrace.so /bin/echo 42
or
LD_PRELOAD=./libmmtrace.so /bin/echo 42
Is it ok for you?
Related
Is it possible to determine in Linux as a parent process how much time a child process sleeps, whenever it sleeps using the nanosleep() system call?
Thanks!
You can wrap glibc:
% gcc -fPIC -shared -o nanosleep.so nanosleep.c -ldl
% export LD_PRELOAD=/path/to/your/nanosleep.so /path/to/app/using/test
Wrapper code nanosleep.c:
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int (*real_nanosleep)(const struct timespec *req, struct timespec *rem) = NULL;
/* wrapping nanosleep function call */
int nanosleep(const struct timespec *req, struct timespec *rem)
{
printf("How much %d used\n", req->tv_nsec);
/* Fetch the real nanosleep function from glibc */
real_nanosleep = dlsym(RTLD_NEXT, "nanosleep");
return real_nanosleep(req, rem);
}
Test program test.c:
#include <stdio.h>
#include <time.h>
int main () {
struct timespec req = {0}, rem = {0};
req.tv_sec = 2;
req.tv_nsec = 1000000000;
nanosleep(&req, &rem);
return(0);
}
I'm testing an antidebug solution with ptrace method; and i compile the program by using ndk21e cross-compile.
The problem is that it compiles successfully with gcc, but fails with ndk cross-compile.
ndk cross-compile compiles all other programs without problems
#include <stdlib.h>
#include <stdio.h>
#include<sys/ptrace.h>
#include <dlfcn.h>
#include <string.h>
int main(int argc, char **argv) {
void *handle;
long (*go)(enum __ptrace_request request, pid_t pid);
// get a handle to the library that contains 'ptrace'
handle = dlopen ("/lib/x86_64-linux-gnu/libc.so.6", RTLD_LAZY);
// reference to the dynamically-resolved function 'ptrace'
go = dlsym(handle, "ptrace");
if (go(PTRACE_TRACEME, 0) < 0) {
puts("being traced");
exit(1);
}
puts("not being traced");
// cleanup
dlclose(handle);
return 0;
}
And it shows the error like the picture as follow:
gcc compileresult and cross-compile error result
How can i solve this problem. Thanks.
I am using embedded system and I'm testing transparent QWS server where is my Qt4.7.3.
I faced the afterimage in the QDialog when moving cursor in test program which as the QWS client, but it didn't happen in the QMainWindow which in QWS server program.
Can anyone help me to fix the issue?
There is the issue
Here is test program source code.
#include "mainwindow.h"
#include <QApplication>
#include<QWSServer>
#include <QDialog>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
#include "qscreenlinuxfb_qws.h"
#include "qscreendriverfactory_qws.h"
#include <errno.h>
extern "C" {
extern int Test();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDialog w;
QWSServer::setBackground(QColor(0,0,0,0));
QWSServer::setCursorVisible(false);
w.setStyleSheet("background-color:transparent;");
w.show();
return a.exec();
}
OK I found the issue. In QT source code.
src\gui\embedded\qscreen_qws.cpp
if (!blendSize.isNull()) {
*blendbuffer = new QImage(blendSize, d_ptr->preferredImageFormat());
}
to
if (!blendSize.isNull()) {
*blendbuffer = new QImage(blendSize, d_ptr->preferredImageFormat());
QPixmap temp = QPixmap(blendSize);
temp.fill(Qt::transparent);
**blendbuffer = temp.toImage();
}
I am getting compilation error:error: ‘scmp_filter_ctx’ was not declared in this scope.
I have declared a seccomp filter.
scmp_filter_ctx ctx;
I have included the library #include <linux/seccomp.h>
and already installed the library libseccomp-dev using
sudo apt-get install libseccomp-dev.
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <unistd.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <linux/seccomp.h>
#include <sys/prctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <bits/stdc++.h>
void create_sandbox(){
scmp_filter_ctx ctx;
ctx = seccomp_init(SCMP_ACT_KILL); // default action: kill
seccomp_load(ctx);
}
I managed to solve this error using the following:
Replaced #include <linux/seccomp.h> with #include <seccomp.h>
and while compiling, used g++ filename.cpp -lseccomp.
This worked for me.
I'm trying to print the value of CLOCK_TICK_RATE with the following program:
#include <fcntl.h>
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <linux/kd.h>
#include <linux/timex.h>
int main() {
printf("%d\n",CLOCK_TICK_RATE);
}
I get an compilation error:
error: ‘CLOCK_TICK_RATE’ undeclared.
I looked for the definition of CLOCK_TICK_RATE, I found that in timex.h, but even after I included timex.h CLOCK_TICK_RATE is still undeclared.
thanks in advance.
Maybe you are looking for this?
#include <stdio.h>
#include <unistd.h>
int main (void) {
printf (
"%ld\n",
sysconf(_SC_CLK_TCK)
);
return 0;
}
It's in hertz and is normally 100.