def init_session():
    try:
        session = None
        if platform == 'ios' or platform == 'android':
            try:
                device = frida.get_usb_device(3) # added timeout to wait for 3 seconds
            except Exception as e:
                print((colored(str(e), "red")))
                traceback.print_exc()
                if platform == 'android':
                    print((colored("Troubleshooting Help", "blue")))
                    print((colored("HINT: Is USB Debugging enabled?", "blue")))
                    print((colored("HINT: Is `frida-server` running on mobile device (with +x permissions)?", "blue")))
                    print((colored("HINT: Is `adb` daemon running?", "blue")))
                    sys.exit(1)
                elif platform == "ios":
                    print((colored("Troubleshooting Help", "blue")))
                    print((colored("HINT: Have you installed `frida` module from Cydia?", "blue")))
                    print((colored("HINT: Have used `ipa_installer` to inject the `FridaGadget` shared lbrary?", "blue")))
                    sys.exit(1)
        elif platform == 'iossim':
            try:
                device = frida.get_remote_device()
            except Exception as e:
                print((colored("Troubleshooting Help", "blue")))
                print((colored("HINT: Have you successfully integrated the FridaGadget dylib with the XCode Project?", "blue")))
                print((colored("HINT: Do you see a message similar to \"[Frida INFO] Listening on 127.0.0.1 TCP port 27042\" on XCode console logs?", "blue")))
                sys.exit(1)
        elif platform == 'macos':
            device = frida.get_local_device()
        else:
            print((colored('[ERROR] Unsupported Platform', 'red')))
            sys.exit(1)
        pid = None
        if app_name:
            try:
                if platform == 'android' and spawn == 1:
                    print((colored("Now Spawning %s" % app_name, "green")))
                    pid = device.spawn([app_name])
                    #time.sleep(5)
                    session = device.attach(pid)
                    #time.sleep(5)
                elif (platform == 'ios' or platform == 'macos') and spawn == 1:
                    bundleID = getBundleID(device, app_name, platform)
                    if bundleID:
                        print((colored("Now Spawning %s" % bundleID, "green")))
                        pid = device.spawn([bundleID])
                        #time.sleep(5)
                        session = device.attach(pid)
                    else:
                        print((colored("[ERROR] Can't spawn %s" % app_name, "red")))
                        traceback.print_exc()
                        sys.exit(1)
                else:
                    arg_to_attach = app_name
                    if app_name.isdigit():
                        arg_to_attach = int(app_name)

                    session = device.attach(arg_to_attach)
            except Exception as e:
                print((colored('[ERROR] ' + str(e), 'red')))
                traceback.print_exc()
        if session:
            print((colored('[INFO] Attached to %s' % (app_name), 'yellow')))
            session.on('detached', on_detached)
    except Exception as e:
        print((colored('[ERROR] ' + str(e), 'red')))
        traceback.print_exc()
        sys.exit(1)
    return device, session, pid