Skip to content

Commit

Permalink
Using development endpoint if the debugger is attached (#115)
Browse files Browse the repository at this point in the history
* Using development endpoint if the debugger is attached

* Update Sources/EmbraceCore/Options/Embrace+Endpoints.swift

Co-authored-by: Austin Treat Emmons <austin.emmons@embrace.io>

---------

Co-authored-by: Austin Treat Emmons <austin.emmons@embrace.io>
  • Loading branch information
NachoEmbrace and atreat authored Jan 19, 2024
1 parent 28f0d38 commit a6b1c76
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 53 deletions.
5 changes: 4 additions & 1 deletion Sources/EmbraceCore/Internal/Embrace+Setup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import EmbraceCommon
import EmbraceOTel
import EmbraceStorage
import EmbraceUpload
import EmbraceObjCUtils

extension Embrace {
static func createStorage(options: Embrace.Options) throws -> EmbraceStorage {
Expand All @@ -27,7 +28,9 @@ extension Embrace {

static func createUpload(options: Embrace.Options, deviceId: String) -> EmbraceUpload? {
// endpoints
guard let sessionsURL = URL.sessionsEndpoint(basePath: options.endpoints.baseURL),
guard let sessionsURL = EMBDevice.isDebuggerAttached ?
URL.sessionsEndpoint(basePath: options.endpoints.developmentBaseURL) :
URL.sessionsEndpoint(basePath: options.endpoints.baseURL),
let blobsURL = URL.blobsEndpoint(basePath: options.endpoints.baseURL) else {
ConsoleLog.error("Failed to initialize endpoints!")
return nil
Expand Down
35 changes: 17 additions & 18 deletions Sources/EmbraceObjCUtils/include/EMBDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,31 @@

NS_ASSUME_NONNULL_BEGIN

extern bool IsDebuggerAttached(void);

@interface EMBDevice : NSObject

-(instancetype)init NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;

@property (class, nullable, readonly) NSString * appVersion;
@property (class, nullable, readonly) NSString * buildUUID;
@property (class, nullable, readonly) NSString * screenResolution;
@property (class, nullable, readonly) NSString *appVersion;
@property (class, nullable, readonly) NSString *buildUUID;
@property (class, nullable, readonly) NSString *screenResolution;

@property (class, readonly) NSString * environment;
@property (class, readonly) NSString * environmentDetail;
@property (class, readonly) NSString * bundleVersion;
@property (class, readonly) NSString * manufacturer;
@property (class, readonly) NSString * model;
@property (class, readonly) NSString * architecture;
@property (class, readonly) NSString * locale;
@property (class, readonly) NSString *environment;
@property (class, readonly) NSString *environmentDetail;
@property (class, readonly) NSString *bundleVersion;
@property (class, readonly) NSString *manufacturer;
@property (class, readonly) NSString *model;
@property (class, readonly) NSString *architecture;
@property (class, readonly) NSString *locale;

@property (class, readonly) NSString * operatingSystemType;
@property (class, readonly) NSString * operatingSystemVersion;
@property (class, readonly) NSString * operatingSystemBuild;
@property (class, readonly) NSString * timezoneDescription;
@property (class, readonly) NSString *operatingSystemType;
@property (class, readonly) NSString *operatingSystemVersion;
@property (class, readonly) NSString *operatingSystemBuild;
@property (class, readonly) NSString *timezoneDescription;

@property (class, readonly) NSNumber * totalDiskSpace;
@property (class, readonly) NSNumber *totalDiskSpace;

@property (class, readonly) BOOL isJailbroken;
@property (class, readonly) BOOL isDebuggerAttached;

@end

Expand Down
68 changes: 34 additions & 34 deletions Sources/EmbraceObjCUtils/source/EMBDevice.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,40 +56,6 @@ typedef NS_ENUM(NSInteger, EMBReleaseMode) {
EMBReleaseAppStore
};

// see: https://developer.apple.com/library/content/qa/qa1361/_index.html
bool IsDebuggerAttached(void)
// Returns true if the current process is being debugged (either
// running under the debugger or has a debugger attached post facto).
{
int junk;
int mib[4];
struct kinfo_proc info;
size_t size;

// Initialize the flags so that, if sysctl fails for some bizarre
// reason, we get a predictable result.

info.kp_proc.p_flag = 0;

// Initialize mib, which tells sysctl the info we want, in this case
// we're looking for information about a specific process ID.

mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();

// Call sysctl.

size = sizeof(info);
junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
assert(junk == 0);

// We're being debugged if the P_TRACED flag is set.

return ( (info.kp_proc.p_flag & P_TRACED) != 0 );
}

@implementation EMBDevice

#pragma mark - Accessors
Expand Down Expand Up @@ -484,4 +450,38 @@ + (EMBReleaseMode)getReleaseMode
#endif
}

// see: https://developer.apple.com/library/content/qa/qa1361/_index.html
// Returns true if the current process is being debugged (either
// running under the debugger or has a debugger attached post facto).
+ (BOOL)isDebuggerAttached
{
int junk;
int mib[4];
struct kinfo_proc info;
size_t size;

// Initialize the flags so that, if sysctl fails for some bizarre
// reason, we get a predictable result.

info.kp_proc.p_flag = 0;

// Initialize mib, which tells sysctl the info we want, in this case
// we're looking for information about a specific process ID.

mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();

// Call sysctl.

size = sizeof(info);
junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
assert(junk == 0);

// We're being debugged if the P_TRACED flag is set.

return ( (info.kp_proc.p_flag & P_TRACED) != 0 );
}

@end

0 comments on commit a6b1c76

Please sign in to comment.