In code, I'd use
#include <sys/types.h>
#include <sys/ptrace.h>
ptrace(PT_DENY_ATTACH, 0, 0, 0);
to deny attaching to the process. I was wondering if there was a way to rename "ptrace()" to something less obvious. I tried copying ptrace.h into my own header file and changing int ptrace to something else, but that just failed with an undefined symbol error. And I can't find any other references to the function :\
Thank you in advance for anything on this.
ptrace is a system call. Even if you renamed that function in your C code, the actual ptrace call would still have to be made, so it would be visible in for example strace output (with all the parameters).
Using a macro trick will only make it very slightly less obvious (you'd need two greps instead of one to find it in your codebase). So I don't really see the point. (A macro trick would not change anything to the compiled code.)
You could try running the actual syscall yourself with syscall, but that's a lot of work and still wouldn't hide anything to strace up to that point. It would make it just a tiny bit harder to break there in gdb.
So IMO: what you're trying to do is not worth the effort.
Use #define in your header to create a new macro:
#define MyTrace(a,b,c,d) ptrace(a,b,c,d)
Related
I'm currently writing my own operating system. It is a non-preemptive OS and my threads are working fine with the scheduler. I have one edge case though. If I don't call my yield() function from C but do asm("call yield") the calculations later on fails but only with -O2 optimization.
Like many have said, it is always our fault
I have tried all approaches i can think of but now I'm desperate.
So if anybody has some tips on what might be happening or what i should investigate please share.
I guess this gets shutdown for being offtopic but any tips is greatly appreciated.
When the compiler generates a call to a function, it preserves the contents of any registers which may be modified by the called function ("caller-save registers") before making the call.
Since you've buried this function call within an inline assembler block, the compiler doesn't know it needs to save and restore registers around the call.
Simple solution: Don't do that, then. If you want to call a function, use C function call syntax.
Complicated solution: Declare which registers this function call will clobber using extended inline assembler syntax.
Thanks for all the comments. It helped allot. I finally figured it out. My context switch did not handle the state of the FPU. After going back to dissable once again I observed that the normal generated C code helped me save the FPU state.
After implementing FSAVE/FNSAVE and FRSTOR in the context switch the -O2 optimized code also worked as expected.
#melpomene: As you asked:
Have you looked at the generated code?
In retrospect, obviously not hard enough.
Thanks for all suggestions, and I hope this thread can help others to remember the x87 FPU instructions now :-P
I know how to write some 100 lines C, but I do not know how to read/organize larger source like Rebol. Somewhere was a tutorial with hostkit and dll, but it seems R3 is now statically linked. So I do not know where to look.
How would I write a native which gets a value and returns another? Where to put it in the source? What to obey, like telling the GC I created something in C?
Also, how can I embed R3 in other programms, to call it from Python or Node? I ask for Python/Node part comes later. But my learning-main should access R3 in a similar way. Means dll. And are there some typical hooks for startup/shutdown etc in such ffi's?
[Edit: forgot to mention: it is for Rebol 3.]
That's two questions. :-)
Regarding the first (about adding a native)... it's probably best elaborated as developer documentation on a Wiki. Questions in the rebol tag should generally be more about the language from a user's point of view. The answers regarding source code itself typically will be long, and there's really only a few people who can answer it, or care about the answer (what StackOverflow calls "too localized"). And it will wind up being more of a c question at that point, if anything.
So telling the developers to get their act together and write that documentation and put it in a centrally organized place is probably the best idea! :-P But I actually did try this myself. I added a set-env native to set environment variables from the interpreter, and you can look at the diffs it took to do that in GitHub for a bit of an idea.
add SET-ENV, tweaks to host api for environment string handling
An important thing to remember is that when you touch certain files, you have to run make prep which does a lot of scanning and code generation automatically. Adding a native is one of those things where you're definitely going to have to do that, each time you change to an interface that fundamental.
As for your second question, which is more of a user-facing question about interpreter embedding, one of the first places to look is just in how the simple REPL is implemented. There are two versions in the main repository right now, one for Posix and one for Windows.
https://github.com/rebol/r3/blob/master/src/os/host-main.c
So a string goes in and a string comes out. But there are more complicated forms of interaction, and you can get to them from reb-host.h, which includes these files from src/include
#include "reb-config.h"
#include "reb-c.h"
#include "reb-ext.h" // includes reb-defs.h
#include "reb-args.h"
#include "reb-device.h"
#include "reb-file.h"
#include "reb-event.h"
#include "reb-evtypes.h"
#include "reb-net.h"
#include "reb-filereq.h"
#include "reb-gob.h"
#include "reb-lib.h"
So you can look through those files for the API as it existed at the moment of the December-12th open-sourcing. Things will be evolving, and hopefully much better documented. But for now that's what seems to be available. You could link the host kit as a shared/DLL or static library, it depends on your build settings and what compiler you use.
I just discovered AutoHotKey and it seems like a dream come true. I have two .ahk scripts, A.ahk and B.ahk. I want to call script B from within script A.
The AHK forums are strangely silent on the subject but I am sure this is possible.
It's the #Include directive you are looking for. You include ScriptB.ahk, then call its functions like you normally would.
#include SomeFile.ahk
http://www.autohotkey.com/docs/commands/_Include.htm
Using an #include directive is more common, but occasionally you will need to call an external AHK script. This is easily accomplished using the Run or RunWait commands. While you can pass arguments to the called script through the command line you cannot call functions within it directly. Also, this approach will create a separate thread for the called script, but that may be the point.
What really helped was a combination of the previous answers and a little bit of outside knowledge. I needed a script that would call more than 1 script, and since my files were in different folders, I found that I needed to specify the entire path of the files (I am sure this could be shortened but this was good enough for me at this point). I also didn't want all the different scripts that were being called to appear in the tray of the taskbar so I added an ExitApp statement at the end.
So my 'generalized' code was follows. Hopefully it can help another person.
#SingleInstance, Force
; HotKeys
#Include C:\Users\username\path1\Arrows.ahk
#Include C:\Users\username\path1\HomeEndModifiers.ahk
; SoundKeys
#Include C:\Users\username\path2\VolumeAdjustment.ahk
; Opening Programs
#Include C:\Users\username\path3\OpeningPrograms.ahk
ExitApp
```
I have an ANSI C code that is about 10,000 lines long that I am trying to use in an iPhone project. When I compile the code with gcc on the command line, I type the following:
gcc -o myprog -O3 myprog.c
This program reads in large jpeg files and does some fancy processing on them, so I call it with the following
./myprog mypic.jpg
and from the command line, this takes take about 0.1 seconds.
I'm trying to import this code into an iPhone project but I'm not entirely sure how. I was able to get it to compile and run successfully by renaming myprog.c to myprog.h and then calling the functions in the C code from within a generic NSObject class. I added the O3 optimization to the project's Other C Flags. However, when I do this, the code on the simulator takes about 2 seconds to run and on the iPhone about 7 seconds to run which renders an unacceptable user experience.
Any tips on on hoe to get this going would be much appreciated.
It's hard to say for sure where the slowness comes from, or if there is any way around it, but right off the bat you've done something wrong.
You shouldn't have renamed a .c file to a .h file and included it. You should have written a .h (header) file that had the function, variable, and type declarations declared:
myprog.h:
#ifndef MYPROG_H_
#define MYPROG_H_
struct thing {
int a;
int b;
};
extern int woof;
int foo(void * buf, int size);
#endif /* MYPROG_H_ */
Then you should compile the .c file to an object file (or library) and link the main program against that. If you were to have included the .h file that was really just a renamed .c file into more than one source code files it may have resulted in having multiple versions of some data and code in your program.
You'll probably also want to go through and separate out any code in myprog.c that you won't be using in your iPhone program. I'll bet that there is plenty.
As far as why the program is slowing down, this could have to do with myprog being written to make use of some resources that aren't available on the iPhone. The first thing that comes to mind is large amounts of RAM, since many desktop applications are written as though available RAM is infinite, and I could see how some .jpg manipulation code could be written this way. The way to get around this would be to try to rework the algorithm so that it did not load as much of the picture at one time while working on it.
The second thing that come is floating point code. Floating point operations are common in image manipulation code, but often either not available or severely limited in embedded systems. In the case of iPhones they are available, but according to something I heard, their performance is noticeably hampered if you compile your code to thumb rather than regular ARM code. (I've never developed for an iPhone or its particular processor so I don't know for sure, but it is worth looking into).
Another place where things could be slowing down would be if there were some sort of translation between Objective C objects and C structures that you have somehow introduced and is happening a lot more often than it should need to. There are probably other slow downs that could happen because of this, but you might be able to test this theory out by creating a objective C program for your desktop that uses the myprog.c code in a manner similar to the iPhone program's use of it.
Another thing you probably should look into is profiling your iPhone program. Profiling determines (or only helps to determine, in some cases) where the program is spending its time. Knowing this doesn't necessarily tell you that the code that runs the most is bad or that anything about it could be improved, but it does tell you where to look. And sometimes you may look at the results and immediately know that some function that you thought was only going to be called once at the beginning of the program is actually being called repeatedly, which highly suggests that some improvement can be made.
I'm sure that a little searching will turn up how to go about this.
I'm working in an embedded system (RTXC) where I need to disable the debugger functionality which is enabled through a #define command. However, when I change the #define to undefine, compilation goes off fine, but when the linker runs, it encounters an error about a symbol not existing that belongs to the debug code (which should have been taken care of by the debugger variable not being defined). Is there any way for Make to ensure that a preprocessor variable does not get defined or stays undefined ?
The answer to your question is no, Make can't absolutely prevent a variable from being defined by, say, a #define expression in the code.
You seem to have an elusive problem. It could be a bug in your Makefiles, a misspelled directive, a bad macro (if you'll pardon the tautology) or something trivial. I'd suggest burning the forest: cut out everything until the problem stops, then see where it was hiding. If you get down to HelloWorld and the problem persists, let us know.
No. You will need to fix the bug in your code.
More specifically, there is something that is referencing the debug side of things outside of an #ifdef. Make won't be able to help you there.
Another possibility is that you have a .o or something left over from a previous build; you might want to try cleaning the build tree.